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

royalty fee manager tests #54

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions flex_marketplace/src/mocks/erc721.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use starknet::{ContractAddress, contract_address_const};

#[starknet::interface]
trait IER721CamelOnly<TState> {
fn transferFrom(
Expand Down Expand Up @@ -31,6 +33,10 @@ mod ERC721 {
struct Storage {
#[substorage(v0)]
src5: SRC5Component::Storage,
name: felt252,
symbol: felt252,
owners: LegacyMap::<u256, ContractAddress>,
balances: LegacyMap::<ContractAddress, u256>,
}

#[event]
Expand All @@ -40,9 +46,23 @@ mod ERC721 {
SRC5Event: SRC5Component::Event,
}

fn RECIPIENT() -> starknet::ContractAddress {
starknet::contract_address_const::<'RECIPIENT'>()
}
fn ACCOUNT1() -> ContractAddress {
starknet::contract_address_const::<'ACCOUNT1'>()
}
fn ACCOUNT2() -> ContractAddress {
starknet::contract_address_const::<'ACCOUNT2'>()
}

#[constructor]
fn constructor(ref self: ContractState,) {
fn constructor(ref self: ContractState) {
self.src5.register_interface(0x80ac58cd);
self.name.write('flex');
self.symbol.write('fNFT');
self._mint(ACCOUNT1(), 1);
self._mint(ACCOUNT2(), 2);
}

#[external(v0)]
Expand All @@ -55,11 +75,6 @@ mod ERC721 {
) {}
}


fn RECIPIENT() -> starknet::ContractAddress {
starknet::contract_address_const::<'RECIPIENT'>()
}

#[external(v0)]
impl IERC2981Impl of super::IERC2981<ContractState> {
fn royaltyInfo(
Expand All @@ -68,4 +83,14 @@ mod ERC721 {
(RECIPIENT(), 5000)
}
}

#[generate_trait]
impl StorageImpl of StorageTrait {
fn _mint(ref self: ContractState, to: ContractAddress, token_id: u256) {
assert(!to.is_zero(), 'ERC721: mint to 0');
self.balances.write(to, self.balances.read(to) + 1.into());
self.owners.write(token_id, to);
}
}
}

15 changes: 0 additions & 15 deletions flex_marketplace/tests/royalty_fee_managen_test.cairo

This file was deleted.

112 changes: 112 additions & 0 deletions flex_marketplace/tests/royalty_fee_manager_test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use starknet::{ContractAddress, contract_address_const, ClassHash, class_hash_const};

use snforge_std::{start_prank, CheatTarget, declare};

use flex::marketplace::royalty_fee_manager::{
RoyaltyFeeManager, IRoyaltyFeeManagerDispatcher, IRoyaltyFeeManagerDispatcherTrait
};
use flex::marketplace::royalty_fee_registry::{
RoyaltyFeeRegistry, IRoyaltyFeeRegistryDispatcher, IRoyaltyFeeRegistryDispatcherTrait
};

use openzeppelin::upgrades::UpgradeableComponent::Upgraded;

use tests::utils::{
setup, initialize_test, deploy_mock_nft, pop_log, assert_no_events_left, OWNER, ACCOUNT1,
RECIPIENT
};


fn UPGRADE_CLASSHASH() -> ClassHash {
RoyaltyFeeManager::TEST_CLASS_HASH.try_into().unwrap()
}

fn CLASS_HASH_ZERO() -> ClassHash {
class_hash_const::<0>()
}

fn COLLECTION() -> ContractAddress {
contract_address_const::<'COLLECTION'>()
}

#[test]
#[should_panic(expected: ('Caller is not the owner',))]
fn given_caller_has_no_owner_role_then_test_upgrade_fails() {
let dsp = setup();
initialize_test(dsp);

start_prank(CheatTarget::One(dsp.fee_manager.contract_address), ACCOUNT1());
dsp.fee_manager.upgrade(UPGRADE_CLASSHASH());
}

#[test]
#[should_panic(expected: ('Class hash cannot be zero', 'ENTRYPOINT_FAILED',))]
fn test_upgrade_with_class_hash_zero() {
let dsp = setup();
initialize_test(dsp);

start_prank(CheatTarget::One(dsp.fee_manager.contract_address), OWNER());
dsp.fee_manager.upgrade(CLASS_HASH_ZERO());
}

#[test]
fn test_upgrade_event_when_successful() {
let dsp = setup();
initialize_test(dsp);

start_prank(CheatTarget::One(dsp.fee_manager.contract_address), OWNER());
dsp.fee_manager.upgrade(UPGRADE_CLASSHASH());

let event = pop_log::<Upgraded>(dsp.fee_manager.contract_address).unwrap();
assert(event.class_hash == UPGRADE_CLASSHASH(), 'Invalid class hash');

assert_no_events_left(dsp.fee_manager.contract_address);
}

#[test]
fn test_INTERFACE_ID_ERC2981() {
let dsp = setup();
initialize_test(dsp);

// assert the returned felt252 is equal to 0x2a55205a
assert!(dsp.fee_manager.INTERFACE_ID_ERC2981() == 0x2a55205a, "wrong interface");
}

#[test]
fn test_calculate_royalty_fee_and_get_recipient_success() {
let dsp = setup();
initialize_test(dsp);

// deploy the nft, mint to ACCOUNT1 and ACCOUNT2 in constructor
let nft_address = deploy_mock_nft();

start_prank(CheatTarget::One(dsp.fee_registry.contract_address), OWNER());

// set royalty fee info in fee registry contract
dsp.fee_registry.update_royalty_info_collection(COLLECTION(), OWNER(), RECIPIENT(), 1);
dsp.fee_registry.update_royalty_info_collection(COLLECTION(), OWNER(), RECIPIENT(), 2);

// call fee_manager
start_prank(CheatTarget::One(dsp.fee_manager.contract_address), OWNER());

dsp.fee_manager.calculate_royalty_fee_and_get_recipient(COLLECTION(), 1, 1);
dsp.fee_manager.calculate_royalty_fee_and_get_recipient(COLLECTION(), 2, 1);
}

#[test]
fn test_get_royalty_fee_registry() {
let dsp = setup();
initialize_test(dsp);

// ensure the address saved in storage matches the address from the dsp
assert!(
dsp
.fee_manager
.get_royalty_fee_registry()
.contract_address == dsp
.fee_registry
.contract_address,
"wrong fee registry"
);
}

26 changes: 26 additions & 0 deletions flex_marketplace/tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use starknet::{
ContractAddress, contract_address_const, get_block_timestamp, get_contract_address,
get_caller_address, class_hash::ClassHash
};
use starknet::testing;
use snforge_std::{
PrintTrait, declare, ContractClassTrait, start_warp, start_prank, stop_prank, CheatTarget
};
Expand Down Expand Up @@ -270,3 +271,28 @@ fn deploy_test() {
let dsp = setup();
initialize_test(dsp);
}
/// Pop the earliest unpopped logged event for the contract as the requested type
/// and checks there's no more keys or data left on the event, preventing unaccounted params.
///
/// This function also removes the first key from the event, to match the event
/// structure key params without the event ID.
///
/// This method doesn't currently work for components events that are not flattened
/// because an extra key is added, pushing the event ID key to the second position.
fn pop_log<T, +Drop<T>, +starknet::Event<T>>(address: ContractAddress) -> Option<T> {
let (mut keys, mut data) = testing::pop_log_raw(address)?;

// Remove the event ID from the keys
keys.pop_front();

let ret = starknet::Event::deserialize(ref keys, ref data);
assert(data.is_empty(), 'Event has extra data');
assert(keys.is_empty(), 'Event has extra keys');
ret
}

fn assert_no_events_left(address: ContractAddress) {
assert(testing::pop_log_raw(address).is_none(), 'Events remaining on queue');
}


Loading