Skip to content

Commit 1635bc8

Browse files
committed
feat: impl constructor
1 parent c82c889 commit 1635bc8

File tree

5 files changed

+128
-5
lines changed

5 files changed

+128
-5
lines changed

packages/usdc_migration/Scarb.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ starkware_utils_testing.workspace = true
2222

2323
[[test]]
2424
name = "usdc_migration_unittest"
25+
build-external-contracts = ["starkware_utils::erc20::erc20_mocks::DualCaseERC20Mock"]
2526

2627
[scripts]
2728
test = "SNFORGE_BACKTRACE=1 snforge test"
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
pub mod test_usdc_migration;
1+
#[cfg(test)]
2+
pub(crate) mod test_usdc_migration;
3+
#[cfg(test)]
4+
pub(crate) mod test_utils;
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
1+
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
2+
use starkware_utils::constants::MAX_U256;
3+
use usdc_migration::tests::test_utils::{deploy_usdc_migration, load_contract_address};
4+
#[test]
5+
fn test_constructor() {
6+
let cfg = deploy_usdc_migration();
7+
let usdc_migration_contract = cfg.usdc_migration_contract;
8+
// Assert contract storage is initialized correctly.
9+
assert_eq!(
10+
cfg.usdc_e_token, load_contract_address(usdc_migration_contract, selector!("usdc_e_token")),
11+
);
12+
assert_eq!(
13+
cfg.usdc_token, load_contract_address(usdc_migration_contract, selector!("usdc_token")),
14+
);
15+
assert_eq!(
16+
cfg.owner_l1_address,
17+
load_contract_address(usdc_migration_contract, selector!("owner_l1_address")),
18+
);
19+
assert_eq!(
20+
cfg.owner_l2_address,
21+
load_contract_address(usdc_migration_contract, selector!("owner_l2_address")),
22+
);
23+
// Assert infinite approval to owner_l2_address for both USDC.e and USDC.
24+
let usdc_e_dispacther = IERC20Dispatcher { contract_address: cfg.usdc_e_token };
25+
let usdc_dispacther = IERC20Dispatcher { contract_address: cfg.usdc_token };
26+
assert_eq!(
27+
usdc_e_dispacther.allowance(owner: usdc_migration_contract, spender: cfg.owner_l2_address),
28+
MAX_U256,
29+
);
30+
assert_eq!(
31+
usdc_dispacther.allowance(owner: usdc_migration_contract, spender: cfg.owner_l2_address),
32+
MAX_U256,
33+
);
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use constants::{INITIAL_SUPPLY, OWNER_ADDRESS};
2+
use snforge_std::{ContractClassTrait, DeclareResultTrait};
3+
use starknet::ContractAddress;
4+
use starkware_utils_testing::test_utils::{Deployable, TokenConfig};
5+
6+
#[derive(Debug, Drop, Copy)]
7+
pub(crate) struct USDCMigrationCfg {
8+
pub usdc_migration_contract: ContractAddress,
9+
pub usdc_e_token: ContractAddress,
10+
pub usdc_token: ContractAddress,
11+
pub owner_l1_address: ContractAddress,
12+
pub owner_l2_address: ContractAddress,
13+
}
14+
15+
pub(crate) mod constants {
16+
use starknet::ContractAddress;
17+
18+
pub const INITIAL_SUPPLY: u256 = 1000000000000000000000000000;
19+
pub fn OWNER_ADDRESS() -> ContractAddress {
20+
'OWNER_ADDRESS'.try_into().unwrap()
21+
}
22+
}
23+
24+
pub(crate) fn deploy_usdc_migration() -> USDCMigrationCfg {
25+
// Deploy USDC-E and USDC tokens.
26+
let usdc_e_config = TokenConfig {
27+
name: "USDC-E", symbol: "USDC-E", initial_supply: INITIAL_SUPPLY, owner: OWNER_ADDRESS(),
28+
};
29+
let usdc_config = TokenConfig {
30+
name: "USDC", symbol: "USDC", initial_supply: INITIAL_SUPPLY, owner: OWNER_ADDRESS(),
31+
};
32+
let usdc_e_state = usdc_e_config.deploy();
33+
let usdc_state = usdc_config.deploy();
34+
let usdc_e_token = usdc_e_state.address;
35+
let usdc_token = usdc_state.address;
36+
// Deploy USDCMigration contract.
37+
let mut calldata = ArrayTrait::new();
38+
usdc_e_token.serialize(ref calldata);
39+
usdc_token.serialize(ref calldata);
40+
OWNER_ADDRESS().serialize(ref calldata);
41+
OWNER_ADDRESS().serialize(ref calldata);
42+
let usdc_migration_contract = snforge_std::declare("USDCMigration").unwrap().contract_class();
43+
let (usdc_migration_contract_address, _) = usdc_migration_contract.deploy(@calldata).unwrap();
44+
// Return the configuration with the deployed contract address.
45+
USDCMigrationCfg {
46+
usdc_migration_contract: usdc_migration_contract_address,
47+
usdc_e_token,
48+
usdc_token,
49+
owner_l2_address: OWNER_ADDRESS(),
50+
owner_l1_address: OWNER_ADDRESS(),
51+
}
52+
}
53+
54+
// TODO: Move to starkware_utils_testing.
55+
pub(crate) fn load_contract_address(
56+
target: ContractAddress, storage_address: felt252,
57+
) -> ContractAddress {
58+
let value = snforge_std::load(:target, :storage_address, size: 1);
59+
(*value[0]).try_into().unwrap()
60+
}

packages/usdc_migration/src/usdc_migration.cairo

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#[starknet::contract]
22
pub mod USDCMigration {
3-
//use statements
3+
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
4+
use starknet::ContractAddress;
5+
use starknet::storage::StoragePointerWriteAccess;
6+
use starkware_utils::constants::MAX_U256;
47
use usdc_migration::interface::IUSDCMigration;
58

69
#[storage]
7-
struct Storage { //storage variables
10+
struct Storage {
11+
/// Deprecated USDC.e token address.
12+
usdc_e_token: ContractAddress,
13+
/// New USDC token address.
14+
usdc_token: ContractAddress,
15+
/// Address in L1 that gets the USDC.e.
16+
owner_l1_address: ContractAddress,
17+
/// Address in L2 that gets the remaining USDC.
18+
owner_l2_address: ContractAddress,
819
}
920

1021
#[event]
@@ -13,7 +24,22 @@ pub mod USDCMigration {
1324
}
1425

1526
#[constructor]
16-
fn constructor(ref self: ContractState) { //constructor logic
27+
fn constructor(
28+
ref self: ContractState,
29+
usdc_e_token: ContractAddress,
30+
usdc_token: ContractAddress,
31+
owner_l1_address: ContractAddress,
32+
owner_l2_address: ContractAddress,
33+
) {
34+
self.usdc_e_token.write(usdc_e_token);
35+
self.usdc_token.write(usdc_token);
36+
self.owner_l1_address.write(owner_l1_address);
37+
self.owner_l2_address.write(owner_l2_address);
38+
// Infinite approval to l2 address for both USDC.e and USDC.
39+
let usdc_e_dispacther = IERC20Dispatcher { contract_address: usdc_e_token };
40+
let usdc_dispacther = IERC20Dispatcher { contract_address: usdc_token };
41+
usdc_e_dispacther.approve(spender: owner_l2_address, amount: MAX_U256);
42+
usdc_dispacther.approve(spender: owner_l2_address, amount: MAX_U256);
1743
}
1844

1945
#[abi(embed_v0)]

0 commit comments

Comments
 (0)