Skip to content

Commit

Permalink
Add NodeClient MegaVault example
Browse files Browse the repository at this point in the history
  • Loading branch information
v0-e committed Nov 29, 2024
1 parent 57c117a commit c9d7acb
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 29 deletions.
76 changes: 76 additions & 0 deletions v4-client-rs/client/examples/validator_megavault.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
mod support;
use anyhow::{Error, Result};
use bigdecimal::num_bigint::BigInt;
use dydx::config::ClientConfig;
use dydx::node::{BigIntExt, NodeClient, Wallet};
use support::constants::TEST_MNEMONIC;
use tokio::time::{sleep, Duration};

pub struct MegaVaulter {
client: NodeClient,
wallet: Wallet,
}

impl MegaVaulter {
pub async fn connect() -> Result<Self> {
let config = ClientConfig::from_file("client/tests/testnet.toml").await?;
let client = NodeClient::connect(config.node).await?;
let wallet = Wallet::from_mnemonic(TEST_MNEMONIC)?;
Ok(Self { client, wallet })
}
}

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt().try_init().map_err(Error::msg)?;
#[cfg(feature = "telemetry")]
support::telemetry::metrics_dashboard().await?;

let mut vaulter = MegaVaulter::connect().await?;
let mut account = vaulter.wallet.account(0, &mut vaulter.client).await?;
let address = account.address().clone();
let subaccount = account.subaccount(0)?;

// Deposit 1 USDC into the MegaVault
let tx_hash = vaulter
.client
.megavault()
.deposit(&mut account, subaccount.clone(), 1)
.await?;
tracing::info!("Deposit transaction hash: {:?}", tx_hash);

sleep(Duration::from_secs(2)).await;

// Withdraw 1 share from the MegaVault
let number_of_shares: BigInt = 1.into();
let tx_hash = vaulter
.client
.megavault()
.withdraw(&mut account, subaccount, 0, Some(&number_of_shares))
.await?;
tracing::info!("Withdraw transaction hash: {:?}", tx_hash);

// Query methods

let owner_shares = vaulter
.client
.megavault()
.get_owner_shares(&address)
.await?;
tracing::info!("Get owner shares: {owner_shares:?}");

// Convert serialized integer into an integer (`BigIntExt` trait)
if let Some(shares) = owner_shares.shares {
let nshares = BigInt::from_serializable_int(&shares.num_shares)?;
tracing::info!("Number of owned shares: {}", nshares);
}

let withdrawal_info = vaulter
.client
.megavault()
.get_withdrawal_info(&number_of_shares)
.await?;
tracing::info!("Get withdrawal info: {withdrawal_info:?}");

Ok(())
}
32 changes: 24 additions & 8 deletions v4-client-rs/client/src/node/client/megavault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ use super::*;
use crate::node::utils::BigIntExt;

use anyhow::{anyhow as err, Error};
use dydx_proto::dydxprotocol::{subaccounts::SubaccountId, vault::{MsgDepositToMegavault, MsgWithdrawFromMegavault, NumShares, QueryMegavaultOwnerSharesRequest, QueryMegavaultOwnerSharesResponse, QueryMegavaultWithdrawalInfoRequest, QueryMegavaultWithdrawalInfoResponse}};
use dydx_proto::dydxprotocol::{
subaccounts::SubaccountId,
vault::{
MsgDepositToMegavault, MsgWithdrawFromMegavault, NumShares,
QueryMegavaultOwnerSharesRequest, QueryMegavaultOwnerSharesResponse,
QueryMegavaultWithdrawalInfoRequest, QueryMegavaultWithdrawalInfoResponse,
},
};

use bigdecimal::num_bigint::ToBigInt;

Expand Down Expand Up @@ -79,7 +86,10 @@ impl<'a> MegaVault<'a> {
client.broadcast_transaction(tx_raw).await
}

