-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathfee.rs
1014 lines (926 loc) · 35.2 KB
/
fee.rs
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use anyhow::Result;
use chrono::{DateTime, Duration, Utc, MAX_DATETIME};
use futures::future::TryFutureExt;
use gas_estimation::GasPriceEstimating;
use model::{app_id::AppId, order::OrderKind};
use primitive_types::{H160, U256};
use shared::{
bad_token::BadTokenDetecting,
price_estimation::{
self, ensure_token_supported, native::native_single_estimate, PriceEstimating,
PriceEstimationError,
},
price_estimation::{native::NativePriceEstimating, single_estimate},
};
use std::{
collections::{HashMap, HashSet},
sync::{Arc, Mutex},
};
use crate::cow_subsidy::CowSubsidy;
pub type Measurement = (U256, DateTime<Utc>);
/// Fee subsidy configuration.
///
/// Given an estimated fee for a trade, the mimimum fee required for an order is
/// computed using the following formula:
/// ```text
/// (estimated_fee_in_eth - fee_discount) * fee_factor * (partner_additional_fee_factor || 1.0)
/// ```
pub struct FeeSubsidyConfiguration {
/// A flat discount nominated in the native token to discount from fees.
///
/// Flat fee discounts are applied **before** any multiplicative discounts.
pub fee_discount: f64,
/// Minimum fee amount after applying the flat subsidy. This prevents flat
/// fee discounts putting the fee amount below 0.
///
/// Flat fee discounts are applied **before** any multiplicative discounts.
pub min_discounted_fee: f64,
/// A factor to multiply the estimated trading fee with in order to compute
/// subsidized minimum fee.
///
/// Fee factors are applied **after** flat fee discounts.
pub fee_factor: f64,
/// Additional factors per order app ID for computing the subsidized minimum
/// fee.
///
/// Fee factors are applied **after** flat fee discounts.
pub partner_additional_fee_factors: HashMap<AppId, f64>,
}
impl Default for FeeSubsidyConfiguration {
fn default() -> Self {
Self {
fee_discount: 0.,
fee_factor: 1.,
min_discounted_fee: 0.,
partner_additional_fee_factors: HashMap::new(),
}
}
}
pub struct MinFeeCalculator {
price_estimator: Arc<dyn PriceEstimating>,
gas_estimator: Arc<dyn GasPriceEstimating>,
measurements: Arc<dyn MinFeeStoring>,
now: Box<dyn Fn() -> DateTime<Utc> + Send + Sync>,
bad_token_detector: Arc<dyn BadTokenDetecting>,
fee_subsidy: FeeSubsidyConfiguration,
native_price_estimator: Arc<dyn NativePriceEstimating>,
cow_subsidy: Arc<dyn CowSubsidy>,
liquidity_order_owners: HashSet<H160>,
}
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub struct FeeData {
pub sell_token: H160,
pub buy_token: H160,
// For sell orders this is the sell amount before fees.
pub amount: U256,
pub kind: OrderKind,
}
/// Everything required to compute the fee amount in sell token
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FeeParameters {
pub gas_amount: f64,
pub gas_price: f64,
pub sell_token_price: f64,
}
impl Default for FeeParameters {
fn default() -> Self {
Self {
gas_amount: 0.,
gas_price: 0.,
// We can't use `derive(Default)` because then this field would have
// a value of `0.` and it is used in division. The actual value we
// use here doesn't really matter as long as its non-zero (since the
// resulting amount in native token or sell token will be 0
// regardless), but the multiplicative identity seemed like a
// natural default value to use.
sell_token_price: 1.,
}
}
}
// We want the conversion from f64 to U256 to use ceil because:
// 1. For final amounts that end up close to 0 atoms we always take a fee so we are not attackable
// through low decimal tokens.
// 2. When validating fees this consistently picks the same amount.
impl FeeParameters {
pub fn amount_in_sell_token(&self) -> U256 {
U256::from_f64_lossy((self.gas_amount * self.gas_price / self.sell_token_price).ceil())
}
fn apply_fee_factor(
&self,
config: &FeeSubsidyConfiguration,
app_data: AppId,
cow_factor: f64,
) -> U256 {
let fee_in_eth = self.gas_amount * self.gas_price;
let mut discounted_fee_in_eth = fee_in_eth - config.fee_discount;
if discounted_fee_in_eth < config.min_discounted_fee {
tracing::warn!(
"fee after applying fee discount below minimum: {}, capping at {}",
discounted_fee_in_eth,
config.min_discounted_fee,
);
discounted_fee_in_eth = config.min_discounted_fee;
}
let factor = config
.partner_additional_fee_factors
.get(&app_data)
.copied()
.unwrap_or(1.0)
* config.fee_factor
* cow_factor;
U256::from_f64_lossy((discounted_fee_in_eth * factor / self.sell_token_price).ceil())
}
}
// Convenience to allow using u32 in tests instead of the struct
#[cfg(test)]
impl From<u32> for FeeParameters {
fn from(v: u32) -> Self {
FeeParameters {
gas_amount: v as f64,
gas_price: 1.0,
sell_token_price: 1.0,
}
}
}
#[derive(Debug)]
pub enum GetUnsubsidizedMinFeeError {
InsufficientFee,
PriceEstimationError(PriceEstimationError),
Other(anyhow::Error),
}
impl From<anyhow::Error> for GetUnsubsidizedMinFeeError {
fn from(err: anyhow::Error) -> Self {
Self::Other(err)
}
}
// The user address is mandatory. If some code does not know the user it can pass the 0 address
// which is guaranteed to not have any balance for the cow token.
#[cfg_attr(test, mockall::automock)]
#[async_trait::async_trait]
pub trait MinFeeCalculating: Send + Sync {
/// Returns the minimum amount of fee required to accept an order selling
/// the specified order and an expiry date for the estimate. The returned
/// amount applies configured "fee factors" for subsidizing user trades.
///
/// Returns an error if there is some estimation error and `Ok(None)` if no
/// information about the given token exists
async fn compute_subsidized_min_fee(
&self,
fee_data: FeeData,
app_data: AppId,
user: H160,
) -> Result<Measurement, PriceEstimationError>;
/// Validates that the given subsidized fee is enough to process an order for the given token.
/// Returns current fee estimate (i.e., unsubsidized fee) if the given subsidized fee passes
/// a check. Returns `Err` if the check failed.
async fn get_unsubsidized_min_fee(
&self,
fee_data: FeeData,
app_data: AppId,
subsidized_fee: U256,
user: H160,
) -> Result<FeeParameters, GetUnsubsidizedMinFeeError>;
}
#[cfg_attr(test, mockall::automock)]
#[async_trait::async_trait]
pub trait MinFeeStoring: Send + Sync {
/// Stores the given measurement. Returns an error if this fails
async fn save_fee_measurement(
&self,
fee_data: FeeData,
expiry: DateTime<Utc>,
estimate: FeeParameters,
) -> Result<()>;
/// Returns lowest previously stored measurements that hasn't expired. FeeData has to match
/// exactly.
async fn find_measurement_exact(
&self,
fee_data: FeeData,
min_expiry: DateTime<Utc>,
) -> Result<Option<FeeParameters>>;
/// Returns lowest previously stored measurements that hasn't expired. FeeData has to match
/// exactly except for the amount which is allowed to be larger than the amount in fee data.
async fn find_measurement_including_larger_amount(
&self,
fee_data: FeeData,
min_expiry: DateTime<Utc>,
) -> Result<Option<FeeParameters>>;
}
// We use a longer validity internally for persistence to avoid writing a value to storage on every request
// This way we can serve a previous estimate if the same token is queried again shortly after
const STANDARD_VALIDITY_FOR_FEE_IN_SEC: i64 = 60;
const PERSISTED_VALIDITY_FOR_FEE_IN_SEC: i64 = 120;
impl MinFeeCalculator {
#[allow(clippy::too_many_arguments)]
pub fn new(
price_estimator: Arc<dyn PriceEstimating>,
gas_estimator: Arc<dyn GasPriceEstimating>,
measurements: Arc<dyn MinFeeStoring>,
bad_token_detector: Arc<dyn BadTokenDetecting>,
fee_subsidy: FeeSubsidyConfiguration,
native_price_estimator: Arc<dyn NativePriceEstimating>,
cow_subsidy: Arc<dyn CowSubsidy>,
liquidity_order_owners: HashSet<H160>,
) -> Self {
Self {
price_estimator,
gas_estimator,
measurements,
now: Box::new(Utc::now),
bad_token_detector,
fee_subsidy,
native_price_estimator,
cow_subsidy,
liquidity_order_owners,
}
}
/// Computes unsubsidized min fee.
async fn compute_unsubsidized_min_fee(
&self,
fee_data: FeeData,
) -> Result<FeeParameters, PriceEstimationError> {
let buy_token_query = price_estimation::Query {
sell_token: fee_data.sell_token,
buy_token: fee_data.buy_token,
in_amount: fee_data.amount,
kind: fee_data.kind,
};
let (gas_estimate, buy_token_estimate, sell_token_price) = futures::try_join!(
self.gas_estimator
.estimate()
.map_err(PriceEstimationError::from),
single_estimate(self.price_estimator.as_ref(), &buy_token_query),
native_single_estimate(self.native_price_estimator.as_ref(), &fee_data.sell_token),
)?;
let gas_price = gas_estimate.effective_gas_price();
let gas_amount = buy_token_estimate.gas as f64;
let fee_parameters = FeeParameters {
gas_amount,
gas_price,
sell_token_price,
};
let fee_in_eth = gas_price * gas_amount;
let fee_in_sell_token = fee_parameters.amount_in_sell_token();
tracing::debug!(
?fee_data, %gas_price, %gas_amount, %sell_token_price,
%fee_in_eth, %fee_in_sell_token,
"unsubsidized fee amount"
);
Ok(fee_parameters)
}
}
#[async_trait::async_trait]
impl MinFeeCalculating for MinFeeCalculator {
async fn compute_subsidized_min_fee(
&self,
fee_data: FeeData,
app_data: AppId,
user: H160,
) -> Result<Measurement, PriceEstimationError> {
if fee_data.buy_token == fee_data.sell_token {
return Ok((U256::zero(), MAX_DATETIME));
}
if self.liquidity_order_owners.contains(&user) {
return Ok((U256::zero(), MAX_DATETIME));
}
ensure_token_supported(fee_data.sell_token, self.bad_token_detector.as_ref()).await?;
ensure_token_supported(fee_data.buy_token, self.bad_token_detector.as_ref()).await?;
let now = (self.now)();
let official_valid_until = now + Duration::seconds(STANDARD_VALIDITY_FOR_FEE_IN_SEC);
let internal_valid_until = now + Duration::seconds(PERSISTED_VALIDITY_FOR_FEE_IN_SEC);
tracing::debug!(?fee_data, ?app_data, ?user, "computing subsidized fee",);
let cow_factor = async {
self.cow_subsidy
.cow_subsidy_factor(user)
.await
.map_err(PriceEstimationError::Other)
};
let unsubsidized_min_fee = async {
if let Some(past_fee) = self
.measurements
.find_measurement_exact(fee_data, official_valid_until)
.await?
{
tracing::debug!("using existing fee measurement {:?}", past_fee);
Ok(past_fee)
} else {
let current_fee = self.compute_unsubsidized_min_fee(fee_data).await?;
if let Err(err) = self
.measurements
.save_fee_measurement(fee_data, internal_valid_until, current_fee)
.await
{
tracing::warn!(?err, "error saving fee measurement");
}
tracing::debug!("using new fee measurement {:?}", current_fee);
Ok(current_fee)
}
};
let (cow_factor, unsubsidized_min_fee) =
futures::future::try_join(cow_factor, unsubsidized_min_fee).await?;
let subsidized_min_fee =
unsubsidized_min_fee.apply_fee_factor(&self.fee_subsidy, app_data, cow_factor);
tracing::debug!(
"computed subsidized fee of {:?}",
(subsidized_min_fee, fee_data.sell_token),
);
Ok((subsidized_min_fee, official_valid_until))
}
async fn get_unsubsidized_min_fee(
&self,
fee_data: FeeData,
app_data: AppId,
subsidized_fee: U256,
user: H160,
) -> Result<FeeParameters, GetUnsubsidizedMinFeeError> {
if self.liquidity_order_owners.contains(&user) {
return Ok(FeeParameters::default());
}
let cow_factor = self.cow_subsidy.cow_subsidy_factor(user);
let past_fee = self
.measurements
.find_measurement_including_larger_amount(fee_data, (self.now)());
let (cow_factor, past_fee) = futures::future::try_join(cow_factor, past_fee).await?;
// When validating we allow fees taken for larger amounts because as the amount increases
// the fee increases too because it is worth to trade off more gas use for a slightly better
// price. Thus it is acceptable if the new order has an amount <= an existing fee
// measurement.
// We do not use "exact" here because for sell orders the final sell_amount might have been
// calculated as original_sell_amount + fee_response or sell_amount
// (sell amount before vs after fee).
// Once we have removed the `fee` route and moved all fee requests to the `quote` route we
// might no longer need this workaround as we will know exactly how the amounts in the order
// have been picked.
if let Some(past_fee) = past_fee {
tracing::debug!("found past fee {:?}", past_fee);
if subsidized_fee >= past_fee.apply_fee_factor(&self.fee_subsidy, app_data, cow_factor)
{
tracing::debug!("given fee matches past fee");
return Ok(past_fee);
} else {
tracing::debug!("given fee does not match past fee");
}
}
let current_fee = self
.compute_unsubsidized_min_fee(fee_data)
.await
.map_err(GetUnsubsidizedMinFeeError::PriceEstimationError)?;
tracing::debug!("estimated new fee {:?}", current_fee);
if subsidized_fee >= current_fee.apply_fee_factor(&self.fee_subsidy, app_data, cow_factor) {
tracing::debug!("given fee matches new fee");
Ok(current_fee)
} else {
tracing::debug!("given fee does not match new fee");
Err(GetUnsubsidizedMinFeeError::InsufficientFee)
}
}
}
struct FeeMeasurement {
fee_data: FeeData,
expiry: DateTime<Utc>,
estimate: FeeParameters,
}
#[derive(Default)]
struct InMemoryFeeStore(Mutex<Vec<FeeMeasurement>>);
#[async_trait::async_trait]
impl MinFeeStoring for InMemoryFeeStore {
async fn save_fee_measurement(
&self,
fee_data: FeeData,
expiry: DateTime<Utc>,
estimate: FeeParameters,
) -> Result<()> {
self.0
.lock()
.expect("Thread holding Mutex panicked")
.push(FeeMeasurement {
fee_data,
expiry,
estimate,
});
Ok(())
}
async fn find_measurement_exact(
&self,
fee_data: FeeData,
min_expiry: DateTime<Utc>,
) -> Result<Option<FeeParameters>> {
let guard = self.0.lock().expect("Thread holding Mutex panicked");
Ok(guard
.iter()
.filter(|measurement| {
measurement.expiry >= min_expiry && measurement.fee_data == fee_data
})
.map(|measurement| measurement.estimate)
.min_by_key(|estimate| estimate.amount_in_sell_token()))
}
async fn find_measurement_including_larger_amount(
&self,
fee_data: FeeData,
min_expiry: DateTime<Utc>,
) -> Result<Option<FeeParameters>> {
let guard = self.0.lock().expect("Thread holding Mutex panicked");
Ok(guard
.iter()
.filter(|measurement| {
measurement.expiry >= min_expiry
&& measurement.fee_data.sell_token == fee_data.sell_token
&& measurement.fee_data.buy_token == fee_data.buy_token
&& measurement.fee_data.kind == fee_data.kind
&& measurement.fee_data.amount >= fee_data.amount
})
.map(|measurement| measurement.estimate)
.min_by_key(|estimate| estimate.amount_in_sell_token()))
}
}
#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
use chrono::Duration;
use futures::FutureExt;
use gas_estimation::GasPrice1559;
use maplit::{hashmap, hashset};
use mockall::{predicate::*, Sequence};
use shared::{
bad_token::list_based::ListBasedDetector, gas_price_estimation::FakeGasPriceEstimator,
price_estimation::mocks::FakePriceEstimator,
price_estimation::native::NativePriceEstimator,
};
use std::sync::Arc;
use crate::cow_subsidy::FixedCowSubsidy;
use super::*;
fn create_default_native_token_estimator(
price_estimator: Arc<dyn PriceEstimating>,
) -> Arc<dyn NativePriceEstimating> {
Arc::new(NativePriceEstimator::new(
price_estimator,
Default::default(),
1.into(),
))
}
impl MinFeeCalculator {
fn new_for_test(
gas_estimator: Arc<dyn GasPriceEstimating>,
price_estimator: Arc<dyn PriceEstimating>,
now: Box<dyn Fn() -> DateTime<Utc> + Send + Sync>,
) -> Self {
Self {
gas_estimator,
price_estimator: price_estimator.clone(),
measurements: Arc::new(InMemoryFeeStore::default()),
now,
bad_token_detector: Arc::new(ListBasedDetector::deny_list(Vec::new())),
fee_subsidy: Default::default(),
native_price_estimator: create_default_native_token_estimator(price_estimator),
cow_subsidy: Arc::new(FixedCowSubsidy::default()),
liquidity_order_owners: Default::default(),
}
}
}
#[tokio::test]
async fn accepts_min_fee_if_validated_before_expiry() {
let gas_price = Arc::new(Mutex::new(GasPrice1559 {
max_fee_per_gas: 100.0,
max_priority_fee_per_gas: 50.0,
base_fee_per_gas: 30.0,
}));
let time = Arc::new(Mutex::new(Utc::now()));
let gas_price_estimator = Arc::new(FakeGasPriceEstimator(gas_price.clone()));
let price_estimator = FakePriceEstimator(price_estimation::Estimate {
out_amount: 1.into(),
gas: 1,
});
let time_copy = time.clone();
let now = move || *time_copy.lock().unwrap();
let fee_estimator = MinFeeCalculator::new_for_test(
gas_price_estimator,
Arc::new(price_estimator),
Box::new(now),
);
let token = H160::from_low_u64_be(1);
let fee_data = FeeData {
sell_token: token,
..Default::default()
};
let (fee, expiry) = fee_estimator
.compute_subsidized_min_fee(fee_data, Default::default(), Default::default())
.await
.unwrap();
// Gas price increase after measurement
let new_gas_price = gas_price.lock().unwrap().bump(2.0);
*gas_price.lock().unwrap() = new_gas_price;
// fee is valid before expiry
*time.lock().unwrap() = expiry - Duration::seconds(10);
assert!(fee_estimator
.get_unsubsidized_min_fee(fee_data, Default::default(), fee, Default::default())
.await
.is_ok());
// fee is invalid for some uncached token
let token = H160::from_low_u64_be(2);
assert!(fee_estimator
.get_unsubsidized_min_fee(
FeeData {
sell_token: token,
..Default::default()
},
Default::default(),
fee,
Default::default()
)
.await
.is_err());
}
#[tokio::test]
async fn accepts_fee_if_higher_than_current_min_fee() {
let gas_price = Arc::new(Mutex::new(GasPrice1559 {
max_fee_per_gas: 100.0,
max_priority_fee_per_gas: 50.0,
base_fee_per_gas: 30.0,
}));
let gas_price_estimator = Arc::new(FakeGasPriceEstimator(gas_price.clone()));
let price_estimator = Arc::new(FakePriceEstimator(price_estimation::Estimate {
out_amount: 1.into(),
gas: 1,
}));
let fee_estimator = MinFeeCalculator::new_for_test(
gas_price_estimator,
price_estimator,
Box::new(Utc::now),
);
let token = H160::from_low_u64_be(1);
let fee_data = FeeData {
sell_token: token,
..Default::default()
};
let (fee, _) = fee_estimator
.compute_subsidized_min_fee(fee_data, Default::default(), Default::default())
.await
.unwrap();
dbg!(fee);
let lower_fee = fee - 1;
// slightly lower fee is not valid
assert!(fee_estimator
.get_unsubsidized_min_fee(fee_data, Default::default(), lower_fee, Default::default())
.await
.is_err());
// Gas price reduces, and slightly lower fee is now valid
let new_gas_price = gas_price.lock().unwrap().bump(0.5);
*gas_price.lock().unwrap() = new_gas_price;
assert!(fee_estimator
.get_unsubsidized_min_fee(fee_data, Default::default(), lower_fee, Default::default())
.await
.is_ok());
}
#[tokio::test]
async fn fails_for_unsupported_tokens() {
let unsupported_token = H160::from_low_u64_be(1);
let supported_token = H160::from_low_u64_be(2);
let gas_price_estimator =
Arc::new(FakeGasPriceEstimator(Arc::new(Mutex::new(GasPrice1559 {
max_fee_per_gas: 100.0,
max_priority_fee_per_gas: 50.0,
base_fee_per_gas: 30.0,
}))));
let price_estimator = Arc::new(FakePriceEstimator(price_estimation::Estimate {
out_amount: 1.into(),
gas: 1000,
}));
let native_price_estimator = create_default_native_token_estimator(price_estimator.clone());
let fee_estimator = MinFeeCalculator {
price_estimator,
gas_estimator: gas_price_estimator,
measurements: Arc::new(InMemoryFeeStore::default()),
now: Box::new(Utc::now),
bad_token_detector: Arc::new(ListBasedDetector::deny_list(vec![unsupported_token])),
fee_subsidy: Default::default(),
native_price_estimator,
cow_subsidy: Arc::new(FixedCowSubsidy::default()),
liquidity_order_owners: Default::default(),
};
// Selling unsupported token
let result = fee_estimator
.compute_subsidized_min_fee(
FeeData {
sell_token: unsupported_token,
buy_token: supported_token,
amount: 100.into(),
kind: OrderKind::Sell,
},
Default::default(),
Default::default(),
)
.await;
assert!(matches!(
result,
Err(PriceEstimationError::UnsupportedToken(t)) if t == unsupported_token
));
// Buying unsupported token
let result = fee_estimator
.compute_subsidized_min_fee(
FeeData {
sell_token: supported_token,
buy_token: unsupported_token,
amount: 100.into(),
kind: OrderKind::Sell,
},
Default::default(),
Default::default(),
)
.await;
assert!(matches!(
result,
Err(PriceEstimationError::UnsupportedToken(t)) if t == unsupported_token
));
}
#[tokio::test]
async fn is_valid_fee() {
shared::tracing::initialize_for_tests("debug");
let sell_token = H160::from_low_u64_be(1);
let fee_data = FeeData {
sell_token,
..Default::default()
};
let gas_price_estimator =
Arc::new(FakeGasPriceEstimator(Arc::new(Mutex::new(GasPrice1559 {
max_fee_per_gas: 100.0,
max_priority_fee_per_gas: 50.0,
base_fee_per_gas: 30.0,
}))));
let price_estimator = Arc::new(FakePriceEstimator(price_estimation::Estimate {
out_amount: 1.into(),
gas: 1000,
}));
let native_price_estimator = create_default_native_token_estimator(price_estimator.clone());
let app_data = AppId([1u8; 32]);
let user = H160::zero();
let mut fee_estimator = MinFeeCalculator {
price_estimator,
gas_estimator: gas_price_estimator,
measurements: Arc::new(InMemoryFeeStore::default()),
now: Box::new(Utc::now),
bad_token_detector: Arc::new(ListBasedDetector::deny_list(vec![])),
fee_subsidy: FeeSubsidyConfiguration {
partner_additional_fee_factors: hashmap! { app_data => 0.5 },
..Default::default()
},
native_price_estimator,
cow_subsidy: Arc::new(FixedCowSubsidy(0.5)),
liquidity_order_owners: Default::default(),
};
let (fee, _) = fee_estimator
.compute_subsidized_min_fee(fee_data, app_data, user)
.await
.unwrap();
assert_eq!(
fee_estimator
.get_unsubsidized_min_fee(fee_data, app_data, fee, user)
.await
.unwrap()
.amount_in_sell_token(),
fee * 4
);
assert!(fee_estimator
.get_unsubsidized_min_fee(fee_data, Default::default(), fee, user)
.await
.is_err());
let lower_fee = fee - 1;
assert!(fee_estimator
.get_unsubsidized_min_fee(fee_data, app_data, lower_fee, user)
.await
.is_err());
// repeat without user so no extra cow subsidy
fee_estimator.cow_subsidy = Arc::new(FixedCowSubsidy(1.0));
let (fee_2, _) = fee_estimator
.compute_subsidized_min_fee(fee_data, app_data, user)
.await
.unwrap();
assert_eq!(fee_2, fee * 2);
}
#[tokio::test]
async fn applies_fee_factor_to_past_and_new_fees() {
let sell_token = H160::from_low_u64_be(1);
let fee_data = FeeData {
sell_token,
..Default::default()
};
let native_token_price_estimation_amount = 100.;
let sell_token_price = 0.1;
let gas_estimate = 42.;
let unsubsidized_min_fee = FeeParameters {
gas_amount: 1337.,
sell_token_price,
gas_price: gas_estimate,
};
let gas_estimator = Arc::new(FakeGasPriceEstimator(Arc::new(Mutex::new(GasPrice1559 {
base_fee_per_gas: 0.0,
max_fee_per_gas: 42.0,
max_priority_fee_per_gas: 42.0,
}))));
let price_estimator = Arc::new(FakePriceEstimator(price_estimation::Estimate {
out_amount: U256::from_f64_lossy(
native_token_price_estimation_amount / sell_token_price,
),
gas: 1337,
}));
let native_price_estimator = Arc::new(NativePriceEstimator::new(
price_estimator.clone(),
Default::default(),
U256::from_f64_lossy(native_token_price_estimation_amount),
));
let mut measurements = MockMinFeeStoring::new();
let mut seq = Sequence::new();
measurements
.expect_find_measurement_exact()
.times(1)
.in_sequence(&mut seq)
.with(eq(fee_data), always())
.returning(|_, _| Ok(None));
measurements
.expect_save_fee_measurement()
.times(1)
.in_sequence(&mut seq)
.with(eq(fee_data), always(), eq(unsubsidized_min_fee))
.returning(|_, _, _| Ok(()));
measurements
.expect_find_measurement_exact()
.times(1)
.in_sequence(&mut seq)
.with(eq(fee_data), always())
.returning(move |_, _| Ok(Some(unsubsidized_min_fee)));
let app_data = AppId([1u8; 32]);
let fee_estimator = MinFeeCalculator {
price_estimator,
gas_estimator,
measurements: Arc::new(measurements),
now: Box::new(Utc::now),
bad_token_detector: Arc::new(ListBasedDetector::deny_list(vec![])),
fee_subsidy: FeeSubsidyConfiguration {
fee_factor: 0.8,
partner_additional_fee_factors: hashmap! { app_data => 0.5 },
..Default::default()
},
native_price_estimator,
cow_subsidy: Arc::new(FixedCowSubsidy::default()),
liquidity_order_owners: Default::default(),
};
let (fee, _) = fee_estimator
.compute_subsidized_min_fee(fee_data, app_data, Default::default())
.await
.unwrap();
assert_eq!(
fee,
U256::from_f64_lossy(
unsubsidized_min_fee.amount_in_sell_token().to_f64_lossy() * 0.8 * 0.5
)
);
let (fee, _) = fee_estimator
.compute_subsidized_min_fee(fee_data, Default::default(), Default::default())
.await
.unwrap();
assert_eq!(
fee,
U256::from_f64_lossy(unsubsidized_min_fee.amount_in_sell_token().to_f64_lossy() * 0.8)
);
}
#[test]
fn test_apply_fee_factor_capped_at_minimum() {
let unsubsidized = FeeParameters {
gas_amount: 100_000.,
gas_price: 1_000_000_000.,
sell_token_price: 1.,
};
let fee_configuration = FeeSubsidyConfiguration {
fee_discount: 500_000_000_000_000.,
min_discounted_fee: 1_000_000.,
fee_factor: 0.5,
..Default::default()
};
assert_eq!(
unsubsidized.apply_fee_factor(&fee_configuration, Default::default(), 1.0),
// Note that the fee factor is applied to the minimum discounted fee!
500_000.into(),
);
}
#[test]
fn test_apply_fee_factor_order() {
let unsubsidized = FeeParameters {
gas_amount: 100_000.,
gas_price: 1_000_000_000.,
sell_token_price: 1.,
};
let app_id = AppId([1u8; 32]);
let fee_configuration = FeeSubsidyConfiguration {
fee_discount: 50_000_000_000_000.,
fee_factor: 0.5,
min_discounted_fee: 0.,
partner_additional_fee_factors: maplit::hashmap! {
app_id => 0.1,
},
};
// (100G - 50G) * 0.5
assert_eq!(
unsubsidized.apply_fee_factor(&fee_configuration, Default::default(), 1.0),
25_000_000_000_000u64.into()
);
// Additionally multiply with 0.1 if partner app id is used
assert_eq!(
unsubsidized.apply_fee_factor(&fee_configuration, app_id, 1.0),
2_500_000_000_000u64.into()
);
}
#[test]
fn fee_rounds_up() {
let fee_data = FeeData {
sell_token: H160::from_low_u64_be(0),
buy_token: H160::from_low_u64_be(1),
..Default::default()
};
let gas_price_estimator =
Arc::new(FakeGasPriceEstimator(Arc::new(Mutex::new(GasPrice1559 {
base_fee_per_gas: 0.0,
max_fee_per_gas: 1.0,
max_priority_fee_per_gas: 1.0,
}))));
let price_estimator = Arc::new(FakePriceEstimator(price_estimation::Estimate {
out_amount: 1.into(),
gas: 9,
}));
let native_price_estimator = create_default_native_token_estimator(price_estimator.clone());
let fee_estimator = MinFeeCalculator {
price_estimator,
gas_estimator: gas_price_estimator,
measurements: Arc::new(InMemoryFeeStore::default()),
now: Box::new(Utc::now),
bad_token_detector: Arc::new(ListBasedDetector::deny_list(vec![])),
fee_subsidy: FeeSubsidyConfiguration {
fee_factor: 0.5,
..Default::default()
},
native_price_estimator,
cow_subsidy: Arc::new(FixedCowSubsidy::default()),
liquidity_order_owners: Default::default(),
};
let (fee, _) = fee_estimator
.compute_subsidized_min_fee(fee_data, Default::default(), Default::default())
.now_or_never()
.unwrap()
.unwrap();
// In floating point the fee would be 4.5 but we always want to round atoms up.
assert_eq!(fee, 5.into());
// Fee validates.
fee_estimator
.get_unsubsidized_min_fee(fee_data, Default::default(), fee, Default::default())
.now_or_never()
.unwrap()
.unwrap();
}
#[test]
fn no_fees_for_pmms() {
let liquidity_order_owner = H160([0x42; 20]);
let fee_estimator = MinFeeCalculator {
liquidity_order_owners: hashset!(liquidity_order_owner),
..MinFeeCalculator::new_for_test(
Arc::new(FakeGasPriceEstimator(Arc::new(Mutex::new(GasPrice1559 {
base_fee_per_gas: 0.0,
max_fee_per_gas: 1.0,
max_priority_fee_per_gas: 1.0,
})))),
Arc::new(FakePriceEstimator(price_estimation::Estimate {
out_amount: 1.into(),
gas: 9,
})),
Box::new(Utc::now),
)
};
let fee_data = FeeData {
sell_token: H160([1; 20]),
buy_token: H160([2; 20]),
..Default::default()
};
assert_eq!(
fee_estimator
.compute_subsidized_min_fee(fee_data, AppId::default(), liquidity_order_owner)
.now_or_never()
.unwrap()
.unwrap(),
(U256::from(0), MAX_DATETIME),
);
assert_eq!(
fee_estimator
.get_unsubsidized_min_fee(
fee_data,
AppId::default(),