generated from substrate-developer-hub/substrate-node-template
-
Notifications
You must be signed in to change notification settings - Fork 60
/
lib.rs
637 lines (540 loc) · 19.7 KB
/
lib.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
// This file is part of HydraDX.
// Copyright (C) 2020-2022 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.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::traits::{Contains, EnsureOrigin};
use frame_support::weights::Weight;
use frame_support::{ensure, pallet_prelude::DispatchResult, traits::Get};
use frame_system::ensure_signed_or_root;
use frame_system::pallet_prelude::{BlockNumberFor, OriginFor};
use scale_info::TypeInfo;
use sp_core::MaxEncodedLen;
use sp_runtime::traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Zero};
use sp_runtime::{ArithmeticError, DispatchError, RuntimeDebug};
pub mod weights;
#[cfg(any(feature = "runtime-benchmarks", test))]
mod benchmarking;
#[cfg(test)]
mod tests;
/// Max trade volume limit multiplier of liquidity that can be traded in a block
pub const MAX_LIMIT_VALUE: u32 = 10_000;
#[derive(Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo, Eq, PartialEq)]
#[scale_info(skip_type_params(T))]
pub struct TradeVolumeLimit<T: Config> {
pub volume_in: T::Balance,
pub volume_out: T::Balance,
pub limit: T::Balance,
}
impl<T: Config> TradeVolumeLimit<T>
where
T::Balance: PartialOrd,
{
pub fn update_amounts(&mut self, amount_in: T::Balance, amount_out: T::Balance) -> DispatchResult {
self.volume_in = self
.volume_in
.checked_add(&amount_in)
.ok_or(ArithmeticError::Overflow)?;
self.volume_out = self
.volume_out
.checked_add(&amount_out)
.ok_or(ArithmeticError::Overflow)?;
Ok(())
}
pub fn check_outflow_limit(&self) -> DispatchResult {
if self.volume_out > self.volume_in {
let diff = self
.volume_out
.checked_sub(&self.volume_in)
.ok_or(ArithmeticError::Underflow)?;
ensure!(diff <= self.limit, Error::<T>::TokenOutflowLimitReached);
}
Ok(())
}
pub fn check_influx_limit(&self) -> DispatchResult {
if self.volume_in > self.volume_out {
let diff = self
.volume_in
.checked_sub(&self.volume_out)
.ok_or(ArithmeticError::Underflow)?;
ensure!(diff <= self.limit, Error::<T>::TokenInfluxLimitReached);
}
Ok(())
}
pub fn check_limits(&self) -> DispatchResult {
self.check_outflow_limit()?;
self.check_influx_limit()?;
Ok(())
}
}
#[derive(Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo, Eq, PartialEq)]
#[scale_info(skip_type_params(T))]
pub struct LiquidityLimit<T: Config> {
pub liquidity: T::Balance,
pub limit: T::Balance,
}
impl<T: Config> LiquidityLimit<T>
where
T::Balance: PartialOrd,
{
pub fn update_amount(&mut self, liquidity_in: T::Balance) -> DispatchResult {
self.liquidity = self
.liquidity
.checked_add(&liquidity_in)
.ok_or(ArithmeticError::Overflow)?;
Ok(())
}
pub fn check_limit(&self) -> DispatchResult {
ensure!(
self.liquidity <= self.limit,
Error::<T>::MaxLiquidityLimitPerBlockReached
);
Ok(())
}
}
// Re-export pallet items so that they can be accessed from the crate namespace.
pub use pallet::*;
pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use codec::HasCompact;
use frame_support::pallet_prelude::*;
use frame_support::traits::Contains;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
T::WeightInfo::on_finalize(0, 0)
}
fn on_finalize(_n: BlockNumberFor<T>) {
let _ = <AllowedTradeVolumeLimitPerAsset<T>>::clear(u32::MAX, None);
let _ = <AllowedAddLiquidityAmountPerAsset<T>>::clear(u32::MAX, None);
let _ = <AllowedRemoveLiquidityAmountPerAsset<T>>::clear(u32::MAX, None);
}
fn integrity_test() {
assert!(
Self::validate_limit(T::DefaultMaxNetTradeVolumeLimitPerBlock::get()).is_ok(),
"Circuit Breaker: Max net trade volume limit per block is set to invalid value."
);
if let Some(liquidity_limit) = T::DefaultMaxAddLiquidityLimitPerBlock::get() {
assert!(
Self::validate_limit(liquidity_limit).is_ok(),
"Circuit Breaker: Max add liquidity limit per block is set to invalid value."
);
}
if let Some(liquidity_limit) = T::DefaultMaxRemoveLiquidityLimitPerBlock::get() {
assert!(
Self::validate_limit(liquidity_limit).is_ok(),
"Circuit Breaker: Max remove liquidity limit per block is set to invalid value."
);
}
}
}
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Identifier for the class of asset.
type AssetId: Member
+ Parameter
+ Default
+ Copy
+ HasCompact
+ MaybeSerializeDeserialize
+ MaxEncodedLen
+ TypeInfo
+ AtLeast32BitUnsigned;
/// Balance type.
type Balance: Parameter
+ Member
+ Copy
+ PartialOrd
+ MaybeSerializeDeserialize
+ Default
+ CheckedAdd
+ CheckedSub
+ AtLeast32BitUnsigned
+ MaxEncodedLen
+ From<u128>;
/// Origin able to change the trade volume limit of an asset.
type TechnicalOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// List of accounts that bypass checks for adding/removing liquidity. Root is always whitelisted
type WhitelistedAccounts: Contains<Self::AccountId>;
/// The maximum percentage of a pool's liquidity that can be traded in a block.
/// Represented as a non-zero fraction (nominator, denominator) with the max value being 10_000.
#[pallet::constant]
type DefaultMaxNetTradeVolumeLimitPerBlock: Get<(u32, u32)>;
/// The maximum percentage of a pool's liquidity that can be added in a block.
/// Represented as an optional non-zero fraction (nominator, denominator) with the max value being 10_000.
/// If set to None, the limits are not enforced.
#[pallet::constant]
type DefaultMaxAddLiquidityLimitPerBlock: Get<Option<(u32, u32)>>;
/// The maximum percentage of a pool's liquidity that can be removed in a block.
/// Represented as an optional non-zero fraction (nominator, denominator) with the max value being 10_000.
/// If set to None, the limits are not enforced.
#[pallet::constant]
type DefaultMaxRemoveLiquidityLimitPerBlock: Get<Option<(u32, u32)>>;
/// Omnipool's hub asset id. The limits are not tracked for this asset.
type OmnipoolHubAsset: Get<Self::AssetId>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(_);
/// Default maximum net trade volume limit per block
#[pallet::type_value]
pub fn DefaultTradeVolumeLimit<T: Config>() -> (u32, u32) {
T::DefaultMaxNetTradeVolumeLimitPerBlock::get()
}
#[pallet::storage]
/// Trade volume limits of assets set by set_trade_volume_limit.
/// If not set, returns the default limit.
#[pallet::getter(fn trade_volume_limit_per_asset)]
pub type TradeVolumeLimitPerAsset<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, (u32, u32), ValueQuery, DefaultTradeVolumeLimit<T>>;
#[pallet::storage]
/// Trade volumes per asset
#[pallet::getter(fn allowed_trade_volume_limit_per_asset)]
pub type AllowedTradeVolumeLimitPerAsset<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, TradeVolumeLimit<T>>;
/// Default maximum add liquidity limit per block
#[pallet::type_value]
pub fn DefaultAddLiquidityLimit<T: Config>() -> Option<(u32, u32)> {
T::DefaultMaxAddLiquidityLimitPerBlock::get()
}
#[pallet::storage]
/// Liquidity limits of assets for adding liquidity.
/// If not set, returns the default limit.
#[pallet::getter(fn add_liquidity_limit_per_asset)]
pub type LiquidityAddLimitPerAsset<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, Option<(u32, u32)>, ValueQuery, DefaultAddLiquidityLimit<T>>;
#[pallet::storage]
/// Add liquidity volumes per asset
#[pallet::getter(fn allowed_add_liquidity_limit_per_asset)]
pub type AllowedAddLiquidityAmountPerAsset<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, LiquidityLimit<T>>;
/// Default maximum remove liquidity limit per block
#[pallet::type_value]
pub fn DefaultRemoveLiquidityLimit<T: Config>() -> Option<(u32, u32)> {
T::DefaultMaxRemoveLiquidityLimitPerBlock::get()
}
#[pallet::storage]
/// Liquidity limits of assets for removing liquidity.
/// If not set, returns the default limit.
#[pallet::getter(fn remove_liquidity_limit_per_asset)]
pub type LiquidityRemoveLimitPerAsset<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, Option<(u32, u32)>, ValueQuery, DefaultRemoveLiquidityLimit<T>>;
#[pallet::storage]
/// Remove liquidity volumes per asset
#[pallet::getter(fn allowed_remove_liquidity_limit_per_asset)]
pub type AllowedRemoveLiquidityAmountPerAsset<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, LiquidityLimit<T>>;
#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// Trade volume limit of an asset was changed.
TradeVolumeLimitChanged {
asset_id: T::AssetId,
trade_volume_limit: (u32, u32),
},
/// Add liquidity limit of an asset was changed.
AddLiquidityLimitChanged {
asset_id: T::AssetId,
liquidity_limit: Option<(u32, u32)>,
},
/// Remove liquidity limit of an asset was changed.
RemoveLiquidityLimitChanged {
asset_id: T::AssetId,
liquidity_limit: Option<(u32, u32)>,
},
}
#[pallet::error]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Error<T> {
/// Invalid value for a limit. Limit must be non-zero.
InvalidLimitValue,
/// Allowed liquidity limit is not stored for asset
LiquidityLimitNotStoredForAsset,
/// Token trade outflow per block has been reached
TokenOutflowLimitReached,
/// Token trade influx per block has been reached
TokenInfluxLimitReached,
/// Maximum pool's liquidity limit per block has been reached
MaxLiquidityLimitPerBlockReached,
/// Asset is not allowed to have a limit
NotAllowed,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set trade volume limit for an asset.
///
/// Parameters:
/// - `origin`: The dispatch origin for this call. Must be `TechnicalOrigin`
/// - `asset_id`: The identifier of an asset
/// - `trade_volume_limit`: New trade volume limit represented as a percentage
///
/// Emits `TradeVolumeLimitChanged` event when successful.
///
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::set_trade_volume_limit())]
pub fn set_trade_volume_limit(
origin: OriginFor<T>,
asset_id: T::AssetId,
trade_volume_limit: (u32, u32),
) -> DispatchResult {
T::TechnicalOrigin::ensure_origin(origin)?;
ensure!(asset_id != T::OmnipoolHubAsset::get(), Error::<T>::NotAllowed);
Self::validate_limit(trade_volume_limit)?;
<TradeVolumeLimitPerAsset<T>>::insert(asset_id, trade_volume_limit);
Self::deposit_event(Event::TradeVolumeLimitChanged {
asset_id,
trade_volume_limit,
});
Ok(())
}
/// Set add liquidity limit for an asset.
///
/// Parameters:
/// - `origin`: The dispatch origin for this call. Must be `TechnicalOrigin`
/// - `asset_id`: The identifier of an asset
/// - `liquidity_limit`: Optional add liquidity limit represented as a percentage
///
/// Emits `AddLiquidityLimitChanged` event when successful.
///
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::set_add_liquidity_limit())]
pub fn set_add_liquidity_limit(
origin: OriginFor<T>,
asset_id: T::AssetId,
liquidity_limit: Option<(u32, u32)>,
) -> DispatchResult {
T::TechnicalOrigin::ensure_origin(origin)?;
ensure!(asset_id != T::OmnipoolHubAsset::get(), Error::<T>::NotAllowed);
if let Some(limit) = liquidity_limit {
Self::validate_limit(limit)?;
}
<LiquidityAddLimitPerAsset<T>>::insert(asset_id, liquidity_limit);
Self::deposit_event(Event::AddLiquidityLimitChanged {
asset_id,
liquidity_limit,
});
Ok(())
}
/// Set remove liquidity limit for an asset.
///
/// Parameters:
/// - `origin`: The dispatch origin for this call. Must be `TechnicalOrigin`
/// - `asset_id`: The identifier of an asset
/// - `liquidity_limit`: Optional remove liquidity limit represented as a percentage
///
/// Emits `RemoveLiquidityLimitChanged` event when successful.
///
#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::set_remove_liquidity_limit())]
pub fn set_remove_liquidity_limit(
origin: OriginFor<T>,
asset_id: T::AssetId,
liquidity_limit: Option<(u32, u32)>,
) -> DispatchResult {
T::TechnicalOrigin::ensure_origin(origin)?;
ensure!(asset_id != T::OmnipoolHubAsset::get(), Error::<T>::NotAllowed);
if let Some(limit) = liquidity_limit {
Self::validate_limit(limit)?;
}
<LiquidityRemoveLimitPerAsset<T>>::insert(asset_id, liquidity_limit);
Self::deposit_event(Event::RemoveLiquidityLimitChanged {
asset_id,
liquidity_limit,
});
Ok(())
}
}
}
impl<T: Config> Pallet<T> {
fn initialize_trade_limit(asset_id: T::AssetId, initial_asset_reserve: T::Balance) -> DispatchResult {
if asset_id != T::OmnipoolHubAsset::get() && !<AllowedTradeVolumeLimitPerAsset<T>>::contains_key(asset_id) {
let limit = Self::calculate_limit(
initial_asset_reserve,
Pallet::<T>::trade_volume_limit_per_asset(asset_id),
)?;
<AllowedTradeVolumeLimitPerAsset<T>>::insert(
asset_id,
TradeVolumeLimit::<T> {
limit,
volume_in: Zero::zero(),
volume_out: Zero::zero(),
},
);
}
Ok(())
}
fn calculate_and_store_liquidity_limits(asset_id: T::AssetId, initial_liquidity: T::Balance) -> DispatchResult {
// we don't track liquidity limits for the Omnipool Hub asset
if asset_id == T::OmnipoolHubAsset::get() {
return Ok(());
}
// add liquidity
if let Some(limit) = Pallet::<T>::add_liquidity_limit_per_asset(asset_id) {
if !<AllowedAddLiquidityAmountPerAsset<T>>::contains_key(asset_id) {
let max_limit = Self::calculate_limit(initial_liquidity, limit)?;
<AllowedAddLiquidityAmountPerAsset<T>>::insert(
asset_id,
LiquidityLimit::<T> {
limit: max_limit,
liquidity: Zero::zero(),
},
);
}
}
// remove liquidity
if let Some(limit) = Pallet::<T>::remove_liquidity_limit_per_asset(asset_id) {
if !<AllowedRemoveLiquidityAmountPerAsset<T>>::contains_key(asset_id) {
let max_limit = Self::calculate_limit(initial_liquidity, limit)?;
<AllowedRemoveLiquidityAmountPerAsset<T>>::insert(
asset_id,
LiquidityLimit::<T> {
limit: max_limit,
liquidity: Zero::zero(),
},
);
}
}
Ok(())
}
fn ensure_and_update_trade_volume_limit(
asset_in: T::AssetId,
amount_in: T::Balance,
asset_out: T::AssetId,
amount_out: T::Balance,
) -> DispatchResult {
// liquidity in
// ignore Omnipool's hub asset
if asset_in != T::OmnipoolHubAsset::get() {
let mut allowed_liquidity_range = Pallet::<T>::allowed_trade_volume_limit_per_asset(asset_in)
.ok_or(Error::<T>::LiquidityLimitNotStoredForAsset)?;
allowed_liquidity_range.update_amounts(amount_in, Zero::zero())?;
allowed_liquidity_range.check_limits()?;
<AllowedTradeVolumeLimitPerAsset<T>>::insert(asset_in, allowed_liquidity_range);
}
// liquidity out
// ignore Omnipool's hub asset
if asset_out != T::OmnipoolHubAsset::get() {
let mut allowed_liquidity_range = Pallet::<T>::allowed_trade_volume_limit_per_asset(asset_out)
.ok_or(Error::<T>::LiquidityLimitNotStoredForAsset)?;
allowed_liquidity_range.update_amounts(Zero::zero(), amount_out)?;
allowed_liquidity_range.check_limits()?;
<AllowedTradeVolumeLimitPerAsset<T>>::insert(asset_out, allowed_liquidity_range);
}
Ok(())
}
fn ensure_and_update_add_liquidity_limit(asset_id: T::AssetId, added_liquidity: T::Balance) -> DispatchResult {
if asset_id != T::OmnipoolHubAsset::get() && Pallet::<T>::add_liquidity_limit_per_asset(asset_id).is_some() {
let mut allowed_liquidity_limit = Pallet::<T>::allowed_add_liquidity_limit_per_asset(asset_id)
.ok_or(Error::<T>::LiquidityLimitNotStoredForAsset)?;
allowed_liquidity_limit.update_amount(added_liquidity)?;
allowed_liquidity_limit.check_limit()?;
<AllowedAddLiquidityAmountPerAsset<T>>::insert(asset_id, allowed_liquidity_limit);
}
Ok(())
}
fn ensure_and_update_remove_liquidity_limit(asset_id: T::AssetId, removed_liquidity: T::Balance) -> DispatchResult {
if asset_id != T::OmnipoolHubAsset::get() && Pallet::<T>::remove_liquidity_limit_per_asset(asset_id).is_some() {
let mut allowed_liquidity_limit = Pallet::<T>::allowed_remove_liquidity_limit_per_asset(asset_id)
.ok_or(Error::<T>::LiquidityLimitNotStoredForAsset)?;
allowed_liquidity_limit.update_amount(removed_liquidity)?;
allowed_liquidity_limit.check_limit()?;
<AllowedRemoveLiquidityAmountPerAsset<T>>::insert(asset_id, allowed_liquidity_limit);
}
Ok(())
}
pub fn validate_limit(limit: (u32, u32)) -> DispatchResult {
let (numerator, denominator) = (limit.0, limit.1);
ensure!(
numerator <= MAX_LIMIT_VALUE && denominator <= MAX_LIMIT_VALUE,
Error::<T>::InvalidLimitValue
);
ensure!(
!numerator.is_zero() && !denominator.is_zero(),
Error::<T>::InvalidLimitValue
);
Ok(())
}
pub fn calculate_limit(liquidity: T::Balance, limit: (u32, u32)) -> Result<T::Balance, DispatchError> {
let (numerator, denominator) = (limit.0, limit.1);
// TODO: use u256
liquidity
.checked_mul(&T::Balance::from(numerator))
.ok_or(ArithmeticError::Overflow)?
.checked_div(&T::Balance::from(denominator))
.ok_or_else(|| ArithmeticError::DivisionByZero.into())
}
pub fn ensure_pool_state_change_limit(
asset_in: T::AssetId,
asset_in_reserve: T::Balance,
amount_in: T::Balance,
asset_out: T::AssetId,
asset_out_reserve: T::Balance,
amount_out: T::Balance,
) -> Result<Weight, DispatchError> {
Pallet::<T>::initialize_trade_limit(asset_in, asset_in_reserve)?;
Pallet::<T>::initialize_trade_limit(asset_out, asset_out_reserve)?;
Pallet::<T>::ensure_and_update_trade_volume_limit(asset_in, amount_in, asset_out, amount_out)?;
Ok(T::WeightInfo::ensure_pool_state_change_limit())
}
pub fn ensure_add_liquidity_limit(
origin: OriginFor<T>,
asset_id: T::AssetId,
initial_liquidity: T::Balance,
added_liquidity: T::Balance,
) -> Result<Weight, DispatchError> {
let is_whitelisted = Self::is_origin_whitelisted_or_root(origin)?;
if is_whitelisted {
return Ok(Weight::zero());
}
Pallet::<T>::calculate_and_store_liquidity_limits(asset_id, initial_liquidity)?;
Pallet::<T>::ensure_and_update_add_liquidity_limit(asset_id, added_liquidity)?;
Ok(T::WeightInfo::ensure_add_liquidity_limit())
}
pub fn ensure_remove_liquidity_limit(
origin: OriginFor<T>,
asset_id: T::AssetId,
initial_liquidity: T::Balance,
removed_liquidity: T::Balance,
) -> Result<Weight, DispatchError> {
let is_whitelisted = Self::is_origin_whitelisted_or_root(origin)?;
if is_whitelisted {
return Ok(Weight::zero());
}
Pallet::<T>::calculate_and_store_liquidity_limits(asset_id, initial_liquidity)?;
Pallet::<T>::ensure_and_update_remove_liquidity_limit(asset_id, removed_liquidity)?;
Ok(T::WeightInfo::ensure_remove_liquidity_limit())
}
pub(crate) fn is_origin_whitelisted_or_root(origin: OriginFor<T>) -> Result<bool, DispatchError> {
let who = ensure_signed_or_root(origin)?;
match who {
Some(account) => {
if T::WhitelistedAccounts::contains(&account) {
Ok(true)
} else {
Ok(false)
}
}
None => {
//origin is root
//root is always whitelisted
Ok(true)
}
}
}
}