From bbc9507ede6a016b516f9c810d56ea9ffe84f9b0 Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Wed, 13 Nov 2024 00:50:19 +0000 Subject: [PATCH] init --- Cargo.lock | 2 + crates/bindings/src/js_api.rs | 83 +------ crates/bindings/src/lib.rs | 3 + crates/bindings/src/wasm_traits.rs | 204 ++++++++++++++++++ crates/quote/src/js_api/impls.rs | 22 +- crates/quote/src/js_api/mod.rs | 8 - crates/quote/src/order_quotes.rs | 3 +- crates/subgraph/Cargo.toml | 12 +- crates/subgraph/src/multi_orderbook_client.rs | 36 ++-- crates/subgraph/src/pagination.rs | 13 +- crates/subgraph/src/types/common.rs | 155 +++++++------ crates/subgraph/src/types/impls.rs | 31 --- crates/subgraph/src/types/mod.rs | 1 - 13 files changed, 353 insertions(+), 220 deletions(-) create mode 100644 crates/bindings/src/wasm_traits.rs delete mode 100644 crates/subgraph/src/types/impls.rs diff --git a/Cargo.lock b/Cargo.lock index 2a931b96f..9930af496 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6483,6 +6483,7 @@ dependencies = [ "cynic-codegen", "cynic-introspection", "futures", + "getrandom", "httpmock", "insta", "js-sys", @@ -6497,6 +6498,7 @@ dependencies = [ "typeshare", "url", "wasm-bindgen", + "wasm-bindgen-futures", ] [[package]] diff --git a/crates/bindings/src/js_api.rs b/crates/bindings/src/js_api.rs index ea0567097..736977438 100644 --- a/crates/bindings/src/js_api.rs +++ b/crates/bindings/src/js_api.rs @@ -12,15 +12,9 @@ use alloy::{ sol_types::SolCall, }; use serde::{Deserialize, Serialize}; -use serde_wasm_bindgen::{from_value, to_value}; use std::str::FromStr; use tsify::Tsify; -use wasm_bindgen::{ - convert::*, - describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR}, - prelude::*, - JsValue, UnwrapThrowExt, -}; +use wasm_bindgen::prelude::*; // a serializer fn for serializing Vec as Uint8Array for js fn bytes_serilializer(val: &[u8], serializer: S) -> Result { @@ -29,7 +23,6 @@ fn bytes_serilializer(val: &[u8], serializer: S) -> Result #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct EvaluableV3 { pub interpreter: String, pub store: String, @@ -40,7 +33,6 @@ pub struct EvaluableV3 { #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct IO { pub token: String, pub decimals: u8, @@ -49,7 +41,6 @@ pub struct IO { #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct OrderV3 { pub owner: String, pub evaluable: EvaluableV3, @@ -60,7 +51,6 @@ pub struct OrderV3 { #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct SignedContextV1 { pub signer: String, pub context: Vec, @@ -71,7 +61,6 @@ pub struct SignedContextV1 { #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Quote { pub order: OrderV3, #[serde(rename = "inputIOIndex")] @@ -83,7 +72,6 @@ pub struct Quote { #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct TakeOrderConfigV3 { order: OrderV3, #[serde(rename = "inputIOIndex")] @@ -95,7 +83,6 @@ pub struct TakeOrderConfigV3 { #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct TakeOrdersConfigV3 { minimum_input: String, maximum_input: String, @@ -385,65 +372,17 @@ impl From for TakeOrdersConfigV3 { } } -#[macro_export] -macro_rules! impl_wasm_traits { - ($struct_name:ident) => { - impl RefFromWasmAbi for $struct_name { - type Abi = ::Abi; - type Anchor = Box<$struct_name>; - unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { - Box::new($struct_name::from_abi(js)) - } - } - impl LongRefFromWasmAbi for $struct_name { - type Abi = ::Abi; - type Anchor = Box<$struct_name>; - unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor { - Box::new($struct_name::from_abi(js)) - } - } - impl VectorIntoWasmAbi for $struct_name { - type Abi = as IntoWasmAbi>::Abi; - fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi { - js_value_vector_into_abi(vector) - } - } - impl VectorFromWasmAbi for $struct_name { - type Abi = as IntoWasmAbi>::Abi; - unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> { - js_value_vector_from_abi(js) - } - } - impl WasmDescribeVector for $struct_name { - fn describe_vector() { - inform(VECTOR); - $struct_name::describe(); - } - } - impl From<$struct_name> for JsValue { - fn from(value: $struct_name) -> Self { - let mut err = "".to_string(); - to_value(&value) - .inspect_err(|e| err.push_str(&e.to_string())) - .expect_throw(&err) - } - } - impl TryFromJsValue for $struct_name { - type Error = serde_wasm_bindgen::Error; - fn try_from_js_value(value: JsValue) -> Result { - from_value(value) - } - } - }; -} +mod impls { + use super::super::{impl_all_wasm_traits, wasm_traits::prelude::*}; -impl_wasm_traits!(IO); -impl_wasm_traits!(Quote); -impl_wasm_traits!(OrderV3); -impl_wasm_traits!(EvaluableV3); -impl_wasm_traits!(SignedContextV1); -impl_wasm_traits!(TakeOrderConfigV3); -impl_wasm_traits!(TakeOrdersConfigV3); + impl_all_wasm_traits!(super::IO); + impl_all_wasm_traits!(super::Quote); + impl_all_wasm_traits!(super::OrderV3); + impl_all_wasm_traits!(super::EvaluableV3); + impl_all_wasm_traits!(super::SignedContextV1); + impl_all_wasm_traits!(super::TakeOrderConfigV3); + impl_all_wasm_traits!(super::TakeOrdersConfigV3); +} #[cfg(test)] mod tests { diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 6ad48cfe5..ff9f08472 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -24,3 +24,6 @@ sol!( #[cfg(target_family = "wasm")] pub mod js_api; + +#[cfg(target_family = "wasm")] +pub mod wasm_traits; diff --git a/crates/bindings/src/wasm_traits.rs b/crates/bindings/src/wasm_traits.rs new file mode 100644 index 000000000..ee38239e1 --- /dev/null +++ b/crates/bindings/src/wasm_traits.rs @@ -0,0 +1,204 @@ +pub mod prelude { + pub use serde_wasm_bindgen::{from_value, to_value}; + pub use tsify::Tsify; + pub use wasm_bindgen::{ + convert::*, + describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR}, + prelude::*, + JsValue, UnwrapThrowExt, + }; +} + +pub use prelude::*; + +#[macro_export] +macro_rules! impl_main_wasm_traits { + ($struct_name:path) => { + impl WasmDescribe for $struct_name { + #[inline] + fn describe() { + ::JsType::describe() + } + } + impl IntoWasmAbi for $struct_name { + type Abi = <::JsType as IntoWasmAbi>::Abi; + + #[inline] + fn into_abi(self) -> Self::Abi { + let mut err = "".to_string(); + self.into_js() + .inspect_err(|e| err.push_str(&e.to_string())) + .expect_throw(&err) + .into_abi() + } + } + impl OptionIntoWasmAbi for $struct_name { + #[inline] + fn none() -> Self::Abi { + <::JsType as OptionIntoWasmAbi>::none() + } + } + impl FromWasmAbi for $struct_name { + type Abi = <::JsType as FromWasmAbi>::Abi; + + #[inline] + unsafe fn from_abi(js: Self::Abi) -> Self { + let mut err = "".to_string(); + Self::from_js(::JsType::from_abi(js)) + .inspect_err(|e| err.push_str(&e.to_string())) + .expect_throw(&err) + } + } + impl OptionFromWasmAbi for $struct_name { + #[inline] + fn is_none(js: &Self::Abi) -> bool { + <::JsType as OptionFromWasmAbi>::is_none(js) + } + } + }; +} + +#[macro_export] +macro_rules! impl_complementary_wasm_traits { + ($struct_name:path) => { + impl RefFromWasmAbi for $struct_name { + type Abi = ::Abi; + type Anchor = Box<$struct_name>; + unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { + Box::new(<$struct_name>::from_abi(js)) + } + } + impl LongRefFromWasmAbi for $struct_name { + type Abi = ::Abi; + type Anchor = Box<$struct_name>; + unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor { + Box::new(<$struct_name>::from_abi(js)) + } + } + impl VectorIntoWasmAbi for $struct_name { + type Abi = as IntoWasmAbi>::Abi; + fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi { + js_value_vector_into_abi(vector) + } + } + impl VectorFromWasmAbi for $struct_name { + type Abi = as IntoWasmAbi>::Abi; + unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> { + js_value_vector_from_abi(js) + } + } + impl WasmDescribeVector for $struct_name { + fn describe_vector() { + inform(VECTOR); + <$struct_name>::describe(); + } + } + impl From<$struct_name> for JsValue { + fn from(value: $struct_name) -> Self { + let mut err = "".to_string(); + to_value(&value) + .inspect_err(|e| err.push_str(&e.to_string())) + .expect_throw(&err) + } + } + impl TryFromJsValue for $struct_name { + type Error = serde_wasm_bindgen::Error; + fn try_from_js_value(value: JsValue) -> Result { + from_value(value) + } + } + }; +} + +#[macro_export] +macro_rules! impl_all_wasm_traits { + ($struct_name:path) => { + impl WasmDescribe for $struct_name { + #[inline] + fn describe() { + ::JsType::describe() + } + } + impl IntoWasmAbi for $struct_name { + type Abi = <::JsType as IntoWasmAbi>::Abi; + + #[inline] + fn into_abi(self) -> Self::Abi { + let mut err = "".to_string(); + self.into_js() + .inspect_err(|e| err.push_str(&e.to_string())) + .expect_throw(&err) + .into_abi() + } + } + impl OptionIntoWasmAbi for $struct_name { + #[inline] + fn none() -> Self::Abi { + <::JsType as OptionIntoWasmAbi>::none() + } + } + impl FromWasmAbi for $struct_name { + type Abi = <::JsType as FromWasmAbi>::Abi; + + #[inline] + unsafe fn from_abi(js: Self::Abi) -> Self { + let mut err = "".to_string(); + Self::from_js(::JsType::from_abi(js)) + .inspect_err(|e| err.push_str(&e.to_string())) + .expect_throw(&err) + } + } + impl OptionFromWasmAbi for $struct_name { + #[inline] + fn is_none(js: &Self::Abi) -> bool { + <::JsType as OptionFromWasmAbi>::is_none(js) + } + } + impl RefFromWasmAbi for $struct_name { + type Abi = ::Abi; + type Anchor = Box<$struct_name>; + unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { + Box::new(<$struct_name>::from_abi(js)) + } + } + impl LongRefFromWasmAbi for $struct_name { + type Abi = ::Abi; + type Anchor = Box<$struct_name>; + unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor { + Box::new(<$struct_name>::from_abi(js)) + } + } + impl VectorIntoWasmAbi for $struct_name { + type Abi = as IntoWasmAbi>::Abi; + fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi { + js_value_vector_into_abi(vector) + } + } + impl VectorFromWasmAbi for $struct_name { + type Abi = as IntoWasmAbi>::Abi; + unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> { + js_value_vector_from_abi(js) + } + } + impl WasmDescribeVector for $struct_name { + fn describe_vector() { + inform(VECTOR); + <$struct_name>::describe(); + } + } + impl From<$struct_name> for JsValue { + fn from(value: $struct_name) -> Self { + let mut err = "".to_string(); + to_value(&value) + .inspect_err(|e| err.push_str(&e.to_string())) + .expect_throw(&err) + } + } + impl TryFromJsValue for $struct_name { + type Error = serde_wasm_bindgen::Error; + fn try_from_js_value(value: JsValue) -> Result { + from_value(value) + } + } + }; +} diff --git a/crates/quote/src/js_api/impls.rs b/crates/quote/src/js_api/impls.rs index 516075ac7..d8de07415 100644 --- a/crates/quote/src/js_api/impls.rs +++ b/crates/quote/src/js_api/impls.rs @@ -6,17 +6,12 @@ use alloy::primitives::{ hex::{encode_prefixed, FromHex}, Address, U256, }; -use rain_orderbook_bindings::impl_wasm_traits; use rain_orderbook_bindings::js_api::Quote; use rain_orderbook_bindings::IOrderBookV4::{ Quote as MainQuote, SignedContextV1 as MainSignedContextV1, }; -use serde_wasm_bindgen::{from_value, to_value}; +use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*}; use std::str::FromStr; -use wasm_bindgen::{ - describe::{inform, WasmDescribeVector, VECTOR}, - JsValue, UnwrapThrowExt, -}; impl From for MainOrderQuoteValue { fn from(value: OrderQuoteValue) -> Self { @@ -161,13 +156,14 @@ impl From for MainBatchOrderQuotesResponse { } } -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); +impl_all_wasm_traits!(Pair); +impl_all_wasm_traits!(QuoteSpec); +impl_all_wasm_traits!(QuoteTarget); +impl_all_wasm_traits!(QuoteResult); +impl_all_wasm_traits!(BatchQuoteSpec); +impl_all_wasm_traits!(OrderQuoteValue); +impl_all_wasm_traits!(BatchQuoteTarget); +impl_all_wasm_traits!(BatchOrderQuotesResponse); #[cfg(test)] mod tests { diff --git a/crates/quote/src/js_api/mod.rs b/crates/quote/src/js_api/mod.rs index 677c4874d..7845ff7fe 100644 --- a/crates/quote/src/js_api/mod.rs +++ b/crates/quote/src/js_api/mod.rs @@ -20,7 +20,6 @@ mod impls; /// Holds quoted order max output and ratio #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct OrderQuoteValue { pub max_output: String, pub ratio: String, @@ -29,7 +28,6 @@ pub struct OrderQuoteValue { /// A quote target #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Tsify)] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct QuoteTarget { pub quote_config: Quote, pub orderbook: String, @@ -38,13 +36,11 @@ pub struct QuoteTarget { /// Batch quote target #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Tsify)] #[serde(transparent)] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct BatchQuoteTarget(pub Vec); /// A quote target specifier, where the order details need to be fetched from a /// source (such as subgraph) to build a [QuoteTarget] out of it #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] #[serde(rename_all = "camelCase")] pub struct QuoteSpec { pub order_hash: String, @@ -59,11 +55,9 @@ pub struct QuoteSpec { /// Batch quote spec #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Tsify)] #[serde(transparent)] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct BatchQuoteSpec(pub Vec); #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] #[serde(untagged)] pub enum QuoteResult { Ok(OrderQuoteValue), @@ -180,7 +174,6 @@ 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, @@ -189,7 +182,6 @@ pub struct Pair { #[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, diff --git a/crates/quote/src/order_quotes.rs b/crates/quote/src/order_quotes.rs index fe2b0c7b7..1b746450b 100644 --- a/crates/quote/src/order_quotes.rs +++ b/crates/quote/src/order_quotes.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use crate::{ error::Error, quote::{BatchQuoteTarget, QuoteTarget}, @@ -10,6 +8,7 @@ use alloy_ethers_typecast::transaction::ReadableClient; use rain_orderbook_bindings::IOrderBookV4::{OrderV3, Quote}; use rain_orderbook_subgraph_client::types::common::Order; use serde::{Deserialize, Serialize}; +use std::str::FromStr; use typeshare::typeshare; #[typeshare] diff --git a/crates/subgraph/Cargo.toml b/crates/subgraph/Cargo.toml index d5758b98e..262dcd1d1 100644 --- a/crates/subgraph/Cargo.toml +++ b/crates/subgraph/Cargo.toml @@ -18,15 +18,19 @@ serde_json = { workspace = true } alloy = { workspace = true, features = ["rand"] } rain_orderbook_bindings = { workspace = true } chrono = { workspace = true } -url = { workspace = true } +url = { workspace = true, features = ["serde"]} cynic-introspection = "3.7.3" futures = "0.3.17" -tsify = { version = "0.4.5", default-features = false, features = ["js", "wasm-bindgen"] } -wasm-bindgen = { version = "0.2.92" } + +[target.'cfg(target_family = "wasm")'.dependencies] 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"] } +getrandom = { version = "0", features = ["js", "js-sys"] } -[dev-dependencies] +[target.'cfg(not(target_family = "wasm"))'.dev-dependencies] insta = { workspace = true } tokio = { workspace = true, features = ["full"] } httpmock = "0.7.0" diff --git a/crates/subgraph/src/multi_orderbook_client.rs b/crates/subgraph/src/multi_orderbook_client.rs index 8eb5bff87..f846cce6b 100644 --- a/crates/subgraph/src/multi_orderbook_client.rs +++ b/crates/subgraph/src/multi_orderbook_client.rs @@ -1,38 +1,30 @@ -use futures::future::join_all; -use reqwest::Url; -use serde::{Deserialize, Serialize}; -use tsify::Tsify; - use crate::{ types::common::{ OrderWithSubgraphName, OrdersListFilterArgs, VaultWithSubgraphName, VaultsListFilterArgs, }, OrderbookSubgraphClient, OrderbookSubgraphClientError, PaginationArgs, }; - -#[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}; +use futures::future::join_all; +use reqwest::Url; +use serde::{Deserialize, Serialize}; #[cfg(target_family = "wasm")] -use wasm_bindgen::{JsValue, UnwrapThrowExt}; +use tsify::Tsify; -#[derive(Debug, Clone, Serialize, Deserialize, Tsify)] -#[tsify(into_wasm_abi, from_wasm_abi)] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] pub struct MultiSubgraphArgs { - #[tsify(type = "string")] + #[cfg_attr(target_family = "wasm", tsify(type = "string"))] url: Url, name: String, } + #[cfg(target_family = "wasm")] -impl_wasm_traits!(MultiSubgraphArgs); +mod wasm_impls { + use super::*; + use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*}; + + impl_all_wasm_traits!(MultiSubgraphArgs); +} pub struct MultiOrderbookSubgraphClient { subgraphs: Vec, diff --git a/crates/subgraph/src/pagination.rs b/crates/subgraph/src/pagination.rs index a3ec4518a..123489d6b 100644 --- a/crates/subgraph/src/pagination.rs +++ b/crates/subgraph/src/pagination.rs @@ -2,18 +2,27 @@ use crate::{cynic_client::CynicClientError, utils::slice_list}; use serde::{Deserialize, Serialize}; use std::num::TryFromIntError; use thiserror::Error; +#[cfg(target_family = "wasm")] use tsify::Tsify; use typeshare::typeshare; -#[derive(Clone, Serialize, Deserialize, Debug, Tsify)] +#[derive(Clone, Serialize, Deserialize, Debug)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] #[serde(rename_all = "camelCase")] pub struct PaginationArgs { pub page: u16, pub page_size: u16, } +#[cfg(target_family = "wasm")] +mod wasm_impls { + use super::*; + use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*}; + + impl_all_wasm_traits!(PaginationArgs); +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub struct QueryPaginationVariables { pub skip: Option, diff --git a/crates/subgraph/src/types/common.rs b/crates/subgraph/src/types/common.rs index 0db2de374..fdff13807 100644 --- a/crates/subgraph/src/types/common.rs +++ b/crates/subgraph/src/types/common.rs @@ -1,5 +1,6 @@ use crate::schema; use serde::{Deserialize, Serialize}; +#[cfg(target_family = "wasm")] use tsify::Tsify; use typeshare::typeshare; @@ -9,10 +10,10 @@ pub struct IdQueryVariables<'a> { pub id: &'a cynic::Id, } -#[derive(Debug, Clone, Serialize, Deserialize, Tsify)] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[serde(rename_all = "camelCase")] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct OrdersListFilterArgs { pub owners: Vec, pub active: Option, @@ -65,21 +66,21 @@ pub struct PaginationWithTimestampQueryVariables { pub timestamp_lte: Option, } -#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)] +#[derive(cynic::QueryFragment, Debug, Serialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Orderbook { pub id: Bytes, } #[typeshare] -#[tsify::declare] +#[cfg_attr(target_family = "wasm", tsify::declare)] pub type RainMetaV1 = Bytes; -#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)] +#[derive(cynic::QueryFragment, Debug, Serialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Order { pub id: Bytes, pub order_bytes: Bytes, @@ -89,18 +90,18 @@ pub struct Order { pub inputs: Vec, pub orderbook: Orderbook, pub active: bool, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub timestamp_added: BigInt, - #[tsify(type = "string | undefined")] + #[cfg_attr(target_family = "wasm", tsify(type = "string | undefined | null"))] pub meta: Option, pub add_events: Vec, pub trades: Vec, } -#[derive(Debug, Serialize, Deserialize, Clone, Tsify)] +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct OrderWithSubgraphName { pub order: Order, pub subgraph_name: String, @@ -115,21 +116,21 @@ pub struct TradeStructPartialOrder { pub order_hash: Bytes, } -#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)] +#[derive(cynic::QueryFragment, Debug, Serialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[cynic(graphql_type = "Order")] #[serde(rename_all = "camelCase")] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct OrderAsIO { pub id: Bytes, pub order_hash: Bytes, pub active: bool, } -#[derive(Debug, Clone, Serialize, Deserialize, Tsify)] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[serde(rename_all = "camelCase")] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct VaultsListFilterArgs { pub owners: Vec, pub hide_zero_balance: bool, @@ -154,16 +155,16 @@ pub struct VaultsListQueryVariables { pub filters: Option, } -#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)] +#[derive(cynic::QueryFragment, Debug, Serialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Vault { pub id: Bytes, pub owner: Bytes, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub vault_id: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub balance: BigInt, pub token: Erc20, pub orderbook: Orderbook, @@ -176,22 +177,22 @@ pub struct Vault { pub balance_changes: Vec, } -#[derive(Debug, Serialize, Deserialize, Clone, Tsify)] +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct VaultWithSubgraphName { pub vault: Vault, pub subgraph_name: String, } -#[derive(cynic::QueryFragment, Debug, Clone, Serialize, Tsify)] +#[derive(cynic::QueryFragment, Debug, Clone, Serialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[cynic(graphql_type = "Vault")] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct VaultBalanceChangeVault { pub id: Bytes, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub vault_id: BigInt, pub token: Erc20, } @@ -211,11 +212,11 @@ pub struct VaultBalanceChangeUnwrapped { pub orderbook: Orderbook, } -#[derive(cynic::InlineFragments, Debug, Clone, Serialize, Tsify)] +#[derive(cynic::InlineFragments, Debug, Clone, Serialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[serde(tag = "__typename", content = "data")] #[serde(rename_all = "camelCase")] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub enum VaultBalanceChange { Withdrawal(Withdrawal), TradeVaultBalanceChange(TradeVaultBalanceChange), @@ -225,81 +226,81 @@ pub enum VaultBalanceChange { Unknown, } -#[derive(cynic::QueryFragment, Debug, Clone, Serialize, Tsify)] +#[derive(cynic::QueryFragment, Debug, Clone, Serialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Deposit { pub id: Bytes, pub __typename: String, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub amount: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub new_vault_balance: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub old_vault_balance: BigInt, pub vault: VaultBalanceChangeVault, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub timestamp: BigInt, pub transaction: Transaction, pub orderbook: Orderbook, } -#[derive(cynic::QueryFragment, Debug, Clone, Serialize, Tsify)] +#[derive(cynic::QueryFragment, Debug, Clone, Serialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Withdrawal { pub id: Bytes, pub __typename: String, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub amount: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub new_vault_balance: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub old_vault_balance: BigInt, pub vault: VaultBalanceChangeVault, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub timestamp: BigInt, pub transaction: Transaction, pub orderbook: Orderbook, } -#[derive(cynic::QueryFragment, Debug, Clone, Serialize, Tsify)] +#[derive(cynic::QueryFragment, Debug, Clone, Serialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct TradeVaultBalanceChange { pub id: Bytes, pub __typename: String, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub amount: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub new_vault_balance: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub old_vault_balance: BigInt, pub vault: VaultBalanceChangeVault, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub timestamp: BigInt, pub transaction: Transaction, pub orderbook: Orderbook, } -#[derive(cynic::QueryFragment, Debug, Clone, Serialize, Tsify)] +#[derive(cynic::QueryFragment, Debug, Clone, Serialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct ClearBounty { pub id: Bytes, pub __typename: String, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub amount: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub new_vault_balance: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub old_vault_balance: BigInt, pub vault: VaultBalanceChangeVault, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub timestamp: BigInt, pub transaction: Transaction, pub orderbook: Orderbook, @@ -326,56 +327,56 @@ pub struct Trade { pub orderbook: Orderbook, } -#[derive(cynic::QueryFragment, Debug, Clone, Serialize, Tsify)] +#[derive(cynic::QueryFragment, Debug, Clone, Serialize)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[cynic(graphql_type = "Trade")] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct OrderStructPartialTrade { pub id: Bytes, } -#[derive(cynic::QueryFragment, Debug, Serialize, Clone, PartialEq, Tsify)] +#[derive(cynic::QueryFragment, Debug, Serialize, Clone, PartialEq)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[cynic(graphql_type = "ERC20")] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Erc20 { pub id: Bytes, pub address: Bytes, pub name: Option, pub symbol: Option, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub decimals: Option, } -#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)] +#[derive(cynic::QueryFragment, Debug, Serialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] #[serde(rename_all = "camelCase")] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Transaction { pub id: Bytes, pub from: Bytes, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub block_number: BigInt, - #[tsify(type = "SgBigInt")] + #[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))] pub timestamp: BigInt, } -#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)] +#[derive(cynic::QueryFragment, Debug, Serialize, Clone)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct AddOrder { pub transaction: Transaction, } -#[derive(cynic::Scalar, Debug, Clone, PartialEq, Tsify)] +#[derive(cynic::Scalar, Debug, Clone, PartialEq)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] #[serde(rename = "SgBigInt")] pub struct BigInt(pub String); -#[derive(cynic::Scalar, Debug, Clone, PartialEq, Tsify)] +#[derive(cynic::Scalar, Debug, Clone, PartialEq)] +#[cfg_attr(target_family = "wasm", derive(Tsify))] #[typeshare] -#[tsify(into_wasm_abi, from_wasm_abi)] pub struct Bytes(pub String); #[derive(cynic::Enum, Clone, Copy, Debug)] @@ -562,3 +563,27 @@ pub enum VaultOrderBy { #[cynic(rename = "balanceChanges")] BalanceChanges, } + +#[cfg(target_family = "wasm")] +mod impls { + use super::*; + use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*}; + + impl_all_wasm_traits!(Order); + impl_all_wasm_traits!(Vault); + impl_all_wasm_traits!(AddOrder); + impl_all_wasm_traits!(OrderAsIO); + impl_all_wasm_traits!(VaultBalanceChangeVault); + impl_all_wasm_traits!(VaultBalanceChange); + impl_all_wasm_traits!(Withdrawal); + impl_all_wasm_traits!(TradeVaultBalanceChange); + impl_all_wasm_traits!(Deposit); + impl_all_wasm_traits!(ClearBounty); + impl_all_wasm_traits!(OrderStructPartialTrade); + impl_all_wasm_traits!(Erc20); + impl_all_wasm_traits!(Transaction); + impl_all_wasm_traits!(BigInt); + impl_all_wasm_traits!(Bytes); + impl_all_wasm_traits!(OrdersListFilterArgs); + impl_all_wasm_traits!(VaultsListFilterArgs); +} diff --git a/crates/subgraph/src/types/impls.rs b/crates/subgraph/src/types/impls.rs deleted file mode 100644 index d801d4619..000000000 --- a/crates/subgraph/src/types/impls.rs +++ /dev/null @@ -1,31 +0,0 @@ -#[cfg(target_family = "wasm")] -mod js_api { - use super::super::common::{ - AddOrder, BigInt, Bytes, ClearBounty, Deposit, Erc20, Order, OrderAsIO, - OrderStructPartialTrade, TradeVaultBalanceChange, Transaction, Vault, VaultBalanceChange, - VaultBalanceChangeVault, Withdrawal, - }; - use rain_orderbook_bindings::impl_wasm_traits; - use serde_wasm_bindgen::{from_value, to_value}; - use wasm_bindgen::{convert::*, describe::WasmDescribe}; - use wasm_bindgen::{ - describe::{inform, WasmDescribeVector, VECTOR}, - JsValue, UnwrapThrowExt, - }; - - impl_wasm_traits!(Order); - impl_wasm_traits!(Vault); - impl_wasm_traits!(AddOrder); - impl_wasm_traits!(OrderAsIO); - impl_wasm_traits!(VaultBalanceChangeVault); - impl_wasm_traits!(VaultBalanceChange); - impl_wasm_traits!(Withdrawal); - impl_wasm_traits!(TradeVaultBalanceChange); - impl_wasm_traits!(Deposit); - impl_wasm_traits!(ClearBounty); - impl_wasm_traits!(OrderStructPartialTrade); - impl_wasm_traits!(Erc20); - impl_wasm_traits!(Transaction); - impl_wasm_traits!(BigInt); - impl_wasm_traits!(Bytes); -} diff --git a/crates/subgraph/src/types/mod.rs b/crates/subgraph/src/types/mod.rs index b653da9b9..88b092954 100644 --- a/crates/subgraph/src/types/mod.rs +++ b/crates/subgraph/src/types/mod.rs @@ -1,5 +1,4 @@ pub mod common; -mod impls; pub mod order; pub mod order_detail_traits; pub mod order_trade;