Skip to content

Commit fdaf990

Browse files
feat: fixed transfer untis to L1 (#19)
1 parent 5ca02a6 commit fdaf990

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
1+
pub mod Errors {
2+
pub const THRESHOLD_TOO_SMALL: felt252 = 'Threshold is too small';
3+
}

packages/usdc_migration/src/tests/test_usdc_migration.cairo

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use starkware_utils_testing::test_utils::{
1616
assert_expected_event_emitted, assert_panic_with_error, assert_panic_with_felt_error,
1717
cheat_caller_address_once,
1818
};
19+
use usdc_migration::errors::Errors;
1920
use usdc_migration::events::USDCMigrationEvents::USDCMigrated;
2021
use usdc_migration::interface::{
2122
IUSDCMigrationAdminDispatcher, IUSDCMigrationAdminDispatcherTrait,
@@ -30,6 +31,8 @@ use usdc_migration::tests::test_utils::{
3031
deploy_usdc_migration, generic_test_fixture, load_contract_address, load_u256, new_user,
3132
supply_contract,
3233
};
34+
use usdc_migration::usdc_migration::USDCMigration::{LARGE_BATCH_SIZE, SMALL_BATCH_SIZE};
35+
3336
#[test]
3437
fn test_constructor() {
3538
let cfg = deploy_usdc_migration();
@@ -56,6 +59,7 @@ fn test_constructor() {
5659
load_contract_address(usdc_migration_contract, selector!("starkgate_address")),
5760
);
5861
assert_eq!(LEGACY_THRESHOLD, load_u256(usdc_migration_contract, selector!("legacy_threshold")));
62+
assert_eq!(LARGE_BATCH_SIZE, load_u256(usdc_migration_contract, selector!("batch_size")));
5963
// Assert owner is set correctly.
6064
let ownable_dispatcher = IOwnableDispatcher { contract_address: usdc_migration_contract };
6165
assert_eq!(ownable_dispatcher.owner(), cfg.owner);
@@ -73,6 +77,19 @@ fn test_set_legacy_threshold() {
7377
cheat_caller_address_once(contract_address: usdc_migration_contract, caller_address: cfg.owner);
7478
usdc_migration_admin_dispatcher.set_legacy_threshold(threshold: new_threshold);
7579
assert_eq!(new_threshold, load_u256(usdc_migration_contract, selector!("legacy_threshold")));
80+
assert_eq!(LARGE_BATCH_SIZE, load_u256(usdc_migration_contract, selector!("batch_size")));
81+
// Set the threshold to a new value that is less than the current transfer unit.
82+
let new_threshold = LARGE_BATCH_SIZE - 1;
83+
cheat_caller_address_once(contract_address: usdc_migration_contract, caller_address: cfg.owner);
84+
usdc_migration_admin_dispatcher.set_legacy_threshold(threshold: new_threshold);
85+
assert_eq!(new_threshold, load_u256(usdc_migration_contract, selector!("legacy_threshold")));
86+
assert_eq!(SMALL_BATCH_SIZE, load_u256(usdc_migration_contract, selector!("batch_size")));
87+
// Set the threshold to a new value that is greater than the current transfer unit.
88+
let new_threshold = LEGACY_THRESHOLD;
89+
cheat_caller_address_once(contract_address: usdc_migration_contract, caller_address: cfg.owner);
90+
usdc_migration_admin_dispatcher.set_legacy_threshold(threshold: new_threshold);
91+
assert_eq!(new_threshold, load_u256(usdc_migration_contract, selector!("legacy_threshold")));
92+
assert_eq!(LARGE_BATCH_SIZE, load_u256(usdc_migration_contract, selector!("batch_size")));
7693
}
7794

7895
#[test]
@@ -87,6 +104,12 @@ fn test_set_legacy_threshold_assertions() {
87104
let result = usdc_migration_admin_safe_dispatcher
88105
.set_legacy_threshold(threshold: LEGACY_THRESHOLD);
89106
assert_panic_with_felt_error(:result, expected_error: OwnableErrors::NOT_OWNER);
107+
// Catch the invalid threshold error.
108+
let invalid_threshold = 1000;
109+
cheat_caller_address_once(contract_address: usdc_migration_contract, caller_address: cfg.owner);
110+
let result = usdc_migration_admin_safe_dispatcher
111+
.set_legacy_threshold(threshold: invalid_threshold);
112+
assert_panic_with_felt_error(:result, expected_error: Errors::THRESHOLD_TOO_SMALL);
90113
}
91114

92115
#[test]

packages/usdc_migration/src/tests/test_utils.cairo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ pub(crate) struct USDCMigrationCfg {
2222
pub(crate) mod constants {
2323
use core::num::traits::Pow;
2424
use starknet::{ContractAddress, EthAddress};
25+
use crate::usdc_migration::USDCMigration::LARGE_BATCH_SIZE;
2526

2627
// Total legacy USDC supply is ~140 million.
2728
pub const INITIAL_SUPPLY: u256 = 140
2829
* 10_u256.pow(6)
2930
* 10_u256.pow(6); // 140 * million * decimals
3031
// TODO: Change to the real value.
31-
pub const LEGACY_THRESHOLD: u256 = 100_000;
32+
pub const LEGACY_THRESHOLD: u256 = LARGE_BATCH_SIZE;
3233
pub const INITIAL_CONTRACT_SUPPLY: u256 = INITIAL_SUPPLY / 20;
3334
pub fn OWNER_ADDRESS() -> ContractAddress {
3435
'OWNER_ADDRESS'.try_into().unwrap()

packages/usdc_migration/src/usdc_migration.cairo

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ pub mod USDCMigration {
1010
};
1111
use starkware_utils::constants::MAX_U256;
1212
use starkware_utils::erc20::erc20_utils::CheckedIERC20DispatcherTrait;
13+
use usdc_migration::errors::Errors;
1314
use usdc_migration::events::USDCMigrationEvents::USDCMigrated;
1415
use usdc_migration::interface::{IUSDCMigration, IUSDCMigrationAdmin};
1516

17+
pub(crate) const SMALL_BATCH_SIZE: u256 = 10_000_000_000_u256;
18+
pub(crate) const LARGE_BATCH_SIZE: u256 = 100_000_000_000_u256;
19+
/// Fixed set of batch sizes used when bridging the legacy token to L1.
20+
pub(crate) const FIXED_BATCH_SIZES: [u256; 2] = [SMALL_BATCH_SIZE, LARGE_BATCH_SIZE];
21+
1622
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
1723
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
1824

@@ -34,6 +40,9 @@ pub mod USDCMigration {
3440
starkgate_address: ContractAddress,
3541
/// The threshold amount of legacy token balance, that triggers sending to L1.
3642
legacy_threshold: u256,
43+
/// The exact amount of legacy token sent to L1 in a single withdraw action.
44+
/// Must be a value from FIXED_BATCH_SIZES.
45+
batch_size: u256,
3746
}
3847

3948
#[event]
@@ -60,7 +69,9 @@ pub mod USDCMigration {
6069
self.new_token_dispatcher.write(new_dispatcher);
6170
self.l1_recipient.write(l1_recipient);
6271
self.starkgate_address.write(starkgate_address);
72+
assert(LARGE_BATCH_SIZE <= legacy_threshold, Errors::THRESHOLD_TOO_SMALL);
6373
self.legacy_threshold.write(legacy_threshold);
74+
self.batch_size.write(LARGE_BATCH_SIZE);
6475
self.ownable.initializer(:owner);
6576
}
6677

@@ -94,11 +105,20 @@ pub mod USDCMigration {
94105
pub impl AdminFunctions of IUSDCMigrationAdmin<ContractState> { //impl logic
95106
fn set_legacy_threshold(ref self: ContractState, threshold: u256) {
96107
self.ownable.assert_only_owner();
97-
// TODO: Assert the given threshold is valid according to the fixed transfer units.
98-
// TODO: Allow threshold zero?
108+
let batch_sizes = FIXED_BATCH_SIZES.span();
109+
assert(threshold >= *batch_sizes[0], Errors::THRESHOLD_TOO_SMALL);
99110
self.legacy_threshold.write(threshold);
100-
// TODO: Update transfer unit accordingly.
101-
// TODO: Emit event?
111+
// Infer the batch size from the threshold.
112+
let mut i = batch_sizes.len() - 1;
113+
while i >= 0 {
114+
let batch_size = *batch_sizes[i];
115+
if batch_size <= threshold {
116+
self.batch_size.write(batch_size);
117+
break;
118+
}
119+
i -= 1;
120+
}
121+
// TODO: Emit event?
102122
// TODO: Send to L1 here according the new threshold?
103123
}
104124

0 commit comments

Comments
 (0)