Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(orderbook): implement new executed event #419

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading