@@ -4,7 +4,7 @@ use drift_sdk::{
4
4
event_subscriber:: { try_parse_log, CommitmentConfig } ,
5
5
math:: liquidation:: calculate_liquidation_price_and_unrealized_pnl,
6
6
types:: {
7
- Context , MarketId , MarketType , ModifyOrderParams , RpcSendTransactionConfig , SdkError ,
7
+ self , MarketId , MarketType , ModifyOrderParams , RpcSendTransactionConfig , SdkError ,
8
8
SdkResult , VersionedMessage ,
9
9
} ,
10
10
AccountProvider , DriftClient , Pubkey , RpcAccountProvider , TransactionBuilder , Wallet ,
@@ -15,7 +15,7 @@ use rust_decimal::Decimal;
15
15
use solana_client:: { client_error:: ClientErrorKind , rpc_config:: RpcTransactionConfig } ;
16
16
use solana_sdk:: signature:: Signature ;
17
17
use solana_transaction_status:: { option_serializer:: OptionSerializer , UiTransactionEncoding } ;
18
- use std:: { borrow:: Cow , str:: FromStr , sync:: Arc , time :: Duration } ;
18
+ use std:: { borrow:: Cow , str:: FromStr , sync:: Arc } ;
19
19
use thiserror:: Error ;
20
20
21
21
use crate :: {
@@ -27,7 +27,7 @@ use crate::{
27
27
TxEventsResponse , TxResponse , PRICE_DECIMALS ,
28
28
} ,
29
29
websocket:: map_drift_event_for_account,
30
- LOG_TARGET ,
30
+ Context , LOG_TARGET ,
31
31
} ;
32
32
33
33
pub type GatewayResult < T > = Result < T , ControllerError > ;
@@ -84,9 +84,9 @@ impl AppState {
84
84
let ( state_commitment, tx_commitment) =
85
85
commitment. unwrap_or ( ( CommitmentConfig :: confirmed ( ) , CommitmentConfig :: confirmed ( ) ) ) ;
86
86
let context = if devnet {
87
- Context :: DevNet
87
+ types :: Context :: DevNet
88
88
} else {
89
- Context :: MainNet
89
+ types :: Context :: MainNet
90
90
} ;
91
91
92
92
let account_provider = RpcAccountProvider :: with_commitment ( endpoint, state_commitment) ;
@@ -132,35 +132,36 @@ impl AppState {
132
132
/// 4) catch all. cancel all orders
133
133
pub async fn cancel_orders (
134
134
& self ,
135
+ ctx : Context ,
135
136
req : CancelOrdersRequest ,
136
- sub_account_id : Option < u16 > ,
137
- cu_limit : Option < u32 > ,
138
137
) -> GatewayResult < TxResponse > {
139
- let sub_account = self . resolve_sub_account ( sub_account_id) ;
138
+ let sub_account = self . resolve_sub_account ( ctx . sub_account_id ) ;
140
139
let ( account_data, pf) = tokio:: join!(
141
140
self . client. get_user_account( & sub_account) ,
142
141
get_priority_fee( & self . client)
143
142
) ;
143
+ let priority_fee = ctx. cu_price . unwrap_or ( pf) ;
144
+ debug ! ( target: LOG_TARGET , "priority_fee: {priority_fee:?}" ) ;
144
145
let builder = TransactionBuilder :: new (
145
146
self . client . program_data ( ) ,
146
147
sub_account,
147
148
Cow :: Owned ( account_data?) ,
148
149
self . delegated ,
149
150
)
150
- . with_priority_fee ( pf , cu_limit) ;
151
+ . with_priority_fee ( priority_fee , ctx . cu_limit ) ;
151
152
let tx = build_cancel_ix ( builder, req) ?. build ( ) ;
152
153
self . send_tx ( tx, "cancel_orders" ) . await
153
154
}
154
155
155
156
/// Return orders by position if given, otherwise return all positions
156
157
pub async fn get_positions (
157
158
& self ,
159
+ ctx : Context ,
158
160
req : Option < GetPositionsRequest > ,
159
- sub_account_id : Option < u16 > ,
160
161
) -> GatewayResult < GetPositionsResponse > {
161
162
let ( all_spot, all_perp) = self
162
163
. client
163
- . all_positions ( & self . resolve_sub_account ( sub_account_id) )
164
+ . all_positions ( & self . resolve_sub_account ( ctx . sub_account_id ) )
164
165
. await ?;
165
166
166
167
// calculating spot token balance requires knowing the 'spot market account' data
@@ -204,10 +205,10 @@ impl AppState {
204
205
205
206
pub async fn get_position_extended (
206
207
& self ,
207
- sub_account_id : Option < u16 > ,
208
+ ctx : Context ,
208
209
market : Market ,
209
210
) -> GatewayResult < PerpPosition > {
210
- let sub_account = self . resolve_sub_account ( sub_account_id) ;
211
+ let sub_account = self . resolve_sub_account ( ctx . sub_account_id ) ;
211
212
let ( perp_position, user, oracle) = tokio:: join!(
212
213
self . client. perp_position( & sub_account, market. market_index) ,
213
214
self . client. get_user_account( & sub_account) ,
@@ -247,10 +248,10 @@ impl AppState {
247
248
/// Return orders by market if given, otherwise return all orders
248
249
pub async fn get_orders (
249
250
& self ,
251
+ ctx : Context ,
250
252
req : Option < GetOrdersRequest > ,
251
- sub_account_id : Option < u16 > ,
252
253
) -> GatewayResult < GetOrdersResponse > {
253
- let sub_account = self . resolve_sub_account ( sub_account_id) ;
254
+ let sub_account = self . resolve_sub_account ( ctx . sub_account_id ) ;
254
255
let orders = self . client . all_orders ( & sub_account) . await ?;
255
256
Ok ( GetOrdersResponse {
256
257
orders : orders
@@ -299,9 +300,8 @@ impl AppState {
299
300
300
301
pub async fn cancel_and_place_orders (
301
302
& self ,
303
+ ctx : Context ,
302
304
req : CancelAndPlaceRequest ,
303
- sub_account_id : Option < u16 > ,
304
- cu_limit : Option < u32 > ,
305
305
) -> GatewayResult < TxResponse > {
306
306
let orders = req
307
307
. place
@@ -313,7 +313,7 @@ impl AppState {
313
313
} )
314
314
. collect ( ) ;
315
315
316
- let sub_account = self . resolve_sub_account ( sub_account_id) ;
316
+ let sub_account = self . resolve_sub_account ( ctx . sub_account_id ) ;
317
317
let ( account_data, pf) = tokio:: join!(
318
318
self . client. get_user_account( & sub_account) ,
319
319
get_priority_fee( & self . client)
@@ -325,7 +325,7 @@ impl AppState {
325
325
Cow :: Owned ( account_data?) ,
326
326
self . delegated ,
327
327
)
328
- . with_priority_fee ( pf , cu_limit) ;
328
+ . with_priority_fee ( ctx . cu_price . unwrap_or ( pf ) , ctx . cu_limit ) ;
329
329
330
330
let builder = build_cancel_ix ( builder, req. cancel ) ?;
331
331
let tx = build_modify_ix ( builder, req. modify , self . client . program_data ( ) ) ?
@@ -337,16 +337,18 @@ impl AppState {
337
337
338
338
pub async fn place_orders (
339
339
& self ,
340
+ ctx : Context ,
340
341
req : PlaceOrdersRequest ,
341
- sub_account_id : Option < u16 > ,
342
- cu_limit : Option < u32 > ,
343
342
) -> GatewayResult < TxResponse > {
344
- let sub_account = self . resolve_sub_account ( sub_account_id) ;
343
+ let sub_account = self . resolve_sub_account ( ctx . sub_account_id ) ;
345
344
let ( account_data, pf) = tokio:: join!(
346
345
self . client. get_user_account( & sub_account) ,
347
346
get_priority_fee( & self . client)
348
347
) ;
349
348
349
+ let priority_fee = ctx. cu_price . unwrap_or ( pf) ;
350
+ debug ! ( target: LOG_TARGET , "priority fee: {priority_fee:?}" ) ;
351
+
350
352
let orders = req
351
353
. orders
352
354
. into_iter ( )
@@ -361,7 +363,7 @@ impl AppState {
361
363
Cow :: Owned ( account_data?) ,
362
364
self . delegated ,
363
365
)
364
- . with_priority_fee ( pf , cu_limit)
366
+ . with_priority_fee ( priority_fee , ctx . cu_limit )
365
367
. place_orders ( orders)
366
368
. build ( ) ;
367
369
@@ -370,11 +372,10 @@ impl AppState {
370
372
371
373
pub async fn modify_orders (
372
374
& self ,
375
+ ctx : Context ,
373
376
req : ModifyOrdersRequest ,
374
- sub_account_id : Option < u16 > ,
375
- cu_limit : Option < u32 > ,
376
377
) -> GatewayResult < TxResponse > {
377
- let sub_account = self . resolve_sub_account ( sub_account_id) ;
378
+ let sub_account = self . resolve_sub_account ( ctx . sub_account_id ) ;
378
379
let ( account_data, pf) = tokio:: join!(
379
380
self . client. get_user_account( & sub_account) ,
380
381
get_priority_fee( & self . client)
@@ -386,7 +387,7 @@ impl AppState {
386
387
Cow :: Owned ( account_data?) ,
387
388
self . delegated ,
388
389
)
389
- . with_priority_fee ( pf , cu_limit) ;
390
+ . with_priority_fee ( ctx . cu_price . unwrap_or ( pf ) , ctx . cu_limit ) ;
390
391
let tx = build_modify_ix ( builder, req, self . client . program_data ( ) ) ?. build ( ) ;
391
392
self . send_tx ( tx, "modify_orders" ) . await
392
393
}
@@ -402,8 +403,8 @@ impl AppState {
402
403
403
404
pub async fn get_tx_events_for_subaccount_id (
404
405
& self ,
406
+ ctx : Context ,
405
407
tx_sig : & str ,
406
- sub_account_id : Option < u16 > ,
407
408
) -> GatewayResult < TxEventsResponse > {
408
409
let signature = Signature :: from_str ( tx_sig) . map_err ( |err| {
409
410
warn ! ( target: LOG_TARGET , "failed to parse transaction signature: {err:?}" ) ;
@@ -432,7 +433,7 @@ impl AppState {
432
433
if let Some ( meta) = tx. transaction . meta {
433
434
match meta. log_messages {
434
435
OptionSerializer :: Some ( logs) => {
435
- let sub_account = self . resolve_sub_account ( sub_account_id) ;
436
+ let sub_account = self . resolve_sub_account ( ctx . sub_account_id ) ;
436
437
for ( tx_idx, log) in logs. iter ( ) . enumerate ( ) {
437
438
if let Some ( evt) = try_parse_log ( log. as_str ( ) , tx_sig, tx_idx) {
438
439
let ( _, gw_event) = map_drift_event_for_account (
@@ -508,7 +509,6 @@ impl AppState {
508
509
let client = Arc :: clone ( & self . client ) ;
509
510
let commitment = self . tx_commitment . commitment ;
510
511
tokio:: spawn ( async move {
511
- tokio:: time:: sleep ( Duration :: from_millis ( 200 ) ) . await ;
512
512
if let Err ( err) = client
513
513
. inner ( )
514
514
. send_transaction_with_config (
@@ -641,7 +641,7 @@ async fn get_priority_fee<T: AccountProvider>(client: &DriftClient<T>) -> u64 {
641
641
. await
642
642
{
643
643
recent_fees. sort_unstable ( ) ;
644
- let idx = ( recent_fees. len ( ) * 3 ) / 4 ; // 75 -th percentile
644
+ let idx = ( recent_fees. len ( ) * 90 ) / 100 ; // 90 -th percentile
645
645
priority_fee = recent_fees[ idx] ;
646
646
} else {
647
647
warn ! ( target: "controller" , "failed to fetch live priority fee" ) ;
@@ -660,7 +660,7 @@ mod tests {
660
660
// flakey needs a mainnet RPC getProgramAccounts
661
661
let account_provider = RpcAccountProvider :: new ( "https://api.devnet.solana.com" ) ;
662
662
let client = DriftClient :: new (
663
- Context :: DevNet ,
663
+ types :: Context :: DevNet ,
664
664
account_provider,
665
665
Wallet :: read_only ( Pubkey :: new_unique ( ) ) ,
666
666
)
0 commit comments