Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create market permissionless #304

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")),
}
}
Loading