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

erc20 dual dispatcher #622

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
aea2e6f
add camel methods
andrew-fleming May 27, 2023
504fb97
update erc20 dispatcher path
andrew-fleming May 27, 2023
a4c65a0
refactor tests, add tests for camel methods
andrew-fleming May 27, 2023
ed63cae
fix formatting
andrew-fleming May 27, 2023
d80f3f3
Merge branch 'cairo-1' into cairo1-erc20-camel
andrew-fleming Jun 14, 2023
20f4f10
add try_selector_with_fallback and bool impl
andrew-fleming Jun 16, 2023
66078d3
add erc20 selectors
andrew-fleming Jun 16, 2023
af5a0c6
add abi to traits
andrew-fleming Jun 16, 2023
85dd8d2
add dual20 mocks
andrew-fleming Jun 16, 2023
a19137d
add panic trait
andrew-fleming Jun 16, 2023
cfcf476
add dual20 mocks
andrew-fleming Jun 16, 2023
18cfcf8
add dual20
andrew-fleming Jun 16, 2023
f96c57f
add dual20
andrew-fleming Jun 16, 2023
1f9d52e
add dual20 tests
andrew-fleming Jun 16, 2023
6799bd6
tidy up tests
andrew-fleming Jun 18, 2023
17aa5ff
move tests to token dir
andrew-fleming Jun 18, 2023
732a9e5
fix formatting
andrew-fleming Jun 18, 2023
905e845
clean up tests
andrew-fleming Jun 18, 2023
e6f9e88
restructure token tests
andrew-fleming Jun 27, 2023
9f4be25
remove panic trait
andrew-fleming Jun 27, 2023
5583ef0
reorder imports
andrew-fleming Jun 27, 2023
086c00b
remove unused imports
andrew-fleming Jun 27, 2023
7ae7144
fix comments
andrew-fleming Jun 27, 2023
feeeb85
update const selector casings
andrew-fleming Jun 27, 2023
d687134
remove comments
andrew-fleming Jun 27, 2023
2c83765
Apply suggestions from code review
andrew-fleming Jun 27, 2023
0e118ab
change target member to contract_address
andrew-fleming Jun 27, 2023
5f08490
fix formatting
andrew-fleming Jun 27, 2023
a62f872
fix dispatcher calls
andrew-fleming Jun 27, 2023
b22a014
change dispatcher to dual_dispatcher
andrew-fleming Jun 27, 2023
ba2be65
reorder use clauses
andrew-fleming Jun 29, 2023
e856350
move selectors to selectors mod
andrew-fleming Jun 29, 2023
c25dfe9
fix formatting
andrew-fleming Jun 29, 2023
ef1bdd6
reorder selectors
andrew-fleming Jun 29, 2023
c7f9299
fix conflicts
andrew-fleming Jul 3, 2023
aff9184
remove duplicate selectors
andrew-fleming Jul 3, 2023
312e500
remove ignore
andrew-fleming Jul 3, 2023
b166179
simplify dispatcher vars
andrew-fleming Jul 3, 2023
ea5157c
fix conflicts
andrew-fleming Jul 3, 2023
7130178
fix comment
andrew-fleming Jul 3, 2023
2dc69c0
Apply suggestions from code review
andrew-fleming Jul 4, 2023
772338c
fix test constants
andrew-fleming Jul 4, 2023
c14d171
add SerializedAppend trait
andrew-fleming Jul 4, 2023
8cde2d9
integrate append_serde
andrew-fleming Jul 4, 2023
1967aff
integrate append_serde and utils selector
andrew-fleming Jul 4, 2023
d462dbb
change u256 fns to constants
andrew-fleming Jul 4, 2023
29947ac
change u256 fns to constants
andrew-fleming Jul 4, 2023
3617355
move dual721 test to token/, apply append_serde, change u256 fn to co…
andrew-fleming Jul 4, 2023
51b43d4
fix formatting
andrew-fleming Jul 4, 2023
6eb3ca3
add append_serde
andrew-fleming Jul 5, 2023
0f98817
move append_serde to serde
andrew-fleming Jul 5, 2023
9844d12
update append_serde path
andrew-fleming Jul 5, 2023
07cf3c1
change dispatcher to IERC20
andrew-fleming Jul 5, 2023
8cc4a49
remove unnecessary into()
andrew-fleming Jul 5, 2023
a91263c
remove bindings
andrew-fleming Jul 5, 2023
fe90c2a
add binding to empty arrays
andrew-fleming Jul 5, 2023
7d4be03
fix formatting
andrew-fleming Jul 5, 2023
bc4bb07
fix conflicts
andrew-fleming Jul 5, 2023
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: 5 additions & 3 deletions src/openzeppelin/access/ownable/dual_ownable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use starknet::ContractAddress;
use starknet::Felt252TryIntoContractAddress;
use starknet::SyscallResultTrait;
use starknet::call_contract_syscall;
use traits::Into;
use traits::TryInto;

