Skip to content
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
1 change: 1 addition & 0 deletions src/tests/mocks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) mod erc1155_mocks;
pub(crate) mod erc1155_receiver_mocks;
pub(crate) mod erc20_mocks;
pub(crate) mod erc20_votes_mocks;
pub(crate) mod erc2981_mocks;
pub(crate) mod erc721_mocks;
pub(crate) mod erc721_receiver_mocks;
pub(crate) mod eth_account_mocks;
Expand Down
44 changes: 44 additions & 0 deletions src/tests/mocks/erc2981_mocks.cairo
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);
}
}
1 change: 1 addition & 0 deletions src/tests/token.cairo
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;
1 change: 1 addition & 0 deletions src/tests/token/erc2981.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod test_erc2981;
172 changes: 172 additions & 0 deletions src/tests/token/erc2981/test_erc2981.cairo
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'>()
}
Copy link
Collaborator

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


// 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);
}
3 changes: 3 additions & 0 deletions src/token.cairo
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;
}
5 changes: 5 additions & 0 deletions src/token/common/erc2981.cairo
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};
Loading