Skip to content

Commit

Permalink
refactor: minor improvements for token module
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnordelo committed Nov 12, 2024
1 parent fcace70 commit 4ddb0c0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
13 changes: 6 additions & 7 deletions packages/token/src/common/erc2981/erc2981.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ 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;
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
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

0 comments on commit 4ddb0c0

Please sign in to comment.