From 5568bec2094e82f7f5cbca5b7a4f814a4022f29d Mon Sep 17 00:00:00 2001 From: srosati Date: Tue, 23 Jan 2024 16:19:52 -0300 Subject: [PATCH 1/3] test(cairo): cancel_order --- contracts/cairo/src/escrow.cairo | 6 +- contracts/cairo/src/tests/test_escrow.cairo | 70 ++++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/contracts/cairo/src/escrow.cairo b/contracts/cairo/src/escrow.cairo index 26611fd3..646d66ab 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 already withdrew'); 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..19dfa6f2 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 already withdrew',))] + 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); // set time to 43201 + start_prank(CheatTarget::One(escrow.contract_address), MM_STARKNET()); + escrow.cancel_order(order_id); + } } From 34fb61eb3258da6c0f48bd578cb2b2f95980889a Mon Sep 17 00:00:00 2001 From: srosati Date: Wed, 24 Jan 2024 09:34:34 -0300 Subject: [PATCH 2/3] refactor(cairo): order withdrew error message --- contracts/cairo/src/escrow.cairo | 2 +- contracts/cairo/src/tests/test_escrow.cairo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/cairo/src/escrow.cairo b/contracts/cairo/src/escrow.cairo index 646d66ab..66b1baff 100644 --- a/contracts/cairo/src/escrow.cairo +++ b/contracts/cairo/src/escrow.cairo @@ -145,7 +145,7 @@ mod Escrow { } fn cancel_order(ref self: ContractState, order_id: u256) { - assert(!self.orders_used.read(order_id), 'Order already withdrew'); + assert(!self.orders_used.read(order_id), 'Order withdrew or nonexistent'); assert( get_block_timestamp() - self.orders_timestamps.read(order_id) > 43200, 'Not enough time has passed' diff --git a/contracts/cairo/src/tests/test_escrow.cairo b/contracts/cairo/src/tests/test_escrow.cairo index 19dfa6f2..6f0316c0 100644 --- a/contracts/cairo/src/tests/test_escrow.cairo +++ b/contracts/cairo/src/tests/test_escrow.cairo @@ -195,7 +195,7 @@ mod Escrow { } #[test] - #[should_panic(expected: ('Order already withdrew',))] + #[should_panic(expected: ('Order withdrew or nonexistent',))] fn tets_cancel_order_fail_withdrew() { let (escrow, eth_token) = setup_balance(500); From 6d2bf7842a8a43758257219a1c3b0ec07ee183a4 Mon Sep 17 00:00:00 2001 From: Santos Rosati Date: Wed, 24 Jan 2024 09:38:11 -0300 Subject: [PATCH 3/3] refactor(cairo): remove test comment Co-authored-by: Roberto J Catalan --- contracts/cairo/src/tests/test_escrow.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/cairo/src/tests/test_escrow.cairo b/contracts/cairo/src/tests/test_escrow.cairo index 6f0316c0..60a2a716 100644 --- a/contracts/cairo/src/tests/test_escrow.cairo +++ b/contracts/cairo/src/tests/test_escrow.cairo @@ -219,7 +219,7 @@ mod Escrow { 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); // set time to 43201 + start_warp(CheatTarget::One(escrow.contract_address), 43201); start_prank(CheatTarget::One(escrow.contract_address), MM_STARKNET()); escrow.cancel_order(order_id); }