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

Minor improvements for the token module #1208

Merged
merged 3 commits into from
Nov 13, 2024
Merged
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
8 changes: 4 additions & 4 deletions docs/modules/ROOT/pages/api/token_common.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Sets the royalty information that all ids in this contract will default to.
[[IERC2981Admin-delete_default_royalty]]
==== `[.contract-item-name]#++delete_default_royalty++#++()++` [.item-kind]#external#

Removes default royalty information.
Sets the default royalty percentage and receiver to zero.
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍


[.contract-item]
[[IERC2981Admin-set_token_royalty]]
Expand Down Expand Up @@ -283,7 +283,7 @@ Requirements:
[[ERC2981AdminOwnableImpl-delete_default_royalty]]
==== `[.contract-item-name]#++delete_default_royalty++#++(ref self: ContractState)++` [.item-kind]#external#

Removes default royalty information.
Sets the default royalty percentage and receiver to zero.

Requirements:

Expand Down Expand Up @@ -341,7 +341,7 @@ Requirements:
[[ERC2981AdminAccessControlImpl-delete_default_royalty]]
==== `[.contract-item-name]#++delete_default_royalty++#++(ref self: ContractState)++` [.item-kind]#external#

Removes default royalty information.
Sets the default royalty percentage and receiver to zero.

Requirements:

Expand Down Expand Up @@ -413,7 +413,7 @@ Requirements:
[[ERC2981Component-_delete_default_royalty]]
==== `[.contract-item-name]#++_delete_default_royalty++#++(ref self: ContractState)++` [.item-kind]#internal#

Removes default royalty information.
Sets the default royalty percentage and receiver to zero.

[.contract-item]
[[ERC2981Component-_token_royalty]]
Expand Down
17 changes: 8 additions & 9 deletions packages/token/src/common/erc2981/erc2981.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ pub mod ERC2981Component {
use openzeppelin_introspection::src5::SRC5Component::InternalTrait as SRC5InternalTrait;
use openzeppelin_introspection::src5::SRC5Component::SRC5Impl;
use openzeppelin_introspection::src5::SRC5Component;

use starknet::ContractAddress;
use starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
};

/// Role for the admin responsible for managing royalty settings.
pub const ROYALTY_ADMIN_ROLE: felt252 = selector!("ROYALTY_ADMIN_ROLE");

// This default denominator is only used when the DefaultConfig
// is in scope in the implementing contract.
pub const DEFAULT_FEE_DENOMINATOR: u128 = 10_000;

