-
Notifications
You must be signed in to change notification settings - Fork 82
/
auction.rs
975 lines (925 loc) · 35.3 KB
/
auction.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
use {
super::serialize,
app_data::AppDataHash,
bigdecimal::BigDecimal,
number::serialization::HexOrDecimalU256,
serde::Deserialize,
serde_json::{Number, Value},
serde_with::{serde_as, DisplayFromStr},
std::collections::HashMap,
utoipa::{
openapi::{
AllOfBuilder,
ArrayBuilder,
ObjectBuilder,
OneOfBuilder,
Ref,
RefOr,
Schema,
SchemaType::{self},
},
ToSchema,
},
web3::types::{H160, H256, U256},
};
/// The abstract auction to be solved by the searcher.
#[serde_as]
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Auction {
/// An opaque identifier for the auction. Will be set to `null` for requests
/// that are not part of an auction (when quoting token prices for example).
#[serde_as(as = "Option<DisplayFromStr>")]
#[schema(value_type = String)]
pub id: Option<i64>,
/// A map of token addresses to token information.
pub tokens: HashMap<H160, TokenInfo>,
/// The solvable orders included in the auction.
pub orders: Vec<Order>,
/// On-chain liquidity that can be used by the solution.
pub liquidity: Vec<Liquidity>,
/// The current estimated gas price that will be paid when executing a
/// settlement. Additionally, this is the gas price that is multiplied with
/// a settlement's gas estimate for solution scoring.
#[serde_as(as = "HexOrDecimalU256")]
#[schema(value_type = TokenAmount)]
pub effective_gas_price: U256,
/// The deadline by which a solution to the auction is required. Requests
/// that go beyond this deadline are expected to be cancelled by the caller.
#[schema(value_type = DateTime)]
pub deadline: chrono::DateTime<chrono::Utc>,
}
/// CoW Protocol order information relevant to execution.
#[serde_as]
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Order {
#[schema(value_type = OrderUid)]
#[serde_as(as = "serialize::Hex")]
pub uid: [u8; 56],
#[schema(value_type = Token)]
pub sell_token: H160,
#[schema(value_type = Token)]
pub buy_token: H160,
#[schema(value_type = TokenAmount)]
#[serde_as(as = "HexOrDecimalU256")]
pub sell_amount: U256,
#[schema(value_type = TokenAmount)]
#[serde_as(as = "HexOrDecimalU256")]
pub full_sell_amount: U256,
#[schema(value_type = TokenAmount)]
#[serde_as(as = "HexOrDecimalU256")]
pub buy_amount: U256,
#[schema(value_type = TokenAmount)]
#[serde_as(as = "HexOrDecimalU256")]
pub full_buy_amount: U256,
pub fee_policies: Option<Vec<FeePolicy>>,
pub valid_to: u32,
pub kind: OrderKind,
#[schema(value_type = Address)]
pub receiver: Option<H160>,
#[schema(value_type = Address)]
pub owner: H160,
/// Whether or not this order can be partially filled. If this is false,
/// then the order is a "fill-or-kill" order, meaning it needs to be
/// completely filled or not at all.
pub partially_fillable: bool,
pub pre_interactions: Vec<InteractionData>,
pub post_interactions: Vec<InteractionData>,
pub sell_token_source: SellTokenSource,
pub buy_token_destination: BuyTokenDestination,
pub class: OrderClass,
#[schema(value_type = AppData)]
pub app_data: AppDataHash,
pub signing_scheme: LegacySigningScheme,
#[serde(with = "bytes_hex")]
#[schema(value_type = Signature)]
pub signature: Vec<u8>,
}
/// Destination for which the buyAmount should be transferred to order's
/// receiver to upon fulfillment
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum BuyTokenDestination {
/// Pay trade proceeds as an ERC20 token transfer
Erc20,
/// Pay trade proceeds as a Vault internal balance transfer
Internal,
}
/// Source from which the sellAmount should be drawn upon order fulfillment
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum SellTokenSource {
/// Direct ERC20 allowances to the Vault relayer contract
Erc20,
/// Internal balances to the Vault with GPv2 relayer approval
External,
/// ERC20 allowances to the Vault with GPv2 relayer approval
Internal,
}
#[serde_as]
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct InteractionData {
#[schema(value_type = Address)]
pub target: H160,
#[schema(value_type = TokenAmount)]
#[serde_as(as = "HexOrDecimalU256")]
pub value: U256,
#[schema(value_type = String, example = "0x01020304")]
#[serde(with = "bytes_hex")]
pub call_data: Vec<u8>,
}
// todo: There is a conflict between solution's SigningScheme which is in
// camelCase. There is no way to keep 2 object with the same name in the OpenAPI
// schema. Temporarily renamed the struct. Must be migrated to the camelCase.
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum LegacySigningScheme {
Eip712,
EthSign,
Eip1271,
PreSign,
}
/// How the CoW Protocol order was classified.
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub enum OrderKind {
Sell,
Buy,
}
/// How the CoW Protocol order was classified.
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub enum OrderClass {
Market,
Limit,
Liquidity,
}
/// A fee policy that applies to an order.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum FeePolicy {
/// If the order receives more than limit price, pay the protocol a factor
/// of the difference.
#[serde(rename_all = "camelCase")]
Surplus {
/// The factor of the user surplus that the protocol will request from
/// the solver after settling the order
factor: f64,
/// Never charge more than that percentage of the order volume.
max_volume_factor: f64,
},
/// A cut from the price improvement over the best quote is taken as a
/// protocol fee.
#[serde(rename_all = "camelCase")]
PriceImprovement {
/// The factor of the user surplus that the protocol will request from
/// the solver after settling the order.
factor: f64,
/// Never charge more than that percentage of the order volume.
max_volume_factor: f64,
quote: Quote,
},
#[serde(rename_all = "camelCase")]
Volume { factor: f64 },
}
impl ToSchema<'static> for FeePolicy {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"FeePolicy",
Schema::OneOf(
OneOfBuilder::new()
.description(Some("A fee policy that applies to an order"))
.item(Ref::from_schema_name("SurplusFee"))
.item(Ref::from_schema_name("PriceImprovement"))
.item(Ref::from_schema_name("VolumeFee"))
.build(),
)
.into(),
)
}
}
#[serde_as]
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Quote {
#[serde_as(as = "HexOrDecimalU256")]
#[schema(value_type = TokenAmount)]
pub sell_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
#[schema(value_type = TokenAmount)]
pub buy_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
#[schema(value_type = TokenAmount)]
pub fee: U256,
}
/// Information about a token relevant to the auction.
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenInfo {
/// The ERC20.decimals value for this token. This may be missing for ERC20
/// tokens that don't implement the optional metadata extension.
pub decimals: Option<u8>,
/// The ERC20.symbol value for this token. This may be missing for ERC20
/// tokens that don't implement the optional metadata extension.
pub symbol: Option<String>,
/// The reference price of this token for the auction used for scoring. This
/// price is only included for tokens for which there are CoW Protocol
/// orders.
#[serde_as(as = "Option<HexOrDecimalU256>")]
pub reference_price: Option<U256>,
/// The balance held by the Settlement contract that is available during a
/// settlement.
#[serde_as(as = "HexOrDecimalU256")]
pub available_balance: U256,
/// A flag which indicates that solvers are allowed to perform gas cost
/// optimizations for this token by not routing the trades via an AMM, and
/// instead use its available balances, as specified by CIP-2.
pub trusted: bool,
}
impl ToSchema<'static> for TokenInfo {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"TokenInfo",
Schema::Object(
ObjectBuilder::new()
.description(Some("Information about an ERC20 token."))
.required("availableBalance")
.required("trusted")
.property(
"decimals",
ObjectBuilder::new()
.schema_type(SchemaType::Integer)
.description(Some(
"The ERC20.decimals value for this token. This may be missing for \
ERC20 tokens that don't implement the optional metadata \
extension.",
)),
)
.property(
"symbol",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.description(Some(
"The ERC20.symbol value for this token. This may be missing for \
ERC20 tokens that don't implement the optional metadata \
extension.",
)),
)
.property(
"referencePrice",
AllOfBuilder::new()
.description(Some(
"The reference price of this token for the auction used for \
scoring. This price is only included for tokens for which there \
are CoW Protocol orders.",
))
.item(Ref::from_schema_name("NativePrice")),
)
.property(
"availableBalance",
AllOfBuilder::new()
.description(Some(
"The balance held by the Settlement contract that is available \
during a settlement.",
))
.item(Ref::from_schema_name("TokenAmount")),
)
.property(
"trusted",
ObjectBuilder::new()
.schema_type(SchemaType::Boolean)
.description(Some(
"A flag which indicates that solvers are allowed to perform gas \
cost optimizations for this token by not routing the trades via \
an AMM, and instead use its available balances, as specified by \
CIP-2.",
)),
)
.build(),
)
.into(),
)
}
}
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum Liquidity {
ConstantProduct(ConstantProductPool),
WeightedProduct(WeightedProductPool),
Stable(StablePool),
ConcentratedLiquidity(ConcentratedLiquidityPool),
LimitOrder(ForeignLimitOrder),
}
// todo: Currently, it strictly follows the manual api schema. This has to be
// automated and deleted.
impl ToSchema<'static> for Liquidity {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"Liquidity",
Schema::AllOf(
AllOfBuilder::new()
.description(Some(
"On-chain liquidity that can be used in a solution. This liquidity is \
provided to facilitate onboarding new solvers. Additional liquidity that \
is not included in this set may still be used in solutions.",
))
.item(Ref::from_schema_name("LiquidityParameters"))
.item(Schema::Object(
ObjectBuilder::new()
.property(
"id",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.description(Some(
"An opaque ID used for uniquely identifying the liquidity \
within a single auction (note that they are **not** \
guaranteed to be unique across auctions). This ID is \
used in the solution for matching interactions with the \
executed liquidity.",
)),
)
.property(
"address",
AllOfBuilder::new()
.description(Some(
"A rough approximation of gas units required to use this \
liquidity on-chain.",
))
.item(Ref::from_schema_name("Address")),
)
.property(
"gasEstimate",
AllOfBuilder::new()
.description(Some(
"A rough approximation of gas units required to use this \
liquidity on-chain.",
))
.item(Ref::from_schema_name("BigInt")),
)
.required("id")
.required("address")
.required("gasEstimate")
.build(),
))
.build(),
)
.into(),
)
}
}
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", deny_unknown_fields)]
pub enum LiquidityParameters {
ConstantProduct(ConstantProductPool),
WeightedProduct(WeightedProductPool),
Stable(StablePool),
ConcentratedLiquidity(ConcentratedLiquidityPool),
LimitOrder(ForeignLimitOrder),
}
impl ToSchema<'static> for LiquidityParameters {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"LiquidityParameters",
Schema::OneOf(
OneOfBuilder::new()
.item(Ref::from_schema_name("ConstantProductPool"))
.item(Ref::from_schema_name("WeightedProductPool"))
.item(Ref::from_schema_name("StablePool"))
.item(Ref::from_schema_name("ConcentratedLiquidityPool"))
.item(Ref::from_schema_name("ForeignLimitOrder"))
.build(),
)
.into(),
)
}
}
/// A UniswapV2-like constant product liquidity pool for a token pair.
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConstantProductPool {
pub id: String,
pub address: H160,
pub router: H160,
#[serde_as(as = "HexOrDecimalU256")]
pub gas_estimate: U256,
pub tokens: HashMap<H160, ConstantProductReserve>,
pub fee: BigDecimal,
}
impl ToSchema<'static> for ConstantProductPool {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"ConstantProductPool",
Schema::Object(
ObjectBuilder::new()
.description(Some(
"A UniswapV2-like constant product liquidity pool for a token pair.",
))
.required("kind")
.required("router")
.required("tokens")
.required("fee")
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["constantProduct"])),
)
.property("router", Ref::from_schema_name("Address"))
.property(
"tokens",
ObjectBuilder::new()
.description(Some("A mapping of token address to its reserve amounts."))
.additional_properties(Some(Ref::from_schema_name("TokenReserve"))),
)
.property("fee", Ref::from_schema_name("Decimal"))
.build(),
)
.into(),
)
}
}
/// A reserve of tokens in an on-chain liquidity pool.
#[serde_as]
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
#[schema(title = "TokenReserve")]
pub struct ConstantProductReserve {
#[serde_as(as = "HexOrDecimalU256")]
pub balance: U256,
}
/// A Balancer-like weighted product liquidity pool of N tokens.
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WeightedProductPool {
pub id: String,
pub address: H160,
pub balancer_pool_id: H256,
#[serde_as(as = "HexOrDecimalU256")]
pub gas_estimate: U256,
pub tokens: HashMap<H160, WeightedProductReserve>,
pub fee: BigDecimal,
pub version: WeightedProductVersion,
}
impl ToSchema<'static> for WeightedProductPool {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"WeightedProductPool",
Schema::Object(
ObjectBuilder::new()
.description(Some(
"A Balancer-like weighted product liquidity pool of N tokens.",
))
.required("kind")
.required("tokens")
.required("fee")
.required("balancer_pool_id")
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["weightedProduct"])),
)
.property(
"tokens",
ObjectBuilder::new()
.description(Some(
"A mapping of token address to its reserve amounts with weights.",
))
.additional_properties(Some(Schema::AllOf(
AllOfBuilder::new()
.item(Ref::from_schema_name("TokenReserve"))
.item(
ObjectBuilder::new()
.required("weight")
.property("weight", Ref::from_schema_name("Decimal"))
.property(
"scalingFactor",
Ref::from_schema_name("Decimal"),
)
.build(),
)
.build(),
))),
)
.property("fee", Ref::from_schema_name("Decimal"))
.property("balancer_pool_id", Ref::from_schema_name("BalancerPoolId"))
.property(
"version",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["v0", "v3Plus"])),
)
.build(),
)
.into(),
)
}
}
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WeightedProductReserve {
#[serde_as(as = "HexOrDecimalU256")]
pub balance: U256,
pub scaling_factor: BigDecimal,
pub weight: BigDecimal,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum WeightedProductVersion {
V0,
V3Plus,
}
/// A Curve-like stable pool of N tokens.
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StablePool {
pub id: String,
pub address: H160,
pub balancer_pool_id: H256,
#[serde_as(as = "HexOrDecimalU256")]
pub gas_estimate: U256,
pub tokens: HashMap<H160, StableReserve>,
pub amplification_parameter: BigDecimal,
pub fee: BigDecimal,
}
impl ToSchema<'static> for StablePool {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"StablePool",
Schema::Object(
ObjectBuilder::new()
.description(Some("A Curve-like stable pool of N tokens."))
.required("kind")
.required("tokens")
.required("amplificationParameter")
.required("fee")
.required("balancer_pool_id")
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["stable"])),
)
.property(
"tokens",
ObjectBuilder::new()
.description(Some(
"A mapping of token address to token balance and scaling rate.",
))
.additional_properties(Some(Schema::AllOf(
AllOfBuilder::new()
.item(Ref::from_schema_name("TokenReserve"))
.item(
ObjectBuilder::new()
.required("scalingFactor")
.property(
"scalingFactor",
Ref::from_schema_name("Decimal"),
)
.build(),
)
.build(),
))),
)
.property("amplificationParameter", Ref::from_schema_name("Decimal"))
.property("fee", Ref::from_schema_name("Decimal"))
.property("balancer_pool_id", Ref::from_schema_name("BalancerPoolId"))
.build(),
)
.into(),
)
}
}
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StableReserve {
#[serde_as(as = "HexOrDecimalU256")]
pub balance: U256,
pub scaling_factor: BigDecimal,
}
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConcentratedLiquidityPool {
pub id: String,
pub address: H160,
pub router: H160,
#[serde_as(as = "HexOrDecimalU256")]
pub gas_estimate: U256,
pub tokens: Vec<H160>,
#[serde_as(as = "HexOrDecimalU256")]
pub sqrt_price: U256,
#[serde_as(as = "DisplayFromStr")]
pub liquidity: u128,
pub tick: i32,
#[serde_as(as = "HashMap<DisplayFromStr, DisplayFromStr>")]
pub liquidity_net: HashMap<i32, i128>,
pub fee: BigDecimal,
}
impl ToSchema<'static> for ConcentratedLiquidityPool {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"ConcentratedLiquidityPool",
Schema::Object(
ObjectBuilder::new()
.description(Some("A Uniswap V3-like concentrated liquidity pool."))
.required("kind")
.required("router")
.required("tokens")
.required("sqrtPrice")
.required("liquidity")
.required("tick")
.required("liquidityNet")
.required("fee")
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["concentratedLiquidity"])),
)
.property("router", Ref::from_schema_name("Address"))
.property(
"tokens",
ArrayBuilder::new().items(Ref::from_schema_name("Token")),
)
.property("sqrtPrice", Ref::from_schema_name("U256"))
.property("liquidity", Ref::from_schema_name("U128"))
.property("tick", Ref::from_schema_name("I32"))
.property(
"liquidityNet",
ObjectBuilder::new()
.description(Some("A map of tick indices to their liquidity values."))
.additional_properties(Some(Ref::from_schema_name("I128"))),
)
.property("fee", Ref::from_schema_name("Decimal"))
.build(),
)
.into(),
)
}
}
#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ForeignLimitOrder {
pub id: String,
pub address: H160,
#[serde_as(as = "HexOrDecimalU256")]
pub gas_estimate: U256,
#[serde_as(as = "serialize::Hex")]
// todo: not used/not a part of the API schema for some reason
pub hash: [u8; 32],
pub maker_token: H160,
pub taker_token: H160,
#[serde_as(as = "HexOrDecimalU256")]
pub maker_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub taker_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub taker_token_fee_amount: U256,
}
impl ToSchema<'static> for ForeignLimitOrder {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"ForeignLimitOrder",
Schema::Object(
ObjectBuilder::new()
.description(Some("A 0x-like limit order external to CoW Protocol."))
.required("kind")
.required("makerToken")
.required("takerToken")
.required("makerAmount")
.required("takerAmount")
.required("takerTokenFeeAmount")
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["limitOrder"])),
)
.property("makerToken", Ref::from_schema_name("Token"))
.property("takerToken", Ref::from_schema_name("Token"))
.property("makerAmount", Ref::from_schema_name("TokenAmount"))
.property("takerAmount", Ref::from_schema_name("TokenAmount"))
.property("takerTokenFeeAmount", Ref::from_schema_name("TokenAmount"))
.build(),
)
.into(),
)
}
}
// Structs for the utoipa OpenAPI schema generator.
/// The price in wei of the native token (Ether on Mainnet for example) to buy
/// 10**18 of a token.
#[derive(ToSchema)]
#[schema(example = "1234567890")]
#[allow(dead_code)]
pub struct NativePrice(String);
/// An ISO-8601 formatted date-time.
#[derive(ToSchema)]
#[schema(example = "1970-01-01T00:00:00.000Z")]
#[allow(dead_code)]
pub struct DateTime(String);
/// An arbitrary-precision integer value.
#[derive(ToSchema)]
#[schema(example = "1234567890")]
#[allow(dead_code)]
pub struct BigInt(String);
/// An arbitrary-precision decimal value.
#[derive(ToSchema)]
#[schema(example = "13.37")]
#[allow(dead_code)]
pub struct Decimal(String);
/// A hex-encoded 32 byte string containing the pool address (0..20), the pool
/// specialization (20..22) and the poolnonce (22..32).
#[derive(ToSchema)]
#[schema(example = "0xc88c76dd8b92408fe9bea1a54922a31e232d873c0002000000000000000005b2")]
#[allow(dead_code)]
pub struct BalancerPoolId(String);
#[serde_as]
#[derive(ToSchema, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TokenReserve {
#[schema(value_type = TokenAmount)]
pub balance: U256,
}
/// 256 bit unsigned integer in decimal notation.
#[derive(ToSchema)]
#[schema(as = U256, example = "1234567890")]
#[allow(dead_code)]
pub struct U256Schema(String);
/// 128 bit unsigned integer in decimal notation.
#[derive(ToSchema)]
#[schema(example = "1234567890")]
#[allow(dead_code)]
pub struct U128(String);
/// 128 bit signed integer in decimal notation.
#[derive(ToSchema)]
#[schema(example = "-1234567890")]
#[allow(dead_code)]
pub struct I128(String);
/// 32 bit signed integer in decimal notation.
#[derive(ToSchema)]
#[schema(example = "-12345")]
#[allow(dead_code)]
pub struct I32(String);
/// Unique identifier for the order. Order UIDs are 56 bytes long, where bytes
/// [0, 32) represent the order digest used for signing, bytes [32, 52)
/// represent the owner address and bytes [52, 56) represent the order's
/// `validTo` field.
#[derive(ToSchema)]
#[schema(
example = "0x30cff40d9f60caa68a37f0ee73253ad6ad72b45580c945fe3ab67596476937197854163b1b0d24e77dca702b97b5cc33e0f83dcb626122a6"
)]
#[allow(dead_code)]
pub struct OrderUid(String);
/// Signature bytes.
#[derive(ToSchema)]
#[schema(
example = "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
)]
#[allow(dead_code)]
pub struct Signature(String);
/// If the order receives more than limit price, pay the protocol a factor of
/// the difference.
pub struct SurplusFee {
pub factor: f64,
pub max_volume_factor: f64,
}
impl ToSchema<'static> for SurplusFee {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"SurplusFee",
Schema::Object(
ObjectBuilder::new()
.description(Some(
"If the order receives more than limit price, pay the protocol a factor \
of the difference.",
))
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["surplus"])),
)
.property(
"factor",
ObjectBuilder::new()
.description(Some(
"The factor of the user surplus that the protocol will request \
from the solver after settling the order",
))
.schema_type(SchemaType::Number)
.example(Number::from_f64(0.5).map(Value::Number))
.build(),
)
.property(
"maxVolumeFactor",
ObjectBuilder::new()
.description(Some(
"Never charge more than that percentage of the order volume.",
))
.schema_type(SchemaType::Number)
.example(Number::from_f64(0.05).map(Value::Number))
.minimum(Some(0.0))
.maximum(Some(0.99999))
.build(),
)
.build(),
)
.into(),
)
}
}
/// A cut from the price improvement over the best quote is taken as a protocol
/// fee.
pub struct PriceImprovement {
pub factor: f64,
pub max_volume_factor: f64,
pub quote: Quote,
}
impl ToSchema<'static> for PriceImprovement {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"PriceImprovement",
Schema::Object(
ObjectBuilder::new()
.description(Some(
"A cut from the price improvement over the best quote is taken as a \
protocol fee.",
))
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["priceImprovement"])),
)
.property(
"factor",
ObjectBuilder::new()
.description(Some(
"The factor of the user surplus that the protocol will request \
from the solver after settling the order",
))
.schema_type(SchemaType::Number)
.example(Number::from_f64(0.5).map(Value::Number)),
)
.property(
"maxVolumeFactor",
ObjectBuilder::new()
.description(Some(
"Never charge more than that percentage of the order volume.",
))
.schema_type(SchemaType::Number)
.example(Number::from_f64(0.01).map(Value::Number))
.minimum(Some(0.0))
.maximum(Some(0.99999)),
)
.property("quote", Ref::from_schema_name("Quote"))
.build(),
)
.into(),
)
}
}
pub struct VolumeFee {
pub factor: f64,
}
impl ToSchema<'static> for VolumeFee {
fn schema() -> (&'static str, RefOr<Schema>) {
(
"VolumeFee",
Schema::Object(
ObjectBuilder::new()
.property(
"kind",
ObjectBuilder::new()
.schema_type(SchemaType::String)
.enum_values(Some(["volume"])),
)
.property(
"factor",
ObjectBuilder::new()
.description(Some(
"The fraction of the order's volume that the protocol will \
request from the solver after settling the order.",
))
.schema_type(SchemaType::Number)
.example(Number::from_f64(0.5).map(Value::Number)),
)
.build(),
)
.into(),
)
}
}