From 629c52c0459fbfc70af6e3f5e78cc8315a36f0fb Mon Sep 17 00:00:00 2001 From: leohscl <62845407+leohscl@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:54:57 +0200 Subject: [PATCH 1/2] test(revolut_test): Added _get_next_timestamp_key tests (#105) * test(revolut_test): Added _get_next_timestamp_key_tests, slight refactor of _get_next_timestamp_key function * added tests * changed _get_next_timestamp_key to return 0 on 0 input * _get_next_timestamp_key is more readable [CONTRACTS] implement _get_next_timestamp_key tests #88 * minor fix --------- Co-authored-by: 0xChqrles --- .../src/contracts/ramps/revolut/revolut.cairo | 12 +++++-- .../ramps/revolut/revolut_test.cairo | 36 +++++++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/contracts/src/contracts/ramps/revolut/revolut.cairo b/contracts/src/contracts/ramps/revolut/revolut.cairo index a707675..0127a2b 100644 --- a/contracts/src/contracts/ramps/revolut/revolut.cairo +++ b/contracts/src/contracts/ramps/revolut/revolut.cairo @@ -347,7 +347,7 @@ pub mod RevolutRamp { // #[generate_trait] - impl InternalImpl of InternalTrait { + pub impl InternalImpl of InternalTrait { fn _get_available_liquidity(self: @ContractState, liquidity_key: LiquidityKey) -> u256 { let mut amount = self.liquidity.read(liquidity_key); let current_timestamp = get_block_timestamp(); @@ -362,8 +362,14 @@ pub mod RevolutRamp { } fn _get_next_timestamp_key(self: @ContractState, after: u64) -> u64 { - // minus 1 in order to return `after` if it's already a valid key timestamp. - after - 1 + LOCK_DURATION_STEP - ((after - 1) % LOCK_DURATION_STEP) + if after.is_zero() { + 0 + } else { + // minus 1 in order to return `after` if it's already a valid key timestamp. + let increment_step = (after - 1) / LOCK_DURATION_STEP + 1; + // returns a multiple of LOCK_DURATION_STEP + LOCK_DURATION_STEP * increment_step + } } } } diff --git a/contracts/src/contracts/ramps/revolut/revolut_test.cairo b/contracts/src/contracts/ramps/revolut/revolut_test.cairo index f2376e7..e211cda 100644 --- a/contracts/src/contracts/ramps/revolut/revolut_test.cairo +++ b/contracts/src/contracts/ramps/revolut/revolut_test.cairo @@ -9,8 +9,9 @@ use snforge_std::{ use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey}; use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{ Event, LiquidityAdded, LiquidityRetrieved, LiquidityLocked, LiquidityShareRequested, LiquidityShareWithdrawn, - MINIMUM_LOCK_DURATION + InternalImpl as RevolutRampInternalImpl, MINIMUM_LOCK_DURATION, LOCK_DURATION_STEP }; +use zkramp::contracts::ramps::revolut::revolut::RevolutRamp; use zkramp::tests::constants; use zkramp::tests::utils; @@ -1167,19 +1168,40 @@ fn test_withdraw_liquidity_twice() { // _get_next_timestamp_key // -// #[test] +#[test] fn test__get_next_timestamp_key_basic() { - panic!("Not implemented yet"); + // setup + let state = RevolutRamp::contract_state_for_testing(); + + // test a value between 0 and LOCK_DURATION_STEP + let after = 42; + + // should be rounded to the next threshold + assert_eq!(state._get_next_timestamp_key(:after), LOCK_DURATION_STEP); } -// #[test] +#[test] fn test__get_next_timestamp_key_for_timestamp_key() { - panic!("Not implemented yet"); + // setup + let state = RevolutRamp::contract_state_for_testing(); + + // test a multiple of LOCK_DURATION_STEP + let after = LOCK_DURATION_STEP * 42; + + // should return the same value + assert_eq!(state._get_next_timestamp_key(:after), after); } -// #[test] +#[test] fn test__get_next_timestamp_key_from_zero() { - panic!("Not implemented yet"); + // setup + let state = RevolutRamp::contract_state_for_testing(); + + // test with 0 + let after = 0; + + // should return the same value + assert_eq!(state._get_next_timestamp_key(:after), 0); } // #[test] From 9cfc69b4c2fb4e9e98f28885fc3447139dd0d9fd Mon Sep 17 00:00:00 2001 From: leohscl <62845407+leohscl@users.noreply.github.com> Date: Mon, 7 Oct 2024 11:06:57 +0200 Subject: [PATCH 2/2] test(revolut_test): Added available_liquidity tests (#106) * test(revolut_test): Added available_liquidity tests Added tests for available liquidity. No bugs to report, everything seem to be working properly [CONTRACTS] implement available_liquidity tests #89 * minor fix --------- Co-authored-by: Chqrles --- .../ramps/revolut/revolut_test.cairo | 191 ++++++++++++++++-- 1 file changed, 174 insertions(+), 17 deletions(-) diff --git a/contracts/src/contracts/ramps/revolut/revolut_test.cairo b/contracts/src/contracts/ramps/revolut/revolut_test.cairo index e211cda..166f00b 100644 --- a/contracts/src/contracts/ramps/revolut/revolut_test.cairo +++ b/contracts/src/contracts/ramps/revolut/revolut_test.cairo @@ -1204,43 +1204,200 @@ fn test__get_next_timestamp_key_from_zero() { assert_eq!(state._get_next_timestamp_key(:after), 0); } -// #[test] -fn test_get_available_liquidity_basic() { - panic!("Not implemented yet"); -} - // // available_liquidity & _get_available_liquidity // -// #[test] +#[test] fn test_available_liquidity_empty() { - panic!("Not implemented yet"); + let (revolut_ramp, _) = setup(); + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + + // assert no liquidity available + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), 0); } -// #[test] +#[test] fn test_available_liquidity_without_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let amount = 42; + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + + // fund the account + fund_and_approve(token: erc20, recipient: liquidity_owner, spender: revolut_ramp.contract_address, :amount); + + // register offchain ID + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(:amount, :offchain_id); + + // assert liquidity is available + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount); } -// #[test] +#[test] fn test_available_liquidity_locked() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let amount = 42; + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + + // fund the account + fund_and_approve(token: erc20, recipient: liquidity_owner, spender: revolut_ramp.contract_address, :amount); + + // register offchain ID + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(:amount, :offchain_id); + + // initiate retrieval, locking liquidity + revolut_ramp.initiate_liquidity_retrieval(:liquidity_key); + + // assert liquidity is not available + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), 0); } -// #[test] +#[test] fn test_available_liquidity_with_expired_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + + // off-ramper + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + let amount = 100; + + // on-ramper + let withdrawer = constants::OTHER(); + let withdrawer_offchain_id = constants::REVOLUT_ID2(); + let requested_amount = 42; + + // fund the account + fund_and_approve(token: erc20, recipient: liquidity_owner, spender: revolut_ramp.contract_address, amount: amount); + + // register offchain ID + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(:amount, :offchain_id); + + // withdrawer initiates withdrawal + start_cheat_caller_address(revolut_ramp.contract_address, withdrawer); + revolut_ramp.register(offchain_id: withdrawer_offchain_id); + revolut_ramp + .initiate_liquidity_withdrawal(:liquidity_key, amount: requested_amount, offchain_id: withdrawer_offchain_id); + + // assert state before expiration + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); + + // offer expires + start_cheat_block_timestamp_global(MINIMUM_LOCK_DURATION); + + // assert state after expiration + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount); } -// #[test] +#[test] fn test_available_liquidity_with_pending_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + + // off-ramper + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + let amount = 100; + + // on-ramper + let withdrawer = constants::OTHER(); + let withdrawer_offchain_id = constants::REVOLUT_ID2(); + let requested_amount = 42; + + // fund the account + fund_and_approve(token: erc20, recipient: liquidity_owner, spender: revolut_ramp.contract_address, amount: amount); + + // register offchain ID + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(:amount, :offchain_id); + + // assert state before withdrawal initiated + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount); + + // withdrawer initiates withdrawal + start_cheat_caller_address(revolut_ramp.contract_address, withdrawer); + revolut_ramp.register(offchain_id: withdrawer_offchain_id); + revolut_ramp + .initiate_liquidity_withdrawal(:liquidity_key, amount: requested_amount, offchain_id: withdrawer_offchain_id); + + // assert state after withdrawal initiated + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); + + // offer almost expires + start_cheat_block_timestamp_global(MINIMUM_LOCK_DURATION - 1); + + // assert state when offer is close to expiration + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); } -// #[test] +#[test] fn test_available_liquidity_with_withdrawn_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + + // off-ramper + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + let amount = 100; + + // on-ramper + let withdrawer = constants::OTHER(); + let withdrawer_offchain_id = constants::REVOLUT_ID2(); + let requested_amount = 42; + let proof = constants::PROOF(); + + // fund the account + fund_and_approve(token: erc20, recipient: liquidity_owner, spender: revolut_ramp.contract_address, amount: amount); + + // register offchain ID + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(:amount, :offchain_id); + + // withdrawer initiates withdrawal + start_cheat_caller_address(revolut_ramp.contract_address, withdrawer); + revolut_ramp.register(offchain_id: withdrawer_offchain_id); + revolut_ramp + .initiate_liquidity_withdrawal(:liquidity_key, amount: requested_amount, offchain_id: withdrawer_offchain_id); + + // assert state before withdrawal + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); + + // withdrawers withdraws liquidity + revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: withdrawer_offchain_id, :proof); + + // assert state after withdrawal + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount - requested_amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); } // #[test]