Skip to content

Commit

Permalink
test withdraw liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Oct 2, 2024
1 parent 9b4c4fd commit d0ed05a
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 19 deletions.
2 changes: 1 addition & 1 deletion contracts/src/contracts/ramps/revolut/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use zkramp::components::registry::interface::OffchainId;

#[derive(Drop, Serde)]
pub struct Proof {
foo: felt252
pub foo: felt252
}

#[derive(Drop, Copy, Hash, Serde, starknet::Store)]
Expand Down
247 changes: 229 additions & 18 deletions contracts/src/contracts/ramps/revolut/revolut_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use snforge_std::{
EventSpyAssertionsTrait, spy_events, declare, DeclareResultTrait, ContractClassTrait, start_cheat_caller_address,
stop_cheat_caller_address, test_address, start_cheat_block_timestamp_global
};
use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey};
use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey, Proof};
use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{
Event, LiquidityAdded, LiquidityRetrieved, LiquidityLocked, LiquidityShareRequested, MINIMUM_LOCK_DURATION
Event, LiquidityAdded, LiquidityRetrieved, LiquidityLocked, LiquidityShareRequested, LiquidityShareWithdrawn,
MINIMUM_LOCK_DURATION
};
use zkramp::tests::constants;
use zkramp::tests::utils;
Expand Down Expand Up @@ -914,39 +915,249 @@ fn test_initiate_liquidity_withdraw_twice() {
revolut_ramp.initiate_liquidity_withdrawal(:liquidity_key, amount: amount2, offchain_id: withdrawer_offchain_id);
}

// #[test]
// #[should_panic(expected: 'Liquidity share not available')]
#[test]
#[should_panic(expected: 'Liquidity share not available')]
fn test_withdraw_liquidity_without_request() {
panic!("Not implemented yet");
let (revolut_ramp, erc20) = setup();

// off-ramper
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;

// on-ramper
let withdrawer = constants::OTHER();
let withdrawer_offchain_id = constants::REVOLUT_ID2();
let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id };
let proof = Proof { foo: 0 };

// 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 tries to withdraw
start_cheat_caller_address(revolut_ramp.contract_address, withdrawer);
revolut_ramp.register(offchain_id: withdrawer_offchain_id);
revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: withdrawer_offchain_id, :proof);
}

// #[test]
// #[should_panic(expected: 'Liquidity share not available')]
#[test]
#[should_panic(expected: 'Liquidity share not available')]
fn test_withdraw_liquidity_after_expiration() {
panic!("Not implemented yet");
let (revolut_ramp, erc20) = setup();

// off-ramper
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;

// on-ramper
let withdrawer = constants::OTHER();
let withdrawer_offchain_id = constants::REVOLUT_ID2();
let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id };
let proof = Proof { foo: 0 };

// 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, offchain_id: withdrawer_offchain_id);

// offer expires
start_cheat_block_timestamp_global(MINIMUM_LOCK_DURATION);

// withdrawer withdraws
revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: withdrawer_offchain_id, :proof);
}

// #[test]
// #[should_panic(expected: 'Liquidity share not available')]
#[test]
#[should_panic(expected: 'Liquidity share not available')]
fn test_withdraw_liquidity_from_another_caller() {
panic!("Not implemented yet");
let (revolut_ramp, erc20) = setup();

// off-ramper
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;

// on-ramper
let withdrawer = constants::OTHER();
let withdrawer_offchain_id = constants::REVOLUT_ID2();
let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id };
let proof = Proof { foo: 0 };

// other on-ramper
let other_withdrawer = constants::OTHER2();

// 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, offchain_id: withdrawer_offchain_id);

// other caller tries to withdraw
start_cheat_caller_address(revolut_ramp.contract_address, other_withdrawer);
revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: offchain_id, :proof);
}

// #[test]
// #[should_panic(expected: 'Liquidity share not available')]
#[test]
#[should_panic(expected: 'Liquidity share not available')]
fn test_withdraw_liquidity_from_another_offchain_id() {
let (revolut_ramp, erc20) = setup();

// off-ramper
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;

// on-ramper
let withdrawer = constants::OTHER();
let withdrawer_offchain_id = constants::REVOLUT_ID2();
let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id };
let proof = Proof { foo: 0 };

// other on-ramper
let other_withdrawer_offchain_id = constants::REVOLUT_ID3();

// 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, offchain_id: withdrawer_offchain_id);

// other offchain-id tries to withdraw
revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: other_withdrawer_offchain_id, :proof);
panic!("Not implemented yet");
}

// #[test]
#[test]
fn test_withdraw_liquidity() {
panic!("Not implemented yet");
let (revolut_ramp, erc20) = setup();
let mut spy = spy_events();

// off-ramper
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;

// on-ramper
let withdrawer = constants::OTHER();
let withdrawer_offchain_id = constants::REVOLUT_ID2();
let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id };
let proof = Proof { foo: 0 };

// 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
assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount);
assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount);
assert_eq!(erc20.balance_of(withdrawer), 0);

// 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, offchain_id: withdrawer_offchain_id);

// withdrawer withdraws
revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: withdrawer_offchain_id, :proof);

// assert state after
assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), 0);
assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), 0);
assert_eq!(erc20.balance_of(withdrawer), amount);

// check on emitted events
spy
.assert_emitted(
@array![
(
revolut_ramp.contract_address,
Event::LiquidityShareWithdrawn(
LiquidityShareWithdrawn {
liquidity_key, amount, withdrawer, offchain_id: withdrawer_offchain_id,
}
)
)
]
)
}

// #[test]
// #[should_panic(expected: 'Liquidity share not available')]
#[test]
#[should_panic(expected: 'Liquidity share not available')]
fn test_withdraw_liquidity_twice() {
panic!("Not implemented yet");
let (revolut_ramp, erc20) = setup();

// off-ramper
let liquidity_owner = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();
let amount = 42;

// on-ramper
let withdrawer = constants::OTHER();
let withdrawer_offchain_id = constants::REVOLUT_ID2();
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: 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, offchain_id: withdrawer_offchain_id);

// withdrawer withdraws
revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: withdrawer_offchain_id, proof: Proof { foo: 0 });

// withdrawer withdraws again
revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: withdrawer_offchain_id, proof: Proof { foo: 0 });
}

//
Expand Down

0 comments on commit d0ed05a

Please sign in to comment.