-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathassets.rs
1247 lines (1109 loc) · 44.5 KB
/
assets.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
// This file is part of HydraDX-node.
// Copyright (C) 2020-2023 Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
use crate::system::NativeAssetId;
use hydradx_adapters::{
AssetFeeOraclePriceProvider, EmaOraclePriceAdapter, FreezableNFT, MultiCurrencyLockedBalance, OmnipoolHookAdapter,
OracleAssetVolumeProvider, PriceAdjustmentAdapter, StableswapHooksAdapter, VestingInfo,
};
use hydradx_adapters::{RelayChainBlockHashProvider, RelayChainBlockNumberProvider};
use hydradx_traits::{
router::{inverse_route, PoolType, Trade},
AccountIdFor, AssetKind, AssetPairAccountIdFor, OnTradeHandler, OraclePeriod, Source,
};
use pallet_currencies::BasicCurrencyAdapter;
use pallet_omnipool::{
traits::{EnsurePriceWithin, OmnipoolHooks},
weights::WeightInfo as OmnipoolWeights,
};
use pallet_otc::NamedReserveIdentifier;
use pallet_stableswap::weights::WeightInfo as StableswapWeights;
use pallet_transaction_multi_payment::{AddTxAssetOnAccount, RemoveTxAssetOnKilled};
use primitives::constants::chain::XYK_SOURCE;
use primitives::constants::time::DAYS;
use primitives::constants::{
chain::OMNIPOOL_SOURCE,
currency::{NATIVE_EXISTENTIAL_DEPOSIT, UNITS},
};
use core::ops::RangeInclusive;
use frame_support::{
parameter_types,
sp_runtime::app_crypto::sp_core::crypto::UncheckedFrom,
sp_runtime::traits::{One, PhantomData},
sp_runtime::{FixedU128, Perbill, Permill},
traits::{
AsEnsureOriginWithArg, ConstU32, Contains, Currency, EnsureOrigin, Imbalance, NeverEnsureOrigin, OnUnbalanced,
},
BoundedVec, PalletId,
};
use frame_system::{EnsureRoot, EnsureSigned, RawOrigin};
use orml_traits::currency::MutationHooks;
use orml_traits::{GetByKey, MultiCurrency};
use pallet_dynamic_fees::types::FeeParams;
use pallet_lbp::weights::WeightInfo as LbpWeights;
use pallet_route_executor::{weights::WeightInfo as RouterWeights, AmmTradeWeights, MAX_NUMBER_OF_TRADES};
use pallet_staking::types::Action;
use pallet_staking::SigmoidPercentage;
use pallet_xyk::weights::WeightInfo as XykWeights;
use sp_runtime::{DispatchError, FixedPointNumber};
use sp_std::num::NonZeroU16;
parameter_types! {
pub const NativeExistentialDeposit: u128 = NATIVE_EXISTENTIAL_DEPOSIT;
pub const MaxLocks: u32 = 50;
pub const MaxReserves: u32 = 50;
}
// pallet-treasury did not impl OnUnbalanced<Credit>, need an adapter to handle dust.
type CreditOf = frame_support::traits::fungible::Credit<<Runtime as frame_system::Config>::AccountId, Balances>;
type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;
pub struct DustRemovalAdapter;
impl OnUnbalanced<CreditOf> for DustRemovalAdapter {
fn on_nonzero_unbalanced(amount: CreditOf) {
let new_amount = NegativeImbalance::new(amount.peek());
Treasury::on_nonzero_unbalanced(new_amount);
}
}
parameter_types! {
pub const MaxHolds: u32 = 0;
pub const MaxFreezes: u32 = 0;
}
impl pallet_balances::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = weights::balances::HydraWeight<Runtime>;
type Balance = Balance;
type DustRemoval = DustRemovalAdapter;
type ExistentialDeposit = NativeExistentialDeposit;
type AccountStore = System;
type ReserveIdentifier = [u8; 8];
type RuntimeHoldReason = ();
type FreezeIdentifier = ();
type MaxLocks = MaxLocks;
type MaxReserves = MaxReserves;
type MaxHolds = MaxHolds;
type MaxFreezes = MaxFreezes;
}
pub struct CurrencyHooks;
impl MutationHooks<AccountId, AssetId, Balance> for CurrencyHooks {
type OnDust = Duster;
type OnSlash = ();
type PreDeposit = ();
type PostDeposit = ();
type PreTransfer = ();
type PostTransfer = ();
type OnNewTokenAccount = AddTxAssetOnAccount<Runtime>;
type OnKilledTokenAccount = RemoveTxAssetOnKilled<Runtime>;
}
impl orml_tokens::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type Amount = Amount;
type CurrencyId = AssetId;
type WeightInfo = weights::tokens::HydraWeight<Runtime>;
type ExistentialDeposits = AssetRegistry;
type CurrencyHooks = CurrencyHooks;
type MaxLocks = MaxLocks;
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
type DustRemovalWhitelist = DustRemovalWhitelist;
}
// The latest versions of the orml-currencies pallet don't emit events.
// The infrastructure relies on the events from this pallet, so we use the latest version of
// the pallet that contains and emit events and was updated to the polkadot version we use.
impl pallet_currencies::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type MultiCurrency = Tokens;
type NativeCurrency = BasicCurrencyAdapter<Runtime, Balances, Amount, BlockNumber>;
type GetNativeCurrencyId = NativeAssetId;
type WeightInfo = weights::currencies::HydraWeight<Runtime>;
}
pub struct RootAsVestingPallet;
impl EnsureOrigin<RuntimeOrigin> for RootAsVestingPallet {
type Success = AccountId;
fn try_origin(o: RuntimeOrigin) -> Result<Self::Success, RuntimeOrigin> {
Into::<Result<RawOrigin<AccountId>, RuntimeOrigin>>::into(o).and_then(|o| match o {
RawOrigin::Root => Ok(VestingPalletId::get().into_account_truncating()),
r => Err(RuntimeOrigin::from(r)),
})
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<RuntimeOrigin, ()> {
let zero_account_id = AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
.expect("infinite length input; no invalid inputs for type; qed");
Ok(RuntimeOrigin::from(RawOrigin::Signed(zero_account_id)))
}
}
parameter_types! {
pub MinVestedTransfer: Balance = 100;
pub const MaxVestingSchedules: u32 = 100;
pub const VestingPalletId: PalletId = PalletId(*b"py/vstng");
}
impl orml_vesting::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type MinVestedTransfer = MinVestedTransfer;
type VestedTransferOrigin = RootAsVestingPallet;
type WeightInfo = weights::vesting::HydraWeight<Runtime>;
type MaxVestingSchedules = MaxVestingSchedules;
type BlockNumberProvider = RelayChainBlockNumberProvider<Runtime>;
}
parameter_types! {
pub ClaimMessagePrefix: &'static [u8] = b"I hereby claim all my HDX tokens to wallet:";
}
impl pallet_claims::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Prefix = ClaimMessagePrefix;
type WeightInfo = weights::claims::HydraWeight<Runtime>;
type Currency = Balances;
type CurrencyBalance = Balance;
}
parameter_types! {
pub const RegistryStrLimit: u32 = 32;
pub const SequentialIdOffset: u32 = 1_000_000;
}
impl pallet_asset_registry::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RegistryOrigin = SuperMajorityTechCommittee;
type AssetId = AssetId;
type Balance = Balance;
type AssetNativeLocation = AssetLocation;
type StringLimit = RegistryStrLimit;
type SequentialIdStartAt = SequentialIdOffset;
type NativeAssetId = NativeAssetId;
type WeightInfo = weights::registry::HydraWeight<Runtime>;
}
parameter_types! {
pub const CollectionDeposit: Balance = 0;
pub const ItemDeposit: Balance = 0;
pub const KeyLimit: u32 = 256; // Max 256 bytes per key
pub const ValueLimit: u32 = 1024; // Max 1024 bytes per value
pub const UniquesMetadataDepositBase: Balance = 1_000 * UNITS;
pub const AttributeDepositBase: Balance = UNITS;
pub const DepositPerByte: Balance = UNITS;
pub const UniquesStringLimit: u32 = 72;
}
impl pallet_uniques::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CollectionId = CollectionId;
type ItemId = ItemId;
type Currency = Balances;
type ForceOrigin = MajorityOfCouncil;
// Standard collection creation is disallowed
type CreateOrigin = AsEnsureOriginWithArg<NeverEnsureOrigin<AccountId>>;
type Locker = ();
type CollectionDeposit = CollectionDeposit;
type ItemDeposit = ItemDeposit;
type MetadataDepositBase = UniquesMetadataDepositBase;
type AttributeDepositBase = AttributeDepositBase;
type DepositPerByte = DepositPerByte;
type StringLimit = UniquesStringLimit;
type KeyLimit = KeyLimit;
type ValueLimit = ValueLimit;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type WeightInfo = ();
}
parameter_types! {
pub const LRNA: AssetId = 1;
pub const MinTradingLimit : Balance = 1_000u128;
pub const MinPoolLiquidity: Balance = 1_000_000u128;
pub const MaxInRatio: Balance = 3u128;
pub const MaxOutRatio: Balance = 3u128;
pub const OmnipoolCollectionId: CollectionId = 1337u128;
pub const EmaOracleSpotPriceLastBlock: OraclePeriod = OraclePeriod::LastBlock;
pub const EmaOracleSpotPriceShort: OraclePeriod = OraclePeriod::Short;
pub const OmnipoolMaxAllowedPriceDifference: Permill = Permill::from_percent(1);
pub MinimumWithdrawalFee: Permill = Permill::from_rational(1u32,10000);
}
impl pallet_omnipool::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type Currency = Currencies;
type AuthorityOrigin = EnsureRoot<AccountId>;
type TechnicalOrigin = SuperMajorityTechCommittee;
type AssetRegistry = AssetRegistry;
type HdxAssetId = NativeAssetId;
type HubAssetId = LRNA;
type MinWithdrawalFee = MinimumWithdrawalFee;
type MinimumTradingLimit = MinTradingLimit;
type MinimumPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type PositionItemId = ItemId;
type CollectionId = CollectionId;
type NFTCollectionId = OmnipoolCollectionId;
type NFTHandler = Uniques;
type WeightInfo = weights::omnipool::HydraWeight<Runtime>;
type OmnipoolHooks = OmnipoolHookAdapter<Self::RuntimeOrigin, NativeAssetId, LRNA, Runtime>;
type PriceBarrier = (
EnsurePriceWithin<
AccountId,
AssetId,
EmaOraclePriceAdapter<EmaOracleSpotPriceLastBlock, Runtime>,
OmnipoolMaxAllowedPriceDifference,
CircuitBreakerWhitelist,
>,
EnsurePriceWithin<
AccountId,
AssetId,
EmaOraclePriceAdapter<EmaOracleSpotPriceShort, Runtime>,
OmnipoolMaxAllowedPriceDifference,
CircuitBreakerWhitelist,
>,
);
type ExternalPriceOracle = EmaOraclePriceAdapter<EmaOracleSpotPriceShort, Runtime>;
type Fee = pallet_dynamic_fees::UpdateAndRetrieveFees<Runtime>;
}
pub struct CircuitBreakerWhitelist;
impl Contains<AccountId> for CircuitBreakerWhitelist {
fn contains(a: &AccountId) -> bool {
<PalletId as AccountIdConversion<AccountId>>::into_account_truncating(&TreasuryPalletId::get()) == *a
}
}
parameter_types! {
pub const DefaultMaxNetTradeVolumeLimitPerBlock: (u32, u32) = (5_000, 10_000); // 50%
pub const DefaultMaxLiquidityLimitPerBlock: Option<(u32, u32)> = Some((500, 10_000)); // 5%
}
impl pallet_circuit_breaker::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type Balance = Balance;
type TechnicalOrigin = SuperMajorityTechCommittee;
type WhitelistedAccounts = CircuitBreakerWhitelist;
type DefaultMaxNetTradeVolumeLimitPerBlock = DefaultMaxNetTradeVolumeLimitPerBlock;
type DefaultMaxAddLiquidityLimitPerBlock = DefaultMaxLiquidityLimitPerBlock;
type DefaultMaxRemoveLiquidityLimitPerBlock = DefaultMaxLiquidityLimitPerBlock;
type OmnipoolHubAsset = LRNA;
type WeightInfo = weights::circuit_breaker::HydraWeight<Runtime>;
}
parameter_types! {
pub SupportedPeriods: BoundedVec<OraclePeriod, ConstU32<{ pallet_ema_oracle::MAX_PERIODS }>> = BoundedVec::truncate_from(vec![
OraclePeriod::LastBlock, OraclePeriod::Short, OraclePeriod::TenMinutes]);
}
impl pallet_ema_oracle::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = weights::ema_oracle::HydraWeight<Runtime>;
/// The definition of the oracle time periods currently assumes a 6 second block time.
/// We use the parachain blocks anyway, because we want certain guarantees over how many blocks correspond
/// to which smoothing factor.
type BlockNumberProvider = System;
type SupportedPeriods = SupportedPeriods;
/// With every asset trading against LRNA we will only have as many pairs as there will be assets, so
/// 40 seems a decent upper bound for the forseeable future.
///
type MaxUniqueEntries = ConstU32<40>;
}
pub struct DustRemovalWhitelist;
impl Contains<AccountId> for DustRemovalWhitelist {
fn contains(a: &AccountId) -> bool {
get_all_module_accounts().contains(a) || pallet_duster::DusterWhitelist::<Runtime>::contains(a)
}
}
parameter_types! {
pub const DustingReward: u128 = 0;
}
impl pallet_duster::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type Amount = Amount;
type CurrencyId = AssetId;
type MultiCurrency = Currencies;
type MinCurrencyDeposits = AssetRegistry;
type Reward = DustingReward;
type NativeCurrencyId = NativeAssetId;
type BlacklistUpdateOrigin = SuperMajorityTechCommittee;
type WeightInfo = ();
}
parameter_types! {
pub const OmniWarehouseLMPalletId: PalletId = PalletId(*b"OmniWhLM");
#[derive(PartialEq, Eq)]
pub const MaxEntriesPerDeposit: u8 = 5; //NOTE: Rebenchmark when this change, TODO:
pub const MaxYieldFarmsPerGlobalFarm: u8 = 50; //NOTE: Includes deleted/destroyed farms, TODO:
pub const MinPlannedYieldingPeriods: BlockNumber = 14_440; //1d with 6s blocks, TODO:
pub const MinTotalFarmRewards: Balance = NATIVE_EXISTENTIAL_DEPOSIT * 100; //TODO:
}
type OmnipoolLiquidityMiningInstance = warehouse_liquidity_mining::Instance1;
impl warehouse_liquidity_mining::Config<OmnipoolLiquidityMiningInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type MultiCurrency = Currencies;
type PalletId = OmniWarehouseLMPalletId;
type MinTotalFarmRewards = MinTotalFarmRewards;
type MinPlannedYieldingPeriods = MinPlannedYieldingPeriods;
type BlockNumberProvider = RelayChainBlockNumberProvider<Runtime>;
type AmmPoolId = AssetId;
type MaxFarmEntriesPerDeposit = MaxEntriesPerDeposit;
type MaxYieldFarmsPerGlobalFarm = MaxYieldFarmsPerGlobalFarm;
type AssetRegistry = AssetRegistry;
type NonDustableWhitelistHandler = Duster;
type PriceAdjustment = PriceAdjustmentAdapter<Runtime, OmnipoolLiquidityMiningInstance>;
}
parameter_types! {
pub const OmniLMPalletId: PalletId = PalletId(*b"Omni//LM");
pub const OmnipoolLMCollectionId: CollectionId = 2584_u128;
pub const OmnipoolLMOraclePeriod: OraclePeriod = OraclePeriod::TenMinutes;
pub const OmnipoolLMOracleSource: Source = OMNIPOOL_SOURCE;
}
impl pallet_omnipool_liquidity_mining::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Currencies;
type CreateOrigin = AllTechnicalCommitteeMembers;
type PalletId = OmniLMPalletId;
type NFTCollectionId = OmnipoolLMCollectionId;
type NFTHandler = Uniques;
type LiquidityMiningHandler = OmnipoolWarehouseLM;
type OracleSource = OmnipoolLMOracleSource;
type OraclePeriod = OmnipoolLMOraclePeriod;
type PriceOracle = EmaOracle;
type WeightInfo = weights::omnipool_lm::HydraWeight<Runtime>;
}
// The reason why there is difference between PROD and benchmark is that it is not possible
// to set validation data in parachain system pallet in the benchmarks.
// So for benchmarking, we mock it out and return some hardcoded parent hash
pub struct RelayChainBlockHashProviderAdapter<Runtime>(sp_std::marker::PhantomData<Runtime>);
#[cfg(not(feature = "runtime-benchmarks"))]
impl<Runtime> RelayChainBlockHashProvider for RelayChainBlockHashProviderAdapter<Runtime>
where
Runtime: cumulus_pallet_parachain_system::Config,
{
fn parent_hash() -> Option<cumulus_primitives_core::relay_chain::Hash> {
let validation_data = cumulus_pallet_parachain_system::Pallet::<Runtime>::validation_data();
match validation_data {
Some(data) => Some(data.parent_head.hash()),
None => None,
}
}
}
#[cfg(feature = "runtime-benchmarks")]
impl<Runtime> RelayChainBlockHashProvider for RelayChainBlockHashProviderAdapter<Runtime>
where
Runtime: cumulus_pallet_parachain_system::Config,
{
fn parent_hash() -> Option<cumulus_primitives_core::relay_chain::Hash> {
None
}
}
use hydradx_traits::pools::SpotPriceProvider;
#[cfg(feature = "runtime-benchmarks")]
use hydradx_traits::PriceOracle;
#[cfg(feature = "runtime-benchmarks")]
use hydra_dx_math::ema::EmaPrice;
#[cfg(feature = "runtime-benchmarks")]
pub struct DummyOraclePriceProvider;
#[cfg(feature = "runtime-benchmarks")]
impl PriceOracle<AssetId> for DummyOraclePriceProvider {
type Price = EmaPrice;
fn price(_route: &[Trade<AssetId>], _period: OraclePeriod) -> Option<Self::Price> {
Some(EmaPrice::one())
}
}
#[cfg(not(feature = "runtime-benchmarks"))]
use hydradx_adapters::OraclePriceProvider;
#[cfg(feature = "runtime-benchmarks")]
pub struct DummySpotPriceProvider;
#[cfg(feature = "runtime-benchmarks")]
impl SpotPriceProvider<AssetId> for DummySpotPriceProvider {
type Price = FixedU128;
fn pair_exists(_asset_a: AssetId, _asset_b: AssetId) -> bool {
true
}
fn spot_price(_asset_a: AssetId, _asset_b: AssetId) -> Option<Self::Price> {
Some(FixedU128::one())
}
}
parameter_types! {
pub MinBudgetInNativeCurrency: Balance = 1000 * UNITS;
pub MaxSchedulesPerBlock: u32 = 20;
pub MaxPriceDifference: Permill = Permill::from_rational(15u32, 1000u32);
pub NamedReserveId: NamedReserveIdentifier = *b"dcaorder";
pub MaxNumberOfRetriesOnError: u8 = 3;
pub DCAOraclePeriod: OraclePeriod = OraclePeriod::Short;
}
impl pallet_dca::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type TechnicalOrigin = SuperMajorityTechCommittee;
type Currencies = Currencies;
type RelayChainBlockHashProvider = RelayChainBlockHashProviderAdapter<Runtime>;
type RandomnessProvider = DCA;
#[cfg(not(feature = "runtime-benchmarks"))]
type OraclePriceProvider = OraclePriceProvider<AssetId, EmaOracle, LRNA>;
#[cfg(feature = "runtime-benchmarks")]
type OraclePriceProvider = DummyOraclePriceProvider;
#[cfg(not(feature = "runtime-benchmarks"))]
type RouteExecutor = Router;
#[cfg(feature = "runtime-benchmarks")]
type RouteExecutor = pallet_route_executor::DummyRouter<Runtime>;
type RouteProvider = Router;
type MaxPriceDifferenceBetweenBlocks = MaxPriceDifference;
type MaxSchedulePerBlock = MaxSchedulesPerBlock;
type MaxNumberOfRetriesOnError = MaxNumberOfRetriesOnError;
type NativeAssetId = NativeAssetId;
type MinBudgetInNativeCurrency = MinBudgetInNativeCurrency;
type MinimumTradingLimit = MinTradingLimit;
type FeeReceiver = TreasuryAccount;
type NamedReserveId = NamedReserveId;
type WeightToFee = WeightToFee;
type AmmTradeWeights = RouterWeightInfo;
type WeightInfo = weights::dca::HydraWeight<Runtime>;
#[cfg(not(feature = "runtime-benchmarks"))]
type NativePriceOracle = AssetFeeOraclePriceProvider<
NativeAssetId,
MultiTransactionPayment,
Router,
OraclePriceProvider<AssetId, EmaOracle, LRNA>,
MultiTransactionPayment,
DCAOraclePeriod,
>;
#[cfg(feature = "runtime-benchmarks")]
type NativePriceOracle = AssetFeeOraclePriceProvider<
NativeAssetId,
MultiTransactionPayment,
Router,
DummyOraclePriceProvider,
MultiTransactionPayment,
DCAOraclePeriod,
>;
}
// Provides weight info for the router. Router extrinsics can be executed with different AMMs, so we split the router weights into two parts:
// the router extrinsic overhead and the AMM weight.
pub struct RouterWeightInfo;
// Calculates the overhead of Router extrinsics. To do that, we benchmark Router::sell with single LBP trade and subtract the weight of LBP::sell.
// This allows us to calculate the weight of any route by adding the weight of AMM trades to the overhead of a router extrinsic.
impl RouterWeightInfo {
pub fn sell_and_calculate_sell_trade_amounts_overhead_weight(
num_of_calc_sell: u32,
num_of_execute_sell: u32,
) -> Weight {
weights::route_executor::HydraWeight::<Runtime>::calculate_and_execute_sell_in_lbp(num_of_calc_sell)
.saturating_sub(weights::lbp::HydraWeight::<Runtime>::router_execution_sell(
num_of_calc_sell.saturating_add(num_of_execute_sell),
num_of_execute_sell,
))
}
pub fn buy_and_calculate_buy_trade_amounts_overhead_weight(
num_of_calc_buy: u32,
num_of_execute_buy: u32,
) -> Weight {
let router_weight = weights::route_executor::HydraWeight::<Runtime>::calculate_and_execute_buy_in_lbp(
num_of_calc_buy,
num_of_execute_buy,
);
// Handle this case separately. router_execution_buy provides incorrect weight for the case when only calculate_buy is executed.
let lbp_weight = if (num_of_calc_buy, num_of_execute_buy) == (1, 0) {
weights::lbp::HydraWeight::<Runtime>::calculate_buy()
} else {
weights::lbp::HydraWeight::<Runtime>::router_execution_buy(
num_of_calc_buy.saturating_add(num_of_execute_buy),
num_of_execute_buy,
)
};
router_weight.saturating_sub(lbp_weight)
}
pub fn set_route_overweight() -> Weight {
let number_of_times_calculate_sell_amounts_executed = 5; //4 calculations + in the validation
let number_of_times_execute_sell_amounts_executed = 0; //We do have it once executed in the validation of the route, but it is without writing to database (as rolled back), and since we pay back successful set_route, we just keep this overhead
let set_route_overweight = weights::route_executor::HydraWeight::<Runtime>::set_route_for_xyk();
set_route_overweight.saturating_sub(weights::xyk::HydraWeight::<Runtime>::router_execution_sell(
number_of_times_calculate_sell_amounts_executed,
number_of_times_execute_sell_amounts_executed,
))
}
}
impl AmmTradeWeights<Trade<AssetId>> for RouterWeightInfo {
// Used in Router::sell extrinsic, which calls AMM::calculate_sell and AMM::execute_sell
fn sell_weight(route: &[Trade<AssetId>]) -> Weight {
let mut weight = Weight::zero();
let c = 1; // number of times AMM::calculate_sell is executed
let e = 1; // number of times AMM::execute_sell is executed
for trade in route {
weight.saturating_accrue(Self::sell_and_calculate_sell_trade_amounts_overhead_weight(0, 1));
let amm_weight = match trade.pool {
PoolType::Omnipool => weights::omnipool::HydraWeight::<Runtime>::router_execution_sell(c, e)
.saturating_add(
<OmnipoolHookAdapter<RuntimeOrigin, NativeAssetId, LRNA, Runtime> as OmnipoolHooks<
RuntimeOrigin,
AccountId,
AssetId,
Balance,
>>::on_trade_weight(),
)
.saturating_add(
<OmnipoolHookAdapter<RuntimeOrigin, NativeAssetId, LRNA, Runtime> as OmnipoolHooks<
RuntimeOrigin,
AccountId,
AssetId,
Balance,
>>::on_liquidity_changed_weight(),
),
PoolType::LBP => weights::lbp::HydraWeight::<Runtime>::router_execution_sell(c, e),
PoolType::Stableswap(_) => weights::stableswap::HydraWeight::<Runtime>::router_execution_sell(c, e),
PoolType::XYK => weights::xyk::HydraWeight::<Runtime>::router_execution_sell(c, e)
.saturating_add(<Runtime as pallet_xyk::Config>::AMMHandler::on_trade_weight()),
};
weight.saturating_accrue(amm_weight);
}
weight
}
// Used in Router::buy extrinsic, which calls AMM::calculate_buy and AMM::execute_buy
fn buy_weight(route: &[Trade<AssetId>]) -> Weight {
let mut weight = Weight::zero();
let c = 1; // number of times AMM::calculate_buy is executed
let e = 1; // number of times AMM::execute_buy is executed
for trade in route {
weight.saturating_accrue(Self::buy_and_calculate_buy_trade_amounts_overhead_weight(0, 1));
let amm_weight = match trade.pool {
PoolType::Omnipool => weights::omnipool::HydraWeight::<Runtime>::router_execution_buy(c, e)
.saturating_add(
<OmnipoolHookAdapter<RuntimeOrigin, NativeAssetId, LRNA, Runtime> as OmnipoolHooks<
RuntimeOrigin,
AccountId,
AssetId,
Balance,
>>::on_trade_weight(),
)
.saturating_add(
<OmnipoolHookAdapter<RuntimeOrigin, NativeAssetId, LRNA, Runtime> as OmnipoolHooks<
RuntimeOrigin,
AccountId,
AssetId,
Balance,
>>::on_liquidity_changed_weight(),
),
PoolType::LBP => weights::lbp::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::Stableswap(_) => weights::stableswap::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::XYK => weights::xyk::HydraWeight::<Runtime>::router_execution_buy(c, e)
.saturating_add(<Runtime as pallet_xyk::Config>::AMMHandler::on_trade_weight()),
};
weight.saturating_accrue(amm_weight);
}
weight
}
// Used in DCA::schedule extrinsic, which calls Router::calculate_buy_trade_amounts
fn calculate_buy_trade_amounts_weight(route: &[Trade<AssetId>]) -> Weight {
let mut weight = Weight::zero();
let c = 1; // number of times AMM::calculate_buy is executed
let e = 0; // number of times AMM::execute_buy is executed
for trade in route {
weight.saturating_accrue(Self::buy_and_calculate_buy_trade_amounts_overhead_weight(1, 0));
let amm_weight = match trade.pool {
PoolType::Omnipool => weights::omnipool::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::LBP => weights::lbp::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::Stableswap(_) => weights::stableswap::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::XYK => weights::xyk::HydraWeight::<Runtime>::router_execution_buy(c, e)
.saturating_add(<Runtime as pallet_xyk::Config>::AMMHandler::on_trade_weight()),
};
weight.saturating_accrue(amm_weight);
}
weight
}
// Used in DCA::on_initialize for Order::Sell, which calls Router::calculate_sell_trade_amounts and Router::sell.
fn sell_and_calculate_sell_trade_amounts_weight(route: &[Trade<AssetId>]) -> Weight {
let mut weight = Weight::zero();
let c = 2; // number of times AMM::calculate_sell is executed
let e = 1; // number of times AMM::execute_sell is executed
for trade in route {
weight.saturating_accrue(Self::sell_and_calculate_sell_trade_amounts_overhead_weight(1, 1));
let amm_weight = match trade.pool {
PoolType::Omnipool => weights::omnipool::HydraWeight::<Runtime>::router_execution_sell(c, e),
PoolType::LBP => weights::lbp::HydraWeight::<Runtime>::router_execution_sell(c, e),
PoolType::Stableswap(_) => weights::stableswap::HydraWeight::<Runtime>::router_execution_sell(c, e),
PoolType::XYK => weights::xyk::HydraWeight::<Runtime>::router_execution_sell(c, e)
.saturating_add(<Runtime as pallet_xyk::Config>::AMMHandler::on_trade_weight()),
};
weight.saturating_accrue(amm_weight);
}
weight
}
// Used in DCA::on_initialize for Order::Buy, which calls 2 * Router::calculate_buy_trade_amounts and Router::buy.
fn buy_and_calculate_buy_trade_amounts_weight(route: &[Trade<AssetId>]) -> Weight {
let mut weight = Weight::zero();
let c = 3; // number of times AMM::calculate_buy is executed
let e = 1; // number of times AMM::execute_buy is executed
for trade in route {
weight.saturating_accrue(Self::buy_and_calculate_buy_trade_amounts_overhead_weight(2, 1));
let amm_weight = match trade.pool {
PoolType::Omnipool => weights::omnipool::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::LBP => weights::lbp::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::Stableswap(_) => weights::stableswap::HydraWeight::<Runtime>::router_execution_buy(c, e),
PoolType::XYK => weights::xyk::HydraWeight::<Runtime>::router_execution_buy(c, e)
.saturating_add(<Runtime as pallet_xyk::Config>::AMMHandler::on_trade_weight()),
};
weight.saturating_accrue(amm_weight);
}
weight
}
fn set_route_weight(route: &[Trade<AssetId>]) -> Weight {
let mut weight = Weight::zero();
//We ignore the calls for AMM:get_liquidty_depth, as the same logic happens in AMM calculation/execution
//Overweight
weight.saturating_accrue(Self::set_route_overweight());
//Add a sell weight as we do a dry-run sell as validation
weight.saturating_accrue(Self::sell_weight(route));
//For the stored route we expect a worst case with max number of trades in the most expensive pool which is stableswap
//We have have two sell calculation for that, normal and inverse
weights::stableswap::HydraWeight::<Runtime>::router_execution_sell(2, 0)
.checked_mul(MAX_NUMBER_OF_TRADES.into());
//Calculate sell amounts for the new route
for trade in route {
let amm_weight = match trade.pool {
PoolType::Omnipool => weights::omnipool::HydraWeight::<Runtime>::router_execution_sell(1, 0),
PoolType::LBP => weights::lbp::HydraWeight::<Runtime>::router_execution_sell(1, 0),
PoolType::Stableswap(_) => weights::stableswap::HydraWeight::<Runtime>::router_execution_sell(1, 0),
PoolType::XYK => weights::xyk::HydraWeight::<Runtime>::router_execution_sell(1, 0),
};
weight.saturating_accrue(amm_weight);
}
//Calculate sell amounts for the inversed new route
for trade in inverse_route(route.to_vec()) {
let amm_weight = match trade.pool {
PoolType::Omnipool => weights::omnipool::HydraWeight::<Runtime>::router_execution_sell(1, 0),
PoolType::LBP => weights::lbp::HydraWeight::<Runtime>::router_execution_sell(1, 0),
PoolType::Stableswap(_) => weights::stableswap::HydraWeight::<Runtime>::router_execution_sell(1, 0),
PoolType::XYK => weights::xyk::HydraWeight::<Runtime>::router_execution_sell(1, 0),
};
weight.saturating_accrue(amm_weight);
}
weight
}
}
parameter_types! {
pub const DefaultRoutePoolType: PoolType<AssetId> = PoolType::Omnipool;
}
impl pallet_route_executor::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type Balance = Balance;
type Currency = FungibleCurrencies<Runtime>;
type WeightInfo = RouterWeightInfo;
type AMM = (Omnipool, Stableswap, XYK, LBP);
type DefaultRoutePoolType = DefaultRoutePoolType;
type NativeAssetId = NativeAssetId;
}
parameter_types! {
pub const ExistentialDepositMultiplier: u8 = 5;
}
impl pallet_otc::Config for Runtime {
type AssetId = AssetId;
type AssetRegistry = AssetRegistry;
type Currency = Currencies;
type RuntimeEvent = RuntimeEvent;
type ExistentialDeposits = AssetRegistry;
type ExistentialDepositMultiplier = ExistentialDepositMultiplier;
type WeightInfo = weights::otc::HydraWeight<Runtime>;
}
// Dynamic fees
parameter_types! {
pub AssetFeeParams: FeeParams<Permill> = FeeParams{
min_fee: Permill::from_rational(25u32,10000u32), // 0.25%
max_fee: Permill::from_rational(5u32,100u32), // 5%
decay: FixedU128::from_rational(1,100000), // 0.001%
amplification: FixedU128::from(2), // 2
};
pub ProtocolFeeParams: FeeParams<Permill> = FeeParams{
min_fee: Permill::from_rational(5u32,10000u32), // 0.05%
max_fee: Permill::from_rational(1u32,1000u32), // 0.1%
decay: FixedU128::from_rational(5,1000000), // 0.0005%
amplification: FixedU128::one(), // 1
};
pub const DynamicFeesOraclePeriod: OraclePeriod = OraclePeriod::Short;
}
impl pallet_dynamic_fees::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type BlockNumberProvider = System;
type Fee = Permill;
type AssetId = AssetId;
type Oracle = OracleAssetVolumeProvider<Runtime, LRNA, DynamicFeesOraclePeriod>;
type AssetFeeParameters = AssetFeeParams;
type ProtocolFeeParameters = ProtocolFeeParams;
}
// Stableswap
parameter_types! {
pub StableswapAmplificationRange: RangeInclusive<NonZeroU16> = RangeInclusive::new(NonZeroU16::new(2).unwrap(), NonZeroU16::new(10_000).unwrap());
}
pub struct StableswapAccountIdConstructor<T: frame_system::Config>(PhantomData<T>);
impl<T: frame_system::Config> AccountIdFor<AssetId> for StableswapAccountIdConstructor<T>
where
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
{
type AccountId = T::AccountId;
fn from_assets(asset: &AssetId, identifier: Option<&[u8]>) -> Self::AccountId {
let name = Self::name(asset, identifier);
T::AccountId::unchecked_from(<T::Hashing as frame_support::sp_runtime::traits::Hash>::hash(&name[..]))
}
fn name(asset: &u32, identifier: Option<&[u8]>) -> Vec<u8> {
let mut buf = identifier.map_or_else(Vec::new, |v| v.to_vec());
buf.extend_from_slice(&(asset).to_le_bytes());
buf
}
}
use pallet_currencies::fungibles::FungibleCurrencies;
#[cfg(not(feature = "runtime-benchmarks"))]
use hydradx_adapters::price::OraclePriceProviderUsingRoute;
#[cfg(feature = "runtime-benchmarks")]
use hydradx_traits::price::PriceProvider;
use pallet_referrals::traits::Convert;
use pallet_referrals::{FeeDistribution, Level};
#[cfg(feature = "runtime-benchmarks")]
use pallet_stableswap::BenchmarkHelper;
#[cfg(feature = "runtime-benchmarks")]
use sp_runtime::DispatchResult;
#[cfg(feature = "runtime-benchmarks")]
pub struct RegisterAsset<T>(PhantomData<T>);
#[cfg(feature = "runtime-benchmarks")]
impl<T: pallet_asset_registry::Config> BenchmarkHelper<AssetId> for RegisterAsset<T> {
fn register_asset(asset_id: AssetId, decimals: u8) -> DispatchResult {
let asset_name = asset_id.to_le_bytes().to_vec();
let name: BoundedVec<u8, RegistryStrLimit> = asset_name
.clone()
.try_into()
.map_err(|_| pallet_asset_registry::Error::<T>::TooLong)?;
AssetRegistry::register_asset(
name,
pallet_asset_registry::AssetType::<AssetId>::Token,
1,
Some(asset_id),
None,
)?;
AssetRegistry::set_metadata(RuntimeOrigin::root(), asset_id, asset_name, decimals)?;
Ok(())
}
}
impl pallet_stableswap::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type BlockNumberProvider = System;
type AssetId = AssetId;
type Currency = Currencies;
type ShareAccountId = StableswapAccountIdConstructor<Runtime>;
type AssetInspection = AssetRegistry;
type AuthorityOrigin = EnsureRoot<AccountId>;
type DustAccountHandler = Duster;
type Hooks = StableswapHooksAdapter<Runtime>;
type MinPoolLiquidity = MinPoolLiquidity;
type MinTradingLimit = MinTradingLimit;
type AmplificationRange = StableswapAmplificationRange;
type WeightInfo = weights::stableswap::HydraWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = RegisterAsset<Runtime>;
}
// Bonds
parameter_types! {
pub ProtocolFee: Permill = Permill::from_percent(2);
pub const BondsPalletId: PalletId = PalletId(*b"pltbonds");
}
pub struct AssetTypeWhitelist;
impl Contains<AssetKind> for AssetTypeWhitelist {
fn contains(t: &AssetKind) -> bool {
matches!(t, AssetKind::Token | AssetKind::XYK | AssetKind::StableSwap)
}
}
impl pallet_bonds::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type Currency = Currencies;
type AssetRegistry = AssetRegistry;
type ExistentialDeposits = AssetRegistry;
type TimestampProvider = Timestamp;
type PalletId = BondsPalletId;
type IssueOrigin = EnsureSigned<AccountId>;
type AssetTypeWhitelist = AssetTypeWhitelist;
type ProtocolFee = ProtocolFee;
type FeeReceiver = TreasuryAccount;
type WeightInfo = weights::bonds::HydraWeight<Runtime>;
}
// Staking
parameter_types! {
pub const StakingPalletId: PalletId = PalletId(*b"staking#");
pub const MinStake: Balance = 1_000 * UNITS;
pub const PeriodLength: BlockNumber = DAYS;
pub const TimePointsW:Permill = Permill::from_percent(100);
pub const ActionPointsW: Perbill = Perbill::from_percent(20);
pub const TimePointsPerPeriod: u8 = 1;
pub const CurrentStakeWeight: u8 = 2;
pub const UnclaimablePeriods: BlockNumber = 1;
pub const PointPercentage: FixedU128 = FixedU128::from_rational(2,100);
}
pub struct PointsPerAction;
impl GetByKey<Action, u32> for PointsPerAction {
fn get(k: &Action) -> u32 {
match k {
Action::DemocracyVote => 100_u32,
}
}
}
impl pallet_staking::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AuthorityOrigin = MajorityOfCouncil;
type AssetId = AssetId;
type Currency = Currencies;
type PeriodLength = PeriodLength;
type PalletId = StakingPalletId;
type NativeAssetId = NativeAssetId;
type MinStake = MinStake;
type TimePointsWeight = TimePointsW;
type ActionPointsWeight = ActionPointsW;
type TimePointsPerPeriod = TimePointsPerPeriod;
type UnclaimablePeriods = UnclaimablePeriods;
type CurrentStakeWeight = CurrentStakeWeight;
type PayablePercentage = SigmoidPercentage<PointPercentage, ConstU32<2_000>>;
type BlockNumberProvider = System;
type PositionItemId = u128;
type CollectionId = u128;
type NFTCollectionId = ConstU128<2222>;
type Collections = FreezableNFT<Runtime, Self::RuntimeOrigin>;
type NFTHandler = Uniques;
type MaxVotes = MaxVotes;
type ReferendumInfo = pallet_staking::integrations::democracy::ReferendumStatus<Runtime>;
type MaxPointsPerAction = PointsPerAction;
type Vesting = VestingInfo<Runtime>;
type WeightInfo = weights::staking::HydraWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type MaxLocks = MaxLocks;
}
// LBP
pub struct AssetPairAccountId<T: frame_system::Config>(PhantomData<T>);
impl<T: frame_system::Config> AssetPairAccountIdFor<AssetId, T::AccountId> for AssetPairAccountId<T>
where
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
{
fn from_assets(asset_a: AssetId, asset_b: AssetId, identifier: &str) -> T::AccountId {
let mut buf: Vec<u8> = identifier.as_bytes().to_vec();
if asset_a < asset_b {
buf.extend_from_slice(&asset_a.to_le_bytes());
buf.extend_from_slice(&asset_b.to_le_bytes());