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

Create javascript bindings crate and implement order queries #978

Merged
merged 15 commits into from
Nov 8, 2024
Merged
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@
"CI_FORK_SEPOLIA_DEPLOYER_ADDRESS": "",
"CI_FORK_SEPOLIA_BLOCK_NUMBER": "",
"COMMIT_SHA": ""
}
},
}
16 changes: 16 additions & 0 deletions Cargo.lock

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

22 changes: 22 additions & 0 deletions crates/js_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "rain_orderbook_js_api"
description = "Javascript bindings for common functions accross the app."
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
cynic = { workspace = true }
reqwest = { workspace = true }
rain_orderbook_subgraph_client = { workspace = true }
js-sys = { version = "0.3.69" }
wasm-bindgen = { version = "0.2.92" }
serde-wasm-bindgen = { version = "0.6.5" }
wasm-bindgen-futures = { version = "0.4.42" }
tsify = { version = "0.4.5", default-features = false, features = ["js", "wasm-bindgen"] }
tokio = { workspace = true, features = ["sync", "macros", "io-util", "rt", "time"] }
2 changes: 2 additions & 0 deletions crates/js_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(target_family = "wasm")]
pub mod subgraph;
1 change: 1 addition & 0 deletions crates/js_api/src/subgraph/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod order;
31 changes: 31 additions & 0 deletions crates/js_api/src/subgraph/order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use cynic::Id;
use rain_orderbook_subgraph_client::{
types::common::OrdersListFilterArgs, MultiOrderbookSubgraphClient, MultiSubgraphArgs,
OrderbookSubgraphClient, OrderbookSubgraphClientError, PaginationArgs,
};
use reqwest::Url;
use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsValue;

/// Fetch all orders from multiple subgraphs
/// Returns a list of OrderWithSubgraphName structs
#[wasm_bindgen(js_name = "getOrders")]
pub async fn get_orders(
subgraphs: Vec<MultiSubgraphArgs>,
filter_args: OrdersListFilterArgs,
pagination_args: PaginationArgs,
) -> Result<JsValue, OrderbookSubgraphClientError> {
let client = MultiOrderbookSubgraphClient::new(subgraphs);
let orders = client.orders_list(filter_args, pagination_args).await?;
Ok(to_value(&orders)?)
}

/// Fetch a single order
/// Returns the Order struct
#[wasm_bindgen(js_name = "getOrder")]
pub async fn get_order(url: &str, id: &str) -> Result<JsValue, OrderbookSubgraphClientError> {
let client = OrderbookSubgraphClient::new(Url::parse(url)?);
let order = client.order_detail(Id::new(id)).await?;
Ok(to_value(&order)?)
}
1 change: 1 addition & 0 deletions crates/subgraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde_json = { workspace = true }
alloy = { workspace = true, features = ["rand"] }
rain_orderbook_bindings = { workspace = true }
chrono = { workspace = true }
url = { workspace = true }
cynic-introspection = "3.7.3"
futures = "0.3.17"
tsify = { version = "0.4.5", default-features = false, features = ["js", "wasm-bindgen"] }
Expand Down
23 changes: 21 additions & 2 deletions crates/subgraph/src/multi_orderbook_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use futures::future::join_all;
use reqwest::Url;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use tsify::Tsify;

use crate::{
types::common::{
Expand All @@ -9,11 +10,29 @@ use crate::{
OrderbookSubgraphClient, OrderbookSubgraphClientError, PaginationArgs,
};

#[derive(Debug, Clone, Deserialize)]
#[cfg(target_family = "wasm")]
use rain_orderbook_bindings::impl_wasm_traits;
#[cfg(target_family = "wasm")]
use serde_wasm_bindgen::{from_value, to_value};
#[cfg(target_family = "wasm")]
use wasm_bindgen::convert::{
js_value_vector_from_abi, js_value_vector_into_abi, FromWasmAbi, IntoWasmAbi,
LongRefFromWasmAbi, RefFromWasmAbi, TryFromJsValue, VectorFromWasmAbi, VectorIntoWasmAbi,
};
#[cfg(target_family = "wasm")]
use wasm_bindgen::describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR};
#[cfg(target_family = "wasm")]
use wasm_bindgen::{JsValue, UnwrapThrowExt};

#[derive(Debug, Clone, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct MultiSubgraphArgs {
#[tsify(type = "string")]
url: Url,
name: String,
}
#[cfg(target_family = "wasm")]
impl_wasm_traits!(MultiSubgraphArgs);

pub struct MultiOrderbookSubgraphClient {
subgraphs: Vec<MultiSubgraphArgs>,
Expand Down
15 changes: 15 additions & 0 deletions crates/subgraph/src/orderbook_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use cynic::Id;
use reqwest::Url;
use thiserror::Error;

#[cfg(target_family = "wasm")]
use wasm_bindgen::{JsError, JsValue};

const ALL_PAGES_QUERY_PAGE_SIZE: u16 = 200;

#[derive(Error, Debug)]
Expand All @@ -25,6 +28,18 @@ pub enum OrderbookSubgraphClientError {
PaginationClientError(#[from] PaginationClientError),
#[error(transparent)]
ParseError(#[from] alloy::primitives::ruint::ParseError),
#[error(transparent)]
UrlParseError(#[from] url::ParseError),
#[cfg(target_family = "wasm")]
#[error(transparent)]
SerdeWasmBindgenError(#[from] serde_wasm_bindgen::Error),
}

#[cfg(target_family = "wasm")]
impl From<OrderbookSubgraphClientError> for JsValue {
fn from(value: OrderbookSubgraphClientError) -> Self {
JsError::new(&value.to_string()).into()
}
}

pub struct OrderbookSubgraphClient {
Expand Down
7 changes: 6 additions & 1 deletion crates/subgraph/src/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ use crate::{cynic_client::CynicClientError, utils::slice_list};
use serde::{Deserialize, Serialize};
use std::num::TryFromIntError;
use thiserror::Error;
use tsify::Tsify;
use typeshare::typeshare;

#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, Tsify)]
#[typeshare]
#[tsify(into_wasm_abi, from_wasm_abi)]
#[serde(rename_all = "camelCase")]
pub struct PaginationArgs {
pub page: u16,
pub page_size: u16,
Expand Down
8 changes: 6 additions & 2 deletions crates/subgraph/src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ pub struct IdQueryVariables<'a> {
pub id: &'a cynic::Id,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Tsify)]
#[serde(rename_all = "camelCase")]
#[typeshare]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct OrdersListFilterArgs {
pub owners: Vec<Bytes>,
pub active: Option<bool>,
Expand Down Expand Up @@ -89,14 +91,16 @@ pub struct Order {
pub active: bool,
#[tsify(type = "SgBigInt")]
pub timestamp_added: BigInt,
#[tsify(type = "string | undefined")]
pub meta: Option<RainMetaV1>,
pub add_events: Vec<AddOrder>,
pub trades: Vec<OrderStructPartialTrade>,
}

#[derive(Debug, Serialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Tsify)]
#[typeshare]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct OrderWithSubgraphName {
pub order: Order,
pub subgraph_name: String,
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ execSync("npm run build-wasm");
// build specified packages and include them in final index file
// list of packages to build can be extended by adding new package
// names to the list below
const packages = ["common", "quote"];
const packages = ["common", "quote", "js_api"];
for (const package of packages) {
execSync(`node scripts/buildPackage ${package}`);
}
Expand Down
1 change: 1 addition & 0 deletions tauri-app/src-tauri/Cargo.lock

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

Loading
Loading