From beffd832f34b657ca61d7296e02f8241bf5b6309 Mon Sep 17 00:00:00 2001 From: Leo Henches Date: Thu, 26 Sep 2024 18:05:22 +0200 Subject: [PATCH] test(revolut_test): added initiate_liquidity_retrieval tests [CONTRACTS] implement initiate_liquidity_retrival tests #84 --- contracts/.tool-versions | 2 +- .../contracts/ramps/revolut/interface.cairo | 1 + .../src/contracts/ramps/revolut/revolut.cairo | 4 +- .../ramps/revolut/revolut_test.cairo | 166 ++++++++++++++++-- 4 files changed, 158 insertions(+), 15 deletions(-) diff --git a/contracts/.tool-versions b/contracts/.tool-versions index ab54af5..cc60fd6 100644 --- a/contracts/.tool-versions +++ b/contracts/.tool-versions @@ -1 +1 @@ -scarb 2.8.0 +scarb 2.8.2 diff --git a/contracts/src/contracts/ramps/revolut/interface.cairo b/contracts/src/contracts/ramps/revolut/interface.cairo index e977848..1f93846 100644 --- a/contracts/src/contracts/ramps/revolut/interface.cairo +++ b/contracts/src/contracts/ramps/revolut/interface.cairo @@ -54,6 +54,7 @@ pub trait ZKRampABI { fn liquidity_share_request(self: @TState, offchain_id: OffchainId) -> Option; fn add_liquidity(ref self: TState, amount: u256, offchain_id: OffchainId); fn retrieve_liquidity(ref self: TState, liquidity_key: LiquidityKey); + fn initiate_liquidity_retrieval(ref self: TState, liquidity_key: LiquidityKey); fn initiate_liquidity_withdrawal( ref self: TState, liquidity_key: LiquidityKey, amount: u256, offchain_id: OffchainId ); diff --git a/contracts/src/contracts/ramps/revolut/revolut.cairo b/contracts/src/contracts/ramps/revolut/revolut.cairo index ad4bad1..958ca70 100644 --- a/contracts/src/contracts/ramps/revolut/revolut.cairo +++ b/contracts/src/contracts/ramps/revolut/revolut.cairo @@ -83,7 +83,7 @@ pub mod RevolutRamp { #[event] #[derive(Drop, starknet::Event)] - enum Event { + pub enum Event { #[flat] OwnableEvent: OwnableComponent::Event, #[flat] @@ -203,7 +203,7 @@ pub mod RevolutRamp { self.liquidity.write(liquidity_key, existing_amount + amount); // unlocks liquidity - self.locked_liquidity.write(liquidity_key, true); + self.locked_liquidity.write(liquidity_key, false); // use the escrow to lock the funds self.escrow.lock(from: caller, :token, :amount); diff --git a/contracts/src/contracts/ramps/revolut/revolut_test.cairo b/contracts/src/contracts/ramps/revolut/revolut_test.cairo index c4a7d46..716d47f 100644 --- a/contracts/src/contracts/ramps/revolut/revolut_test.cairo +++ b/contracts/src/contracts/ramps/revolut/revolut_test.cairo @@ -1,8 +1,14 @@ +use core::num::traits::Bounded; use core::starknet::{ContractAddress, get_caller_address}; use openzeppelin::presets::interfaces::ERC20UpgradeableABIDispatcher; +use openzeppelin::presets::interfaces::ERC20UpgradeableABIDispatcherTrait; use openzeppelin::utils::serde::SerializedAppend; -use snforge_std::{declare, DeclareResultTrait, ContractClassTrait}; +use snforge_std::{ + EventSpyAssertionsTrait, spy_events, declare, DeclareResultTrait, ContractClassTrait, start_cheat_caller_address, + stop_cheat_caller_address +}; use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey}; +use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{Event, LiquidityLocked}; use zkramp::tests::constants; use zkramp::tests::utils; @@ -63,29 +69,143 @@ fn test_add_liquidity_to_locked_liquidity() { } // -// initiate_liquidity_retrival & retrieve_liquidity +// initiate_liquidity_retrieval & retrieve_liquidity // -// #[test] -// #[should_panic(expected: 'Amount cannot be null')] +#[test] +#[should_panic(expected: 'Amount cannot be null')] fn test_initiate_empty_liquidity_retrieval() { - panic!("Not implemented yet"); + // setup + let (revolut_ramp, _) = setup(); + let liquidity_key = LiquidityKey { owner: constants::OWNER(), offchain_id: constants::REVOLUT_ID() }; + + // initiate liquidity retrieval + revolut_ramp.initiate_liquidity_retrieval(liquidity_key); } -// #[test] -// #[should_panic(expected: 'Caller is not the owner')] +#[test] +#[should_panic(expected: 'Caller is not the owner')] fn test_initiate_liquidity_retrieval_not_owner() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let liquidity_owner = constants::OTHER(); + 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); + + // change owner + let new_owner = constants::CALLER(); + revolut_ramp.transfer_ownership(:new_owner); + assert_eq!(new_owner, revolut_ramp.owner()); + + // initiate with wrong owner + revolut_ramp.initiate_liquidity_retrieval(liquidity_key); } -// #[test] +#[test] fn test_initiate_liquidity_retrieval() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let mut spy = spy_events(); + let liquidity_owner = constants::OTHER(); + 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 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); + + revolut_ramp.initiate_liquidity_retrieval(:liquidity_key); + + // 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::LiquidityLocked(LiquidityLocked { liquidity_key }))] + ) } -// #[test] +#[test] fn test_initiate_liquidity_retrieval_twice() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let mut spy = spy_events(); + let liquidity_owner = constants::OTHER(); + let offchain_id = constants::REVOLUT_ID(); + 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: amount1 + amount2 + ); + + // register offchain ID + start_cheat_caller_address(revolut_ramp.contract_address, liquidity_owner); + revolut_ramp.register(:offchain_id); + + // add liquidity + revolut_ramp.add_liquidity(amount1, offchain_id); + + // assert state before + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount1); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount1); + assert_eq!(erc20.balance_of(liquidity_owner), amount2); + + revolut_ramp.initiate_liquidity_retrieval(:liquidity_key); + + // assert state after + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount1); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), 0); + assert_eq!(erc20.balance_of(liquidity_owner), amount2); + + // add liquidity + revolut_ramp.add_liquidity(amount2, offchain_id); + + // assert state before + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount1 + amount2); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount1 + amount2); + assert_eq!(erc20.balance_of(liquidity_owner), 0); + + revolut_ramp.initiate_liquidity_retrieval(:liquidity_key); + + // assert state after + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount1 + amount2); + 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::LiquidityLocked(LiquidityLocked { liquidity_key })), + (revolut_ramp.contract_address, Event::LiquidityLocked(LiquidityLocked { liquidity_key })) + ] + ) } #[test] @@ -342,3 +462,25 @@ fn test_liquidity_share_request_valid() { fn test_liquidity_share_request_withdrawn() { panic!("Not implemented yet"); } + +// +// Helpers +// + +fn fund(token: ERC20UpgradeableABIDispatcher, recipient: ContractAddress, amount: u256) { + // fund from owner + start_cheat_caller_address(token.contract_address, constants::OWNER()); + token.transfer(:recipient, :amount); + stop_cheat_caller_address(token.contract_address); +} + +fn fund_and_approve( + token: ERC20UpgradeableABIDispatcher, recipient: ContractAddress, spender: ContractAddress, amount: u256 +) { + fund(:token, :recipient, :amount); + + // approve + start_cheat_caller_address(token.contract_address, recipient); + token.approve(:spender, amount: Bounded::MAX); + stop_cheat_caller_address(token.contract_address); +}