Skip to content

Commit

Permalink
FEAT: Upgrade Cairo,Starknet Foundry and Openzeppelin dependencies (#119
Browse files Browse the repository at this point in the history
)

* Upgrade cairo and snforge version

* Make contract compile with scarb 2.8.4
- Update openzeppelin

* Update tests

* Update tool versions
  • Loading branch information
danielcdz authored Nov 4, 2024
1 parent 8ca6d6d commit 40005d9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 32 deletions.
4 changes: 3 additions & 1 deletion snphone-contracts/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
starknet-foundry 0.20.0
starknet-foundry 0.32.0
scarb 2.8.4
openzeppelin_token 0.18.0
44 changes: 38 additions & 6 deletions snphone-contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,48 @@ version = 1
name = "contracts"
version = "0.1.0"
dependencies = [
"openzeppelin",
"openzeppelin_token",
"snforge_std",
]

[[package]]
name = "openzeppelin"
version = "0.10.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.10.0#d77082732daab2690ba50742ea41080eb23299d3"
name = "openzeppelin_account"
version = "0.18.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.18.0#2f37306b490e63c0afa9e33ad192ba428141b487"
dependencies = [
"openzeppelin_introspection",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_introspection"
version = "0.18.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.18.0#2f37306b490e63c0afa9e33ad192ba428141b487"

[[package]]
name = "openzeppelin_token"
version = "0.18.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.18.0#2f37306b490e63c0afa9e33ad192ba428141b487"
dependencies = [
"openzeppelin_account",
"openzeppelin_introspection",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_utils"
version = "0.18.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.18.0#2f37306b490e63c0afa9e33ad192ba428141b487"

[[package]]
name = "snforge_scarb_plugin"
version = "0.32.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2"

[[package]]
name = "snforge_std"
version = "0.20.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.20.0#423eecf7847469e353258321274394b9155d24eb"
version = "0.32.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2"
dependencies = [
"snforge_scarb_plugin",
]
4 changes: 2 additions & 2 deletions snphone-contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ version = "0.1.0"

[dependencies]
starknet = "2.6.3"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.10.0" }
openzeppelin_token = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.18.0" }

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.20.0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.32.0" }

[[target.starknet-contract]]
sierra = true
Expand Down
5 changes: 3 additions & 2 deletions snphone-contracts/src/mocks/erc20_mock.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[starknet::contract]
pub mod ERC20Mock {
use openzeppelin::token::erc20::{ERC20Component};
use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl};

use starknet::ContractAddress;

component!(path: ERC20Component, storage: erc20, event: ERC20Event);
Expand Down Expand Up @@ -31,6 +32,6 @@ pub mod ERC20Mock {
recipient: ContractAddress
) {
self.erc20.initializer(name, symbol);
self.erc20._mint(recipient, initial_supply);
self.erc20.mint(recipient, initial_supply);
}
}
41 changes: 20 additions & 21 deletions snphone-contracts/tests/test_account.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use snforge_std::{declare, ContractClassTrait, ContractClass};
use snforge_std::{start_prank, stop_prank, CheatTarget, prank, CheatSpan};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use snforge_std::{declare, ContractClassTrait, DeclareResultTrait, start_cheat_caller_address, stop_cheat_caller_address};
use openzeppelin_token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::contract_address_const;
use starknet::{ContractAddress, {account::Call}};
use contracts::account::{IStarknetPhoneAccountDispatcher, IStarknetPhoneAccountDispatcherTrait};
Expand All @@ -25,21 +24,21 @@ fn ALICE() -> ContractAddress {
}

fn deploy_erc20_mock(name: ByteArray, symbol: ByteArray,) -> IERC20Dispatcher {
let class = declare("ERC20Mock");
let class = declare("ERC20Mock").unwrap().contract_class();
let mut calldata = array![];
name.serialize(ref calldata);
symbol.serialize(ref calldata);
INITIAL_SUPPLY.serialize(ref calldata);
OWNER().serialize(ref calldata);

let contract_address = class.deploy(@calldata).unwrap();
let (contract_address, _) = class.deploy(@calldata).unwrap();

IERC20Dispatcher { contract_address }
}

fn deploy_wallet() -> IStarknetPhoneAccountDispatcher {
let class_hash = declare("StarknetPhoneAccount");
let contract_address = class_hash.deploy(@array![PUB_KEY]).unwrap();
let class_hash = declare("StarknetPhoneAccount").unwrap().contract_class();
let (contract_address, _) = class_hash.deploy(@array![PUB_KEY]).unwrap();
let wallet = IStarknetPhoneAccountDispatcher { contract_address };
wallet
}
Expand Down Expand Up @@ -77,9 +76,9 @@ fn test_only_account_can_change_public_key() {
// Other contract calls function
let new_pub_key = 'new_pub_key';

start_prank(CheatTarget::One(wallet.contract_address), wallet.contract_address);
start_cheat_caller_address(wallet.contract_address, wallet.contract_address);
wallet.set_public_key(new_pub_key);
stop_prank(CheatTarget::One(wallet.contract_address));
stop_cheat_caller_address(wallet.contract_address);

assert(wallet.get_public_key() == new_pub_key, 'Pub key should change');
}
Expand All @@ -94,9 +93,9 @@ fn test_other_account_cannot_change_public_key() {
let not_wallet = contract_address_const::<'not_wallet'>();
let new_pub_key = 'new_pub_key';

start_prank(CheatTarget::One(wallet.contract_address), not_wallet);
start_cheat_caller_address(wallet.contract_address, not_wallet);
wallet.set_public_key(new_pub_key);
stop_prank(CheatTarget::One(wallet.contract_address));
stop_cheat_caller_address(wallet.contract_address);

assert(wallet.get_public_key() != new_pub_key, 'Pub key should not change');
}
Expand Down Expand Up @@ -151,19 +150,19 @@ fn test_execute_with_invalid_caller() {

let calls = array![call];

start_prank(CheatTarget::One(wallet.contract_address), not_wallet);
start_cheat_caller_address(wallet.contract_address, wallet.contract_address);
wallet.__execute__(calls);
stop_prank(CheatTarget::One(wallet.contract_address));
stop_cheat_caller_address(wallet.contract_address);
}

#[test]
fn test_execute() {
let (wallet, mock_erc20) = _setup();

// fund wallet
start_prank(CheatTarget::One(mock_erc20.contract_address), OWNER());
start_cheat_caller_address(mock_erc20.contract_address, OWNER());
mock_erc20.transfer(wallet.contract_address, INITIAL_SUPPLY);
stop_prank(CheatTarget::One(mock_erc20.contract_address));
stop_cheat_caller_address(mock_erc20.contract_address);

// Craft call and add to calls array
let amount = 200_u256;
Expand All @@ -176,9 +175,9 @@ fn test_execute() {
let wallet_ballance_before = mock_erc20.balance_of(wallet.contract_address);

// execute
start_prank(CheatTarget::One(wallet.contract_address), zero);
start_cheat_caller_address(wallet.contract_address, zero);
wallet.__execute__(calls);
stop_prank(CheatTarget::One(wallet.contract_address));
stop_cheat_caller_address(wallet.contract_address);

let wallet_ballance_after = mock_erc20.balance_of(wallet.contract_address);

Expand All @@ -191,9 +190,9 @@ fn test_multicall() {
let (wallet, mock_erc20) = _setup();

// fund wallet
start_prank(CheatTarget::One(mock_erc20.contract_address), OWNER());
start_cheat_caller_address(mock_erc20.contract_address, OWNER());
mock_erc20.transfer(wallet.contract_address, INITIAL_SUPPLY);
stop_prank(CheatTarget::One(mock_erc20.contract_address));
stop_cheat_caller_address(mock_erc20.contract_address);

let first_amount = 300_u256;
let second_amount = 100_u256;
Expand All @@ -215,9 +214,9 @@ fn test_multicall() {
let zero = contract_address_const::<0>();

// execute
start_prank(CheatTarget::One(wallet.contract_address), zero);
start_cheat_caller_address(wallet.contract_address, zero);
wallet.__execute__(calls);
stop_prank(CheatTarget::One(wallet.contract_address));
stop_cheat_caller_address(wallet.contract_address);

let wallet_ballance_after = mock_erc20.balance_of(wallet.contract_address);
let expected_wallet_balance = INITIAL_SUPPLY
Expand Down

0 comments on commit 40005d9

Please sign in to comment.