pub async fn get_owner_shares(&mut self, address: &Address) -> Result<QueryMegavaultOwnerSharesResponse, Error> {
pub async fn get_owner_shares(
&mut self,
address: &Address,
) -> Result<QueryMegavaultOwnerSharesResponse, Error> {
let client = &mut self.client;
let req = QueryMegavaultOwnerSharesRequest {
address: address.to_string(),
Expand All @@ -90,18 +100,24 @@ impl<'a> MegaVault<'a> {
Ok(response)
}

pub async fn get_withdrawal_info(&mut self, shares: &BigInt) -> Result<QueryMegavaultWithdrawalInfoResponse, Error> {
pub async fn get_withdrawal_info(
&mut self,
shares: &BigInt,
) -> Result<QueryMegavaultWithdrawalInfoResponse, Error> {
let client = &mut self.client;
let num_shares = NumShares {
num_shares: shares.to_serializable_vec()?
let num_shares = NumShares {
num_shares: shares.to_serializable_vec()?,
};
let req = QueryMegavaultWithdrawalInfoRequest {
shares_to_withdraw: Some(num_shares)
shares_to_withdraw: Some(num_shares),
};

let response = client.vault.megavault_withdrawal_info(req).await?.into_inner();
let response = client
.vault
.megavault_withdrawal_info(req)
.await?
.into_inner();

Ok(response)
}

}
2 changes: 1 addition & 1 deletion v4-client-rs/client/src/node/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod error;
mod methods;
mod megavault;
mod methods;

use super::{
builder::TxBuilder, config::NodeConfig, order::*, sequencer::*, utils::*, wallet::Account,
Expand Down
1 change: 1 addition & 0 deletions v4-client-rs/client/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub use client::{error::*, Address, NodeClient, Subaccount, TxHash};
pub use config::NodeConfig;
pub use order::*;
pub use types::ChainId;
pub use utils::BigIntExt;
pub use wallet::{Account, Wallet};
26 changes: 6 additions & 20 deletions v4-client-rs/client/tests/test_node_megavault.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
mod env;
use env::TestEnv;

use anyhow::{anyhow as err, Error};
use bigdecimal::{num_traits::cast::ToPrimitive, BigDecimal, One};
use chrono::{TimeDelta, Utc};
use dydx::{
indexer::{OrderExecution, Token},
node::*,
};
use dydx_proto::dydxprotocol::{
clob::{
order::{self, ConditionType, Side, TimeInForce},
Order, OrderBatch, OrderId,
},
subaccounts::SubaccountId,
};
use rand::{thread_rng, Rng};
use anyhow::Error;
use serial_test::serial;
use std::str::FromStr;
use tokio::time::{sleep, Duration};

#[tokio::test]
#[serial]
Expand All @@ -44,7 +28,10 @@ async fn test_node_megavault_withdraw() -> Result<(), Error> {

let subaccount = account.subaccount(0)?;

let tx_res = node.megavault().withdraw(&mut account, subaccount, 0, Some(&1.into())).await;
let tx_res = node
.megavault()
.withdraw(&mut account, subaccount, 0, Some(&1.into()))
.await;

node.query_transaction_result(tx_res).await?;

Expand All @@ -55,7 +42,7 @@ async fn test_node_megavault_withdraw() -> Result<(), Error> {
async fn test_node_megavault_get_owner_shares() -> Result<(), Error> {
let env = TestEnv::testnet().await?;
let mut node = env.node;
let mut account = env.account;
let account = env.account;

node.megavault().get_owner_shares(account.address()).await?;

Expand All @@ -66,7 +53,6 @@ async fn test_node_megavault_get_owner_shares() -> Result<(), Error> {
async fn test_node_megavault_get_withdrawal_info() -> Result<(), Error> {
let env = TestEnv::testnet().await?;
let mut node = env.node;
let mut account = env.account;

node.megavault().get_withdrawal_info(&1.into()).await?;

Expand Down

0 comments on commit c9d7acb

Please sign in to comment.