#[derive(Serde, Drop, starknet::Store)]
struct RoyaltyInfo {
pub struct RoyaltyInfo {
pub receiver: ContractAddress,
pub royalty_fraction: u128,
}
Expand All @@ -52,7 +54,7 @@ pub mod ERC2981Component {
pub ERC2981_token_royalty_info: Map<u256, RoyaltyInfo>,
}

mod Errors {
pub mod Errors {
pub const INVALID_ROYALTY: felt252 = 'ERC2981: invalid royalty';
pub const INVALID_ROYALTY_RECEIVER: felt252 = 'ERC2981: invalid receiver';
pub const INVALID_FEE_DENOMINATOR: felt252 = 'Invalid fee denominator';
Expand Down Expand Up @@ -176,7 +178,7 @@ pub mod ERC2981Component {
self._set_default_royalty(receiver, fee_numerator)
}

/// Removes default royalty information.
/// Sets the default royalty percentage and receiver to zero.
///
/// Requirements:
///
Expand Down Expand Up @@ -219,9 +221,6 @@ pub mod ERC2981Component {
// AccessControl-based implementation of IERC2981Admin
//

/// Role for the admin responsible for managing royalty settings.
pub const ROYALTY_ADMIN_ROLE: felt252 = selector!("ROYALTY_ADMIN_ROLE");

#[embeddable_as(ERC2981AdminAccessControlImpl)]
impl ERC2981AdminAccessControl<
TContractState,
Expand All @@ -245,7 +244,7 @@ pub mod ERC2981Component {
self._set_default_royalty(receiver, fee_numerator)
}

/// Removes default royalty information.
/// Sets the default royalty percentage and receiver to zero.
///
/// Requirements:
///
Expand Down Expand Up @@ -349,7 +348,7 @@ pub mod ERC2981Component {
.write(RoyaltyInfo { receiver, royalty_fraction: fee_numerator })
}

/// Removes default royalty information.
/// Sets the default royalty percentage and receiver to zero.
fn _delete_default_royalty(ref self: ComponentState<TContractState>) {
self
.ERC2981_default_royalty_info
Expand Down
2 changes: 1 addition & 1 deletion packages/token/src/common/erc2981/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait IERC2981Admin<TState> {
/// Sets the royalty information that all ids in this contract will default to.
fn set_default_royalty(ref self: TState, receiver: ContractAddress, fee_numerator: u128);

/// Removes default royalty information.
/// Sets the default royalty percentage and receiver to zero.
fn delete_default_royalty(ref self: TState);

/// Sets the royalty information for a specific token id that takes precedence over the global
Expand Down
15 changes: 5 additions & 10 deletions packages/token/src/erc1155/erc1155.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ pub mod ERC1155Component {
use openzeppelin_introspection::src5::SRC5Component::InternalTrait as SRC5InternalTrait;
use openzeppelin_introspection::src5::SRC5Component::SRC5Impl;
use openzeppelin_introspection::src5::SRC5Component;
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
};
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::{ContractAddress, get_caller_address};

#[storage]
pub struct Storage {
Expand Down Expand Up @@ -181,8 +178,7 @@ pub mod ERC1155Component {
/// - `from` is not the zero address.
/// - `to` is not the zero address.
/// - If `to` refers to a non-account contract, it must implement
/// `IERC1155Receiver::on_ERC1155_received`
/// and return the required magic value.
/// `IERC1155Receiver::on_ERC1155_received` and return the required magic value.
///
/// Emits a `TransferSingle` event.
fn safe_transfer_from(
Expand Down Expand Up @@ -213,8 +209,7 @@ pub mod ERC1155Component {
/// - `to` is not the zero address.
/// - `token_ids` and `values` must have the same length.
/// - If `to` refers to a non-account contract, it must implement
/// `IERC1155Receiver::on_ERC1155_batch_received`
/// and return the acceptance magic value.
/// `IERC1155Receiver::on_ERC1155_batch_received` and return the acceptance magic value.
///
/// Emits either a `TransferSingle` or a `TransferBatch` event, depending on the length of
/// the array arguments.
Expand Down
3 changes: 3 additions & 0 deletions packages/token/src/erc1155/erc1155_receiver.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub mod ERC1155ReceiverComponent {
IERC1155_RECEIVER_ID
}

/// Called whenever the implementing contract receives a batch of `values` through
/// a safe transfer. This function must return `IERC1155_RECEIVER_ID`
/// to confirm the token transfer.
fn on_erc1155_batch_received(
self: @ComponentState<TContractState>,
operator: ContractAddress,
Expand Down
17 changes: 9 additions & 8 deletions packages/token/src/erc20/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub mod ERC20Component {
use starknet::ContractAddress;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::{get_block_timestamp, get_caller_address, get_contract_address, get_tx_info};

#[storage]
pub struct Storage {
Expand Down Expand Up @@ -136,7 +135,7 @@ pub mod ERC20Component {
fn transfer(
ref self: ComponentState<TContractState>, recipient: ContractAddress, amount: u256
) -> bool {
let sender = get_caller_address();
let sender = starknet::get_caller_address();
self._transfer(sender, recipient, amount);
true
}
Expand All @@ -158,7 +157,7 @@ pub mod ERC20Component {
recipient: ContractAddress,
amount: u256
) -> bool {
let caller = get_caller_address();
let caller = starknet::get_caller_address();
self._spend_allowance(sender, caller, amount);
self._transfer(sender, recipient, amount);
true
Expand All @@ -174,7 +173,7 @@ pub mod ERC20Component {
fn approve(
ref self: ComponentState<TContractState>, spender: ContractAddress, amount: u256
) -> bool {
let caller = get_caller_address();
let caller = starknet::get_caller_address();
self._approve(caller, spender, amount);
true
}
Expand Down Expand Up @@ -299,7 +298,7 @@ pub mod ERC20Component {
/// off-chain signatures. This approach allows token holders to delegate their approval to spend
/// tokens without executing an on-chain transaction, reducing gas costs and enhancing
/// usability.
/// See https://eips.ethereum.org/EIPS/eip-2612.
/// See https://eips.ethereum.org/EIPS/eip-2612.
///
/// The message signed and the signature must follow the SNIP-12 standard for hashing and
/// signing typed structured data.
Expand Down Expand Up @@ -344,14 +343,16 @@ pub mod ERC20Component {
signature: Span<felt252>
) {
// 1. Ensure the deadline is not missed
assert(get_block_timestamp() <= deadline, Errors::EXPIRED_PERMIT_SIGNATURE);
assert(starknet::get_block_timestamp() <= deadline, Errors::EXPIRED_PERMIT_SIGNATURE);

// 2. Get the current nonce and increment it
let mut nonces_component = get_dep_component_mut!(ref self, Nonces);
let nonce = nonces_component.use_nonce(owner);

// 3. Make a call to the account to validate permit signature
let permit = Permit { token: get_contract_address(), spender, amount, nonce, deadline };
let permit = Permit {
token: starknet::get_contract_address(), spender, amount, nonce, deadline
};
let permit_hash = permit.get_message_hash(owner);
let is_valid_sig_felt = ISRC6Dispatcher { contract_address: owner }
.is_valid_signature(permit_hash, signature.into());
Expand All @@ -377,7 +378,7 @@ pub mod ERC20Component {
let domain = StarknetDomain {
name: Metadata::name(),
version: Metadata::version(),
chain_id: get_tx_info().unbox().chain_id,
chain_id: starknet::get_tx_info().unbox().chain_id,
revision: 1
};
domain.hash_struct()
Expand Down
9 changes: 3 additions & 6 deletions packages/token/src/erc721/erc721.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ pub mod ERC721Component {
use openzeppelin_introspection::src5::SRC5Component::InternalTrait as SRC5InternalTrait;
use openzeppelin_introspection::src5::SRC5Component::SRC5Impl;
use openzeppelin_introspection::src5::SRC5Component;
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
};
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::{ContractAddress, get_caller_address};

#[storage]
pub struct Storage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/// # ERC721Enumerable Component
///
/// Extension of ERC721 as defined in the EIP that adds enumerability of all the token ids in the
/// contract as well as all token ids owned by each account.
/// This extension allows contracts to publish their entire list of NFTs and make them discoverable.
/// contract as well as all token ids owned by each account. It allows contracts to publish
/// their entire list of NFTs and make them discoverable.
///
/// NOTE: Implementing ERC721Component is a requirement for this component to be implemented.
///
Expand All @@ -24,10 +24,8 @@ pub mod ERC721EnumerableComponent {
use openzeppelin_introspection::src5::SRC5Component::InternalTrait as SRC5InternalTrait;
use openzeppelin_introspection::src5::SRC5Component;
use starknet::ContractAddress;
use starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
};
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};

#[storage]
pub struct Storage {
Expand Down