Skip to content

Commit

Permalink
Merge pull request #945 from rainlanguage/2024-10-16-apy
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyjosh authored Dec 19, 2024
2 parents 82c18af + 3e14914 commit cdec7a5
Show file tree
Hide file tree
Showing 28 changed files with 2,294 additions and 120 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.

15 changes: 15 additions & 0 deletions crates/js_api/src/subgraph/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,18 @@ pub async fn order_vaults_volume(
.await?;
Ok(to_value(&volumes)?)
}

/// Measures an order's performance (including vaults apy and vol and total apy and vol)
#[wasm_bindgen(js_name = "getOrderPerformance")]
pub async fn order_performance(
url: &str,
order_id: &str,
start_timestamp: Option<u64>,
end_timestamp: Option<u64>,
) -> Result<JsValue, OrderbookSubgraphClientError> {
let client = OrderbookSubgraphClient::new(Url::parse(url)?);
let performance = client
.order_performance(Id::new(order_id), start_timestamp, end_timestamp)
.await?;
Ok(to_value(&performance)?)
}
2 changes: 2 additions & 0 deletions crates/subgraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ serde = { workspace = true }
serde_json = { workspace = true }
alloy = { workspace = true, features = ["rand"] }
rain_orderbook_bindings = { workspace = true }
rain_orderbook_math = { workspace = true }
chrono = { workspace = true }
url = { workspace = true, features = ["serde"] }
cynic-introspection = "3.7.3"
once_cell = { workspace = true }
futures = "0.3.17"

[target.'cfg(target_family = "wasm")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/subgraph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ mod cynic_client;
mod multi_orderbook_client;
mod orderbook_client;
mod pagination;
pub mod performance;
pub mod types;
pub mod utils;
pub mod validate;
mod vault_balance_changes_query;
pub mod vol;

#[cynic::schema("orderbook")]
pub mod schema {}
Expand Down
28 changes: 26 additions & 2 deletions crates/subgraph/src/orderbook_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::cynic_client::{CynicClient, CynicClientError};
use crate::pagination::{PaginationArgs, PaginationClient, PaginationClientError};
use crate::performance::vol::{get_vaults_vol, VaultVolume};
use crate::performance::OrderPerformance;
use crate::types::common::*;
use crate::types::order::{
BatchOrderDetailQuery, BatchOrderDetailQueryVariables, OrderDetailQuery, OrderIdList,
Expand All @@ -8,11 +10,10 @@ use crate::types::order::{
use crate::types::order_trade::{OrderTradeDetailQuery, OrderTradesListQuery};
use crate::types::vault::{VaultDetailQuery, VaultsListQuery};
use crate::vault_balance_changes_query::VaultBalanceChangesListPageQueryClient;
use crate::vol::{get_vaults_vol, VaultVolume};
use cynic::Id;
use reqwest::Url;
use std::num::ParseIntError;
use thiserror::Error;

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

Expand All @@ -30,6 +31,10 @@ pub enum OrderbookSubgraphClientError {
ParseError(#[from] alloy::primitives::ruint::ParseError),
#[error(transparent)]
UrlParseError(#[from] url::ParseError),
#[error(transparent)]
PerformanceError(#[from] crate::performance::PerformanceError),
#[error(transparent)]
ParseIntError(#[from] ParseIntError),
#[cfg(target_family = "wasm")]
#[error(transparent)]
SerdeWasmBindgenError(#[from] serde_wasm_bindgen::Error),
Expand Down Expand Up @@ -233,6 +238,25 @@ impl OrderbookSubgraphClient {
Ok(get_vaults_vol(&trades)?)
}

/// Fetches order data and measures an order's detailed performance (apy and vol)
pub async fn order_performance(
&self,
order_id: cynic::Id,
start_timestamp: Option<u64>,
end_timestamp: Option<u64>,
) -> Result<OrderPerformance, OrderbookSubgraphClientError> {
let order = self.order_detail(order_id.clone()).await?;
let trades = self
.order_trades_list_all(order_id, start_timestamp, end_timestamp)
.await?;
Ok(OrderPerformance::measure(
&order,
&trades,
start_timestamp,
end_timestamp,
)?)
}

/// Fetch single vault
pub async fn vault_detail(&self, id: Id) -> Result<Vault, OrderbookSubgraphClientError> {
let data = self
Expand Down
Loading

0 comments on commit cdec7a5

Please sign in to comment.