Skip to content

Commit

Permalink
feat(orderbook): implement new executed event (#419)
Browse files Browse the repository at this point in the history
## Description
1. Update `orderbook::OrderExecuted` event with the following data:
```cairo
    #[derive(Drop, starknet::Event)]
    struct OrderExecuted {
        #[key]
        order_hash: felt252,
        #[key]
        order_status: OrderStatus,
        version: u8,
        transaction_hash: felt252,
        from: ContractAddress,
        to: ContractAddress,
    }
```
2. Update diri to handle `OrderExecuted` version 0 and version 1

<!--
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:`)
- [ ] 🐛 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 Aug 19, 2024
1 parent fae88ab commit 9fdf559
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/arkproject-rs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ jobs:
with:
profile: minimal
toolchain: stable
override: true
- name: Run cargo check
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -79,7 +78,6 @@ jobs:
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt, clippy
- name: Run cargo fmt
uses: actions-rs/cargo@v1
Expand Down
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[workspace]

members = ["ark_common", "ark_orderbook", "ark_starknet", "ark_tokens", "solis"]

[workspace.scripts]
test = "snforge test"
2 changes: 2 additions & 0 deletions contracts/ark_common/src/protocol/order_types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ struct ExecutionValidationInfo {
order_hash: felt252,
transaction_hash: felt252,
starknet_block_timestamp: u64,
from: ContractAddress,
to: ContractAddress,
}

/// Type of an route, that may be defined from
Expand Down
3 changes: 3 additions & 0 deletions contracts/ark_orderbook/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ ark_common = { path = "../ark_common" }
starknet = "2.5.4"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.18.0" }

[scripts]
test.workspace = true

[[target.starknet-contract]]
sierra = true
casm = true
19 changes: 17 additions & 2 deletions contracts/ark_orderbook/src/orderbook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,19 @@ mod orderbook {
order: OrderV1,
}

// must be increased when `OrderExecuted` content change
const ORDER_EXECUTED_EVENT_VERSION: u8 = 1;
/// Event for when an order is executed.
#[derive(Drop, starknet::Event)]
struct OrderExecuted {
#[key]
order_hash: felt252,
#[key]
order_status: OrderStatus,
// info: ExecutionInfo,
version: u8,
transaction_hash: felt252,
from: ContractAddress,
to: ContractAddress,
}

/// Event for when an order is cancelled.
Expand Down Expand Up @@ -235,7 +240,17 @@ mod orderbook {
// TODO: anyway, it can be useful to have an extra check here.
order_status_write(info.order_hash, OrderStatus::Executed);
let order_status = order_status_read(info.order_hash).unwrap();
self.emit(OrderExecuted { order_hash: info.order_hash, order_status: order_status });
self
.emit(
OrderExecuted {
order_hash: info.order_hash,
order_status: order_status,
transaction_hash: info.transaction_hash,
from: info.from,
to: info.to,
version: ORDER_EXECUTED_EVENT_VERSION,
}
);
}

/// Update status : only from solis.
Expand Down
3 changes: 3 additions & 0 deletions contracts/ark_starknet/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ ark_common = { path = "../ark_common" }
ark_tokens = { path = "../ark_tokens" }
ark_oz = { path = "../ark_oz" }

[scripts]
test.workspace = true

[[target.starknet-contract]]
sierra = true
casm = true
Expand Down
2 changes: 2 additions & 0 deletions contracts/ark_starknet/src/executor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ mod executor {
order_hash: execution_info.order_hash,
transaction_hash,
starknet_block_timestamp: block_timestamp,
from: execution_info.nft_from,
to: execution_info.nft_to,
};

let mut vinfo_buf = array![];
Expand Down
3 changes: 3 additions & 0 deletions crates/ark-metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ num-bigint = "0.4.4"
[dev-dependencies]
ark-starknet = { path = "../ark-starknet", features = ["mock"] }
mockall = "0.12.1"

[features]
mock = []
2 changes: 1 addition & 1 deletion crates/diri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tracing = "0.1"
num-bigint = "0.4.4"
num-traits = "0.2.17"

cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.1.7", features = [
cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.1.8", features = [
"abigen-rs",
] }

Expand Down
133 changes: 132 additions & 1 deletion crates/diri/src/orderbook.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,136 @@
use cainome::rs::abigen;
use starknet::{
core::types::{EmittedEvent, FieldElement},
macros::selector,
};

// TODO: check a way to fix the path... because when compiled from
// ark-services, the path is not valid as it's relative to Cargo manifest file.
abigen!(Orderbook, "./artifacts/orderbook.abi.json");
abigen!(
Orderbook,
"./artifacts/orderbook.abi.json",
type_aliases {
ark_orderbook::orderbook::orderbook::Event as EventV0;
ark_orderbook::orderbook::orderbook::OrderExecuted as OrderExecutedV0;
}
);

abigen!(
OrderBook,
r#"
[
{
"type": "event",
"name": "ark_orderbook::orderbook::orderbook::Event",
"kind": "enum",
"variants": [
{
"name": "OrderExecuted",
"type": "ark_orderbook::orderbook::orderbook::OrderExecuted",
"kind": "nested"
}
]
},
{
"type": "event",
"name": "ark_orderbook::orderbook::orderbook::OrderExecuted",
"kind": "struct",
"members": [
{
"name": "order_hash",
"type": "core::felt252",
"kind": "key"
},
{
"name": "order_status",
"type": "ark_common::protocol::order_types::OrderStatus",
"kind": "key"
},
{
"name": "version",
"type": "core::integer::u8",
"kind": "data"
},
{
"name": "transaction_hash",
"type": "core::felt252",
"kind": "data"
},
{
"name": "from",
"type": "core::starknet::contract_address::ContractAddress",
"kind": "data"
},
{
"name": "to",
"type": "core::starknet::contract_address::ContractAddress",
"kind": "data"
}
]
}
]
"#
, type_aliases {
ark_orderbook::orderbook::orderbook::Event as EventV1;
ark_orderbook::orderbook::orderbook::OrderExecuted as OrderExecutedV1;
}
);

#[derive(Debug)]
pub(crate) enum OrderExecuted {
V0(OrderExecutedV0),
V1(OrderExecutedV1),
}

#[derive(Debug)]
pub(crate) enum Event {
OrderPlaced(OrderPlaced),
OrderExecuted(OrderExecuted),
OrderCancelled(OrderCancelled),
RollbackStatus(RollbackStatus),
OrderFulfilled(OrderFulfilled),
Upgraded(Upgraded),
Unknown,
}

impl From<EmittedEvent> for Event {
fn from(ev: EmittedEvent) -> Self {
if ev.keys[0] == selector!("OrderExecuted") {
if ev.data.len() > 0 {
let version = ev.data[0];
if version == FieldElement::ONE {}
// Version 1
TryInto::<EventV1>::try_into(ev).unwrap().into()
} else {
// Version 0
TryInto::<EventV0>::try_into(ev).unwrap().into()
}
} else {
match TryInto::<EventV0>::try_into(ev) {
Ok(ev) => ev.into(),
Err(_) => Event::Unknown,
}
}
}
}

impl From<EventV0> for Event {
fn from(ev: EventV0) -> Self {
match ev {
EventV0::OrderCancelled(ev) => Event::OrderCancelled(ev),
EventV0::OrderPlaced(ev) => Event::OrderPlaced(ev),
EventV0::OrderFulfilled(ev) => Event::OrderFulfilled(ev),
EventV0::RollbackStatus(ev) => Event::RollbackStatus(ev),
EventV0::Upgraded(ev) => Event::Upgraded(ev),
EventV0::OrderExecuted(ev) => Event::OrderExecuted(OrderExecuted::V0(ev)),
}
}
}

impl From<EventV1> for Event {
fn from(ev: EventV1) -> Self {
match ev {
EventV1::OrderExecuted(ev) => Event::OrderExecuted(OrderExecuted::V1(ev)),
}
}
}
21 changes: 19 additions & 2 deletions crates/diri/src/storage/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,30 @@ impl From<OrderFulfilled> for FulfilledData {

#[derive(Debug, Clone)]
pub struct ExecutedData {
pub version: u8,
pub order_hash: String,
pub transaction_hash: Option<String>,
pub from: Option<String>,
pub to: Option<String>,
}

impl From<OrderExecuted> for ExecutedData {
fn from(value: OrderExecuted) -> Self {
Self {
order_hash: to_hex_str(&value.order_hash),
match value {
OrderExecuted::V0(v) => Self {
version: 0,
order_hash: to_hex_str(&v.order_hash),
transaction_hash: None,
from: None,
to: None,
},
OrderExecuted::V1(v) => Self {
version: 1,
order_hash: to_hex_str(&v.order_hash),
transaction_hash: Some(to_hex_str(&FieldElement::from(v.transaction_hash))),
from: Some(to_hex_str(&FieldElement::from(v.from))),
to: Some(to_hex_str(&FieldElement::from(v.to))),
},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.77.1"
channel = "1.80.1"

0 comments on commit 9fdf559

Please sign in to comment.