diff --git a/contracts/ark_orderbook/src/orderbook.cairo b/contracts/ark_orderbook/src/orderbook.cairo index 3db6318de..c9a3c7d68 100644 --- a/contracts/ark_orderbook/src/orderbook.cairo +++ b/contracts/ark_orderbook/src/orderbook.cairo @@ -50,6 +50,8 @@ trait Orderbook { fn upgrade(ref self: T, class_hash: starknet::ClassHash); fn update_starknet_executor_address(ref self: T, value: starknet::ContractAddress); + + fn reset_order_status(ref self: T, order_hash: felt252); } // ************************************************************************* @@ -302,6 +304,16 @@ mod orderbook { self.starknet_executor_address.write(value); } + fn reset_order_status(ref self: ContractState, order_hash: felt252) { + assert(starknet::get_caller_address() == self.admin.read(), 'Unauthorized access'); + let order_type_option = order_type_read(order_hash); + if order_type_option.is_none() { + panic_with_felt252(orderbook_errors::ORDER_NOT_FOUND); + } + order_status_write(order_hash, OrderStatus::Open); + self.emit(RollbackStatus { order_hash, reason: 1 }); // FIXME: add new status ? + } + /// Retrieves the type of an order using its hash. /// # View fn get_order_type(self: @ContractState, order_hash: felt252) -> OrderType { diff --git a/contracts/ark_orderbook/tests/unit/test_orderbook.cairo b/contracts/ark_orderbook/tests/unit/test_orderbook.cairo index 19c5c4918..433c00bcc 100644 --- a/contracts/ark_orderbook/tests/unit/test_orderbook.cairo +++ b/contracts/ark_orderbook/tests/unit/test_orderbook.cairo @@ -10,8 +10,8 @@ use ark_common::protocol::order_database::{ order_read, order_status_read, order_status_write, order_type_read }; use snforge_std::{ - start_warp, declare, ContractClassTrait, spy_events, EventSpy, EventFetcher, EventAssertions, - Event, SpyOn, test_address + start_prank, stop_prank, start_warp, declare, ContractClassTrait, spy_events, EventSpy, + EventFetcher, EventAssertions, Event, SpyOn, test_address }; use array::ArrayTrait; @@ -444,3 +444,39 @@ fn test_fulfill_expired_offer() { orderbook::InternalFunctions::_fulfill_offer(ref state, fulfill_info, order_listing); } + +#[test] +fn test_create_listing_order_fulfill_and_reset_the_order() { + let (mut order_listing_1, order_hash_1, _) = setup_listing_order(600000000000000000); + let mut state = orderbook::contract_state_for_testing(); + let _ = orderbook::InternalFunctions::_create_listing_order( + ref state, order_listing_1, OrderType::Listing, order_hash_1 + ); + + let fulfill_info = FulfillInfo { + order_hash: order_hash_1, + related_order_hash: Option::None, + fulfiller: 0x2584a6517b487be8114013f277f9e2010ac001a24a93e3c48cdf5f8f345a81b + .try_into() + .unwrap(), + token_chain_id: order_listing_1.token_chain_id, + token_address: order_listing_1.token_address, + token_id: order_listing_1.token_id, + fulfill_broker_address: test_address() + }; + + // Try to fulfill the order + orderbook::InternalFunctions::_fulfill_listing_order(ref state, fulfill_info, order_listing_1,); + + // assert order1 is fulfilled + let order_option = order_read::(order_hash_1); + let order_status = order_status_read(order_hash_1); + assert(order_option.is_some(), 'storage order'); + assert(order_status.is_some(), 'storage order'); + assert(order_status.unwrap() == OrderStatus::Fulfilled, 'order status'); + + // Reset the order + orderbook::ImplOrderbook::reset_order_status(ref state, order_hash_1); + let order_status = order_status_read(order_hash_1); + assert(order_status.unwrap() == OrderStatus::Open, 'order status'); +} diff --git a/crates/solis/src/hooker.rs b/crates/solis/src/hooker.rs index 72c67a50c..00427f1cc 100644 --- a/crates/solis/src/hooker.rs +++ b/crates/solis/src/hooker.rs @@ -528,9 +528,11 @@ impl }; // rollback the status + // TODO: add CancelledByAdmin ? + let status = CancelStatus::CancelledUser; self.add_l1_handler_transaction_for_orderbook( selector!("rollback_status_order"), - &[execution_info.order_hash], + &[execution_info.order_hash, status.to_u32().into()], ); } }