-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathclose_position.rs
More file actions
124 lines (109 loc) · 3.33 KB
/
Copy pathclose_position.rs
File metadata and controls
124 lines (109 loc) · 3.33 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
mod support;
use anyhow::{Error, Result};
use bigdecimal::BigDecimal;
use dydx::config::ClientConfig;
use dydx::indexer::{
ClientId, IndexerClient, ListPositionsOpts,
PerpetualPositionResponseObject as PerpetualPosition, PerpetualPositionStatus, Subaccount,
Ticker,
};
use dydx::node::{NodeClient, Wallet};
use std::str::FromStr;
use support::constants::TEST_MNEMONIC;
use tokio::time::{sleep, Duration};
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,
})
}
}
async fn get_open_position(
indexer: &IndexerClient,
subaccount: &Subaccount,
ticker: &Ticker,
) -> Option<PerpetualPosition> {
indexer
.accounts()
.get_subaccount_perpetual_positions(
subaccount,
Some(ListPositionsOpts {
status: Some(PerpetualPositionStatus::Open),
..Default::default()
}),
)
.await
.ok()
.and_then(|positions| positions.into_iter().find(|pos| pos.market == *ticker))
}
#[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 ticker = Ticker(ETH_USD_TICKER.into());
let market = placer
.indexer
.markets()
.get_perpetual_market(Ð_USD_TICKER.into())
.await?;
println!(
"Current open position: {:?}",
get_open_position(&placer.indexer, &subaccount, &ticker).await
);
// Reduce position by an amount, if open, matching best current market prices
let reduce_by = BigDecimal::from_str("0.0001")?;
let tx_hash = placer
.client
.close_position(
&mut account,
subaccount.clone(),
market.clone(),
Some(reduce_by),
ClientId::random(),
)
.await?;
tracing::info!(
"Partial position close broadcast transaction hash: {:?}",
tx_hash
);
sleep(Duration::from_secs(3)).await;
// Fully close the position, if open, matching best current market prices
let tx_hash = placer
.client
.close_position(
&mut account,
subaccount.clone(),
market,
None,
ClientId::random(),
)
.await?;
tracing::info!(
"Fully position close broadcast transaction hash: {:?}",
tx_hash
);
sleep(Duration::from_secs(3)).await;
println!(
"Current open position: {:?}",
get_open_position(&placer.indexer, &subaccount, &ticker).await
);
Ok(())
}