Skip to content

Commit

Permalink
chore: add tests for cancel_order in Escrow contract (#115)
Browse files Browse the repository at this point in the history
* test(cairo): cancel_order

* refactor(cairo): order withdrew error message

* refactor(cairo): remove test comment

Co-authored-by: Roberto J Catalan <[email protected]>

---------

Co-authored-by: Roberto J Catalan <[email protected]>
  • Loading branch information
taturosati and rcatalan98 authored Jan 24, 2024
1 parent 726a7e4 commit c0b2895
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
6 changes: 3 additions & 3 deletions contracts/cairo/src/escrow.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ mod Escrow {
}

fn cancel_order(ref self: ContractState, order_id: u256) {
assert(!self.orders_used.read(order_id), 'Order already withdrawed');
assert(!self.orders_used.read(order_id), 'Order withdrew or nonexistent');
assert(
get_block_timestamp() - self.orders_timestamps.read(order_id) < 43200,
'Didnt passed enough time'
get_block_timestamp() - self.orders_timestamps.read(order_id) > 43200,
'Not enough time has passed'
);

let sender = self.orders_senders.read(order_id);
Expand Down
70 changes: 69 additions & 1 deletion contracts/cairo/src/tests/test_escrow.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod Escrow {
use integer::BoundedInt;

use snforge_std::{declare, ContractClassTrait};
use snforge_std::{CheatTarget, start_prank, stop_prank};
use snforge_std::{CheatTarget, start_prank, stop_prank, start_warp, stop_warp};

use yab::interfaces::IERC20::{IERC20Dispatcher, IERC20DispatcherTrait};
use yab::escrow::{IEscrowDispatcher, IEscrowDispatcherTrait, Order};
Expand Down Expand Up @@ -155,4 +155,72 @@ mod Escrow {
let order_id = escrow.set_order(order);
stop_prank(CheatTarget::One(escrow.contract_address));
}

#[test]
fn test_cancel_order() {
let (escrow, eth_token) = setup_balance(500);

start_prank(CheatTarget::One(escrow.contract_address), USER());
let order = Order { recipient_address: 12345.try_into().unwrap(), amount: 500, fee: 0 };
let order_id = escrow.set_order(order);

// check balance
assert(eth_token.balanceOf(escrow.contract_address) == 500, 'set_order: wrong balance ');
assert(eth_token.balanceOf(MM_STARKNET()) == 0, 'set_order: wrong balance');
assert(eth_token.balanceOf(USER()) == 0, 'set_order: wrong allowance');

start_warp(CheatTarget::One(escrow.contract_address), 43201);
escrow.cancel_order(order_id);
stop_warp(CheatTarget::One(escrow.contract_address));

stop_prank(CheatTarget::One(escrow.contract_address));

// check balance
assert(eth_token.balanceOf(escrow.contract_address) == 0, 'cancel_order: wrong balance ');
assert(eth_token.balanceOf(MM_STARKNET()) == 0, 'cancel_order: wrong balance');
assert(eth_token.balanceOf(USER()) == 500, 'cancel_order: wrong allowance');
}

#[test]
#[should_panic(expected: ('Not enough time has passed',))]
fn test_cancel_order_fail_time() {
let (escrow, eth_token) = setup_balance(500);

start_prank(CheatTarget::One(escrow.contract_address), USER());
let order = Order { recipient_address: 12345.try_into().unwrap(), amount: 500, fee: 0 };
let order_id = escrow.set_order(order);

escrow.cancel_order(order_id);
stop_prank(CheatTarget::One(escrow.contract_address));
}

#[test]
#[should_panic(expected: ('Order withdrew or nonexistent',))]
fn tets_cancel_order_fail_withdrew() {
let (escrow, eth_token) = setup_balance(500);

start_prank(CheatTarget::One(escrow.contract_address), USER());
let order = Order { recipient_address: 12345.try_into().unwrap(), amount: 500, fee: 0 };
let order_id = escrow.set_order(order);

start_prank(CheatTarget::One(escrow.contract_address), MM_STARKNET());
escrow.withdraw(order_id, 0, 0);
stop_prank(CheatTarget::One(escrow.contract_address));

escrow.cancel_order(order_id);
}

#[test]
#[should_panic(expected: ('Only sender allowed',))]
fn test_cancel_order_fail_sender() {
let (escrow, eth_token) = setup_balance(500);

start_prank(CheatTarget::One(escrow.contract_address), USER());
let order = Order { recipient_address: 12345.try_into().unwrap(), amount: 500, fee: 0 };
let order_id = escrow.set_order(order);

start_warp(CheatTarget::One(escrow.contract_address), 43201);
start_prank(CheatTarget::One(escrow.contract_address), MM_STARKNET());
escrow.cancel_order(order_id);
}
}

0 comments on commit c0b2895

Please sign in to comment.