Skip to content

Commit

Permalink
test liquidity_share_request (#103)
Browse files Browse the repository at this point in the history
* test liquidity_share_request

* minor improvements

---------

Co-authored-by: Chqrles <[email protected]>
  • Loading branch information
chachaleo and 0xChqrles authored Oct 7, 2024
1 parent 9cfc69b commit 4d57db4
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 11 deletions.
4 changes: 2 additions & 2 deletions contracts/src/contracts/ramps/revolut/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ pub struct Proof {
pub foo: felt252
}

#[derive(Drop, Copy, Hash, Serde, starknet::Store)]
#[derive(Drop, Copy, Hash, Serde, starknet::Store, Debug, PartialEq)]
pub struct LiquidityKey {
pub owner: ContractAddress,
pub offchain_id: OffchainId,
}

#[derive(Drop, Copy, Serde, starknet::Store)]
#[derive(Drop, Copy, Serde, starknet::Store, Debug, PartialEq)]
pub struct LiquidityShareRequest {
pub requestor: ContractAddress,
pub liquidity_key: LiquidityKey,
Expand Down
150 changes: 141 additions & 9 deletions contracts/src/contracts/ramps/revolut/revolut_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ 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, LiquidityShareRequest
};
use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{
Event, LiquidityAdded, LiquidityRetrieved, LiquidityLocked, LiquidityShareRequested, LiquidityShareWithdrawn,
InternalImpl as RevolutRampInternalImpl, MINIMUM_LOCK_DURATION, LOCK_DURATION_STEP
Expand Down Expand Up @@ -1453,24 +1455,154 @@ fn test_all_liquidity_with_requests() {
// liquidity_share_request
//

// #[test]
#[test]
fn test_liquidity_share_request_empty() {
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();

// 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);

// assert empty liquidity_share_request is None
let maybe_liquidity_share_request = revolut_ramp.liquidity_share_request(offchain_id: withdrawer_offchain_id);
assert!(maybe_liquidity_share_request.is_none());
}

// #[test]
#[test]
fn test_liquidity_share_request_expired() {
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);

// offer expires
start_cheat_block_timestamp_global(MINIMUM_LOCK_DURATION);

// assert expired liquidity_share_request is None
let maybe_liquidity_share_request = revolut_ramp.liquidity_share_request(offchain_id: withdrawer_offchain_id);
assert!(maybe_liquidity_share_request.is_none());
}

// #[test]
#[test]
fn test_liquidity_share_request_valid() {
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 expected_liquidity_share_request = LiquidityShareRequest {
requestor: withdrawer, amount, liquidity_key, expiration_date: MINIMUM_LOCK_DURATION,
};

// 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);

// assert liquidity_share_request
let liquidity_share_request = revolut_ramp
.liquidity_share_request(withdrawer_offchain_id)
.expect('liquidity_share_req expected');

assert_eq!(liquidity_share_request, expected_liquidity_share_request);
}

// #[test]
#[test]
fn test_liquidity_share_request_withdrawn() {
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 = 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, offchain_id: withdrawer_offchain_id);

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

// assert withdrawned liquidity_share_request is None
let maybe_liquidity_share_request = revolut_ramp.liquidity_share_request(withdrawer_offchain_id);
assert!(maybe_liquidity_share_request.is_none());
}

//
Expand Down

0 comments on commit 4d57db4

Please sign in to comment.