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

Conversation

ptisserand
Copy link
Contributor

@ptisserand ptisserand commented Jul 9, 2024

Fixes #1041

This PR add ERC2981 component.

ERC2981 component implements the ERC2981 standard interface:

pub const IERC2981_ID: felt252 = 0x2d3414e45a8700c29f119a54b9f11dca0e29e06ddcb214018fc37340e165ed6;

#[starknet::interface]
pub trait IERC2981<TState> {
    fn royalty_info(self: @TState, token_id: u256, sale_price: u256) -> (ContractAddress, u256);
}

The following internal functions are implemented:

fn initializer(ref self: TState, default_receiver: ContractAddress, default_royalty_fraction: u256);
fn _fee_denominator(self: @TState) -> u256;
fn _default_royalty(self: @TState) -> (ContractAddress, u256, u256);
fn _set_default_royalty(ref self: TState, receiver: ContractAddress,fee_numerator: u256,); 
fn _set_token_royalty(ref self: TState, token_id: u256, receiver: ContractAddress, fee_numerator: u256); 
fn _token_royalty(self: @TState, token_id: u256) -> (ContractAddress, u256, u256);
fn _reset_token_royalty(ref self: TState, token_id: u256);

PR Checklist

  • Tests
  • Documentation
  • Added entry to CHANGELOG.md
  • Tried the feature on a public network

Copy link
Member

@ericnordelo ericnordelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for taking the time @ptisserand. Left some comments. The main suggestion is to be as consistent as possible with the implementation we have in Solidity.

src/token.cairo Outdated
@@ -1,3 +1,4 @@
pub mod erc1155;
pub mod erc20;
pub mod erc2981;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not a different token but an extension to both ERC721 and ERC1155, it makes sense for it to be placed under a common module instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved the implementation to src/token/common/erc2981

Comment on lines 5 to 12
pub use erc2981::ERC2981Component;
pub use fees::FeesImpl;
pub use fees::FeesRatio;
pub use fees::FeesRatioDefault;
pub use interface::IERC2981Dispatcher;
pub use interface::IERC2981DispatcherTrait;
pub use interface::IERC2981SetupDispatcher;
pub use interface::IERC2981SetupDispatcherTrait;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports can be consolidated into three lines.

@@ -0,0 +1,176 @@
// SPDX-License-Identifier: MIT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the idea is good, we try to be as consistent as possible with modules that we have already implemented in Solidity, and we have an implementation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that I should removed my FeesRatio structure and use _feeDenominator in storage as in solidity contract ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solidity implementation doesn't have _feeDenominator in storage, but as part of the bytecode as a function. I mean to be as consistent as possible, only diverging when the benefit is obvious or there is a feature inconsistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your explanation, I have updated source code to be more consistent with solidity implementation.



#[starknet::interface]
pub trait IERC2981Setup<TState> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be provided as internal functions, but since this interface is not part of the standard, we shouldn't provide an external implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this trait and added internal functions matching the ones in Solidity implementation

@ptisserand ptisserand marked this pull request as ready for review July 15, 2024 14:20
@ptisserand ptisserand force-pushed the feature/erc2981-royalty branch from 0cb7fc4 to b9c8c9c Compare July 17, 2024 18:30
@ptisserand
Copy link
Contributor Author

branch rebased on main

@ptisserand
Copy link
Contributor Author

Hi @ericnordelo , are you agree that ERC2981 API documentation should be in docs/modules/ROOT/pages/api/erc2981.adoc ?
Is there a tool to generate documentation from cairo source code ?

src/token/common/erc2981/erc2981.cairo Outdated Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Outdated Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Outdated Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Outdated Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Show resolved Hide resolved
src/tests/token/erc2981/test_erc2981.cairo Outdated Show resolved Hide resolved

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

src/token/common/erc2981/erc2981.cairo Outdated Show resolved Hide resolved
src/tests/token/erc2981/test_erc2981.cairo Show resolved Hide resolved
Copy link
Member

@ericnordelo ericnordelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ptisserand, the PR is looking good and we have left a few comments. We have two big migrations ongoing (as you can see here), and that's why this won't make it into the next release, since those should be merged into it first. With this said, we will prioritize it and get it merged right after that release, and it will be included in the next one as the main priority.

src/tests/mocks/erc2981_mocks.cairo Outdated Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Outdated Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Outdated Show resolved Hide resolved
src/token/common/erc2981/erc2981.cairo Show resolved Hide resolved
.default_royalty_info
.write(RoyaltyInfo { receiver, royalty_fraction: fee_numerator })
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation lacks _deleteDefaultRoyalty function, which is present in the Solidity implementation. Imo, it makes sense to add it for consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please confirm if the following behavior is expected after _deleteDefaultRoyalty is called ?:

  • Default Royalty is 0% and receiver address is address(0)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is the expected one

src/token/common/erc2981/interface.cairo Show resolved Hide resolved
@ericnordelo
Copy link
Member

Thanks @ptisserand. Closing this in favor of #1091.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ERC2981 implementation in Cairo
3 participants