-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathbatch_cancel_orders.rs
More file actions
97 lines (84 loc) · 2.99 KB
/
Copy pathbatch_cancel_orders.rs
File metadata and controls
97 lines (84 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
mod support;
use anyhow::{Error, Result};
use bigdecimal::BigDecimal;
use dydx::config::ClientConfig;
use dydx::indexer::{ClientId, IndexerClient};
use dydx::node::{NodeClient, OrderBuilder, OrderSide, Wallet, SHORT_TERM_ORDER_MAXIMUM_LIFETIME};
use dydx_proto::dydxprotocol::clob::{order::TimeInForce, OrderBatch};
use rand::rng;
use std::str::FromStr;
use support::constants::TEST_MNEMONIC;
use tokio::time::{sleep, Duration};
const N_ORDERS: usize = 6;
const ETH_USD_TICKER: &str = "ETH-USD";
pub struct OrderPlacer {
client: NodeClient,
indexer: IndexerClient,
wallet: Wallet,
}
impl OrderPlacer {
pub async fn connect() -> Result<Self> {
// Initialize rustls crypto provider
support::crypto::init_crypto_provider();
let config = ClientConfig::from_file("client/tests/testnet.toml").await?;
let client = NodeClient::connect(config.node).await?;
let indexer = IndexerClient::new(config.indexer);
let wallet = Wallet::from_mnemonic(TEST_MNEMONIC)?;
Ok(Self {
client,
indexer,
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 placer = OrderPlacer::connect().await?;
let mut account = placer.wallet.account(0, &mut placer.client).await?;
let subaccount = account.subaccount(0)?;
let market = placer
.indexer
.markets()
.get_perpetual_market(Ð_USD_TICKER.into())
.await?;
let builder = OrderBuilder::new(market.clone(), subaccount.clone())
.market(OrderSide::Buy, BigDecimal::from_str("0.001")?)
.price(100)
.reduce_only(false)
.time_in_force(TimeInForce::Unspecified);
let mut client_ids = Vec::new();
// Push some orders
for _id in 0..N_ORDERS {
// Short term orders have a maximum validity of 20 blocks
let height = placer.client.latest_block_height().await?;
let order_builder = builder.clone().until(height.ahead(10));
let (order_id, order) = order_builder.build(ClientId::random_with_rng(&mut rng()))?;
let client_id = order_id.client_id;
client_ids.push(client_id);
let tx_hash = placer.client.place_order(&mut account, order).await?;
tracing::info!("Broadcast order ({client_id}) transaction hash: {tx_hash:?}");
sleep(Duration::from_secs(2)).await;
}
// Batch cancel
let batch = OrderBatch {
clob_pair_id: market.clob_pair_id.0,
client_ids,
};
let til_height = placer
.client
.latest_block_height()
.await?
.ahead(SHORT_TERM_ORDER_MAXIMUM_LIFETIME);
let tx_hash = placer
.client
.batch_cancel_orders(&mut account, subaccount, vec![batch], til_height)
.await?;
tracing::info!(
"Broadcast cancel orders batch transaction hash: {:?}",
tx_hash
);
Ok(())
}