Skip to content

Commit

Permalink
Merge pull request #1028 from rainlanguage/add-order-trades-list-wasm
Browse files Browse the repository at this point in the history
getOrderTradeList, getOrderTradeDetail, getOrderTradeCount
  • Loading branch information
hardyjosh authored Dec 2, 2024
2 parents 7d0c170 + d735db9 commit 7680d8d
Show file tree
Hide file tree
Showing 14 changed files with 1,008 additions and 609 deletions.
56 changes: 56 additions & 0 deletions crates/js_api/src/subgraph/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,59 @@ pub async fn get_order(url: &str, id: &str) -> Result<JsValue, OrderbookSubgraph
let order = client.order_detail(Id::new(id)).await?;
Ok(to_value(&order)?)
}

/// Fetch trades for a specific order
/// Returns a list of Trade structs
#[wasm_bindgen(js_name = "getOrderTradesList")]
pub async fn get_order_trades_list(
url: &str,
order_id: &str,
pagination_args: PaginationArgs,
start_timestamp: Option<u64>,
end_timestamp: Option<u64>,
) -> Result<JsValue, OrderbookSubgraphClientError> {
let client = OrderbookSubgraphClient::new(Url::parse(url)?);
let trades = client
.order_trades_list(
Id::new(order_id),
pagination_args,
start_timestamp,
end_timestamp,
)
.await?;
Ok(to_value(&trades)?)
}

/// Get details for a specific trade
/// Returns a Trade struct
#[wasm_bindgen(js_name = "getOrderTradeDetail")]
pub async fn get_order_trade_detail(
url: &str,
trade_id: &str,
) -> Result<JsValue, OrderbookSubgraphClientError> {
let client = OrderbookSubgraphClient::new(Url::parse(url)?);
let trade = client.order_trade_detail(Id::new(trade_id)).await?;
Ok(to_value(&trade)?)
}

/// Fetch the count of trades for a specific order
/// Returns the count as a JavaScript-compatible number
#[wasm_bindgen(js_name = "getOrderTradesCount")]
pub async fn get_order_trades_count(
url: &str,
order_id: &str,
start_timestamp: Option<u64>,
end_timestamp: Option<u64>,
) -> Result<JsValue, OrderbookSubgraphClientError> {
// Create the subgraph client using the provided URL
let client = OrderbookSubgraphClient::new(Url::parse(url)?);

// Fetch all trades for the specific order and calculate the count
let trades_count = client
.order_trades_list_all(Id::new(order_id), start_timestamp, end_timestamp)
.await?
.len();

// Convert the count to a JavaScript-compatible value and return
Ok(to_value(&trades_count)?)
}
5 changes: 5 additions & 0 deletions crates/subgraph/src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,18 @@ pub struct TradeEvent {
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[cfg_attr(target_family = "wasm", derive(Tsify))]
#[typeshare]
#[serde(rename_all = "camelCase")]
pub struct Trade {
pub id: Bytes,
pub trade_event: TradeEvent,
#[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))]
pub output_vault_balance_change: TradeVaultBalanceChange,
pub order: TradeStructPartialOrder,
#[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))]
pub input_vault_balance_change: TradeVaultBalanceChange,
#[cfg_attr(target_family = "wasm", tsify(type = "SgBigInt"))]
pub timestamp: BigInt,
pub orderbook: Orderbook,
}
Expand Down Expand Up @@ -584,4 +588,5 @@ mod impls {
impl_all_wasm_traits!(Bytes);
impl_all_wasm_traits!(OrdersListFilterArgs);
impl_all_wasm_traits!(VaultsListFilterArgs);
impl_all_wasm_traits!(Trade);
}
Loading

0 comments on commit 7680d8d

Please sign in to comment.