Skip to content

Commit

Permalink
feat: Create market permissionless (#304)
Browse files Browse the repository at this point in the history
Adds an option to create a market permissionless.

Closes #298.
  • Loading branch information
v0-e authored Dec 10, 2024
1 parent 1327f57 commit 976a5e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
28 changes: 26 additions & 2 deletions v4-client-rs/client/src/node/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<TxHash, NodeError> {
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).
Expand Down
24 changes: 23 additions & 1 deletion v4-client-rs/client/tests/test_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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")),
}
}

0 comments on commit 976a5e3

Please sign in to comment.