Skip to content

Commit

Permalink
use is_non_zero and is_zero and simplify update
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-of-bodom committed Aug 13, 2024
1 parent aa8e8c6 commit 40538f6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
38 changes: 17 additions & 21 deletions packages/token/src/erc6909/erc6909.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ pub mod ERC6909Component {
use core::num::traits::Zero;
use openzeppelin_account::interface::ISRC6_ID;
use openzeppelin_token::erc6909::interface;
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::{ContractAddress, get_caller_address};
use starknet::storage::Map;

#[storage]
Expand Down Expand Up @@ -142,7 +141,7 @@ pub mod ERC6909Component {
amount: u256
) -> bool {
let caller = get_caller_address();
self._transfer(caller, caller, receiver, id, amount);
self._transfer(caller, receiver, id, amount);
true
}

Expand All @@ -156,7 +155,7 @@ pub mod ERC6909Component {
) -> bool {
let caller = get_caller_address();
self._spend_allowance(sender, caller, id, amount);
self._transfer(caller, sender, receiver, id, amount);
self._transfer(sender, receiver, id, amount);
true
}

Expand Down Expand Up @@ -208,7 +207,7 @@ pub mod ERC6909Component {
amount: u256
) {
assert(!receiver.is_zero(), Errors::MINT_TO_ZERO);
self.update(get_caller_address(), Zero::zero(), receiver, id, amount);
self.update(Zero::zero(), receiver, id, amount);
}

/// Destroys `amount` of tokens from `account`.
Expand All @@ -226,7 +225,7 @@ pub mod ERC6909Component {
amount: u256
) {
assert(!account.is_zero(), Errors::BURN_FROM_ZERO);
self.update(get_caller_address(), account, Zero::zero(), id, amount);
self.update(account, Zero::zero(), id, amount);
}

/// Transfers an `amount` of tokens from `sender` to `receiver`, or alternatively mints (or
Expand All @@ -240,28 +239,25 @@ pub mod ERC6909Component {
/// Emits a `Transfer` event.
fn update(
ref self: ComponentState<TContractState>,
caller: ContractAddress, // For the `Transfer` event
sender: ContractAddress, // from
receiver: ContractAddress, // to
id: u256,
amount: u256
) {
Hooks::before_update(ref self, sender, receiver, id, amount);

let zero_address = Zero::zero();

if (sender != zero_address) {
if (sender.is_non_zero()) {
let sender_balance = self.ERC6909_balances.read((sender, id));
assert(sender_balance >= amount, Errors::INSUFFICIENT_BALANCE);
self.ERC6909_balances.write((sender, id), sender_balance - amount);
}

if (receiver != zero_address) {
if (receiver.is_non_zero()) {
let receiver_balance = self.ERC6909_balances.read((receiver, id));
self.ERC6909_balances.write((receiver, id), receiver_balance + amount);
}

self.emit(Transfer { caller, sender, receiver, id, amount });
self.emit(Transfer { caller: get_caller_address(), sender, receiver, id, amount });

Hooks::after_update(ref self, sender, receiver, id, amount);
}
Expand All @@ -282,18 +278,19 @@ pub mod ERC6909Component {
/// operator.
fn _spend_allowance(
ref self: ComponentState<TContractState>,
sender: ContractAddress,
owner: ContractAddress,
spender: ContractAddress,
id: u256,
amount: u256
) {
// In accordance with the transferFrom method, spenders with operator permission are not
// subject to allowance restrictions (https://eips.ethereum.org/EIPS/eip-6909).
if sender != spender && !self.ERC6909_operators.read((sender, spender)) {
let sender_allowance = self.ERC6909_allowances.read((sender, spender, id));
assert(sender_allowance >= amount, Errors::INSUFFICIENT_ALLOWANCE);
if owner != spender && !self.ERC6909_operators.read((owner, spender)) {
let sender_allowance = self.ERC6909_allowances.read((owner, spender, id));

if sender_allowance != Bounded::MAX {
self._approve(sender, spender, id, sender_allowance - amount)
assert(sender_allowance >= amount, Errors::INSUFFICIENT_ALLOWANCE);
self._approve(owner, spender, id, sender_allowance - amount)
}
}
}
Expand All @@ -314,8 +311,8 @@ pub mod ERC6909Component {
id: u256,
amount: u256
) {
assert(!owner.is_zero(), Errors::APPROVE_FROM_ZERO);
assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);
assert(owner.is_non_zero(), Errors::APPROVE_FROM_ZERO);
assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);
self.ERC6909_allowances.write((owner, spender, id), amount);
self.emit(Approval { owner, spender, id, amount });
}
Expand All @@ -331,15 +328,14 @@ pub mod ERC6909Component {
/// Emits a `Transfer` event.
fn _transfer(
ref self: ComponentState<TContractState>,
caller: ContractAddress,
sender: ContractAddress,
receiver: ContractAddress,
id: u256,
amount: u256
) {
assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);
assert(!receiver.is_zero(), Errors::TRANSFER_TO_ZERO);
self.update(caller, sender, receiver, id, amount);
self.update(sender, receiver, id, amount);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub mod ERC6909ContentURIComponent {
fn token_uri(self: @ComponentState<TContractState>, id: u256) -> ByteArray {
let contract_uri = self.contract_uri();
if contract_uri.len() == 0 {
return "";
""
} else {
return format!("{}{}", contract_uri, id);
format!("{}{}", contract_uri, id)
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/token/src/erc6909/extensions/erc6909_metadata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ pub mod ERC6909MetadataComponent {
symbol: ByteArray,
decimals: u8
) {
let zero_address = Zero::zero();

// In case of new ID mints update the token metadata
if (sender == zero_address) {
if (sender.is_zero()) {
let token_metadata_exists = self._token_metadata_exists(id);
if (!token_metadata_exists) {
self._set_token_metadata(id, name, symbol, decimals)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ pub mod ERC6909TokenSupplyComponent {
id: u256,
amount: u256
) {
let zero_address = Zero::zero();

// In case of mints we increase the total supply of this token ID
if (sender == zero_address) {
if (sender.is_zero()) {
let total_supply = self.ERC6909TokenSupply_total_supply.read(id);
self.ERC6909TokenSupply_total_supply.write(id, total_supply + amount);
}

// In case of burns we decrease the total supply of this token ID
if (receiver == zero_address) {
if (receiver.is_zero()) {
let total_supply = self.ERC6909TokenSupply_total_supply.read(id);
self.ERC6909TokenSupply_total_supply.write(id, total_supply - amount);
}
Expand Down

0 comments on commit 40538f6

Please sign in to comment.