Skip to content

Commit

Permalink
fix: ERC1155Balance component to respect token/account/id order
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Aug 30, 2023
1 parent fcb2d83 commit cf2bb11
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions crates/dojo-erc/src/erc1155/components.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ struct ERC1155Balance {
#[key]
token: ContractAddress,
#[key]
token_id: felt252,
#[key]
account: ContractAddress,
#[key]
token_id: felt252,
amount: u128
}

Expand All @@ -46,6 +46,20 @@ trait ERC1155BalanceTrait {
ids: Span<felt252>,
amounts: Span<u128>,
);
fn unchecked_increase_balance(
world: IWorldDispatcher,
token: ContractAddress,
owner: ContractAddress,
id: felt252,
amount: u128,
);
fn unchecked_decrease_balance(
world: IWorldDispatcher,
token: ContractAddress,
owner: ContractAddress,
id: felt252,
amount: u128,
);
}

impl ERC1155BalanceImpl of ERC1155BalanceTrait {
Expand All @@ -54,7 +68,7 @@ impl ERC1155BalanceImpl of ERC1155BalanceTrait {
) -> u128 {
// ERC1155: address zero is not a valid owner
assert(account.is_non_zero(), 'ERC1155: invalid owner address');
get!(world, (token, id, account), ERC1155Balance).amount
get!(world, (token, account, id), ERC1155Balance).amount
}

fn unchecked_transfer_tokens(
Expand All @@ -73,16 +87,40 @@ impl ERC1155BalanceImpl of ERC1155BalanceTrait {
let amount: u128 = *amounts.pop_front().unwrap();

if (from.is_non_zero()) {
let mut from_balance = get!(world, (token, id, from), ERC1155Balance);
let mut from_balance = get!(world, (token, from, id), ERC1155Balance);
from_balance.amount -= amount;
set!(world, (from_balance));
}

if (to.is_non_zero()) {
let mut to_balance = get!(world, (token, id, to), ERC1155Balance);
let mut to_balance = get!(world, (token, to, id), ERC1155Balance);
to_balance.amount += amount;
set!(world, (to_balance));
};
};
}

fn unchecked_increase_balance(
world: IWorldDispatcher,
token: ContractAddress,
owner: ContractAddress,
id: felt252,
amount: u128,
) {
let mut balance = get!(world, (token, owner, id), ERC1155Balance);
balance.amount += amount;
set!(world, (balance));
}

fn unchecked_decrease_balance(
world: IWorldDispatcher,
token: ContractAddress,
owner: ContractAddress,
id: felt252,
amount: u128,
) {
let mut balance = get!(world, (token, owner, id), ERC1155Balance);
balance.amount -= amount;
set!(world, (balance));
}
}

0 comments on commit cf2bb11

Please sign in to comment.