From c253959bd0971a5f89f606b4cbf020755746bd75 Mon Sep 17 00:00:00 2001 From: Leo Henches Date: Sat, 5 Oct 2024 09:05:28 +0200 Subject: [PATCH] test(revolut_test): Added available_liquidity tests Added tests for available liquidity. No bugs to report, everything seem to be working properly [CONTRACTS] implement available_liquidity tests #89 --- .../ramps/revolut/revolut_test.cairo | 187 ++++++++++++++++-- 1 file changed, 175 insertions(+), 12 deletions(-) diff --git a/contracts/src/contracts/ramps/revolut/revolut_test.cairo b/contracts/src/contracts/ramps/revolut/revolut_test.cairo index f2376e7..67326ab 100644 --- a/contracts/src/contracts/ramps/revolut/revolut_test.cairo +++ b/contracts/src/contracts/ramps/revolut/revolut_test.cairo @@ -1191,34 +1191,197 @@ fn test_get_available_liquidity_basic() { // available_liquidity & _get_available_liquidity // -// #[test] +#[test] fn test_available_liquidity_empty() { - panic!("Not implemented yet"); + // setup + let (revolut_ramp, _) = setup(); + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + + // assert no liquidity available + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), 0); } -// #[test] +#[test] fn test_available_liquidity_without_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let liquidity_owner = constants::CALLER(); + 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 liquidity is available + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount); } -// #[test] +#[test] fn test_available_liquidity_locked() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + let liquidity_owner = constants::CALLER(); + 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 retrieval, locking liquidity + revolut_ramp.initiate_liquidity_retrieval(:liquidity_key); + + // assert liquidity is not available + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), 0); } -// #[test] +#[test] fn test_available_liquidity_with_expired_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + + // off-ramper + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + let amount = 100; + + // on-ramper + let withdrawer = constants::OTHER(); + let withdrawer_offchain_id = constants::REVOLUT_ID2(); + let requested_amount = 42; + + // 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: requested_amount, offchain_id: withdrawer_offchain_id); + + // assert state before expiration + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); + + // offer expires + start_cheat_block_timestamp_global(MINIMUM_LOCK_DURATION); + + // assert state after expiration + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount); } -// #[test] +#[test] fn test_available_liquidity_with_pending_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + + // off-ramper + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + let amount = 100; + + // on-ramper + let withdrawer = constants::OTHER(); + let withdrawer_offchain_id = constants::REVOLUT_ID2(); + let requested_amount = 42; + + // 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 withdrawal initiated + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount); + + // 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: requested_amount, offchain_id: withdrawer_offchain_id); + + // assert state after withdrawal initiated + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); + + // offer almost expires + start_cheat_block_timestamp_global(MINIMUM_LOCK_DURATION - 1); + + // assert state when offer is close to expiration + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); } -// #[test] +#[test] fn test_available_liquidity_with_withdrawn_requests() { - panic!("Not implemented yet"); + let (revolut_ramp, erc20) = setup(); + + // off-ramper + let liquidity_owner = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); + let liquidity_key = LiquidityKey { owner: liquidity_owner, offchain_id }; + let amount = 100; + + // on-ramper + let withdrawer = constants::OTHER(); + let withdrawer_offchain_id = constants::REVOLUT_ID2(); + let requested_amount = 42; + 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: requested_amount, offchain_id: withdrawer_offchain_id); + + // assert state before withdrawal + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); + + // withdrawers withdraws liquidity + revolut_ramp.withdraw_liquidity(:liquidity_key, offchain_id: withdrawer_offchain_id, :proof); + + // assert state after withdrawal + assert_eq!(revolut_ramp.all_liquidity(:liquidity_key), amount - requested_amount); + assert_eq!(revolut_ramp.available_liquidity(:liquidity_key), amount - requested_amount); } // #[test]