diff --git a/contracts/cairo/src/escrow.cairo b/contracts/cairo/src/escrow.cairo index 26611fd3..66b1baff 100644 --- a/contracts/cairo/src/escrow.cairo +++ b/contracts/cairo/src/escrow.cairo @@ -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); diff --git a/contracts/cairo/src/tests/test_escrow.cairo b/contracts/cairo/src/tests/test_escrow.cairo index e9bf5974..60a2a716 100644 --- a/contracts/cairo/src/tests/test_escrow.cairo +++ b/contracts/cairo/src/tests/test_escrow.cairo @@ -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}; @@ -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); + } }