Skip to content

Commit

Permalink
feat(diri): split diri orderbook event in another package (#482)
Browse files Browse the repository at this point in the history
## Description
- Create `orderbook` package to handle smart contract event parsing.
- Remove unused crates from Cargo.toml

<!--
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:`)
- [x] 🧑‍💻 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 Oct 15, 2024
1 parent 8d298de commit 901f635
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 476 deletions.
389 changes: 82 additions & 307 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 4 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"crates/pontos",
"crates/diri",
"crates/sana",
"crates/orderbook",
]

[workspace.dependencies]
Expand All @@ -27,6 +28,8 @@ ark-metadata = { path = "./crates/ark-metadata" }
pontos = { path = "./crates/pontos" }
sana = { path = "./crates/sana" }
diri = { path = "./crates/diri" }
orderbook = { path = "./crates/orderbook" }

async-trait = "0.1.73"
starknet = "0.10.0"
anyhow = "1.0"
Expand All @@ -46,25 +49,11 @@ ark-metadata.workspace = true
pontos = { path = "./crates/pontos", features = ["sqlxdb"] }
sana = { path = "./crates/sana", features = ["sqlxdb"] }
diri.workspace = true
orderbook.workspace = true
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tracing = "0.1"
tracing-log = "0.1"
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio"] }
alloy-primitives = { version = "0.7.1", default-features = false }
alloy-sol-types = { version = "0.7.1", default-features = false }

[patch.crates-io]
cairo-felt = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "1031381" }
cairo-vm = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "1031381" }

alloy-contract = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }
alloy-json-rpc = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }
alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }
alloy-signer = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }
alloy-signer-wallet = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }
alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "5a5f29eb7d92a194bb2c67b1f879e84b2c6716ce" }

[dev-dependencies]
mockall = "0.12.1"
Expand Down
11 changes: 5 additions & 6 deletions crates/diri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ log = "0.4.17"
thiserror = "1.0.32"
# For now, Diri can't use the ark-starknet due to the dependency
# on ArkProjectNFTs for of starknet-rs.
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "2ddc694" }
starknet-types-core = { git = "https://github.com/starknet-io/types-rs", rev = "f98f048" }
starknet = "0.12.0"
starknet-types-core = "0.1.7"

tracing = "0.1"
num-bigint = "0.4.4"
num-traits = "0.2.19"

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

anyhow.workspace = true
async-trait.workspace = true
tokio.workspace = true

orderbook = { path = "../orderbook" }

[dev-dependencies]
clap = { version = "4.3.19", features = ["derive", "env", "string"] }
dotenv = "0.15.0"
Expand Down
4 changes: 1 addition & 3 deletions crates/diri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use storage::*;
pub mod event_handler;
use event_handler::EventHandler;

mod orderbook;

use starknet::core::types::{
BlockId, EmittedEvent, EventFilter, Felt, MaybePendingBlockWithTxHashes,
};
Expand All @@ -15,7 +13,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use tracing::{error, trace, warn};

use crate::orderbook::Event;
use orderbook::Event;

pub type IndexerResult<T> = Result<T, IndexerError>;

Expand Down
1 change: 0 additions & 1 deletion crates/diri/src/mod.rs

This file was deleted.

107 changes: 107 additions & 0 deletions crates/diri/src/storage/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use orderbook::events::{
common::{to_hex_str, to_hex_str_opt, u256_to_hex, u256_to_hex_opt},
OrderCancelled, OrderExecuted, OrderFulfilled, OrderPlaced, RollbackStatus,
};
use starknet::core::{types::Felt, utils::parse_cairo_short_string};

#[derive(Debug, Clone)]
pub struct PlacedData {
pub order_hash: String,
Expand Down Expand Up @@ -52,3 +58,104 @@ pub struct ExecutedData {
pub from: Option<String>,
pub to: Option<String>,
}

impl From<OrderCancelled> for CancelledData {
fn from(value: OrderCancelled) -> Self {
match value {
OrderCancelled::V1(value) => Self {
order_hash: to_hex_str(&value.order_hash),
order_type: format!("{:?}", value.order_type),
reason: parse_cairo_short_string(&value.reason)
.unwrap_or(to_hex_str(&value.reason)),
},
}
}
}

impl From<OrderExecuted> for ExecutedData {
fn from(value: OrderExecuted) -> Self {
match value {
OrderExecuted::V0(v) => Self {
version: 0,
order_hash: to_hex_str(&v.order_hash),
order_type: None,
transaction_hash: None,
from: None,
to: None,
},
OrderExecuted::V1(v) => Self {
version: 1,
order_hash: to_hex_str(&v.order_hash),
order_type: None,
transaction_hash: Some(to_hex_str(&v.transaction_hash)),
from: Some(to_hex_str(&Felt::from(v.from))),
to: Some(to_hex_str(&Felt::from(v.to))),
},
OrderExecuted::V2(v) => Self {
version: 2,
order_hash: to_hex_str(&v.order_hash),
order_type: Some(format!("{:?}", v.order_type)),
transaction_hash: Some(to_hex_str(&v.transaction_hash)),
from: Some(to_hex_str(&Felt::from(v.from))),
to: Some(to_hex_str(&Felt::from(v.to))),
},
}
}
}

impl From<OrderFulfilled> for FulfilledData {
fn from(value: OrderFulfilled) -> Self {
match value {
OrderFulfilled::V1(value) => {
let related_order_hash = value.related_order_hash.map(Felt::from);

Self {
order_hash: to_hex_str(&value.order_hash),
order_type: format!("{:?}", value.order_type),
fulfiller: to_hex_str(&Felt::from(value.fulfiller)),
related_order_hash: to_hex_str_opt(&related_order_hash),
}
}
}
}
}

impl From<OrderPlaced> for PlacedData {
fn from(value: OrderPlaced) -> Self {
match value {
OrderPlaced::V1(value) => Self {
order_hash: to_hex_str(&value.order_hash),
order_version: to_hex_str(&value.order_version),
order_type: format!("{:?}", value.order_type),
cancelled_order_hash: to_hex_str_opt(&value.cancelled_order_hash),
route: format!("{:?}", value.order.route),
currency_address: to_hex_str(&Felt::from(value.order.currency_address)),
currency_chain_id: to_hex_str(&value.order.currency_chain_id),
salt: to_hex_str(&value.order.salt),
offerer: to_hex_str(&Felt::from(value.order.offerer)),
token_chain_id: format!("0x{:x}", value.order.token_chain_id),
token_address: to_hex_str(&Felt::from(value.order.token_address)),
token_id: u256_to_hex_opt(&value.order.token_id),
quantity: u256_to_hex(&value.order.quantity),
start_amount: u256_to_hex(&value.order.start_amount),
end_amount: u256_to_hex(&value.order.end_amount),
start_date: value.order.start_date,
end_date: value.order.end_date,
broker_id: to_hex_str(&Felt::from(value.order.broker_id)),
},
}
}
}

impl From<RollbackStatus> for RollbackStatusData {
fn from(value: RollbackStatus) -> Self {
match value {
RollbackStatus::V1(value) => Self {
order_hash: to_hex_str(&value.order_hash),
order_type: format!("{:?}", value.order_type),
reason: parse_cairo_short_string(&value.reason)
.unwrap_or(to_hex_str(&value.reason)),
},
}
}
}
14 changes: 14 additions & 0 deletions crates/orderbook/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "orderbook"
version = "0.1.0"
edition = "2021"

[lib]
path = "src/lib.rs"

[dependencies]
cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.2", features = [
"abigen-rs",
] }
num-bigint = "0.4.4"
starknet = "0.12.0"
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use cainome::cairo_serde::U256;
use num_bigint::BigUint;

pub use starknet::core::utils::parse_cairo_short_string;

use std::fmt::LowerHex;

/// Returns the padded hex of '0x' prefixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
pub(crate) mod common;
pub mod common;
pub(crate) mod order_cancelled;
pub(crate) mod order_executed;
pub(crate) mod order_fulfilled;
pub(crate) mod order_placed;
pub(crate) mod rollback_status;

pub(crate) use order_cancelled::OrderCancelled;
pub(crate) use order_executed::OrderExecuted;
pub(crate) use order_fulfilled::OrderFulfilled;
pub(crate) use order_placed::OrderPlaced;
pub(crate) use rollback_status::RollbackStatus;
pub use order_cancelled::OrderCancelled;
pub use order_executed::OrderExecuted;
pub use order_fulfilled::OrderFulfilled;
pub use order_placed::OrderPlaced;
pub use rollback_status::RollbackStatus;

// pub(crate) use cainome::cairo_serde::U256;
use starknet::{core::types::Felt, macros::selector};

#[derive(Debug)]
pub(crate) enum OrderbookParseError {
pub enum OrderbookParseError {
Selector,
KeyLength,
DataLength,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use cainome::rs::abigen;
use starknet::core::{
types::{EmittedEvent, Felt},
utils::parse_cairo_short_string,
};
use starknet::core::types::{EmittedEvent, Felt};

use crate::types::CancelledData;

use super::{common::to_hex_str, OrderbookParseError, ORDER_CANCELLED_SELECTOR};
use super::{OrderbookParseError, ORDER_CANCELLED_SELECTOR};

abigen!(
V1,
Expand Down Expand Up @@ -82,7 +77,7 @@ abigen!(
);

#[derive(Debug)]
pub(crate) enum OrderCancelled {
pub enum OrderCancelled {
V1(OrderCancelledV1),
}

Expand Down Expand Up @@ -111,16 +106,3 @@ impl TryFrom<EmittedEvent> for OrderCancelled {
}
}
}

impl From<OrderCancelled> for CancelledData {
fn from(value: OrderCancelled) -> Self {
match value {
OrderCancelled::V1(value) => Self {
order_hash: to_hex_str(&value.order_hash),
order_type: format!("{:?}", value.order_type),
reason: parse_cairo_short_string(&value.reason)
.unwrap_or(to_hex_str(&value.reason)),
},
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use cainome::rs::abigen;
use starknet::core::types::{EmittedEvent, Felt};

use crate::types::ExecutedData;

use super::{common::to_hex_str, OrderbookParseError, ORDER_EXECUTED_SELECTOR};
use super::{OrderbookParseError, ORDER_EXECUTED_SELECTOR};

abigen!(
V0,
Expand Down Expand Up @@ -327,7 +325,7 @@ abigen!(
);

#[derive(Debug)]
pub(crate) enum OrderExecuted {
pub enum OrderExecuted {
V0(OrderExecutedV0),
V1(OrderExecutedV1),
V2(OrderExecutedV2),
Expand Down Expand Up @@ -371,34 +369,3 @@ impl TryFrom<EmittedEvent> for OrderExecuted {
}
}
}

impl From<OrderExecuted> for ExecutedData {
fn from(value: OrderExecuted) -> Self {
match value {
OrderExecuted::V0(v) => Self {
version: 0,
order_hash: to_hex_str(&v.order_hash),
order_type: None,
transaction_hash: None,
from: None,
to: None,
},
OrderExecuted::V1(v) => Self {
version: 1,
order_hash: to_hex_str(&v.order_hash),
order_type: None,
transaction_hash: Some(to_hex_str(&v.transaction_hash)),
from: Some(to_hex_str(&Felt::from(v.from))),
to: Some(to_hex_str(&Felt::from(v.to))),
},
OrderExecuted::V2(v) => Self {
version: 2,
order_hash: to_hex_str(&v.order_hash),
order_type: Some(format!("{:?}", v.order_type)),
transaction_hash: Some(to_hex_str(&v.transaction_hash)),
from: Some(to_hex_str(&Felt::from(v.from))),
to: Some(to_hex_str(&Felt::from(v.to))),
},
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use cainome::rs::abigen;
use starknet::core::types::{EmittedEvent, Felt};

use crate::types::FulfilledData;

use super::{
common::{to_hex_str, to_hex_str_opt},
OrderbookParseError, ORDER_FULFILLED_SELECTOR,
};
use super::{OrderbookParseError, ORDER_FULFILLED_SELECTOR};

abigen!(
V1,
Expand Down Expand Up @@ -88,7 +83,7 @@ abigen!(
);

#[derive(Debug)]
pub(crate) enum OrderFulfilled {
pub enum OrderFulfilled {
V1(OrderFulfilledV1),
}

Expand Down Expand Up @@ -116,20 +111,3 @@ impl TryFrom<EmittedEvent> for OrderFulfilled {
}
}
}

impl From<OrderFulfilled> for FulfilledData {
fn from(value: OrderFulfilled) -> Self {
match value {
OrderFulfilled::V1(value) => {
let related_order_hash = value.related_order_hash.map(Felt::from);

Self {
order_hash: to_hex_str(&value.order_hash),
order_type: format!("{:?}", value.order_type),
fulfiller: to_hex_str(&Felt::from(value.fulfiller)),
related_order_hash: to_hex_str_opt(&related_order_hash),
}
}
}
}
}
Loading

0 comments on commit 901f635

Please sign in to comment.