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

Remove token dep from account package #1100

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ dependencies = [
"openzeppelin_introspection",
"openzeppelin_test_common",
"openzeppelin_testing",
"openzeppelin_token",
"openzeppelin_utils",
"snforge_std",
]
Expand Down
1 change: 0 additions & 1 deletion packages/account/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ fmt.workspace = true
starknet.workspace = true
openzeppelin_introspection = { path = "../introspection" }
openzeppelin_utils = { path = "../utils" }
openzeppelin_token = { path = "../token" }

[dev-dependencies]
snforge_std.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/tests/mocks.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) mod account_mocks;
pub(crate) mod erc20_mocks;
pub(crate) mod eth_account_mocks;
pub(crate) mod non_implementing_mock;
pub(crate) mod simple_mock;
40 changes: 0 additions & 40 deletions packages/account/src/tests/mocks/erc20_mocks.cairo

This file was deleted.

25 changes: 25 additions & 0 deletions packages/account/src/tests/mocks/simple_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[starknet::interface]
pub(crate) trait ISimpleMock<TContractState> {
fn increase_balance(ref self: TContractState, amount: felt252) -> bool;
fn get_balance(self: @TContractState) -> felt252;
}

#[starknet::contract]
pub(crate) mod SimpleMock {
#[storage]
struct Storage {
balance: felt252,
}

#[abi(embed_v0)]
impl SimpleMockImpl of super::ISimpleMock<ContractState> {
fn increase_balance(ref self: ContractState, amount: felt252) -> bool {
self.balance.write(self.balance.read() + amount);
true
}

fn get_balance(self: @ContractState) -> felt252 {
self.balance.read()
}
}
}
64 changes: 34 additions & 30 deletions packages/account/src/tests/test_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ use openzeppelin_account::AccountComponent;
use openzeppelin_account::interface::{AccountABIDispatcherTrait, AccountABIDispatcher};
use openzeppelin_account::interface::{ISRC6, ISRC6_ID};
use openzeppelin_account::tests::mocks::account_mocks::DualCaseAccountMock;
use openzeppelin_account::tests::mocks::simple_mock::SimpleMock;
use openzeppelin_account::tests::mocks::simple_mock::{
ISimpleMockDispatcher, ISimpleMockDispatcherTrait
};
use openzeppelin_introspection::interface::{ISRC5, ISRC5_ID};
use openzeppelin_test_common::account::{AccountSpyHelpers, SignedTransactionData};
use openzeppelin_test_common::account::{SIGNED_TX_DATA, get_accept_ownership_signature};
use openzeppelin_test_common::erc20::deploy_erc20;
use openzeppelin_testing as utils;
use openzeppelin_testing::constants::stark::{KEY_PAIR, KEY_PAIR_2};
use openzeppelin_testing::constants::{
SALT, ZERO, OTHER, CALLER, RECIPIENT, QUERY_OFFSET, QUERY_VERSION, MIN_TRANSACTION_VERSION
};
use openzeppelin_testing::signing::StarkKeyPair;
use openzeppelin_token::erc20::interface::IERC20DispatcherTrait;
use openzeppelin_utils::selectors;
use openzeppelin_utils::serde::SerializedAppend;
use snforge_std::{
cheat_signature_global, cheat_transaction_version_global, cheat_transaction_hash_global
};
Expand Down Expand Up @@ -196,16 +197,19 @@ fn test_validate_declare_empty_signature() {
fn test_execute_with_version(version: Option<felt252>) {
let key_pair = KEY_PAIR();
let (account, _) = setup_dispatcher(key_pair, SIGNED_TX_DATA(key_pair));
let erc20 = deploy_erc20(account.contract_address, 1000);
let recipient = RECIPIENT();

// Deploy target contract
let calldata = array![];
let address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address: address };

// Craft call and add to calls array
let mut calldata = array![];
let amount: u256 = 200;
calldata.append_serde(recipient);
calldata.append_serde(amount);
let amount = 200;
let calldata = array![amount];
let call = Call {
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata.span()
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata.span()
};
let calls = array![call];

Expand All @@ -217,9 +221,8 @@ fn test_execute_with_version(version: Option<felt252>) {
// Execute
let ret = account.__execute__(calls);

// Assert that the transfer was successful
assert_eq!(erc20.balance_of(account.contract_address), 800, "Should have remainder");
assert_eq!(erc20.balance_of(recipient), amount, "Should have transferred");
// Assert that the call was successful
assert_eq!(simple_mock.get_balance(), amount);

// Test return value
let mut call_serialized_retval = *ret.at(0);
Expand Down Expand Up @@ -285,36 +288,37 @@ fn test_validate_invalid() {
fn test_multicall() {
let key_pair = KEY_PAIR();
let (account, _) = setup_dispatcher(key_pair, SIGNED_TX_DATA(key_pair));
let erc20 = deploy_erc20(account.contract_address, 1000);
let recipient1 = RECIPIENT();
let recipient2 = OTHER();

// Deploy target contract
let calldata = array![];
let address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address: address };

// Craft call1
let mut calldata1 = array![];
let amount1: u256 = 300;
calldata1.append_serde(recipient1);
calldata1.append_serde(amount1);
let amount1 = 300;
let calldata1 = array![amount1];
let call1 = Call {
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata1.span()
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata1.span()
};

// Craft call2
let mut calldata2 = array![];
let amount2: u256 = 500;
calldata2.append_serde(recipient2);
calldata2.append_serde(amount2);
let amount2 = 500;
let calldata2 = array![amount2];
let call2 = Call {
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata2.span()
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata2.span()
};

// Bundle calls and execute
let calls = array![call1, call2];
let ret = account.__execute__(calls);

// Assert that the transfers were successful
assert_eq!(erc20.balance_of(account.contract_address), 200, "Should have remainder");
assert_eq!(erc20.balance_of(recipient1), 300, "Should have transferred from call1");
assert_eq!(erc20.balance_of(recipient2), 500, "Should have transferred from call2");
// Assert that the txs were successful
let total_balance = amount1 + amount2;
assert_eq!(simple_mock.get_balance(), total_balance);

// Test return values
let mut call1_serialized_retval = *ret.at(0);
Expand Down
70 changes: 36 additions & 34 deletions packages/account/src/tests/test_eth_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use openzeppelin_account::EthAccountComponent;
use openzeppelin_account::interface::{EthAccountABIDispatcherTrait, EthAccountABIDispatcher};
use openzeppelin_account::interface::{ISRC6, ISRC6_ID};
use openzeppelin_account::tests::mocks::eth_account_mocks::DualCaseEthAccountMock;
use openzeppelin_account::tests::mocks::simple_mock::SimpleMock;
use openzeppelin_account::tests::mocks::simple_mock::{
ISimpleMockDispatcher, ISimpleMockDispatcherTrait
};
use openzeppelin_account::utils::secp256k1::{DebugSecp256k1Point, Secp256k1PointPartialEq};
use openzeppelin_account::utils::signature::EthSignature;
use openzeppelin_introspection::interface::{ISRC5, ISRC5_ID};
use openzeppelin_test_common::erc20::deploy_erc20;
use openzeppelin_test_common::eth_account::EthAccountSpyHelpers;
use openzeppelin_test_common::eth_account::{
SIGNED_TX_DATA, SignedTransactionData, get_accept_ownership_signature
Expand All @@ -18,7 +21,6 @@ use openzeppelin_testing::constants::{
ETH_PUBKEY, NEW_ETH_PUBKEY, SALT, ZERO, OTHER, RECIPIENT, CALLER, QUERY_VERSION,
MIN_TRANSACTION_VERSION
};
use openzeppelin_token::erc20::interface::IERC20DispatcherTrait;
use openzeppelin_utils::selectors;
use openzeppelin_utils::serde::SerializedAppend;
use snforge_std::{
Expand Down Expand Up @@ -218,19 +220,21 @@ fn test_validate_declare_empty_signature() {
fn test_execute_with_version(version: Option<felt252>) {
let data = SIGNED_TX_DATA(KEY_PAIR());
let (account, _) = setup_dispatcher(Option::Some(@data));
let erc20 = deploy_erc20(account.contract_address, 1000);
let recipient = RECIPIENT();

// Deploy target contract
let calldata = array![];
let address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address: address };

// Craft call and add to calls array
let mut calldata = array![];
let amount: u256 = 200;
calldata.append_serde(recipient);
calldata.append_serde(amount);
let amount = 200;
let calldata = array![amount];
let call = Call {
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata.span()
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata.span()
};
let mut calls = array![];
calls.append(call);
let calls = array![call];

// Handle version for test
if let Option::Some(version) = version {
Expand All @@ -240,9 +244,8 @@ fn test_execute_with_version(version: Option<felt252>) {
// Execute
let ret = account.__execute__(calls);

// Assert that the transfer was successful
assert_eq!(erc20.balance_of(account.contract_address), 800, "Should have remainder");
assert_eq!(erc20.balance_of(recipient), amount, "Should have transferred");
// Assert that the call was successful
assert_eq!(simple_mock.get_balance(), amount);

// Test return value
let mut call_serialized_retval = *ret.at(0);
Expand Down Expand Up @@ -289,38 +292,37 @@ fn test_validate_invalid() {
#[test]
fn test_multicall() {
let (account, _) = setup_dispatcher(Option::Some(@SIGNED_TX_DATA(KEY_PAIR())));
let erc20 = deploy_erc20(account.contract_address, 1000);
let recipient1 = RECIPIENT();
let recipient2 = OTHER();
let mut calls = array![];

// Deploy target contract
let calldata = array![];
let address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address: address };

// Craft call1
let mut calldata1 = array![];
let amount1: u256 = 300;
calldata1.append_serde(recipient1);
calldata1.append_serde(amount1);
let amount1 = 300;
let calldata1 = array![amount1];
let call1 = Call {
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata1.span()
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata1.span()
};

// Craft call2
let mut calldata2 = array![];
let amount2: u256 = 500;
calldata2.append_serde(recipient2);
calldata2.append_serde(amount2);
let amount2 = 500;
let calldata2 = array![amount2];
let call2 = Call {
to: erc20.contract_address, selector: selectors::transfer, calldata: calldata2.span()
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata2.span()
};

// Bundle calls and execute
calls.append(call1);
calls.append(call2);
let calls = array![call1, call2];
let ret = account.__execute__(calls);

// Assert that the transfers were successful
assert_eq!(erc20.balance_of(account.contract_address), 200, "Should have remainder");
assert_eq!(erc20.balance_of(recipient1), 300, "Should have transferred in call 1");
assert_eq!(erc20.balance_of(recipient2), 500, "Should have transferred in call 2");
// Assert that the txs were successful
let total_balance = amount1 + amount2;
assert_eq!(simple_mock.get_balance(), total_balance);

// Test return value
let mut call1_serialized_retval = *ret.at(0);
Expand Down