Skip to content

Commit

Permalink
prefix unckecked_ for fn requiring safety checks before calling
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Aug 28, 2023
1 parent e7a82e4 commit fcb2d83
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 42 deletions.
4 changes: 2 additions & 2 deletions crates/dojo-erc/src/erc1155/components.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait ERC1155BalanceTrait {
fn balance_of(
world: IWorldDispatcher, token: ContractAddress, account: ContractAddress, id: felt252
) -> u128;
fn transfer_tokens(
fn unchecked_transfer_tokens(
world: IWorldDispatcher,
token: ContractAddress,
from: ContractAddress,
Expand All @@ -57,7 +57,7 @@ impl ERC1155BalanceImpl of ERC1155BalanceTrait {
get!(world, (token, id, account), ERC1155Balance).amount
}

fn transfer_tokens(
fn unchecked_transfer_tokens(
world: IWorldDispatcher,
token: ContractAddress,
from: ContractAddress,
Expand Down
52 changes: 38 additions & 14 deletions crates/dojo-erc/src/erc1155/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn emit_transfer_batch(
emit!(world, event);
}

fn update(
fn unchecked_update(
world: IWorldDispatcher,
operator: ContractAddress,
token: ContractAddress,
Expand All @@ -71,14 +71,7 @@ fn update(
) {
assert(ids.len() == amounts.len(), 'ERC1155: invalid length');

assert(
operator == from
|| OperatorApprovalTrait::is_approved_for_all(world, token, from, operator)
|| from.is_zero(),
'ERC1155: insufficient approval'
);

ERC1155BalanceTrait::transfer_tokens(world, token, from, to, ids.span(), amounts.span());
ERC1155BalanceTrait::unchecked_transfer_tokens(world, token, from, to, ids.span(), amounts.span());

if (ids.len() == 1) {
let id = *ids.at(0);
Expand Down Expand Up @@ -172,7 +165,7 @@ mod ERC1155SetApprovalForAll {
let ERC1155SetApprovalForAllParams{token, owner, operator, approved } = params;
assert(owner != operator, 'ERC1155: wrong approval');

OperatorApprovalTrait::set_approval_for_all(ctx.world, token, owner, operator, approved);
OperatorApprovalTrait::unchecked_set_approval_for_all(ctx.world, token, owner, operator, approved);

let event = ApprovalForAll { owner, operator, approved };
IERC1155EventsDispatcher { contract_address: token }.on_approval_for_all(event.clone());
Expand Down Expand Up @@ -205,6 +198,7 @@ mod ERC1155SafeTransferFrom {
use zeroable::Zeroable;
use starknet::ContractAddress;


#[derive(Drop, Serde)]
struct ERC1155SafeTransferFromParams {
token: ContractAddress,
Expand All @@ -222,7 +216,17 @@ mod ERC1155SafeTransferFrom {
assert(to.is_non_zero(), 'ERC1155: to cannot be 0');
assert(from.is_non_zero(), 'ERC1155: from cannot be 0');

super::update(ctx.world, operator, token, from, to, array![id], array![amount], data);
assert(
operator == from
|| super::OperatorApprovalTrait::is_approved_for_all(
ctx.world, token, from, operator
),
'ERC1155: insufficient approval'
);

super::unchecked_update(
ctx.world, operator, token, from, to, array![id], array![amount], data
);
}
}

Expand Down Expand Up @@ -254,7 +258,15 @@ mod ERC1155SafeBatchTransferFrom {
assert(to.is_non_zero(), 'ERC1155: to cannot be 0');
assert(from.is_non_zero(), 'ERC1155: from cannot be 0');

super::update(ctx.world, operator, token, from, to, ids, amounts, data);
assert(
operator == from
|| super::OperatorApprovalTrait::is_approved_for_all(
ctx.world, token, from, operator
),
'ERC1155: insufficient approval'
);

super::unchecked_update(ctx.world, operator, token, from, to, ids, amounts, data);
}
}

Expand Down Expand Up @@ -284,7 +296,9 @@ mod ERC1155Mint {
assert(ctx.origin == operator || ctx.origin == token, 'ERC1155: not authorized');
assert(to.is_non_zero(), 'ERC1155: invalid receiver');

super::update(ctx.world, operator, token, Zeroable::zero(), to, ids, amounts, data);
super::unchecked_update(
ctx.world, operator, token, Zeroable::zero(), to, ids, amounts, data
);
}
}

Expand Down Expand Up @@ -312,6 +326,16 @@ mod ERC1155Burn {
assert(ctx.origin == operator || ctx.origin == token, 'ERC1155: not authorized');
assert(from.is_non_zero(), 'ERC1155: invalid sender');

super::update(ctx.world, operator, token, from, Zeroable::zero(), ids, amounts, array![]);
assert(
operator == from
|| super::OperatorApprovalTrait::is_approved_for_all(
ctx.world, token, from, operator
),
'ERC1155: insufficient approval'
);

super::unchecked_update(
ctx.world, operator, token, from, Zeroable::zero(), ids, amounts, array![]
);
}
}
21 changes: 10 additions & 11 deletions crates/dojo-erc/src/erc721/components.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait ERC721OwnerTrait {
fn owner_of(
world: IWorldDispatcher, token: ContractAddress, token_id: felt252
) -> ContractAddress;
fn set_owner(
fn unchecked_set_owner(
world: IWorldDispatcher, token: ContractAddress, token_id: felt252, account: ContractAddress
);
}
Expand All @@ -42,8 +42,7 @@ impl ERC721OwnerImpl of ERC721OwnerTrait {
get!(world, (token, token_id), ERC721Owner).address
}

// perform safety checks before calling this fn
fn set_owner(
fn unchecked_set_owner(
world: IWorldDispatcher, token: ContractAddress, token_id: felt252, account: ContractAddress
) {
let mut owner = get!(world, (token, token_id), ERC721Owner);
Expand All @@ -70,19 +69,19 @@ trait ERC721BalanceTrait {
fn balance_of(
world: IWorldDispatcher, token: ContractAddress, account: ContractAddress
) -> u128;
fn transfer_token(
fn unchecked_transfer_token(
world: IWorldDispatcher,
token: ContractAddress,
from: ContractAddress,
to: ContractAddress,
amount: u128,
);

fn increase_balance(
fn unchecked_increase_balance(
world: IWorldDispatcher, token: ContractAddress, owner: ContractAddress, amount: u128,
);

fn decrease_balance(
fn unchecked_decrease_balance(
world: IWorldDispatcher, token: ContractAddress, owner: ContractAddress, amount: u128,
);
}
Expand All @@ -96,7 +95,7 @@ impl ERC721BalanceImpl of ERC721BalanceTrait {
get!(world, (token, account), ERC721Balance).amount
}

fn transfer_token(
fn unchecked_transfer_token(
world: IWorldDispatcher,
token: ContractAddress,
from: ContractAddress,
Expand All @@ -112,15 +111,15 @@ impl ERC721BalanceImpl of ERC721BalanceTrait {
set!(world, (to_balance));
}

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

fn decrease_balance(
fn unchecked_decrease_balance(
world: IWorldDispatcher, token: ContractAddress, owner: ContractAddress, amount: u128,
) {
let mut balance = get!(world, (token, owner), ERC721Balance);
Expand Down Expand Up @@ -149,7 +148,7 @@ trait ERC721TokenApprovalTrait {
world: IWorldDispatcher, token: ContractAddress, token_id: felt252
) -> ContractAddress;

fn approve(
fn unchecked_approve(
world: IWorldDispatcher, token: ContractAddress, token_id: felt252, to: ContractAddress
);
}
Expand All @@ -162,7 +161,7 @@ impl ERC721TokenApprovalImpl of ERC721TokenApprovalTrait {
approval.address
}

fn approve(
fn unchecked_approve(
world: IWorldDispatcher, token: ContractAddress, token_id: felt252, to: ContractAddress
) {
let mut approval = get!(world, (token, token_id), ERC721TokenApproval);
Expand Down
20 changes: 10 additions & 10 deletions crates/dojo-erc/src/erc721/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod ERC721Approve {
);
// // ERC721: approve caller is not token owner or approved for all
assert(caller == owner || is_approved_for_all, 'ERC721: unauthorized caller');
ERC721TokenApprovalTrait::approve(ctx.world, token, token_id, to,);
ERC721TokenApprovalTrait::unchecked_approve(ctx.world, token, token_id, to,);

// emit events
super::emit_approval(ctx.world, token, owner, to, token_id);
Expand Down Expand Up @@ -124,7 +124,7 @@ mod ERC721SetApprovalForAll {
assert(token == ctx.origin, 'ERC721: not authorized');
assert(owner != operator, 'ERC721: self approval');

OperatorApprovalTrait::set_approval_for_all(ctx.world, token, owner, operator, approved);
OperatorApprovalTrait::unchecked_set_approval_for_all(ctx.world, token, owner, operator, approved);

// emit event
super::emit_approval_for_all(ctx.world, token, owner, operator, approved);
Expand Down Expand Up @@ -171,9 +171,9 @@ mod ERC721TransferFrom {
'ERC721: unauthorized caller'
);

ERC721OwnerTrait::set_owner(ctx.world, token, token_id, to);
ERC721BalanceTrait::transfer_token(ctx.world, token, from, to, 1);
ERC721TokenApprovalTrait::approve(ctx.world, token, token_id, Zeroable::zero());
ERC721OwnerTrait::unchecked_set_owner(ctx.world, token, token_id, to);
ERC721BalanceTrait::unchecked_transfer_token(ctx.world, token, from, to, 1);
ERC721TokenApprovalTrait::unchecked_approve(ctx.world, token, token_id, Zeroable::zero());

// emit events
super::emit_transfer(ctx.world, token, from, to, token_id);
Expand Down Expand Up @@ -206,8 +206,8 @@ mod ERC721Mint {
let owner = ERC721OwnerTrait::owner_of(ctx.world, token, token_id);
assert(owner.is_zero(), 'ERC721: already minted');

ERC721BalanceTrait::increase_balance(ctx.world, token, recipient, 1);
ERC721OwnerTrait::set_owner(ctx.world, token, token_id, recipient);
ERC721BalanceTrait::unchecked_increase_balance(ctx.world, token, recipient, 1);
ERC721OwnerTrait::unchecked_set_owner(ctx.world, token, token_id, recipient);
// emit events
super::emit_transfer(ctx.world, token, Zeroable::zero(), recipient, token_id);
}
Expand Down Expand Up @@ -250,8 +250,8 @@ mod ERC721Burn {
'ERC721: unauthorized caller'
);

ERC721BalanceTrait::decrease_balance(ctx.world, token, owner, 1);
ERC721OwnerTrait::set_owner(ctx.world, token, token_id, Zeroable::zero());
ERC721BalanceTrait::unchecked_decrease_balance(ctx.world, token, owner, 1);
ERC721OwnerTrait::unchecked_set_owner(ctx.world, token, token_id, Zeroable::zero());

// emit events
super::emit_transfer(ctx.world, token, owner, Zeroable::zero(), token_id);
Expand All @@ -270,7 +270,7 @@ mod ERC721SetBaseUri {

fn execute(ctx: Context, token: ContractAddress, uri: felt252) {
assert(ctx.origin == token, 'ERC721: not authorized');
BaseUriTrait::set_base_uri(ctx.world, token, uri);
BaseUriTrait::unchecked_set_base_uri(ctx.world, token, uri);
// TODO: emit event
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct BaseUri {

trait BaseUriTrait {
fn get_base_uri(world: IWorldDispatcher, token: ContractAddress) -> felt252;
fn set_base_uri(world: IWorldDispatcher, token: ContractAddress, new_base_uri: felt252);
fn unchecked_set_base_uri(world: IWorldDispatcher, token: ContractAddress, new_base_uri: felt252);
}

impl BaseUriImpl of BaseUriTrait {
Expand All @@ -19,8 +19,7 @@ impl BaseUriImpl of BaseUriTrait {
base_uri.uri
}

// perform safety checks before calling this fn
fn set_base_uri(world: IWorldDispatcher, token: ContractAddress, new_base_uri: felt252) {
fn unchecked_set_base_uri(world: IWorldDispatcher, token: ContractAddress, new_base_uri: felt252) {
let mut base_uri = get!(world, (token), BaseUri);
base_uri.uri = new_base_uri;
set!(world, (base_uri))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait OperatorApprovalTrait {
operator: ContractAddress
) -> bool;

fn set_approval_for_all(
fn unchecked_set_approval_for_all(
world: IWorldDispatcher,
token: ContractAddress,
owner: ContractAddress,
Expand All @@ -41,7 +41,7 @@ impl OperatorApprovalImpl of OperatorApprovalTrait {
}

// perform safety checks before calling this fn
fn set_approval_for_all(
fn unchecked_set_approval_for_all(
world: IWorldDispatcher,
token: ContractAddress,
owner: ContractAddress,
Expand Down

0 comments on commit fcb2d83

Please sign in to comment.