7
7
database:: {
8
8
byte_array:: ByteArray ,
9
9
order_events:: { insert_order_event, OrderEvent , OrderEventLabel } ,
10
- orders:: { FullOrder , OrderKind as DbOrderKind } ,
10
+ orders:: { self , FullOrder , OrderKind as DbOrderKind } ,
11
11
} ,
12
12
ethcontract:: H256 ,
13
13
futures:: { stream:: TryStreamExt , FutureExt , StreamExt } ,
24
24
OrderUid ,
25
25
} ,
26
26
signature:: Signature ,
27
- time:: now_in_epoch_seconds,
28
27
} ,
29
28
num:: Zero ,
30
29
number:: conversions:: { big_decimal_to_big_uint, big_decimal_to_u256, u256_to_big_decimal} ,
49
48
order_validation:: { is_order_outside_market_price, Amounts , LimitOrderCounting } ,
50
49
} ,
51
50
sqlx:: { types:: BigDecimal , Connection , PgConnection } ,
52
- std:: convert:: TryInto ,
51
+ std:: { convert:: TryInto , ops :: Deref } ,
53
52
} ;
54
53
55
54
#[ cfg_attr( test, mockall:: automock) ]
@@ -76,13 +75,44 @@ pub trait OrderStoring: Send + Sync {
76
75
limit : Option < u64 > ,
77
76
) -> Result < Vec < Order > > ;
78
77
async fn latest_order_event ( & self , order_uid : & OrderUid ) -> Result < Option < OrderEvent > > ;
78
+ async fn single_order_with_quote ( & self , uid : & OrderUid ) -> Result < Option < OrderWithQuote > > ;
79
79
}
80
80
81
81
pub struct SolvableOrders {
82
82
pub orders : Vec < Order > ,
83
83
pub latest_settlement_block : u64 ,
84
84
}
85
85
86
+ pub struct OrderWithQuote {
87
+ pub order : Order ,
88
+ pub quote : Option < orders:: Quote > ,
89
+ }
90
+
91
+ impl OrderWithQuote {
92
+ pub fn new ( order : Order , quote : Option < Quote > ) -> Self {
93
+ Self {
94
+ quote : quote. map ( |quote| orders:: Quote {
95
+ order_uid : ByteArray ( order. metadata . uid . 0 ) ,
96
+ gas_amount : quote. data . fee_parameters . gas_amount ,
97
+ gas_price : quote. data . fee_parameters . gas_price ,
98
+ sell_token_price : quote. data . fee_parameters . sell_token_price ,
99
+ sell_amount : u256_to_big_decimal ( & quote. sell_amount ) ,
100
+ buy_amount : u256_to_big_decimal ( & quote. buy_amount ) ,
101
+ solver : ByteArray ( quote. data . solver . 0 ) ,
102
+ } ) ,
103
+ order,
104
+ }
105
+ }
106
+ }
107
+
108
+ impl Deref for OrderWithQuote {
109
+ type Target = Order ;
110
+
111
+ fn deref ( & self ) -> & Self :: Target {
112
+ & self . order
113
+ }
114
+ }
115
+
86
116
#[ derive( Debug ) ]
87
117
pub enum InsertionError {
88
118
DuplicatedRecord ,
@@ -316,6 +346,51 @@ impl OrderStoring for Postgres {
316
346
order. map ( full_order_into_model_order) . transpose ( )
317
347
}
318
348
349
+ async fn single_order_with_quote ( & self , uid : & OrderUid ) -> Result < Option < OrderWithQuote > > {
350
+ let _timer = super :: Metrics :: get ( )
351
+ . database_queries
352
+ . with_label_values ( & [ "single_order_with_quote" ] )
353
+ . start_timer ( ) ;
354
+
355
+ let mut ex = self . pool . acquire ( ) . await ?;
356
+ let order = orders:: single_full_order_with_quote ( & mut ex, & ByteArray ( uid. 0 ) ) . await ?;
357
+ order
358
+ . map ( |order_with_quote| {
359
+ let quote = order_with_quote
360
+ . quote_buy_amount
361
+ . zip ( order_with_quote. quote_sell_amount )
362
+ . zip ( order_with_quote. quote_gas_amount )
363
+ . zip ( order_with_quote. quote_gas_price )
364
+ . zip ( order_with_quote. quote_sell_token_price )
365
+ . zip ( order_with_quote. solver )
366
+ . map (
367
+ |(
368
+ (
369
+ ( ( ( buy_amount, sell_amount) , gas_amount) , gas_price) ,
370
+ sell_token_price,
371
+ ) ,
372
+ solver,
373
+ ) | {
374
+ orders:: Quote {
375
+ order_uid : order_with_quote. full_order . uid ,
376
+ gas_amount,
377
+ gas_price,
378
+ sell_token_price,
379
+ sell_amount,
380
+ buy_amount,
381
+ solver,
382
+ }
383
+ } ,
384
+ ) ;
385
+
386
+ Ok ( OrderWithQuote {
387
+ order : full_order_into_model_order ( order_with_quote. full_order ) ?,
388
+ quote,
389
+ } )
390
+ } )
391
+ . transpose ( )
392
+ }
393
+
319
394
async fn orders_for_tx ( & self , tx_hash : & H256 ) -> Result < Vec < Order > > {
320
395
let _timer = super :: Metrics :: get ( )
321
396
. database_queries
@@ -380,39 +455,37 @@ impl LimitOrderCounting for Postgres {
380
455
. start_timer ( ) ;
381
456
382
457
let mut ex = self . pool . acquire ( ) . await ?;
383
- Ok ( database:: orders:: user_orders_with_quote (
384
- & mut ex,
385
- now_in_epoch_seconds ( ) . into ( ) ,
386
- & ByteArray ( owner. 0 ) ,
458
+ Ok (
459
+ database:: orders:: user_orders_with_quote ( & mut ex, & ByteArray ( owner. 0 ) )
460
+ . await ?
461
+ . into_iter ( )
462
+ . filter ( |order_with_quote| {
463
+ is_order_outside_market_price (
464
+ & Amounts {
465
+ sell : big_decimal_to_u256 ( & order_with_quote. order_sell_amount ) . unwrap ( ) ,
466
+ buy : big_decimal_to_u256 ( & order_with_quote. order_buy_amount ) . unwrap ( ) ,
467
+ fee : 0 . into ( ) ,
468
+ } ,
469
+ & Amounts {
470
+ sell : big_decimal_to_u256 ( & order_with_quote. quote_sell_amount ) . unwrap ( ) ,
471
+ buy : big_decimal_to_u256 ( & order_with_quote. quote_buy_amount ) . unwrap ( ) ,
472
+ fee : FeeParameters {
473
+ gas_amount : order_with_quote. quote_gas_amount ,
474
+ gas_price : order_with_quote. quote_gas_price ,
475
+ sell_token_price : order_with_quote. quote_sell_token_price ,
476
+ }
477
+ . fee ( ) ,
478
+ } ,
479
+ match order_with_quote. order_kind {
480
+ DbOrderKind :: Buy => model:: order:: OrderKind :: Buy ,
481
+ DbOrderKind :: Sell => model:: order:: OrderKind :: Sell ,
482
+ } ,
483
+ )
484
+ } )
485
+ . count ( )
486
+ . try_into ( )
487
+ . unwrap ( ) ,
387
488
)
388
- . await ?
389
- . into_iter ( )
390
- . filter ( |order_with_quote| {
391
- is_order_outside_market_price (
392
- & Amounts {
393
- sell : big_decimal_to_u256 ( & order_with_quote. order_sell_amount ) . unwrap ( ) ,
394
- buy : big_decimal_to_u256 ( & order_with_quote. order_buy_amount ) . unwrap ( ) ,
395
- fee : 0 . into ( ) ,
396
- } ,
397
- & Amounts {
398
- sell : big_decimal_to_u256 ( & order_with_quote. quote_sell_amount ) . unwrap ( ) ,
399
- buy : big_decimal_to_u256 ( & order_with_quote. quote_buy_amount ) . unwrap ( ) ,
400
- fee : FeeParameters {
401
- gas_amount : order_with_quote. quote_gas_amount ,
402
- gas_price : order_with_quote. quote_gas_price ,
403
- sell_token_price : order_with_quote. quote_sell_token_price ,
404
- }
405
- . fee ( ) ,
406
- } ,
407
- match order_with_quote. order_kind {
408
- DbOrderKind :: Buy => model:: order:: OrderKind :: Buy ,
409
- DbOrderKind :: Sell => model:: order:: OrderKind :: Sell ,
410
- } ,
411
- )
412
- } )
413
- . count ( )
414
- . try_into ( )
415
- . unwrap ( ) )
416
489
}
417
490
}
418
491
0 commit comments