-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
erc2981: add test cases for ERC2981Component
- Loading branch information
1 parent
85d6f0f
commit 0cb7fc4
Showing
6 changed files
with
161 additions
and
2 deletions.
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,45 @@ | ||
#[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); | ||
|
||
#[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 | ||
} | ||
|
||
|
||
#[abi(embed_v0)] | ||
impl ERC2981Impl = ERC2981Component::ERC2981Impl<ContractState>; | ||
impl ERC2981InternalImpl = ERC2981Component::InternalImpl<ContractState>; | ||
|
||
// SRC5 | ||
#[abi(embed_v0)] | ||
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>; | ||
|
||
#[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,90 @@ | ||
use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait}; | ||
|
||
use openzeppelin::tests::mocks::erc2981_mocks::ERC2981Mock; | ||
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 COMPONENT_STATE() -> ComponentState { | ||
ERC2981Component::component_state_for_testing() | ||
} | ||
|
||
fn OWNER() -> ContractAddress { | ||
contract_address_const::<'OWNER'>() | ||
} | ||
|
||
fn DEFAULT_RECEIVER() -> ContractAddress { | ||
contract_address_const::<'DEFAULT_RECEIVER'>() | ||
} | ||
|
||
fn RECEIVER() -> ContractAddress { | ||
contract_address_const::<'RECEIVER'>() | ||
} | ||
|
||
// 0.5% (default denominator is 10000) | ||
fn DEFAULT_FEE_NUMERATOR() -> u256 { | ||
50 | ||
} | ||
|
||
// 5% (default denominator is 10000) | ||
fn 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"); | ||
} | ||
|
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