Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests for cancel_order in Escrow contract #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}