From 53ac1e90cf41322c02b2b4f43a4618b12951511c Mon Sep 17 00:00:00 2001 From: v0-e Date: Tue, 10 Dec 2024 09:46:48 +0000 Subject: [PATCH] feat: Create market permissionless --- v4-client-rs/client/src/node/client/mod.rs | 28 ++++++++++++++++++++-- v4-client-rs/client/tests/test_node.rs | 24 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/v4-client-rs/client/src/node/client/mod.rs b/v4-client-rs/client/src/node/client/mod.rs index 79426102..10410c23 100644 --- a/v4-client-rs/client/src/node/client/mod.rs +++ b/v4-client-rs/client/src/node/client/mod.rs @@ -7,7 +7,9 @@ use super::{ }; use megavault::MegaVault; -pub use crate::indexer::{Address, ClientId, Height, OrderFlags, Subaccount, Tokenized, Usdc}; +pub use crate::indexer::{ + Address, ClientId, Height, OrderFlags, Subaccount, Ticker, Tokenized, Usdc, +}; use anyhow::{anyhow as err, Error, Result}; use bigdecimal::{ num_bigint::{BigInt, Sign}, @@ -38,12 +40,13 @@ use dydx_proto::{ Order, OrderBatch, }, feetiers::query_client::QueryClient as FeeTiersClient, + listing::MsgCreateMarketPermissionless, perpetuals::query_client::QueryClient as PerpetualsClient, prices::query_client::QueryClient as PricesClient, rewards::query_client::QueryClient as RewardsClient, sending::{MsgCreateTransfer, MsgDepositToSubaccount, MsgWithdrawFromSubaccount, Transfer}, stats::query_client::QueryClient as StatsClient, - subaccounts::query_client::QueryClient as SubaccountsClient, + subaccounts::{query_client::QueryClient as SubaccountsClient, SubaccountId}, vault::query_client::QueryClient as VaultClient, }, ToAny, @@ -511,6 +514,27 @@ impl NodeClient { MegaVault::new(self) } + /// Create a market permissionless + pub async fn create_market_permissionless( + &mut self, + account: &mut Account, + ticker: &Ticker, + subaccount: &Subaccount, + ) -> Result { + let subaccount_id = SubaccountId { + owner: subaccount.address.to_string(), + number: subaccount.number.0, + }; + let msg = MsgCreateMarketPermissionless { + ticker: ticker.to_string(), + subaccount_id: Some(subaccount_id), + }; + + let tx_raw = self.create_transaction(account, msg).await?; + + self.broadcast_transaction(tx_raw).await + } + /// Simulate a transaction. /// /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/withdraw_other.rs). diff --git a/v4-client-rs/client/tests/test_node.rs b/v4-client-rs/client/tests/test_node.rs index b225a317..f52ef7d5 100644 --- a/v4-client-rs/client/tests/test_node.rs +++ b/v4-client-rs/client/tests/test_node.rs @@ -5,7 +5,7 @@ use anyhow::{anyhow as err, Error}; use bigdecimal::{num_traits::cast::ToPrimitive, BigDecimal, One}; use chrono::{TimeDelta, Utc}; use dydx::{ - indexer::{OrderExecution, Token}, + indexer::{OrderExecution, Ticker, Token}, node::*, }; use dydx_proto::dydxprotocol::{ @@ -353,3 +353,25 @@ async fn test_node_close_position() -> Result<(), Error> { Ok(()) } + +#[tokio::test] +#[serial] +async fn test_node_create_market_permissionless() -> Result<(), Error> { + let env = TestEnv::testnet().await?; + let mut node = env.node; + let mut account = env.account; + + let subaccount = account.subaccount(0)?; + // Avoid creating a new market and just try to create one that already exists + let ticker = Ticker::from("ETH-USD"); + + let tx_res = node + .create_market_permissionless(&mut account, &ticker, &subaccount) + .await; + + match node.query_transaction_result(tx_res).await { + Err(e) if e.to_string().contains("Market params pair already exists") => Ok(()), + Err(e) => Err(e), + Ok(_) => Err(err!("Market creation (ETH-USD) should fail")), + } +}