Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Oct 29, 2024
1 parent b935ec5 commit 5f68182
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 75 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

20 changes: 5 additions & 15 deletions crates/quote/src/js_api/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ impl From<Pair> for MainPair {

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: encode_prefixed(value.block_number.to_be_bytes_vec()),
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,
Expand All @@ -139,14 +142,11 @@ impl From<MainBatchOrderQuotesResponse> for BatchOrderQuotesResponse {
}
impl From<BatchOrderQuotesResponse> for MainBatchOrderQuotesResponse {
fn from(value: BatchOrderQuotesResponse) -> Self {
let mut block_number_error = "block number, ".to_string();
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_str(&value.block_number)
.inspect_err(|e| block_number_error.push_str(&e.to_string()))
.expect_throw(&block_number_error),
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()))
Expand Down Expand Up @@ -252,16 +252,6 @@ mod tests {
assert_eq!(batch_order_quotes_response, expected);
}

#[wasm_bindgen_test]
#[should_panic]
fn test_batch_order_quotes_response_unhappy() {
let main_batch_order_quotes_response = MainBatchOrderQuotesResponse::default();
let mut batch_order_quotes_response =
BatchOrderQuotesResponse::from(main_batch_order_quotes_response);
batch_order_quotes_response.block_number = "abcd".to_string();
let _ = MainBatchOrderQuotesResponse::from(batch_order_quotes_response);
}

#[wasm_bindgen_test]
fn test_pair_roundtrip() {
let main_pair = MainPair::default();
Expand Down
12 changes: 8 additions & 4 deletions crates/quote/src/js_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub struct Pair {
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct BatchOrderQuotesResponse {
pub pair: Pair,
pub block_number: String,
pub block_number: u64,
pub data: Option<OrderQuoteValue>,
pub success: bool,
pub error: Option<String>,
Expand All @@ -202,12 +202,16 @@ pub struct BatchOrderQuotesResponse {
/// Resolves with a BatchOrderQuotesResponse object
#[wasm_bindgen(js_name = "getOrderQuote")]
pub async fn get_order_quote(
order: Order,
order: Vec<Order>,
rpc_url: &str,
block_number: Option<u64>,
) -> Result<JsValue, Error> {
match get_order_quotes(vec![order], block_number, rpc_url.to_string()).await {
match get_order_quotes(order, block_number, rpc_url.to_string()).await {
Err(e) => Err(e),
Ok(v) => Ok(to_value(&BatchOrderQuotesResponse::from(v[0].clone()))?),
Ok(v) => Ok(to_value(
&v.into_iter()
.map(|v| BatchOrderQuotesResponse::from(v))
.collect::<Vec<_>>(),
)?),
}
}
2 changes: 2 additions & 0 deletions crates/subgraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ 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
70 changes: 55 additions & 15 deletions crates/subgraph/src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ pub struct PaginationWithTimestampQueryVariables {
pub timestamp_lte: Option<BigInt>,
}

#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)]
#[typeshare]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Orderbook {
pub id: Bytes,
}
Expand All @@ -76,6 +77,7 @@ pub type RainMetaV1 = Bytes;
#[typeshare]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
// #[cfg_attr(target_family = "wasm", serde(rename(serialize = "SgOrder")))]
pub struct Order {
pub id: Bytes,
pub order_bytes: Bytes,
Expand All @@ -85,6 +87,7 @@ pub struct Order {
pub inputs: Vec<Vault>,
pub orderbook: Orderbook,
pub active: bool,
#[tsify(type = "SgBigInt")]
pub timestamp_added: BigInt,
pub meta: Option<RainMetaV1>,
pub add_events: Vec<AddOrder>,
Expand All @@ -100,10 +103,11 @@ pub struct TradeStructPartialOrder {
pub order_hash: Bytes,
}

#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone, 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,
Expand Down Expand Up @@ -136,13 +140,16 @@ pub struct VaultsListQueryVariables {
pub filters: Option<VaultsListQueryFilters>,
}

#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone, 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")]
pub vault_id: BigInt,
#[tsify(type = "SgBigInt")]
pub balance: BigInt,
pub token: Erc20,
pub orderbook: Orderbook,
Expand All @@ -155,11 +162,13 @@ pub struct Vault {
pub balance_changes: Vec<VaultBalanceChange>,
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[derive(cynic::QueryFragment, Debug, Clone, Serialize, Tsify)]
#[cynic(graphql_type = "Vault")]
#[typeshare]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct VaultBalanceChangeVault {
pub id: Bytes,
#[tsify(type = "SgBigInt")]
pub vault_id: BigInt,
pub token: Erc20,
}
Expand All @@ -179,10 +188,11 @@ pub struct VaultBalanceChangeUnwrapped {
pub orderbook: Orderbook,
}

#[derive(cynic::InlineFragments, Debug, Clone, Serialize)]
#[derive(cynic::InlineFragments, Debug, Clone, Serialize, 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),
Expand All @@ -192,61 +202,81 @@ pub enum VaultBalanceChange {
Unknown,
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[derive(cynic::QueryFragment, Debug, Clone, Serialize, 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")]
pub amount: BigInt,
#[tsify(type = "SgBigInt")]
pub new_vault_balance: BigInt,
#[tsify(type = "SgBigInt")]
pub old_vault_balance: BigInt,
pub vault: VaultBalanceChangeVault,
#[tsify(type = "SgBigInt")]
pub timestamp: BigInt,
pub transaction: Transaction,
pub orderbook: Orderbook,
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[derive(cynic::QueryFragment, Debug, Clone, Serialize, 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")]
pub amount: BigInt,
#[tsify(type = "SgBigInt")]
pub new_vault_balance: BigInt,
#[tsify(type = "SgBigInt")]
pub old_vault_balance: BigInt,
pub vault: VaultBalanceChangeVault,
#[tsify(type = "SgBigInt")]
pub timestamp: BigInt,
pub transaction: Transaction,
pub orderbook: Orderbook,
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[derive(cynic::QueryFragment, Debug, Clone, Serialize, 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")]
pub amount: BigInt,
#[tsify(type = "SgBigInt")]
pub new_vault_balance: BigInt,
#[tsify(type = "SgBigInt")]
pub old_vault_balance: BigInt,
pub vault: VaultBalanceChangeVault,
#[tsify(type = "SgBigInt")]
pub timestamp: BigInt,
pub transaction: Transaction,
pub orderbook: Orderbook,
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[derive(cynic::QueryFragment, Debug, Clone, Serialize, 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")]
pub amount: BigInt,
#[tsify(type = "SgBigInt")]
pub new_vault_balance: BigInt,
#[tsify(type = "SgBigInt")]
pub old_vault_balance: BigInt,
pub vault: VaultBalanceChangeVault,
#[tsify(type = "SgBigInt")]
pub timestamp: BigInt,
pub transaction: Transaction,
pub orderbook: Orderbook,
Expand All @@ -273,46 +303,56 @@ pub struct Trade {
pub orderbook: Orderbook,
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[derive(cynic::QueryFragment, Debug, Clone, Serialize, 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)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone, PartialEq, 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<String>,
pub symbol: Option<String>,
#[tsify(type = "SgBigInt")]
pub decimals: Option<BigInt>,
}

#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone, 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")]
pub block_number: BigInt,
#[tsify(type = "SgBigInt")]
pub timestamp: BigInt,
}

#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone, Tsify)]
#[typeshare]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct AddOrder {
pub transaction: Transaction,
}

#[derive(cynic::Scalar, Debug, Clone, PartialEq)]
#[derive(cynic::Scalar, Debug, Clone, PartialEq, Tsify)]
#[typeshare]
#[tsify(into_wasm_abi, from_wasm_abi)]
#[serde(rename = "SgBigInt")]
pub struct BigInt(pub String);

#[derive(cynic::Scalar, Debug, Clone, PartialEq)]
#[derive(cynic::Scalar, Debug, Clone, PartialEq, Tsify)]
#[typeshare]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Bytes(pub String);

#[derive(cynic::Enum, Clone, Copy, Debug)]
Expand Down
31 changes: 31 additions & 0 deletions crates/subgraph/src/types/impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[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);
}
1 change: 1 addition & 0 deletions crates/subgraph/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod common;
mod impls;
pub mod order;
pub mod order_detail_traits;
pub mod order_trade;
Expand Down
2 changes: 2 additions & 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

0 comments on commit 5f68182

Please sign in to comment.