use openzeppelin::utils::try_selector_with_fallback;
use openzeppelin::utils::Felt252TryIntoBool;
use openzeppelin::utils::selectors;
use openzeppelin::utils::serde::SerializedAppend;

#[derive(Copy, Drop)]
struct DualCaseOwnable {
Expand All @@ -26,7 +26,9 @@ trait DualCaseOwnableTrait {

impl DualCaseOwnableImpl of DualCaseOwnableTrait {
fn owner(self: @DualCaseOwnable) -> ContractAddress {
(*call_contract_syscall(*self.contract_address, selectors::owner, ArrayTrait::new().span())
let args = ArrayTrait::new();

(*call_contract_syscall(*self.contract_address, selectors::owner, args.span())
.unwrap_syscall()
.at(0))
.try_into()
Expand All @@ -35,7 +37,7 @@ impl DualCaseOwnableImpl of DualCaseOwnableTrait {

fn transfer_ownership(self: @DualCaseOwnable, new_owner: ContractAddress) {
let mut args = ArrayTrait::new();
args.append(new_owner.into());
args.append_serde(new_owner);

try_selector_with_fallback(
*self.contract_address,
Expand Down
4 changes: 1 addition & 3 deletions src/openzeppelin/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ mod access;
mod test_reentrancyguard;
mod test_src5;
mod test_account;
mod test_erc20;
mod test_erc721;
mod test_dual721;
mod token;
mod test_initializable;
mod test_pausable;
mod mocks;
Expand Down
4 changes: 4 additions & 0 deletions src/openzeppelin/tests/mocks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ mod reentrancy_mock;
mod erc721_receiver;
mod erc721_panic_mock;
mod mock_pausable;
mod camel20_mock;
mod snake20_mock;
mod erc20_panic;
mod non721_mock;
mod accesscontrol_panic_mock;
mod camel_accesscontrol_mock;
mod snake_accesscontrol_mock;
Expand Down
58 changes: 58 additions & 0 deletions src/openzeppelin/tests/mocks/camel20_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#[contract]
mod CamelERC20Mock {
use starknet::ContractAddress;
use openzeppelin::token::erc20::ERC20;

#[constructor]
fn constructor(
name: felt252, symbol: felt252, initial_supply: u256, recipient: ContractAddress
) {
ERC20::initializer(name, symbol);
ERC20::_mint(recipient, initial_supply);
}

#[view]
fn name() -> felt252 {
ERC20::name()
}

#[view]
fn symbol() -> felt252 {
ERC20::symbol()
}

#[view]
fn decimals() -> u8 {
ERC20::decimals()
}

#[view]
fn totalSupply() -> u256 {
ERC20::totalSupply()
}

#[view]
fn balanceOf(account: ContractAddress) -> u256 {
ERC20::balanceOf(account)
}

#[view]
fn allowance(owner: ContractAddress, spender: ContractAddress) -> u256 {
ERC20::allowance(owner, spender)
}

#[external]
fn transfer(recipient: ContractAddress, amount: u256) -> bool {
ERC20::transfer(recipient, amount)
}

#[external]
fn transferFrom(sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool {
ERC20::transferFrom(sender, recipient, amount)
}

#[external]
fn approve(spender: ContractAddress, amount: u256) -> bool {
ERC20::approve(spender, amount)
}
}
96 changes: 96 additions & 0 deletions src/openzeppelin/tests/mocks/erc20_panic.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Although these modules are designed to panic, functions
// still need a valid return value. We chose:
//
// 3 for felt252 and u8
// zero for ContractAddress
// false for bool
// u256 { 3, 3 } for u256

#[contract]
mod SnakeERC20Panic {
use starknet::ContractAddress;

///
/// Agnostic
///

#[view]
fn name() -> felt252 {
panic_with_felt252('Some error');
3
}

#[view]
fn symbol() -> felt252 {
panic_with_felt252('Some error');
3
}

#[view]
fn decimals() -> u8 {
panic_with_felt252('Some error');
3_u8
}

#[view]
fn allowance(owner: ContractAddress, spender: ContractAddress) -> u256 {
panic_with_felt252('Some error');
u256 { low: 3, high: 3 }
}

#[external]
fn transfer(recipient: ContractAddress, amount: u256) -> bool {
panic_with_felt252('Some error');
false
}

#[external]
fn approve(to: ContractAddress, token_id: u256) -> bool {
panic_with_felt252('Some error');
false
}

///
/// Snake
///

#[view]
fn total_supply() -> u256 {
panic_with_felt252('Some error');
u256 { low: 3, high: 3 }
}

#[view]
fn balance_of(account: ContractAddress) -> u256 {
panic_with_felt252('Some error');
u256 { low: 3, high: 3 }
}

#[external]
fn transfer_from(from: ContractAddress, to: ContractAddress, amount: u256) -> bool {
panic_with_felt252('Some error');
false
}
}

#[contract]
mod CamelERC20Panic {
use starknet::ContractAddress;

#[view]
fn totalSupply() -> u256 {
panic_with_felt252('Some error');
u256 { low: 3, high: 3 }
}

#[view]
fn balanceOf(account: ContractAddress) -> u256 {
panic_with_felt252('Some error');
u256 { low: 3, high: 3 }
}

#[external]
fn transferFrom(sender: ContractAddress, recipient: ContractAddress, amount: u256) {
panic_with_felt252('Some error');
}
}
7 changes: 7 additions & 0 deletions src/openzeppelin/tests/mocks/non721_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[contract]
mod NonERC721 {
#[view]
fn nope() -> bool {
false
}
}
58 changes: 58 additions & 0 deletions src/openzeppelin/tests/mocks/snake20_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#[contract]
mod SnakeERC20Mock {
use starknet::ContractAddress;
use openzeppelin::token::erc20::ERC20;

#[constructor]
fn constructor(
name: felt252, symbol: felt252, initial_supply: u256, recipient: ContractAddress
) {
ERC20::initializer(name, symbol);
ERC20::_mint(recipient, initial_supply);
}

#[view]
fn name() -> felt252 {
ERC20::name()
}

#[view]
fn symbol() -> felt252 {
ERC20::symbol()
}

#[view]
fn decimals() -> u8 {
ERC20::decimals()
}

#[view]
fn total_supply() -> u256 {
ERC20::total_supply()
}

#[view]
fn balance_of(account: ContractAddress) -> u256 {
ERC20::balance_of(account)
}

#[view]
fn allowance(owner: ContractAddress, spender: ContractAddress) -> u256 {
ERC20::allowance(owner, spender)
}

#[external]
fn transfer(recipient: ContractAddress, amount: u256) -> bool {
ERC20::transfer(recipient, amount)
}

#[external]
fn transfer_from(sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool {
ERC20::transfer_from(sender, recipient, amount)
}

#[external]
fn approve(spender: ContractAddress, amount: u256) -> bool {
ERC20::approve(spender, amount)
}
}
39 changes: 19 additions & 20 deletions src/openzeppelin/tests/test_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ use openzeppelin::account::TRANSACTION_VERSION;
use openzeppelin::introspection::src5::ISRC5_ID;
use openzeppelin::tests::utils;
use openzeppelin::token::erc20::ERC20;
use openzeppelin::token::erc20::IERC20Dispatcher;
use openzeppelin::token::erc20::IERC20DispatcherTrait;
use openzeppelin::token::erc20::interface::IERC20Dispatcher;
use openzeppelin::token::erc20::interface::IERC20DispatcherTrait;
use openzeppelin::utils::selectors;
use openzeppelin::utils::serde::SerializedAppend;

const PUBLIC_KEY: felt252 = 0x333333;
const NEW_PUBKEY: felt252 = 0x789789;
const TRANSFER_SELECTOR: felt252 = 0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e;
const SALT: felt252 = 123;

#[derive(Drop)]
Expand Down Expand Up @@ -81,11 +82,10 @@ fn deploy_erc20(recipient: ContractAddress, initial_supply: u256) -> IERC20Dispa
let symbol = 0;
let mut calldata = ArrayTrait::new();

calldata.append(name);
calldata.append(symbol);
calldata.append(initial_supply.low.into());
calldata.append(initial_supply.high.into());
calldata.append(recipient.into());
calldata.append_serde(name);
calldata.append_serde(symbol);
calldata.append_serde(initial_supply);
calldata.append_serde(recipient);

let address = utils::deploy(ERC20::TEST_CLASS_HASH, calldata);
IERC20Dispatcher { contract_address: address }
Expand Down Expand Up @@ -241,10 +241,11 @@ fn test_execute_with_version(version: Option<felt252>) {
// Craft call and add to calls array
let mut calldata = ArrayTrait::new();
let amount: u256 = 200;
calldata.append(recipient.into());
calldata.append(amount.low.into());
calldata.append(amount.high.into());
let call = Call { to: erc20.contract_address, selector: TRANSFER_SELECTOR, calldata: calldata };
calldata.append_serde(recipient);
calldata.append_serde(amount);
let call = Call {
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata
};
let mut calls = ArrayTrait::new();
calls.append(call);

Expand Down Expand Up @@ -318,21 +319,19 @@ fn test_multicall() {
// Craft call1
let mut calldata1 = ArrayTrait::new();
let amount1: u256 = 300;
calldata1.append(recipient1.into());
calldata1.append(amount1.low.into());
calldata1.append(amount1.high.into());
calldata1.append_serde(recipient1);
calldata1.append_serde(amount1);
let call1 = Call {
to: erc20.contract_address, selector: TRANSFER_SELECTOR, calldata: calldata1
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata1
};

// Craft call2
let mut calldata2 = ArrayTrait::new();
let amount2: u256 = 500;
calldata2.append(recipient2.into());
calldata2.append(amount2.low.into());
calldata2.append(amount2.high.into());
calldata2.append_serde(recipient2);
calldata2.append_serde(amount2);
let call2 = Call {
to: erc20.contract_address, selector: TRANSFER_SELECTOR, calldata: calldata2
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata2
};

// Bundle calls and exeute
Expand Down
Loading