-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Vaults REST API (3 methods). An example is also provided. The provided tests test if the Indexer responses are correctly parsed.
- Loading branch information
Showing
8 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#[allow(dead_code)] | ||
pub const TEST_MNEMONIC: &str = "mirror actor skill push coach wait confirm orchard lunch mobile athlete gossip awake miracle matter bus reopen team ladder lazy list timber render wait"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
mod support; | ||
use anyhow::{Error, Result}; | ||
use dydx::config::ClientConfig; | ||
use dydx::indexer::{IndexerClient, PnlTickInterval}; | ||
|
||
pub struct Rester { | ||
indexer: IndexerClient, | ||
} | ||
|
||
impl Rester { | ||
pub async fn connect() -> Result<Self> { | ||
let config = ClientConfig::from_file("client/tests/testnet.toml").await?; | ||
let indexer = IndexerClient::new(config.indexer); | ||
Ok(Self { indexer }) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
tracing_subscriber::fmt().try_init().map_err(Error::msg)?; | ||
let rester = Rester::connect().await?; | ||
let indexer = rester.indexer; | ||
|
||
// Test values | ||
let resolution = PnlTickInterval::Hour; | ||
|
||
let pnls = indexer | ||
.vaults() | ||
.get_megavault_historical_pnl(resolution) | ||
.await?; | ||
tracing::info!("MegaVault historical PnLs: {pnls:?}"); | ||
|
||
let vaults_pnls = indexer | ||
.vaults() | ||
.get_vaults_historical_pnl(resolution) | ||
.await?; | ||
tracing::info!("Vaults historical PnLs: {vaults_pnls:?}"); | ||
|
||
let positions = indexer.vaults().get_megavault_positions().await?; | ||
tracing::info!("MegaVault positions: {positions:?}"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::*; | ||
use anyhow::Error; | ||
|
||
/// Vaults dispatcher. | ||
/// | ||
/// Check [the example](https://github.com/NethermindEth/dydx-v4-rust/blob/trunk/client/examples/vaults_endpoint.rs). | ||
pub struct Vaults<'a> { | ||
rest: &'a RestClient, | ||
} | ||
|
||
impl<'a> Vaults<'a> { | ||
/// Create a new vaults dispatcher. | ||
pub(crate) fn new(rest: &'a RestClient) -> Self { | ||
Self { rest } | ||
} | ||
|
||
/// MegaVault historical PnL. | ||
pub async fn get_megavault_historical_pnl( | ||
&self, | ||
resolution: PnlTickInterval, | ||
) -> Result<Vec<PnlTicksResponseObject>, Error> { | ||
let rest = &self.rest; | ||
const URI: &str = "/v4/vault/v1/megavault/historicalPnl"; | ||
let url = format!("{}{URI}", rest.config.endpoint); | ||
let resp = rest | ||
.client | ||
.get(url) | ||
.query(&[("resolution", resolution)]) | ||
.send() | ||
.await? | ||
.error_for_status()? | ||
.json::<MegaVaultHistoricalPnlResponse>() | ||
.await? | ||
.megavault_pnl; | ||
Ok(resp) | ||
} | ||
|
||
/// Vaults historical PnL. | ||
pub async fn get_vaults_historical_pnl( | ||
&self, | ||
resolution: PnlTickInterval, | ||
) -> Result<Vec<VaultHistoricalPnl>, Error> { | ||
let rest = &self.rest; | ||
const URI: &str = "/v4/vault/v1/vaults/historicalPnl"; | ||
let url = format!("{}{URI}", rest.config.endpoint); | ||
let resp = rest | ||
.client | ||
.get(url) | ||
.query(&[("resolution", resolution)]) | ||
.send() | ||
.await? | ||
.error_for_status()? | ||
.json::<VaultsHistoricalPnLResponse>() | ||
.await? | ||
.vaults_pnl; | ||
Ok(resp) | ||
} | ||
|
||
/// MegaVault positions. | ||
pub async fn get_megavault_positions(&self) -> Result<Vec<VaultPosition>, Error> { | ||
let rest = &self.rest; | ||
const URI: &str = "/v4/vault/v1/megavault/positions"; | ||
let url = format!("{}{URI}", rest.config.endpoint); | ||
let resp = rest | ||
.client | ||
.get(url) | ||
.send() | ||
.await? | ||
.error_for_status()? | ||
.json::<MegaVaultPositionResponse>() | ||
.await? | ||
.positions; | ||
Ok(resp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters