From a5937ad179d48eafd845c51e740daf1fe4f87e2b Mon Sep 17 00:00:00 2001 From: Charlotte Date: Sat, 28 Sep 2024 10:30:34 +0800 Subject: [PATCH] rebase and implementation of remaining tests --- contracts/.snfoundry_cache/.prev_tests_failed | 1 - .../src/components/registry/interface.cairo | 4 +- .../ramps/revolut/revolut_test.cairo | 133 +++++++++++++++--- contracts/src/tests/constants.cairo | 10 +- 4 files changed, 125 insertions(+), 23 deletions(-) diff --git a/contracts/.snfoundry_cache/.prev_tests_failed b/contracts/.snfoundry_cache/.prev_tests_failed index 77628d6..e69de29 100644 --- a/contracts/.snfoundry_cache/.prev_tests_failed +++ b/contracts/.snfoundry_cache/.prev_tests_failed @@ -1 +0,0 @@ -zkramp::contracts::ramps::revolut::revolut_test::test_initiate_liquidity_withdraw_without_enough_liquidity diff --git a/contracts/src/components/registry/interface.cairo b/contracts/src/components/registry/interface.cairo index 12712a5..13a516b 100644 --- a/contracts/src/components/registry/interface.cairo +++ b/contracts/src/components/registry/interface.cairo @@ -3,7 +3,9 @@ use zkramp::utils::hash::HashSerializable; #[derive(Drop, Copy, Serde, Debug, PartialEq, starknet::Store)] pub enum OffchainId { - Revolut: felt252 + #[default] + None, + Revolut: felt252, } #[starknet::interface] diff --git a/contracts/src/contracts/ramps/revolut/revolut_test.cairo b/contracts/src/contracts/ramps/revolut/revolut_test.cairo index 3de3016..a642637 100644 --- a/contracts/src/contracts/ramps/revolut/revolut_test.cairo +++ b/contracts/src/contracts/ramps/revolut/revolut_test.cairo @@ -7,7 +7,9 @@ use snforge_std::{ stop_cheat_caller_address, test_address }; use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey}; -use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{Event, LiquidityAdded, LiquidityLocked}; +use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{ + Event, LiquidityAdded, LiquidityLocked, LiquidityShareRequested +}; use zkramp::tests::constants; use zkramp::tests::utils; @@ -403,7 +405,7 @@ fn test_initiate_liquidity_withdraw_zero_amount() { let liquidity_owner = constants::OTHER(); let liquidity_withdrawer = constants::CALLER(); let offchain_id = constants::REVOLUT_ID(); - let offchain_id_withdrawer = constants::OTHER_REVOLUT_ID(); + let offchain_id_withdrawer = constants::REVOLUT_ID2(); let amount = 42; let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; @@ -432,7 +434,7 @@ fn test_initiate_liquidity_withdraw_locked() { let liquidity_owner = constants::OTHER(); let liquidity_withdrawer = constants::CALLER(); let offchain_id = constants::REVOLUT_ID(); - let offchain_id_withdrawer = constants::OTHER_REVOLUT_ID(); + let offchain_id_withdrawer = constants::REVOLUT_ID2(); let amount = 42; let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; @@ -464,7 +466,7 @@ fn test_initiate_liquidity_withdraw_with_unregistered_offchain_id() { let liquidity_owner = constants::OTHER(); let liquidity_withdrawer = constants::CALLER(); let offchain_id = constants::REVOLUT_ID(); - let offchain_id_withdrawer = constants::OTHER_REVOLUT_ID(); + let offchain_id_withdrawer = constants::REVOLUT_ID2(); let amount = 42; let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; @@ -490,7 +492,7 @@ fn test_initiate_liquidity_withdraw_without_enough_liquidity() { let liquidity_owner = constants::OTHER(); let liquidity_withdrawer = constants::CALLER(); let offchain_id = constants::REVOLUT_ID(); - let offchain_id_withdrawer = constants::OTHER_REVOLUT_ID(); + let offchain_id_withdrawer = constants::REVOLUT_ID2(); let amount1 = 42; let amount2 = 75; let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; @@ -513,14 +515,16 @@ fn test_initiate_liquidity_withdraw_without_enough_liquidity() { revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount2, offchain_id: offchain_id_withdrawer); } -// #[test] -// #[should_panic(expected: 'Not enough liquidity')] +#[test] +#[should_panic(expected: 'Not enough liquidity')] fn test_initiate_liquidity_withdraw_without_enough_available_liquidity() { let (revolut_ramp, erc20) = setup(); let liquidity_owner = constants::OTHER(); - let liquidity_withdrawer = constants::CALLER(); + let liquidity_withdrawer1 = constants::CALLER(); + let liquidity_withdrawer2 = constants::SPENDER(); let offchain_id = constants::REVOLUT_ID(); - let offchain_id_withdrawer = constants::OTHER_REVOLUT_ID(); + let offchain_id_withdrawer1 = constants::REVOLUT_ID2(); + let offchain_id_withdrawer2 = constants::REVOLUT_ID3(); let amount1 = 42; let amount2 = 75; let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; @@ -535,26 +539,117 @@ fn test_initiate_liquidity_withdraw_without_enough_available_liquidity() { // add liquidity revolut_ramp.add_liquidity(amount: amount2, :offchain_id); - // register offchain ID withdrawer - start_cheat_caller_address(revolut_ramp.contract_address, liquidity_withdrawer); - revolut_ramp.register(offchain_id: offchain_id_withdrawer); + // register offchain ID withdrawer 1 + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_withdrawer1); + revolut_ramp.register(offchain_id: offchain_id_withdrawer1); // initiate liquidity withdraw - revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount1, offchain_id: offchain_id_withdrawer); + revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount1, offchain_id: offchain_id_withdrawer1); + + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount2 - amount1); + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount2); + + // register offchain ID withdrawer 2 + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_withdrawer2); + revolut_ramp.register(offchain_id: offchain_id_withdrawer2); // initiate liquidity withdraw - revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount1, offchain_id: offchain_id_withdrawer); + revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount1, offchain_id: offchain_id_withdrawer2); } -// #[test] +#[test] fn test_initiate_liquidity_withdraw() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let mut spy = spy_events(); + let liquidity_owner = constants::OTHER(); + let liquidity_withdrawer = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let offchain_id_withdrawer = constants::REVOLUT_ID2(); + 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 owner + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(amount: amount, :offchain_id); + + // assert state before + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount); + assert_eq!(erc20.balance_of(liquidity_owner), 0); + + // register offchain ID withdrawer + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_withdrawer); + revolut_ramp.register(offchain_id: offchain_id_withdrawer); + + // initiate liquidity withdraw + revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, :amount, offchain_id: offchain_id_withdrawer); + + // assert state after + assert_eq!(revolut_ramp.all_liquidity(liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(liquidity_key), 0); + assert_eq!(erc20.balance_of(liquidity_owner), 0); + + // check on emitted events + spy + .assert_emitted( + @array![ + (revolut_ramp.contract_address, Event::LiquidityAdded(LiquidityAdded { liquidity_key, amount })), + ( + revolut_ramp.contract_address, + Event::LiquidityShareRequested( + LiquidityShareRequested { + requestor: liquidity_withdrawer, + amount, + liquidity_key, + offchain_id: offchain_id_withdrawer, + expiration_date: 3600 + } + ) + ) + ] + ) } -// #[test] -// #[should_panic(expected: 'This offchainID is busy')] +#[test] +#[should_panic(expected: 'This offchainID is busy')] fn test_initiate_liquidity_withdraw_twice() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let liquidity_owner = constants::OTHER(); + let liquidity_withdrawer = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let offchain_id_withdrawer = constants::REVOLUT_ID2(); + let amount1 = 42; + let amount2 = 75; + 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: amount2); + + // register offchain ID owner + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(amount: amount2, :offchain_id); + + // register offchain ID withdrawer + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_withdrawer); + revolut_ramp.register(offchain_id: offchain_id_withdrawer); + + // initiate liquidity withdraw + revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount1, offchain_id: offchain_id_withdrawer); + + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount2 - amount1); + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount2); + + // initiate a second liquidity withdraw + revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount1, offchain_id: offchain_id_withdrawer); } // #[test] diff --git a/contracts/src/tests/constants.cairo b/contracts/src/tests/constants.cairo index e15b14e..5435f15 100644 --- a/contracts/src/tests/constants.cairo +++ b/contracts/src/tests/constants.cairo @@ -3,16 +3,22 @@ use zkramp::components::registry::interface::OffchainId; const REVTAG: felt252 = 'just a random revtag hash'; -const REVTAG2: felt252 = 'just another random revtag hash'; +const REVTAG2: felt252 = 'just a 2nd random revtag hash'; + +const REVTAG3: felt252 = 'just a 3rd random revtag hash'; pub fn REVOLUT_ID() -> OffchainId { OffchainId::Revolut(REVTAG) } -pub fn OTHER_REVOLUT_ID() -> OffchainId { +pub fn REVOLUT_ID2() -> OffchainId { OffchainId::Revolut(REVTAG2) } +pub fn REVOLUT_ID3() -> OffchainId { + OffchainId::Revolut(REVTAG3) +} + pub fn CALLER() -> ContractAddress { contract_address_const::<'caller'>() }