diff --git a/flex_marketplace/src/marketplace/royalty_fee_manager.cairo b/flex_marketplace/src/marketplace/royalty_fee_manager.cairo index ee53ab9..a385317 100644 --- a/flex_marketplace/src/marketplace/royalty_fee_manager.cairo +++ b/flex_marketplace/src/marketplace/royalty_fee_manager.cairo @@ -1,28 +1,41 @@ use starknet::ContractAddress; - +use starknet::class_hash::ClassHash; +use flex::marketplace::royalty_fee_registry::{ + IRoyaltyFeeRegistryDispatcher, IRoyaltyFeeRegistryDispatcherTrait +}; #[starknet::interface] trait IRoyaltyFeeManager { - fn initializer( - ref self: TState, - fee_registry: ContractAddress, - owner: ContractAddress, - proxy_admin: ContractAddress - ); - fn transfer_ownership(ref self: TState, new_owner: ContractAddress); - fn owner(ref self: TState) -> ContractAddress; + fn initializer(ref self: TState, fee_registry: ContractAddress, owner: ContractAddress,); fn INTERFACE_ID_ERC2981(self: @TState) -> felt252; - fn get_royalty_fee_registry(self: @TState) -> ContractAddress; + fn get_royalty_fee_registry(self: @TState) -> IRoyaltyFeeRegistryDispatcher; fn calculate_royalty_fee_and_get_recipient( self: @TState, collection: ContractAddress, token_id: u256, amount: u128 ) -> (ContractAddress, u128); + fn upgrade(ref self: TState, impl_hash: ClassHash); +} + + +#[starknet::interface] +trait IERC2981 { + fn royaltyInfo( + ref self: TContractState, tokenId: u256, salePrice: u128 + ) -> (ContractAddress, u128); } #[starknet::contract] mod RoyaltyFeeManager { - use starknet::{ContractAddress, contract_address_const}; - + use openzeppelin::upgrades::upgradeable::UpgradeableComponent::InternalTrait; use openzeppelin::access::ownable::OwnableComponent; + use openzeppelin::upgrades::UpgradeableComponent; + use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait}; + use starknet::get_caller_address; + use starknet::{ContractAddress, contract_address_const}; + use super::ClassHash; + use super::IERC2981Dispatcher; + use super::IERC2981DispatcherTrait; + use super::{IRoyaltyFeeRegistryDispatcher, IRoyaltyFeeRegistryDispatcherTrait}; component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); + component!(path: UpgradeableComponent, storage: upgradable, event: UpgradeableEvent); #[abi(embed_v0)] impl OwnableImpl = OwnableComponent::OwnableImpl; @@ -32,50 +45,66 @@ mod RoyaltyFeeManager { #[storage] struct Storage { INTERFACE_ID_ERC2981: felt252, - royalty_fee_registry: ContractAddress, + royalty_fee_registry: IRoyaltyFeeRegistryDispatcher, + #[substorage(v0)] + ownable: OwnableComponent::Storage, #[substorage(v0)] - ownable: OwnableComponent::Storage + upgradable: UpgradeableComponent::Storage, } #[event] #[derive(Drop, starknet::Event)] enum Event { + #[flat] OwnableEvent: OwnableComponent::Event, + #[flat] + UpgradeableEvent: UpgradeableComponent::Event, } #[external(v0)] impl RoyaltyFeeManagerImpl of super::IRoyaltyFeeManager { fn initializer( - ref self: ContractState, - fee_registry: ContractAddress, - owner: ContractAddress, - proxy_admin: ContractAddress - ) { // TODO - } - - fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { // TODO - } - - fn owner(ref self: ContractState) -> ContractAddress { - // TODO - contract_address_const::<0>() + ref self: ContractState, fee_registry: ContractAddress, owner: ContractAddress, + ) { + self.INTERFACE_ID_ERC2981.write(0x2a55205a); + self + .royalty_fee_registry + .write(IRoyaltyFeeRegistryDispatcher { contract_address: fee_registry }); + self.ownable.initializer(owner); } fn INTERFACE_ID_ERC2981(self: @ContractState) -> felt252 { - // TODO - 0 + return self.INTERFACE_ID_ERC2981.read(); } - fn get_royalty_fee_registry(self: @ContractState) -> ContractAddress { - // TODO - contract_address_const::<0>() + fn get_royalty_fee_registry(self: @ContractState) -> IRoyaltyFeeRegistryDispatcher { + return self.royalty_fee_registry.read(); } fn calculate_royalty_fee_and_get_recipient( self: @ContractState, collection: ContractAddress, token_id: u256, amount: u128 ) -> (ContractAddress, u128) { - // TODO - (contract_address_const::<0>(), 0) + let feeRegistry = self.get_royalty_fee_registry(); + let (receiver, royaltyAmount) = feeRegistry.get_royalty_fee_info(collection, amount); + if (!receiver.is_zero()) { + return (receiver, royaltyAmount); + } + let interfaceIDERC2981 = self.INTERFACE_ID_ERC2981(); + let supportsERC2981: bool = ISRC5Dispatcher { contract_address: collection } + .supports_interface(interfaceIDERC2981); + if (supportsERC2981) { + let (receiverERC2981, royaltyAmountERC2981) = IERC2981Dispatcher { + contract_address: collection + } + .royaltyInfo(token_id, amount); + return (receiverERC2981, royaltyAmountERC2981); + } + return (receiver, royaltyAmount); + } + + fn upgrade(ref self: ContractState, impl_hash: ClassHash) { + self.ownable.assert_only_owner(); + self.upgradable._upgrade(impl_hash); } } } diff --git a/flex_marketplace/src/marketplace/royalty_fee_registry.cairo b/flex_marketplace/src/marketplace/royalty_fee_registry.cairo index 038ead0..b382099 100644 --- a/flex_marketplace/src/marketplace/royalty_fee_registry.cairo +++ b/flex_marketplace/src/marketplace/royalty_fee_registry.cairo @@ -16,7 +16,9 @@ trait IRoyaltyFeeRegistry { fn transfer_ownership(ref self: TState, new_owner: ContractAddress); fn owner(ref self: TState) -> ContractAddress; fn get_royalty_fee_limit(self: @TState) -> u128; - fn get_royalty_fee_info(self: @TState) -> (ContractAddress, u128); + fn get_royalty_fee_info( + self: @TState, collection: ContractAddress, amount: u128 + ) -> (ContractAddress, u128); fn get_royalty_fee_info_collection( self: @TState, collection: ContractAddress ) -> (ContractAddress, ContractAddress, u128); @@ -107,7 +109,9 @@ mod RoyaltyFeeRegistry { 0 } - fn get_royalty_fee_info(self: @ContractState) -> (ContractAddress, u128) { + fn get_royalty_fee_info( + self: @ContractState, collection: ContractAddress, amount: u128 + ) -> (ContractAddress, u128) { // TODO (contract_address_const::<0>(), 0) }