Skip to content

Commit

Permalink
test(revolut_test): added initiate_liquidity_retrieval tests (#96)
Browse files Browse the repository at this point in the history
* test(revolut_test): added initiate_liquidity_retrieval tests

 [CONTRACTS] implement initiate_liquidity_retrival tests #84

* various fix

---------

Co-authored-by: Chqrles <[email protected]>
  • Loading branch information
leohscl and 0xChqrles authored Sep 27, 2024
1 parent 74c8e3b commit 1399e18
Showing 1 changed file with 117 additions and 21 deletions.
138 changes: 117 additions & 21 deletions contracts/src/contracts/ramps/revolut/revolut_test.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use core::num::traits::Bounded;
use core::starknet::{ContractAddress, get_caller_address};
use core::starknet::ContractAddress;
use openzeppelin::presets::interfaces::{ERC20UpgradeableABIDispatcher, ERC20UpgradeableABIDispatcherTrait};
use openzeppelin::utils::serde::SerializedAppend;
use snforge_std::{
EventSpyAssertionsTrait, spy_events, declare, DeclareResultTrait, ContractClassTrait, start_cheat_caller_address,
stop_cheat_caller_address, test_address
};
use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey};
use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{Event, LiquidityAdded};
use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{Event, LiquidityAdded, LiquidityLocked};
use zkramp::tests::constants;
use zkramp::tests::utils;

Expand Down Expand Up @@ -45,7 +45,7 @@ fn setup() -> (ZKRampABIDispatcher, ERC20UpgradeableABIDispatcher) {
fn test_add_liquidity_with_unregistered_offchain_id() {
let contract_address = test_address();
let (revolut_ramp, _) = setup();
let liquidity_owner = constants::OTHER();
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;

Expand Down Expand Up @@ -77,7 +77,7 @@ fn test_add_zero_liquidity() {
fn test_add_liquidity() {
let (revolut_ramp, erc20) = setup();
let mut spy = spy_events();
let liquidity_owner = constants::OTHER();
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;
let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id };
Expand Down Expand Up @@ -113,7 +113,7 @@ fn test_add_liquidity() {
fn test_add_liquidity_twice() {
let (revolut_ramp, erc20) = setup();
let mut spy = spy_events();
let liquidity_owner = constants::OTHER();
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount1 = 42;
let amount2 = 75;
Expand Down Expand Up @@ -160,7 +160,7 @@ fn test_add_liquidity_twice() {
fn test_add_liquidity_to_locked_liquidity() {
let (revolut_ramp, erc20) = setup();
let mut spy = spy_events();
let liquidity_owner = constants::OTHER();
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount1 = 42;
let amount2 = 75;
Expand Down Expand Up @@ -210,38 +210,134 @@ 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')]
fn test_initiate_empty_liquidity_retrieval() {
panic!("Not implemented yet");
#[test]
#[should_panic(expected: 'Amount cannot be null')]
fn test_initiate_liquidity_retrieval_empty() {
// setup
let (revolut_ramp, _) = setup();
let caller = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let liquidity_key = LiquidityKey { owner: caller, offchain_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::CALLER();
let other_caller = 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);

// initiate liquidity retrieval with wrong owner
start_cheat_caller_address(revolut_ramp.contract_address, other_caller);
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 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);
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 })),
(revolut_ramp.contract_address, Event::LiquidityLocked(LiquidityLocked { liquidity_key }))
]
)
}

#[test]
#[should_panic(expected: 'Liquidity is unlocked')]
fn test_retrieve_unlocked_liquidity() {
let (revolut_ramp, _) = setup();

// create liquidity key
let liquidity_key = LiquidityKey { owner: get_caller_address(), offchain_id: constants::REVOLUT_ID() };
let caller = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let liquidity_key = LiquidityKey { owner: caller, offchain_id };

// try to retrieve liquidity
revolut_ramp.retrieve_liquidity(:liquidity_key);
Expand Down

0 comments on commit 1399e18

Please sign in to comment.