-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ERC2981 component #1043
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8430cd0
feat: add ERC2981 component
ptisserand 9ce0dd9
struct events are public
ptisserand d448665
move erc2981 to common directory
ptisserand 1196dda
erc2981: be more consistent with Solidity implementation
ptisserand 944e9f1
erc2981: add test cases for ERC2981Component
ptisserand b9c8c9c
erc2981: LegacyMap is obsolete with scarb 2.7.0-rc
ptisserand 6f57a4b
erc2981: code style & add some edge case tests.
ptisserand 4cb6f9f
erc2981: add missing test for interface registration.
ptisserand c1ff61b
erc2981: code formatting
ptisserand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#[starknet::contract] | ||
pub(crate) mod ERC2981Mock { | ||
use openzeppelin::introspection::src5::SRC5Component; | ||
use openzeppelin::token::common::erc2981::ERC2981Component; | ||
use starknet::ContractAddress; | ||
|
||
component!(path: ERC2981Component, storage: erc2981, event: ERC2981Event); | ||
component!(path: SRC5Component, storage: src5, event: SRC5Event); | ||
|
||
#[abi(embed_v0)] | ||
impl ERC2981Impl = ERC2981Component::ERC2981Impl<ContractState>; | ||
impl ERC2981InternalImpl = ERC2981Component::InternalImpl<ContractState>; | ||
|
||
// SRC5 | ||
#[abi(embed_v0)] | ||
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
erc2981: ERC2981Component::Storage, | ||
#[substorage(v0)] | ||
src5: SRC5Component::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
ERC2981Event: ERC2981Component::Event, | ||
#[flat] | ||
SRC5Event: SRC5Component::Event | ||
} | ||
|
||
#[constructor] | ||
fn constructor( | ||
ref self: ContractState, | ||
owner: ContractAddress, | ||
default_receiver: ContractAddress, | ||
default_royalty_fraction: u256 | ||
) { | ||
self.erc2981.initializer(default_receiver, default_royalty_fraction); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod erc1155; | ||
pub(crate) mod erc20; | ||
pub(crate) mod erc2981; | ||
pub(crate) mod erc721; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod test_erc2981; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait}; | ||
use openzeppelin::introspection::src5::SRC5Component::SRC5Impl; | ||
|
||
use openzeppelin::tests::mocks::erc2981_mocks::ERC2981Mock; | ||
use openzeppelin::tests::utils::constants::{OTHER, OWNER, ZERO}; | ||
use openzeppelin::token::common::erc2981::ERC2981Component::{ERC2981Impl, InternalImpl}; | ||
use openzeppelin::token::common::erc2981::ERC2981Component; | ||
use openzeppelin::token::common::erc2981::interface::IERC2981_ID; | ||
use openzeppelin::token::common::erc2981::{IERC2981Dispatcher, IERC2981DispatcherTrait}; | ||
|
||
use starknet::{ContractAddress, contract_address_const}; | ||
|
||
|
||
type ComponentState = ERC2981Component::ComponentState<ERC2981Mock::ContractState>; | ||
|
||
fn CONTRACT_STATE() -> ERC2981Mock::ContractState { | ||
ERC2981Mock::contract_state_for_testing() | ||
} | ||
|
||
fn COMPONENT_STATE() -> ComponentState { | ||
ERC2981Component::component_state_for_testing() | ||
} | ||
|
||
fn DEFAULT_RECEIVER() -> ContractAddress { | ||
contract_address_const::<'DEFAULT_RECEIVER'>() | ||
} | ||
|
||
fn RECEIVER() -> ContractAddress { | ||
contract_address_const::<'RECEIVER'>() | ||
} | ||
|
||
// 0.5% (default denominator is 10000) | ||
const DEFAULT_FEE_NUMERATOR: u256 = 50; | ||
// 5% (default denominator is 10000) | ||
const FEE_NUMERATOR: u256 = 500; | ||
|
||
fn setup() -> ComponentState { | ||
let mut state = COMPONENT_STATE(); | ||
state.initializer(DEFAULT_RECEIVER(), DEFAULT_FEE_NUMERATOR); | ||
state | ||
} | ||
|
||
|
||
#[test] | ||
fn test_default_royalty() { | ||
let mut state = setup(); | ||
let token_id = 12; | ||
let sale_price = 1_000_000; | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, DEFAULT_RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 5000, "Default fees incorrect"); | ||
|
||
state._set_default_royalty(RECEIVER(), FEE_NUMERATOR); | ||
|
||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 50000, "Default fees incorrect"); | ||
} | ||
|
||
|
||
#[test] | ||
fn test_token_royalty_token() { | ||
let mut state = setup(); | ||
let token_id = 12; | ||
let another_token_id = 13; | ||
let sale_price = 1_000_000; | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, DEFAULT_RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 5000, "Wrong royalty amount"); | ||
let (receiver, amount) = state.royalty_info(another_token_id, sale_price); | ||
assert_eq!(receiver, DEFAULT_RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 5000, "Wrong royalty amount"); | ||
|
||
state._set_token_royalty(token_id, RECEIVER(), FEE_NUMERATOR); | ||
let (receiver, amount) = state.royalty_info(another_token_id, sale_price); | ||
assert_eq!(receiver, DEFAULT_RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 5000, "Wrong royalty amount"); | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 50000, "Wrong royalty amount"); | ||
|
||
state._reset_token_royalty(token_id); | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, DEFAULT_RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 5000, "Wrong royalty amount"); | ||
} | ||
immrsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// | ||
// check IERC2981_ID is registered | ||
|
||
#[test] | ||
fn test_token_royalty_set_twice() { | ||
let mut state = setup(); | ||
let token_id = 12; | ||
let sale_price = 1_000_000; | ||
|
||
state._set_token_royalty(token_id, RECEIVER(), FEE_NUMERATOR); | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 50000, "Wrong royalty amount"); | ||
|
||
state._set_token_royalty(token_id, OTHER(), FEE_NUMERATOR); | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, OTHER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 50000, "Wrong royalty amount"); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ("Invalid token royalty receiver",))] | ||
fn test_token_royalty_with_zero_receiver() { | ||
let mut state = setup(); | ||
let token_id = 12; | ||
state._set_token_royalty(token_id, ZERO(), FEE_NUMERATOR); | ||
} | ||
|
||
#[test] | ||
fn test_token_royalty_with_zero_royalty_fraction() { | ||
let mut state = setup(); | ||
let token_id = 12; | ||
let sale_price = 1_000_000; | ||
|
||
state._set_token_royalty(token_id, RECEIVER(), 0); | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 0, "Wrong royalty amount"); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ("Invalid token royalty",))] | ||
fn test_token_royalty_with_invalid_fee_numerator() { | ||
let mut state = setup(); | ||
let token_id = 12; | ||
state._set_token_royalty(token_id, RECEIVER(), state._fee_denominator() + 1); | ||
} | ||
|
||
|
||
#[test] | ||
#[should_panic(expected: ("Invalid default royalty receiver",))] | ||
fn test_default_royalty_with_zero_receiver() { | ||
let mut state = setup(); | ||
|
||
state._set_default_royalty(ZERO(), FEE_NUMERATOR); | ||
} | ||
|
||
#[test] | ||
fn test_default_royalty_with_zero_royalty_fraction() { | ||
let mut state = setup(); | ||
let token_id = 12; | ||
let sale_price = 1_000_000; | ||
|
||
state._set_default_royalty(DEFAULT_RECEIVER(), 0); | ||
let (receiver, amount) = state.royalty_info(token_id, sale_price); | ||
assert_eq!(receiver, DEFAULT_RECEIVER(), "Default receiver incorrect"); | ||
assert_eq!(amount, 0, "Wrong royalty amount"); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ("Invalid default royalty",))] | ||
fn test_default_royalty_with_invalid_fee_numerator() { | ||
let mut state = setup(); | ||
|
||
state._set_default_royalty(DEFAULT_RECEIVER(), state._fee_denominator() + 1); | ||
} | ||
|
||
#[test] | ||
fn test_check_ierc2981_interface_is_registered() { | ||
let _state = setup(); | ||
let mock_state = CONTRACT_STATE(); | ||
|
||
let supports_ierc2981 = mock_state.supports_interface(IERC2981_ID); | ||
assert!(supports_ierc2981); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
pub mod erc1155; | ||
pub mod erc20; | ||
pub mod erc721; | ||
pub mod common { | ||
pub mod erc2981; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod erc2981; | ||
pub mod interface; | ||
|
||
pub use erc2981::ERC2981Component; | ||
pub use interface::{IERC2981Dispatcher, IERC2981DispatcherTrait}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constants module already declares similar constants, you can employ them instead