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

Add wasm bindings for order quoting #957

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Cargo.lock

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

84 changes: 84 additions & 0 deletions crates/quote/src/js_api/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use crate::QuoteTarget as MainQuoteTarget;
use crate::{BatchOrderQuotesResponse as MainBatchOrderQuotesResponse, Pair as MainPair};
use crate::{OrderQuoteValue as MainOrderQuoteValue, QuoteSpec as MainQuoteSpec};
use alloy::primitives::{
hex::{encode_prefixed, FromHex},
Expand Down Expand Up @@ -106,11 +107,67 @@ impl From<crate::QuoteResult> for super::QuoteResult {
}
}

impl From<MainPair> for Pair {
fn from(value: MainPair) -> Self {
Pair {
pair_name: value.pair_name,
input_index: value.input_index,
output_index: value.output_index,
}
}
}
impl From<Pair> for MainPair {
fn from(value: Pair) -> Self {
MainPair {
pair_name: value.pair_name,
input_index: value.input_index,
output_index: value.output_index,
}
}
}

impl From<MainBatchOrderQuotesResponse> for BatchOrderQuotesResponse {
fn from(value: MainBatchOrderQuotesResponse) -> Self {
let mut block_number_error = "block number, ".to_string();
BatchOrderQuotesResponse {
pair: value.pair.into(),
block_number: u64::try_from(value.block_number)
.inspect_err(|e| block_number_error.push_str(&e.to_string()))
.expect_throw(&block_number_error),
data: value.data.map(OrderQuoteValue::from),
success: value.success,
error: value.error,
}
}
}
impl From<BatchOrderQuotesResponse> for MainBatchOrderQuotesResponse {
fn from(value: BatchOrderQuotesResponse) -> Self {
let mut max_output_error = "max output, ".to_string();
let mut ratio_error = "ratio, ".to_string();
MainBatchOrderQuotesResponse {
pair: value.pair.into(),
block_number: U256::from(value.block_number),
data: value.data.map(|e| MainOrderQuoteValue {
max_output: U256::from_str(&e.max_output)
.inspect_err(|e| max_output_error.push_str(&e.to_string()))
.expect_throw(&max_output_error),
ratio: U256::from_str(&e.ratio)
.inspect_err(|e| ratio_error.push_str(&e.to_string()))
.expect_throw(&ratio_error),
}),
success: value.success,
error: value.error,
}
}
}

impl_wasm_traits!(QuoteSpec);
impl_wasm_traits!(QuoteTarget);
impl_wasm_traits!(QuoteResult);
impl_wasm_traits!(BatchQuoteSpec);
impl_wasm_traits!(BatchQuoteTarget);
impl_wasm_traits!(Pair);
impl_wasm_traits!(BatchOrderQuotesResponse);

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -179,4 +236,31 @@ mod tests {
quote_target.quote_config.order.owner = "0x1234".to_string();
let _ = MainQuoteTarget::from(quote_target);
}

#[wasm_bindgen_test]
fn test_batch_order_quotes_response_roundtrip() {
let main_batch_order_quotes_response = MainBatchOrderQuotesResponse::default();
let batch_order_quotes_response =
BatchOrderQuotesResponse::from(main_batch_order_quotes_response.clone());
let expected: MainBatchOrderQuotesResponse =
MainBatchOrderQuotesResponse::from(batch_order_quotes_response.clone());
assert_eq!(main_batch_order_quotes_response, expected);

let main_batch_order_quotes_response =
MainBatchOrderQuotesResponse::from(batch_order_quotes_response.clone());
let expected = BatchOrderQuotesResponse::from(main_batch_order_quotes_response.clone());
assert_eq!(batch_order_quotes_response, expected);
}

#[wasm_bindgen_test]
fn test_pair_roundtrip() {
let main_pair = MainPair::default();
let pair = Pair::from(main_pair.clone());
let expected = MainPair::from(pair.clone());
assert_eq!(main_pair, expected);

let main_pair = MainPair::from(pair.clone());
let expected = Pair::from(main_pair.clone());
assert_eq!(pair, expected);
}
}
45 changes: 43 additions & 2 deletions crates/quote/src/js_api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{error::Error, BatchQuoteSpec as MainBatchQuoteSpec, QuoteSpec as MainQuoteSpec};
use crate::{BatchQuoteTarget as MainBatchQuoteTarget, QuoteTarget as MainQuoteTarget};
use crate::{
get_order_quotes, BatchQuoteTarget as MainBatchQuoteTarget, QuoteTarget as MainQuoteTarget,
};
use alloy::primitives::{
hex::{encode_prefixed, FromHex},
Address, U256,
};
use rain_orderbook_bindings::js_api::{Quote, SignedContextV1};
use rain_orderbook_subgraph_client::utils::make_order_id;
use rain_orderbook_subgraph_client::{types::common::Order, utils::make_order_id};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::to_value;
use std::str::FromStr;
Expand Down Expand Up @@ -175,3 +177,42 @@ pub async fn get_batch_quote_target_from_subgraph(
)?),
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Pair {
pub pair_name: String,
pub input_index: u32,
pub output_index: u32,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct BatchOrderQuotesResponse {
pub pair: Pair,
pub block_number: u64,
pub data: Option<OrderQuoteValue>,
pub success: bool,
pub error: Option<String>,
}

/// Get the quote for an order
/// Resolves with a BatchOrderQuotesResponse object
#[wasm_bindgen(js_name = "getOrderQuote")]
pub async fn get_order_quote(
order: Vec<Order>,
rpc_url: &str,
block_number: Option<u64>,
) -> Result<JsValue, Error> {
Ok(to_value(
&get_order_quotes(order, block_number, rpc_url.to_string())
.await
.map(|v| {
v.into_iter()
.map(BatchOrderQuotesResponse::from)
.collect::<Vec<_>>()
})?,
)?)
}
4 changes: 4 additions & 0 deletions crates/subgraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ alloy = { workspace = true, features = ["rand"] }
rain_orderbook_bindings = { workspace = true }
chrono = { workspace = true }
cynic-introspection = "3.7.3"
tsify = { version = "0.4.5", default-features = false, features = ["js", "wasm-bindgen"] }
wasm-bindgen = { version = "0.2.92" }
js-sys = { version = "0.3.69" }
serde-wasm-bindgen = { version = "0.6.5" }

[dev-dependencies]
insta = { workspace = true }
Expand Down
Loading
Loading