Repository navigation
Expand file tree
/
Copy pathplace_order_short_term.rs
More file actions
66 lines (56 loc) · 2.02 KB
/
Copy pathplace_order_short_term.rs
File metadata and controls
66 lines (56 loc) · 2.02 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
mod support;
use anyhow::{Error, Result};
use bigdecimal::BigDecimal;
use dydx::config::ClientConfig;
use dydx::indexer::IndexerClient;
use dydx::node::{NodeClient, OrderBuilder, OrderSide, Wallet};
use dydx_proto::dydxprotocol::clob::order::TimeInForce;
use std::str::FromStr;
use support::constants::TEST_MNEMONIC;
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 current_block_height = placer.client.latest_block_height().await?;
let size = BigDecimal::from_str("0.02")?;
let (_id, order) = OrderBuilder::new(market, subaccount)
.market(OrderSide::Buy, size)
.reduce_only(false)
.price(100) // market-order slippage protection price
.time_in_force(TimeInForce::Unspecified)
.until(current_block_height.ahead(10))
.build(123456)?;
let tx_hash = placer.client.place_order(&mut account, order).await?;
tracing::info!("Broadcast transaction hash: {:?}", tx_hash);
Ok(())
}