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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Trader order stream task spawner and row conversion helpers.
use super::*;
/// Convert the phoenix-types `side` string ("Bid" / "Ask") to our internal
/// `TradingSide`. The SDK formats the enum via `{:?}` so variant names come
/// through verbatim.
pub(in crate::tui::runtime) fn trading_side_from_str(s: &str) -> TradingSide {
match s {
"Bid" | "Buy" | "Long" => TradingSide::Long,
_ => TradingSide::Short,
}
}
fn trigger_side_to_trading_side(side: StopLossTradeSide) -> TradingSide {
match side {
StopLossTradeSide::Bid => TradingSide::Long,
StopLossTradeSide::Ask => TradingSide::Short,
}
}
fn trigger_direction_to_phoenix(direction: StopLossDirection) -> Direction {
match direction {
StopLossDirection::GreaterThan => Direction::GreaterThan,
StopLossDirection::LessThan => Direction::LessThan,
}
}
fn conditional_order_type(kind: StopLossOrderKind) -> String {
match kind {
StopLossOrderKind::IOC => "Market".to_string(),
StopLossOrderKind::Limit => "Limit".to_string(),
}
}
fn push_conditional_trigger_row(
rows: &mut Vec<OrderInfo>,
symbol: &str,
subaccount_index: u8,
order_index: u8,
order_sequence_number: u64,
size_lots: u64,
trigger: &ConditionalOrderTrigger,
) {
if !trigger.is_active {
return;
}
rows.push(OrderInfo {
symbol: symbol.to_string(),
subaccount_index,
order_sequence_number,
side: trigger_side_to_trading_side(trigger.trade_side),
order_type: conditional_order_type(trigger.order_kind),
price_usd: 0.0,
price_ticks: trigger.trigger_price,
size_remaining: size_lots as f64,
initial_size: size_lots as f64,
reduce_only: true,
is_stop_loss: true,
conditional_order_index: Some(order_index),
conditional_trigger_direction: Some(trigger_direction_to_phoenix(
trigger.execution_direction,
)),
});
}
fn conditional_order_rows(
collection: &ConditionalOrderCollection,
asset_symbols: &HashMap<u32, String>,
subaccount_index: u8,
) -> Vec<OrderInfo> {
let mut rows = Vec::new();
for (order_index, order) in collection.active_orders() {
let Some(symbol) = asset_symbols.get(&order.asset_id) else {
continue;
};
let size_lots = order.fillable_size.max(order.max_size);
let base_sequence = 1_000_000_000 + u64::from(order_index) * 2;
push_conditional_trigger_row(
&mut rows,
symbol,
subaccount_index,
order_index,
base_sequence,
size_lots,
&order.greater_trigger_order,
);
push_conditional_trigger_row(
&mut rows,
symbol,
subaccount_index,
order_index,
base_sequence + 1,
size_lots,
&order.less_trigger_order,
);
}
rows
}
async fn fetch_conditional_order_rows(
rpc: &RpcClient,
address: &Pubkey,
asset_symbols: &HashMap<u32, String>,
subaccount_index: u8,
) -> Vec<OrderInfo> {
let response = match rpc
.get_account_with_commitment(address, rpc.commitment())
.await
{
Ok(response) => response,
Err(err) => {
warn!(error = %err, "failed to fetch conditional-orders account");
return Vec::new();
}
};
let Some(account) = response.value.filter(|account| !account.data.is_empty()) else {
return Vec::new();
};
match ConditionalOrderCollection::try_from_account_bytes(&account.data) {
Ok(collection) => conditional_order_rows(&collection, asset_symbols, subaccount_index),
Err(err) => {
warn!(error = %err, "failed to decode conditional-orders account");
Vec::new()
}
}
}
async fn fetch_conditional_order_rows_for_subaccounts(
rpc: &RpcClient,
authority: Pubkey,
subaccount_indexes: impl IntoIterator<Item = u8>,
asset_symbols: &HashMap<u32, String>,
) -> Vec<OrderInfo> {
let mut rows = Vec::new();
for subaccount_index in subaccount_indexes {
let key = TraderKey::new_with_idx(authority, 0, subaccount_index);
let address = get_conditional_orders_address(&key.pda());
rows.extend(
fetch_conditional_order_rows(rpc, &address, asset_symbols, subaccount_index).await,
);
}
rows
}
fn build_order_rows(
trader: &Trader,
stop_triggers: &HashMap<(u8, String, String), TraderStateStopLossTrigger>,
conditional_orders: &[OrderInfo],
) -> Vec<OrderInfo> {
let mut orders: Vec<OrderInfo> = trader
.subaccounts
.values()
.flat_map(|sub| {
sub.orders.values().map(move |o| OrderInfo {
symbol: o.symbol.clone(),
subaccount_index: sub.subaccount_index,
order_sequence_number: o.order_sequence_number,
side: trading_side_from_str(&o.side),
order_type: o.order_type.clone(),
price_usd: o.price_usd.to_string().parse::<f64>().unwrap_or(0.0),
// `price_ticks` is i64 in the SDK but always non-negative for live orders;
// clamp at 0 just in case to keep the cast safe.
price_ticks: o.price_ticks.max(0) as u64,
// UI size is filled in by the main loop using `configs`; raw lots are
// carried in the `f64` as a fallback so the modal still shows magnitude.
size_remaining: o.size_remaining_lots as f64,
initial_size: o.initial_size_lots as f64,
reduce_only: o.reduce_only,
is_stop_loss: o.is_stop_loss,
conditional_order_index: None,
conditional_trigger_direction: None,
})
})
.collect();
// Append synthetic rows for pending stop-loss triggers. These carry no size
// (size is determined at fire-time from the opposing position) and no USD
// price — the main loop converts `price_ticks` via the per-symbol config.
for ((subaccount_index, symbol, stop_id), sl) in stop_triggers {
let trigger_ticks: u64 = sl.trigger.trigger_price_ticks.parse().unwrap_or(0);
let osn: u64 = stop_id
.chars()
.filter(|c| c.is_ascii_digit())
.collect::<String>()
.parse()
.unwrap_or(0);
let side = match sl.trigger.side {
phoenix_rise::types::Side::Bid => TradingSide::Long,
phoenix_rise::types::Side::Ask => TradingSide::Short,
};
orders.push(OrderInfo {
symbol: symbol.clone(),
subaccount_index: *subaccount_index,
order_sequence_number: osn,
side,
order_type: sl.trigger.kind.clone(),
price_usd: 0.0,
price_ticks: trigger_ticks,
size_remaining: 0.0,
initial_size: 0.0,
reduce_only: true,
is_stop_loss: true,
conditional_order_index: None,
conditional_trigger_direction: None,
});
}
for conditional in conditional_orders {
let duplicate = orders.iter().any(|order| {
order.is_stop_loss
&& order.subaccount_index == conditional.subaccount_index
&& order.symbol == conditional.symbol
&& order.side == conditional.side
&& order.price_ticks == conditional.price_ticks
});
if !duplicate {
orders.push(conditional.clone());
}
}
orders
}
/// Spawn a persistent `PhoenixWSClient` subscription to the wallet's trader
/// state. Each update is applied to a local `Trader`, then `all_orders()` is
/// flattened into `Vec<OrderInfo>` and pushed to the main loop, which owns the
/// `configs` map needed for lot→UI conversion.
///
/// Uses raw base-lots in the payload; the main loop converts to UI units via
/// the live configs.
pub(in crate::tui::runtime) fn spawn_trader_orders_ws(
kp: Arc<Keypair>,
orders_tx: UnboundedSender<Vec<OrderInfo>>,
conditional_asset_symbols: HashMap<u32, String>,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let authority = match solana_pubkey::Pubkey::from_str(&kp.pubkey().to_string()) {
Ok(pk) => pk,
Err(e) => {
warn!(error = %e, "failed to convert wallet pubkey for trader WS");
return;
}
};
let mut backoff = WSS_RETRY_INIT;
loop {
let client = match PhoenixWSClient::new_from_env() {
Ok(c) => c,
Err(e) => {
warn!(error = %e, "PhoenixWSClient::new_from_env failed; retrying");
tokio::time::sleep(backoff).await;
backoff = (backoff * 2).min(WSS_RETRY_CAP);
continue;
}
};
let (mut rx, handle) = match client.subscribe_to_trader_state(&authority) {
Ok(pair) => pair,
Err(e) => {
warn!(error = %e, "subscribe_to_trader_state failed; retrying");
tokio::time::sleep(backoff).await;
backoff = (backoff * 2).min(WSS_RETRY_CAP);
continue;
}
};
backoff = WSS_RETRY_INIT;
let key = TraderKey::new(authority);
let conditional_rpc = RpcClient::new_with_commitment(
rpc_http_url_from_env(),
CommitmentConfig::processed(),
);
let mut trader = Trader::new(key);
// Stop-loss triggers live on `TraderStatePositionRow`, not in
// `subaccount.orders`, so the SDK's `Trader::all_orders()` never
// surfaces them. Track them ourselves from the raw ws payload so
// we can emit synthetic OrderInfo rows for the orders modal.
//
// Keyed by `(subaccount, symbol, stop_loss_id)`. On `Snapshot` we rebuild
// from scratch; on `Delta`, updated positions replace their
// subaccount/symbol triggers wholesale (matching
// `TraderStatePositionRow` semantics, where the triggers field is
// always the full current set), and closed positions drop their
// symbol for that subaccount entirely.
let mut stop_triggers: HashMap<(u8, String, String), TraderStateStopLossTrigger> =
HashMap::new();
let mut conditional_orders = fetch_conditional_order_rows_for_subaccounts(
&conditional_rpc,
authority,
[0],
&conditional_asset_symbols,
)
.await;
let mut conditional_interval = tokio::time::interval(Duration::from_millis(1500));
conditional_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
tokio::select! {
msg = rx.recv() => {
let Some(msg) = msg else {
break;
};
trader.apply_update(&msg);
match &msg.content {
TraderStatePayload::Snapshot(s) => {
stop_triggers.clear();
for sub in &s.subaccounts {
for pos in &sub.positions {
for sl in &pos.position.stop_loss_triggers {
stop_triggers.insert(
(
sub.subaccount_index,
pos.symbol.clone(),
sl.stop_loss_id.clone(),
),
sl.clone(),
);
}
}
}
}
TraderStatePayload::Delta(d) => {
for sub in &d.deltas {
for pos_delta in &sub.positions {
match pos_delta.change {
TraderStateRowChangeKind::Closed => {
stop_triggers
.retain(|(sub_idx, sym, _), _| {
*sub_idx != sub.subaccount_index
|| sym != &pos_delta.symbol
});
}
TraderStateRowChangeKind::Updated => {
stop_triggers
.retain(|(sub_idx, sym, _), _| {
*sub_idx != sub.subaccount_index
|| sym != &pos_delta.symbol
});
if let Some(row) = &pos_delta.position {
for sl in &row.stop_loss_triggers {
stop_triggers.insert(
(
sub.subaccount_index,
pos_delta.symbol.clone(),
sl.stop_loss_id.clone(),
),
sl.clone(),
);
}
}
}
}
}
}
}
}
if orders_tx.send(build_order_rows(&trader, &stop_triggers, &conditional_orders)).is_err() {
// Receiver dropped (poller shutting down).
drop(handle);
return;
}
}
_ = conditional_interval.tick() => {
let mut subaccount_indexes: Vec<u8> =
trader.subaccounts.keys().copied().collect();
if subaccount_indexes.is_empty() {
subaccount_indexes.push(0);
}
subaccount_indexes.sort_unstable();
conditional_orders = fetch_conditional_order_rows_for_subaccounts(
&conditional_rpc,
authority,
subaccount_indexes,
&conditional_asset_symbols,
)
.await;
if orders_tx.send(build_order_rows(&trader, &stop_triggers, &conditional_orders)).is_err() {
drop(handle);
return;
}
}
}
}
// Stream closed — reconnect. Drop handle first so the old subscription is
// released.
drop(handle);
tokio::time::sleep(backoff).await;
}
})
}