Skip to content

Commit

Permalink
refac: migrate repo from cairo 2.2.0 - 2.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
EjembiEmmanuel committed Aug 5, 2024
1 parent 4eada5c commit 574eb82
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 143 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
target/
.snfoundry_cache
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
scarb 2.6.5
starknet-foundry 0.27.0
6 changes: 6 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ name = "cairo_erc_2981"
version = "2.0.0"
dependencies = [
"openzeppelin",
"snforge_std",
]

[[package]]
name = "openzeppelin"
version = "0.14.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.14.0#f091c4f51ddeb10297db984acae965328c5a4e5b"

[[package]]
name = "snforge_std"
version = "0.27.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.27.0#2d99b7c00678ef0363881ee0273550c44a9263de"
6 changes: 6 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ version = "2.0.0"
starknet = "2.6.4"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.14.0" }

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.27.0" }

[scripts]
test = "snforge test"

[[target.starknet-contract]]
sierra = true
casm = true
94 changes: 74 additions & 20 deletions src/components/erc2981.cairo
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
//! Component implementing IERC2981.

#[starknet::component]
mod ERC2981Component {
// Starknet deps
use starknet::{ContractAddress};

// OZ deps
use openzeppelin::introspection::{
src5::{
SRC5Component, SRC5Component::InternalTrait as SRC5InternalTrait,
SRC5Component::SRC5Impl
},
interface::{ISRC5Dispatcher, ISRC5DispatcherTrait}
use openzeppelin::{
introspection::{
src5::{
SRC5Component, SRC5Component::InternalTrait as SRC5InternalTrait,
SRC5Component::SRC5Impl
},
interface::{ISRC5Dispatcher, ISRC5DispatcherTrait}
}
};

// Local deps
use cairo_erc_2981::interfaces::erc2981::{IERC2981, IERC2981_ID};
use cairo_erc_2981::interfaces::erc2981::{IERC2981, IERC2981Camel, IERC2981_ID};

#[storage]
struct Storage {
Expand All @@ -31,12 +32,12 @@ mod ERC2981Component {
#[derive(Drop, starknet::Event)]
enum Event {}

#[embeddable_as(ERC2981)]
impl ERC2981Impl<
#[embeddable_as(ERC2981Impl)]
impl ERC2981<
TContractState,
+HasComponent<TContractState>,
+SRC5Component::HasComponent<TContractState>,
+Drop<TContractState>
+Drop<TContractState>,
> of IERC2981<ComponentState<TContractState>> {
/// Return the default royalty.
///
Expand Down Expand Up @@ -125,6 +126,10 @@ mod ERC2981Component {
// [Check] Fee is lower or equal to 1
assert(fee_numerator <= fee_denominator, 'Invalid fee rate');

// [Assert] Caller is owner
// let mut ownable_comp = get_dep_component!(@self, Owner);
// ownable_comp.assert_only_owner();

// [Effect] Store values
self.ERC2981_receiver.write(receiver);
self.ERC2981_fee_numerator.write(fee_numerator);
Expand Down Expand Up @@ -160,13 +165,60 @@ mod ERC2981Component {
// [Check] Fee is lower or equal to 1
assert(fee_numerator <= fee_denominator, 'Invalid fee rate');

// [Assert] Caller is owner
// let mut ownable_comp = get_dep_component!(@self, Owner);
// ownable_comp.assert_only_owner();

// [Effect] Store values
self.ERC2981_token_receiver.write(token_id, receiver);
self.ERC2981_token_fee_numerator.write(token_id, fee_numerator);
self.ERC2981_token_fee_denominator.write(token_id, fee_denominator);
}
}

#[embeddable_as(ERC2981CamelImpl)]
impl ERC2981CamelOnly<
TContractState,
+HasComponent<TContractState>,
+SRC5Component::HasComponent<TContractState>,
+Drop<TContractState>,
> of IERC2981Camel<ComponentState<TContractState>> {
fn defaultRoyalty(self: @ComponentState<TContractState>) -> (ContractAddress, u256, u256) {
self.default_royalty()
}

fn tokenRoyalty(
self: @ComponentState<TContractState>, tokenId: u256
) -> (ContractAddress, u256, u256) {
self.token_royalty(tokenId)
}

fn royaltyInfo(
self: @ComponentState<TContractState>, tokenId: u256, salePrice: u256
) -> (ContractAddress, u256) {
self.royalty_info(tokenId, salePrice)
}

fn setDefaultRoyalty(
ref self: ComponentState<TContractState>,
receiver: ContractAddress,
feeNumerator: u256,
feeDenominator: u256
) {
self.set_default_royalty(receiver, feeNumerator, feeDenominator)
}

fn setTokenRoyalty(
ref self: ComponentState<TContractState>,
tokenId: u256,
receiver: ContractAddress,
feeNumerator: u256,
feeDenominator: u256
) {
self.set_token_royalty(tokenId, receiver, feeNumerator, feeDenominator)
}
}

#[generate_trait]
pub impl InternalImpl<
TContractState,
Expand All @@ -185,7 +237,7 @@ mod ERC2981Component {
ref self: ComponentState<TContractState>,
receiver: ContractAddress,
fee_numerator: u256,
fee_denominator: u256
fee_denominator: u256,
) {
// [Effect] Register interfaces
let mut src5_component = get_dep_component_mut!(ref self, SRC5);
Expand Down Expand Up @@ -235,9 +287,10 @@ mod ERC2981Component {
#[cfg(test)]
mod Test {
// starknet deps
use cairo_erc_2981::interfaces::erc2981::IERC2981;
use starknet::ContractAddress;
use cairo_erc_2981::components::erc2981::ERC2981Component::HasComponent;
use cairo_erc_2981::interfaces::erc2981::{IERC2981};
use cairo_erc_2981::components::erc2981::ERC2981Component::InternalTrait;
use starknet::{contract_address_const};

// Local deps
use super::ERC2981Component;
Expand All @@ -253,28 +306,28 @@ mod Test {

type ERC2981ComponentState = ERC2981Component::ComponentState<MockERC2981::ContractState>;


fn STATE() -> ERC2981ComponentState {
ERC2981Component::component_state_for_testing()
}

fn ZERO() -> starknet::ContractAddress {
contract_address_const::<0>()
starknet::contract_address_const::<0>()
}

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

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

#[test]
#[available_gas(250_000)]
fn test_initialization() {
// [Setup]
let mut state = STATE();

state.initializer(RECEIVER(), FEE_NUMERATOR, FEE_DENOMINATOR);

// [Assert] Default royalty
Expand All @@ -284,6 +337,7 @@ mod Test {
assert(fee_denominator == FEE_DENOMINATOR, 'Invalid fee denominator');
}


#[test]
#[available_gas(105_000)]
#[should_panic(expected: ('Invalid receiver',))]
Expand Down Expand Up @@ -326,7 +380,7 @@ mod Test {
assert(
royalty_amount == SALE_PRICE * FEE_NUMERATOR / FEE_DENOMINATOR,
'Invalid royalty
amount'
amount'
);
}

Expand Down Expand Up @@ -370,7 +424,7 @@ mod Test {
assert(
royalty_amount == SALE_PRICE * FEE_NUMERATOR / FEE_DENOMINATOR,
'Invalid royalty
amount'
amount'
);
}

Expand Down
9 changes: 4 additions & 5 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ mod presets {
mod mocks {
mod erc2981;
}
// #[cfg(test)]
// mod tests {
// mod test_erc721_royalty;
// }


#[cfg(test)]
mod tests {
mod test_erc721_royalty;
}
12 changes: 11 additions & 1 deletion src/mocks/erc2981.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ mod MockERC2981 {

component!(path: ERC2981Component, storage: erc2981, event: ERC2981Event);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

#[abi(embed_v0)]
impl ERC2981Impl = ERC2981Component::ERC2981<ContractState>;
impl ERC2981Impl = ERC2981Component::ERC2981Impl<ContractState>;
impl ERC2981InternalImpl = ERC2981Component::InternalImpl<ContractState>;

// Ownable Mixin
#[abi(embed_v0)]
impl OwnableMixinImpl = OwnableComponent::OwnableMixinImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
erc2981: ERC2981Component::Storage,
#[substorage(v0)]
src5: SRC5Component::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
}

#[event]
Expand All @@ -28,5 +36,7 @@ mod MockERC2981 {
ERC2981Event: ERC2981Component::Event,
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
}
}
43 changes: 23 additions & 20 deletions src/presets/erc721_royalty.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ mod ERC721Royalty {

// OZ deps
use openzeppelin::{
access::ownable::OwnableComponent, introspection::src5::SRC5Component,
token::erc721::{ERC721Component, ERC721HooksEmptyImpl}
access::ownable::{
OwnableComponent, OwnableComponent::{InternalTrait as OwnableInternalTrait}
},
introspection::src5::SRC5Component, token::erc721::{ERC721Component, ERC721HooksEmptyImpl}
};

// Local deps
Expand All @@ -30,8 +32,7 @@ mod ERC721Royalty {
impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>;

// ERC2981
#[abi(embed_v0)]
impl ERC2981Impl = ERC2981Component::ERC2981<ContractState>;
impl ERC2981Impl = ERC2981Component::ERC2981Impl<ContractState>;
impl ERC2981InternalImpl = ERC2981Component::InternalImpl<ContractState>;

#[storage]
Expand Down Expand Up @@ -74,38 +75,40 @@ mod ERC721Royalty {
}

#[abi(embed_v0)]
impl ERC2981CamelImpl of IERC2981Camel<ContractState> {
fn defaultRoyalty(self: @ContractState) -> (ContractAddress, u256, u256) {
impl ERC721RoyaltyImpl of IERC2981<ContractState> {
fn default_royalty(self: @ContractState) -> (ContractAddress, u256, u256) {
self.erc2981.default_royalty()
}

fn tokenRoyalty(self: @ContractState, tokenId: u256) -> (ContractAddress, u256, u256) {
self.erc2981.token_royalty(tokenId)
fn token_royalty(self: @ContractState, token_id: u256) -> (ContractAddress, u256, u256) {
self.erc2981.token_royalty(token_id)
}

fn royaltyInfo(
self: @ContractState, tokenId: u256, salePrice: u256
fn royalty_info(
self: @ContractState, token_id: u256, sale_price: u256
) -> (ContractAddress, u256) {
self.erc2981.royalty_info(tokenId, salePrice)
self.erc2981.royalty_info(token_id, sale_price)
}

fn setDefaultRoyalty(
fn set_default_royalty(
ref self: ContractState,
receiver: ContractAddress,
feeNumerator: u256,
feeDenominator: u256
fee_numerator: u256,
fee_denominator: u256
) {
self.erc2981.set_default_royalty(receiver, feeNumerator, feeDenominator)
self.ownable.assert_only_owner();
self.erc2981.set_default_royalty(receiver, fee_numerator, fee_denominator);
}

fn setTokenRoyalty(
fn set_token_royalty(
ref self: ContractState,
tokenId: u256,
token_id: u256,
receiver: ContractAddress,
feeNumerator: u256,
feeDenominator: u256
fee_numerator: u256,
fee_denominator: u256
) {
self.erc2981.set_token_royalty(tokenId, receiver, feeNumerator, feeDenominator)
self.ownable.assert_only_owner();
self.erc2981.set_token_royalty(token_id, receiver, fee_numerator, fee_denominator);
}
}

Expand Down
Loading

0 comments on commit 574eb82

Please sign in to comment.