Skip to content

Commit

Permalink
feat: add reset_order_status in orderbook (#508)
Browse files Browse the repository at this point in the history
## Description

1. Add `reset_order_status` in orderbook
2. Fix call to`rollback_status` in`on_starknet_tx_failed`

<!--
Please do not leave this blank.
Describe the changes in this PR. What does it [add/remove/fix/replace]?

For crafting a good description, consider using ChatGPT to help
articulate your changes.
-->

## What type of PR is this? (check all applicable)

- [x] 🍕 Feature (`feat:`)
- [x] 🐛 Bug Fix (`fix:`)
- [ ] 📝 Documentation Update (`docs:`)
- [ ] 🎨 Style (`style:`)
- [ ] 🧑‍💻 Code Refactor (`refactor:`)
- [ ] 🔥 Performance Improvements (`perf:`)
- [ ] ✅ Test (`test:`)
- [ ] 🤖 Build (`build:`)
- [ ] 🔁 CI (`ci:`)
- [ ] 📦 Chore (`chore:`)
- [ ] ⏩ Revert (`revert:`)
- [ ] 🚀 Breaking Changes (`BREAKING CHANGE:`)

## Related Tickets & Documents

<!--
Please use this format to link related issues: Fixes #<issue_number>
More info:
https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->

## Added tests?

- [ ] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help

## Added to documentation?

- [ ] 📜 README.md
- [ ] 📓 Documentation
- [ ] 🙅 no documentation needed

## [optional] Are there any post-deployment tasks we need to perform?

<!-- Describe any additional tasks, if any, and provide steps. -->

## [optional] What gif best describes this PR or how it makes you feel?

<!-- Share a fun gif related to your PR! -->

### PR Title and Description Guidelines:

- Ensure your PR title follows semantic versioning standards. This helps
automate releases and changelogs.
- Use types like `feat:`, `fix:`, `chore:`, `BREAKING CHANGE:` etc. in
your PR title.
- Your PR title will be used as a commit message when merging. Make sure
it adheres to [Conventional Commits
standards](https://www.conventionalcommits.org/).

## Closing Issues

<!--
Use keywords to close related issues. This ensures that the associated
issues will automatically close when the PR is merged.

- `Fixes #123` will close issue 123 when the PR is merged.
- `Closes #123` will also close issue 123 when the PR is merged.
- `Resolves #123` will also close issue 123 when the PR is merged.

You can also use multiple keywords in one comment:
- `Fixes #123, Resolves #456`

More info:
https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->
  • Loading branch information
ptisserand authored Dec 5, 2024
1 parent 4239572 commit 029b666
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
12 changes: 12 additions & 0 deletions contracts/ark_orderbook/src/orderbook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ trait Orderbook<T> {
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);
}

// *************************************************************************
Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 38 additions & 2 deletions contracts/ark_orderbook/tests/unit/test_orderbook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::<OrderV1>(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');
}
4 changes: 3 additions & 1 deletion crates/solis/src/hooker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,11 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug, EF: ExecutorFactory>
};

// 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()],
);
}
}
Expand Down

0 comments on commit 029b666

Please sign in to comment.