-
Notifications
You must be signed in to change notification settings - Fork 325
/
lib.rs
770 lines (680 loc) · 27.4 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
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
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::{
debug, decl_error, decl_event, decl_module, decl_storage, ensure, traits::Get, weights::DispatchInfo, IsSubType,
};
use orml_traits::{arithmetic::Signed, MultiCurrency, MultiCurrencyExtended};
use rstd::{convert::TryInto, marker, prelude::*};
use sp_runtime::{
offchain::{storage::StorageValueRef, Duration, Timestamp},
traits::{
BlakeTwo256, CheckedAdd, CheckedSub, Convert, EnsureOrigin, Hash, Saturating, SignedExtension,
UniqueSaturatedInto, Zero,
},
transaction_validity::{
InvalidTransaction, TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransaction,
},
DispatchResult, RandomNumberGenerator, RuntimeDebug,
};
use support::{
CDPTreasury, CDPTreasuryExtended, DexManager, EmergencyShutdown, ExchangeRate, Price, PriceProvider, Rate, Ratio,
RiskManager,
};
use system::{ensure_none, ensure_root, offchain::SubmitUnsignedTransaction};
mod debit_exchange_rate_convertor;
pub use debit_exchange_rate_convertor::DebitExchangeRateConvertor;
mod mock;
mod tests;
const LOCK_EXPIRE_DURATION: u64 = 300_000; // 5 min
const LOCK_UPDATE_DURATION: u64 = 240_000; // 4 min
const DB_PREFIX: &[u8] = b"acala/cdp-engine-offchain-worker/";
type CurrencyIdOf<T> = <<T as loans::Trait>::Currency as MultiCurrency<<T as system::Trait>::AccountId>>::CurrencyId;
type BalanceOf<T> = <<T as loans::Trait>::Currency as MultiCurrency<<T as system::Trait>::AccountId>>::Balance;
type AmountOf<T> = <<T as loans::Trait>::Currency as MultiCurrencyExtended<<T as system::Trait>::AccountId>>::Amount;
pub trait Trait: system::Trait + loans::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type PriceSource: PriceProvider<CurrencyIdOf<Self>, Price>;
type CollateralCurrencyIds: Get<Vec<CurrencyIdOf<Self>>>;
type GlobalStabilityFee: Get<Rate>;
type DefaultLiquidationRatio: Get<Ratio>;
type DefaultDebitExchangeRate: Get<ExchangeRate>;
type DefaultLiquidationPenalty: Get<Rate>;
type MinimumDebitValue: Get<BalanceOf<Self>>;
type GetStableCurrencyId: Get<CurrencyIdOf<Self>>;
type Treasury: CDPTreasuryExtended<Self::AccountId, Balance = BalanceOf<Self>, CurrencyId = CurrencyIdOf<Self>>;
type UpdateOrigin: EnsureOrigin<Self::Origin>;
type MaxSlippageSwapWithDex: Get<Ratio>;
type Currency: MultiCurrency<Self::AccountId, CurrencyId = CurrencyIdOf<Self>, Balance = BalanceOf<Self>>;
type Dex: DexManager<Self::AccountId, CurrencyIdOf<Self>, BalanceOf<Self>>;
/// A dispatchable call type.
type Call: From<Call<Self>> + IsSubType<Module<Self>, Self>;
/// A transaction submitter.
type SubmitTransaction: SubmitUnsignedTransaction<Self, <Self as Trait>::Call>;
}
decl_event!(
pub enum Event<T>
where
<T as system::Trait>::AccountId,
CurrencyId = CurrencyIdOf<T>,
Balance = BalanceOf<T>,
{
LiquidateUnsafeCdp(CurrencyId, AccountId, Balance, Balance),
SettleCdpInDebit(CurrencyId, AccountId),
UpdateStabilityFee(CurrencyId, Option<Rate>),
UpdateLiquidationRatio(CurrencyId, Option<Ratio>),
UpdateLiquidationPenalty(CurrencyId, Option<Rate>),
UpdateRequiredCollateralRatio(CurrencyId, Option<Ratio>),
UpdateMaximumTotalDebitValue(CurrencyId, Balance),
}
);
decl_error! {
/// Error for cdp engine module.
pub enum Error for Module<T: Trait> {
ExceedDebitValueHardCap,
UpdatePositionFailed,
DebitAmountConvertFailed,
AmountConvertFailed,
BelowRequiredCollateralRatio,
BelowLiquidationRatio,
CollateralRatioStillSafe,
NotValidCurrencyId,
RemainDebitValueTooSmall,
GrabCollateralAndDebitFailed,
BalanceOverflow,
InvalidFeedPrice,
AlreadyNoDebit,
AlreadyShutdown,
NoDebitInCdp,
}
}
decl_storage! {
trait Store for Module<T: Trait> as CdpEngine {
pub StabilityFee get(fn stability_fee): map hasher(twox_64_concat) CurrencyIdOf<T> => Option<Rate>;
pub LiquidationRatio get(fn liquidation_ratio): map hasher(twox_64_concat) CurrencyIdOf<T> => Option<Ratio>;
pub LiquidationPenalty get(fn liquidation_penalty): map hasher(twox_64_concat) CurrencyIdOf<T> => Option<Rate>;
pub RequiredCollateralRatio get(fn required_collateral_ratio): map hasher(twox_64_concat) CurrencyIdOf<T> => Option<Ratio>;
pub MaximumTotalDebitValue get(fn maximum_total_debit_value): map hasher(twox_64_concat) CurrencyIdOf<T> => BalanceOf<T>;
pub DebitExchangeRate get(fn debit_exchange_rate): map hasher(twox_64_concat) CurrencyIdOf<T> => Option<ExchangeRate>;
pub IsShutdown get(fn is_shutdown): bool;
}
add_extra_genesis {
config(collaterals_params): Vec<(CurrencyIdOf<T>, Option<Rate>, Option<Ratio>, Option<Rate>, Option<Ratio>, BalanceOf<T>)>;
build(|config: &GenesisConfig<T>| {
config.collaterals_params.iter().for_each(|(
currency_id,
stability_fee,
liquidation_ratio,
liquidation_penalty,
required_collateral_ratio,
maximum_total_debit_value,
)| {
if let Some(val) = stability_fee {
<StabilityFee<T>>::insert(currency_id, val);
}
if let Some(val) = liquidation_ratio {
<LiquidationRatio<T>>::insert(currency_id, val);
}
if let Some(val) = liquidation_penalty {
<LiquidationPenalty<T>>::insert(currency_id, val);
}
if let Some(val) = required_collateral_ratio {
<RequiredCollateralRatio<T>>::insert(currency_id, val);
}
<MaximumTotalDebitValue<T>>::insert(currency_id, maximum_total_debit_value);
});
});
}
}
/// Error which may occur while executing the off-chain code.
#[cfg_attr(test, derive(PartialEq))]
enum OffchainErr {
FailedToAcquireLock,
SubmitTransaction,
NotValidator,
LockStillInLocked,
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
struct LiquidatorLock {
pub previous_position: u32,
pub expire_timestamp: Timestamp,
}
impl rstd::fmt::Debug for OffchainErr {
fn fmt(&self, fmt: &mut rstd::fmt::Formatter) -> rstd::fmt::Result {
match *self {
OffchainErr::FailedToAcquireLock => write!(fmt, "Failed to acquire lock"),
OffchainErr::SubmitTransaction => write!(fmt, "Failed to submit transaction"),
OffchainErr::NotValidator => write!(fmt, "Not validator"),
OffchainErr::LockStillInLocked => write!(fmt, "Liquidator lock is still in locked"),
}
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event() = default;
const CollateralCurrencyIds: Vec<CurrencyIdOf<T>> = T::CollateralCurrencyIds::get();
const GlobalStabilityFee: Rate = T::GlobalStabilityFee::get();
const DefaultLiquidationRatio: Ratio = T::DefaultLiquidationRatio::get();
const DefaultDebitExchangeRate: ExchangeRate = T::DefaultDebitExchangeRate::get();
const MinimumDebitValue: BalanceOf<T> = T::MinimumDebitValue::get();
const GetStableCurrencyId: CurrencyIdOf<T> = T::GetStableCurrencyId::get();
const MaxSlippageSwapWithDex: Ratio = T::MaxSlippageSwapWithDex::get();
const DefaultLiquidationPenalty: Rate = T::DefaultLiquidationPenalty::get();
pub fn set_collateral_params(
origin,
currency_id: CurrencyIdOf<T>,
stability_fee: Option<Option<Rate>>,
liquidation_ratio: Option<Option<Ratio>>,
liquidation_penalty: Option<Option<Rate>>,
required_collateral_ratio: Option<Option<Ratio>>,
maximum_total_debit_value: Option<BalanceOf<T>>,
) {
T::UpdateOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
if let Some(update) = stability_fee {
if let Some(val) = update {
<StabilityFee<T>>::insert(currency_id, val);
} else {
<StabilityFee<T>>::remove(currency_id);
}
Self::deposit_event(RawEvent::UpdateStabilityFee(currency_id, update));
}
if let Some(update) = liquidation_ratio {
if let Some(val) = update {
<LiquidationRatio<T>>::insert(currency_id, val);
} else {
<LiquidationRatio<T>>::remove(currency_id);
}
Self::deposit_event(RawEvent::UpdateLiquidationRatio(currency_id, update));
}
if let Some(update) = liquidation_penalty {
if let Some(val) = update {
<LiquidationPenalty<T>>::insert(currency_id, val);
} else {
<LiquidationPenalty<T>>::remove(currency_id);
}
Self::deposit_event(RawEvent::UpdateLiquidationPenalty(currency_id, update));
}
if let Some(update) = required_collateral_ratio {
if let Some(val) = update {
<RequiredCollateralRatio<T>>::insert(currency_id, val);
} else {
<RequiredCollateralRatio<T>>::remove(currency_id);
}
Self::deposit_event(RawEvent::UpdateRequiredCollateralRatio(currency_id, update));
}
if let Some(val) = maximum_total_debit_value {
<MaximumTotalDebitValue<T>>::insert(currency_id, val);
Self::deposit_event(RawEvent::UpdateMaximumTotalDebitValue(currency_id, val));
}
}
fn on_finalize(_now: T::BlockNumber) {
// collect stability fee for all types of collateral
if !Self::is_shutdown() {
let global_stability_fee = T::GlobalStabilityFee::get();
for currency_id in T::CollateralCurrencyIds::get() {
let debit_exchange_rate = Self::get_debit_exchange_rate(currency_id);
let stability_fee_rate = Self::stability_fee(currency_id)
.unwrap_or_default()
.saturating_add(global_stability_fee);
let total_debits = <loans::Module<T>>::total_debits(currency_id);
if !stability_fee_rate.is_zero() && !total_debits.is_zero() {
let debit_exchange_rate_increment = debit_exchange_rate.saturating_mul(stability_fee_rate);
// update exchange rate
let new_debit_exchange_rate = debit_exchange_rate.saturating_add(debit_exchange_rate_increment);
<DebitExchangeRate<T>>::insert(currency_id, new_debit_exchange_rate);
// issue stablecoin to surplus pool
let total_debit_value = DebitExchangeRateConvertor::<T>::convert((currency_id, total_debits));
let issued_stable_coin_balance = debit_exchange_rate_increment.saturating_mul_int(&total_debit_value);
<T as Trait>::Treasury::on_system_surplus(issued_stable_coin_balance);
}
}
}
}
pub fn liquidate(
origin,
currency_id: CurrencyIdOf<T>,
who: T::AccountId,
) {
ensure_none(origin)?;
ensure!(!Self::is_shutdown(), Error::<T>::AlreadyShutdown);
Self::liquidate_unsafe_cdp(who.clone(), currency_id)?;
}
// Runs after every block.
fn offchain_worker(now: T::BlockNumber) {
debug::RuntimeLogger::init();
if let Err(e) = Self::automatic_liquidation(now) {
debug::debug!(
target: "cdp-engine offchain worker",
"do not start automatic liquidation at {:?}: {:?}",
now,
e,
);
}
}
}
}
impl<T: Trait> Module<T> {
fn required_liquidator_lock() -> Result<LiquidatorLock, OffchainErr> {
let storage_key = DB_PREFIX.to_vec();
let storage = StorageValueRef::persistent(&storage_key);
let collateral_currency_ids = T::CollateralCurrencyIds::get();
let require_lock = storage.mutate(|lock: Option<Option<LiquidatorLock>>| {
match lock {
None => {
//start with random collateral
let random_seed = runtime_io::offchain::random_seed();
let mut rng = RandomNumberGenerator::<BlakeTwo256>::new(BlakeTwo256::hash(&random_seed[..]));
let expire_timestamp =
runtime_io::offchain::timestamp().add(Duration::from_millis(LOCK_EXPIRE_DURATION));
Ok(LiquidatorLock {
previous_position: rng.pick_u32(collateral_currency_ids.len() as u32),
expire_timestamp: expire_timestamp,
})
}
Some(Some(lock)) if runtime_io::offchain::timestamp() >= lock.expire_timestamp => {
let execute_position = if lock.previous_position < collateral_currency_ids.len() as u32 - 1 {
lock.previous_position + 1
} else {
0
};
let expire_timestamp =
runtime_io::offchain::timestamp().add(Duration::from_millis(LOCK_EXPIRE_DURATION));
Ok(LiquidatorLock {
previous_position: execute_position,
expire_timestamp: expire_timestamp,
})
}
_ => Err(OffchainErr::LockStillInLocked),
}
})?;
require_lock.map_err(|_| OffchainErr::FailedToAcquireLock)
}
fn release_liquidator_lock(previous_position: u32) {
let storage_key = DB_PREFIX.to_vec();
let storage = StorageValueRef::persistent(&storage_key);
if let Some(Some(liquidator_lock)) = storage.get::<LiquidatorLock>() {
if liquidator_lock.previous_position == previous_position {
storage.set(&LiquidatorLock {
previous_position: previous_position,
expire_timestamp: runtime_io::offchain::timestamp(),
});
}
}
}
fn extend_expire_duration_if_needed() {
let storage_key = DB_PREFIX.to_vec();
let storage = StorageValueRef::persistent(&storage_key);
if let Some(Some(liquidator_lock)) = storage.get::<LiquidatorLock>() {
if liquidator_lock.expire_timestamp
<= runtime_io::offchain::timestamp().add(Duration::from_millis(LOCK_UPDATE_DURATION))
{
storage.set(&LiquidatorLock {
previous_position: liquidator_lock.previous_position,
expire_timestamp: runtime_io::offchain::timestamp()
.add(Duration::from_millis(LOCK_EXPIRE_DURATION)),
});
}
}
}
fn submit_unsigned_liquidation_tx(currency_id: CurrencyIdOf<T>, who: T::AccountId) -> Result<(), OffchainErr> {
let call = Call::<T>::liquidate(currency_id, who);
T::SubmitTransaction::submit_unsigned(call).map_err(|_| OffchainErr::SubmitTransaction)?;
Ok(())
}
fn liquidate_specific_collateral(currency_id: CurrencyIdOf<T>) {
for (_, key) in <loans::Module<T>>::debits_iterator_with_collateral_prefix(currency_id) {
if let Some((_, account_id)) = key {
// TODO: liquidate unsafe cdp before emergency shutdown, settle cdp with debit when emergency shutdown occurs.
if Self::is_unsafe_cdp(currency_id, &account_id) {
if let Err(e) = Self::submit_unsigned_liquidation_tx(currency_id, account_id) {
debug::debug!(
target: "cdp-engine offchain worker",
"faild to submit unsigned liquidation tx: {:?}",
e,
);
}
}
}
Self::extend_expire_duration_if_needed();
}
}
fn automatic_liquidation(block_number: T::BlockNumber) -> Result<(), OffchainErr> {
// check if we are a potential validator
if !runtime_io::offchain::is_validator() {
return Err(OffchainErr::NotValidator);
}
// check if require offchain worker lock succecced
let LiquidatorLock {
previous_position,
expire_timestamp: _,
} = Self::required_liquidator_lock()?;
// start
let collateral_currency_ids = T::CollateralCurrencyIds::get();
let currency_id = collateral_currency_ids[(previous_position as usize)];
debug::info!(
target: "cdp-engine offchain worker",
"offchain worker start at block: {:?} execute automatic liquidation for collateral: {:?}",
block_number,
currency_id,
);
Self::liquidate_specific_collateral(currency_id);
// finally, release lock
Self::release_liquidator_lock(previous_position);
debug::info!(
target: "cdp-engine offchain worker",
"offchain worker start at block: {:?} done!",
block_number,
);
Ok(())
}
pub fn is_unsafe_cdp(currency_id: CurrencyIdOf<T>, who: &T::AccountId) -> bool {
let debit_balance = <loans::Module<T>>::debits(currency_id, who).0;
let collateral_balance = <loans::Module<T>>::collaterals(who, currency_id);
let stable_currency_id = T::GetStableCurrencyId::get();
if debit_balance.is_zero() {
false
} else if let Some(feed_price) = T::PriceSource::get_price(stable_currency_id, currency_id) {
let collateral_ratio =
Self::calculate_collateral_ratio(currency_id, collateral_balance, debit_balance, feed_price);
let liquidation_ratio = Self::get_liquidation_ratio(currency_id);
collateral_ratio < liquidation_ratio
} else {
// if feed_price is invalid, can not judge the cdp is safe or unsafe!
false
}
}
pub fn get_liquidation_ratio(currency_id: CurrencyIdOf<T>) -> Ratio {
Self::liquidation_ratio(currency_id).unwrap_or_else(T::DefaultLiquidationRatio::get)
}
pub fn get_debit_exchange_rate(currency_id: CurrencyIdOf<T>) -> ExchangeRate {
Self::debit_exchange_rate(currency_id).unwrap_or_else(T::DefaultDebitExchangeRate::get)
}
pub fn get_liquidation_penalty(currency_id: CurrencyIdOf<T>) -> Rate {
Self::liquidation_penalty(currency_id).unwrap_or_else(T::DefaultLiquidationPenalty::get)
}
pub fn emergency_shutdown() {
<IsShutdown>::put(true);
}
pub fn calculate_collateral_ratio(
currency_id: CurrencyIdOf<T>,
collateral_balance: BalanceOf<T>,
debit_balance: T::DebitBalance,
price: Price,
) -> Ratio {
let locked_collateral_value = price.saturating_mul_int(&collateral_balance);
let debit_value = DebitExchangeRateConvertor::<T>::convert((currency_id, debit_balance));
Ratio::from_rational(locked_collateral_value, debit_value)
}
pub fn exceed_debit_value_cap(currency_id: CurrencyIdOf<T>, debit_balance: T::DebitBalance) -> bool {
let hard_cap = Self::maximum_total_debit_value(currency_id);
let issue = DebitExchangeRateConvertor::<T>::convert((currency_id, debit_balance));
issue > hard_cap
}
pub fn update_position(
who: &T::AccountId,
currency_id: CurrencyIdOf<T>,
collateral_adjustment: AmountOf<T>,
debit_adjustment: T::DebitAmount,
) -> DispatchResult {
ensure!(
T::CollateralCurrencyIds::get().contains(¤cy_id),
Error::<T>::NotValidCurrencyId,
);
<loans::Module<T>>::update_position(who, currency_id, collateral_adjustment, debit_adjustment)
.map_err(|_| Error::<T>::UpdatePositionFailed)?;
Ok(())
}
// settle cdp has debit when emergency shutdown
pub fn settle_cdp_has_debit(who: T::AccountId, currency_id: CurrencyIdOf<T>) -> DispatchResult {
let debit_balance = <loans::Module<T>>::debits(currency_id, &who).0;
ensure!(!debit_balance.is_zero(), Error::<T>::AlreadyNoDebit);
// confiscate collateral in cdp to cdp treasury
// and decrease cdp's debit to zero
let collateral_balance = <loans::Module<T>>::collaterals(&who, currency_id);
let settle_price: Price = T::PriceSource::get_price(currency_id, T::GetStableCurrencyId::get())
.ok_or(Error::<T>::InvalidFeedPrice)?;
let debt_in_stable_currency = DebitExchangeRateConvertor::<T>::convert((currency_id, debit_balance));
let confiscate_collateral_amount = rstd::cmp::min(
settle_price.saturating_mul_int(&debt_in_stable_currency),
collateral_balance,
);
let grab_collateral_amount = TryInto::<AmountOf<T>>::try_into(confiscate_collateral_amount)
.map_err(|_| Error::<T>::AmountConvertFailed)?;
let grab_debit_amount =
TryInto::<T::DebitAmount>::try_into(debit_balance).map_err(|_| Error::<T>::AmountConvertFailed)?;
<loans::Module<T>>::update_collaterals_and_debits(
who.clone(),
currency_id,
-grab_collateral_amount,
-grab_debit_amount,
)
.map_err(|_| Error::<T>::GrabCollateralAndDebitFailed)?;
<T as Trait>::Treasury::deposit_system_collateral(currency_id, confiscate_collateral_amount);
<T as Trait>::Treasury::on_system_debit(debt_in_stable_currency);
Self::deposit_event(RawEvent::SettleCdpInDebit(currency_id, who));
Ok(())
}
// liquidate unsafe cdp
pub fn liquidate_unsafe_cdp(who: T::AccountId, currency_id: CurrencyIdOf<T>) -> DispatchResult {
let debit_balance = <loans::Module<T>>::debits(currency_id, &who).0;
let collateral_balance = <loans::Module<T>>::collaterals(&who, currency_id);
let stable_currency_id = T::GetStableCurrencyId::get();
let feed_price =
T::PriceSource::get_price(stable_currency_id, currency_id).ok_or(Error::<T>::InvalidFeedPrice)?;
// first: ensure the cdp is unsafe
ensure!(!debit_balance.is_zero(), Error::<T>::NoDebitInCdp);
let collateral_ratio =
Self::calculate_collateral_ratio(currency_id, collateral_balance, debit_balance, feed_price);
let liquidation_ratio = Self::get_liquidation_ratio(currency_id);
ensure!(
collateral_ratio < liquidation_ratio,
Error::<T>::CollateralRatioStillSafe
);
// second: grab collaterals and debits from unsafe cdp
let grab_amount =
TryInto::<AmountOf<T>>::try_into(collateral_balance).map_err(|_| Error::<T>::AmountConvertFailed)?;
let grab_debit_amount =
TryInto::<T::DebitAmount>::try_into(debit_balance).map_err(|_| Error::<T>::AmountConvertFailed)?;
<loans::Module<T>>::update_collaterals_and_debits(who.clone(), currency_id, -grab_amount, -grab_debit_amount)
.map_err(|_| Error::<T>::GrabCollateralAndDebitFailed)?;
// third: calculate bad_debt and target
let bad_debt = DebitExchangeRateConvertor::<T>::convert((currency_id, debit_balance));
let target = bad_debt.saturating_add(Self::get_liquidation_penalty(currency_id).saturating_mul_int(&bad_debt));
// add system debit to cdp treasury
<T as Trait>::Treasury::on_system_debit(bad_debt);
// if collateral_balance can swap enough native token in DEX and exchange slippage is blow the limit,
// directly exchange with DEX, otherwise create collateral auctions.
let supply_amount = T::Dex::get_supply_amount(currency_id, stable_currency_id, target);
let slippage = T::Dex::get_exchange_slippage(currency_id, stable_currency_id, supply_amount);
let slippage_limit = T::MaxSlippageSwapWithDex::get();
// forth: handle bad debt and collateral
if !supply_amount.is_zero() // supply_amount must not be zero
&& collateral_balance >= supply_amount // can afford supply_amount
&& slippage_limit > Ratio::from_natural(0) // slippage_limit must be greater than zero
&& slippage.map_or(false, |s| s <= slippage_limit)
{
// directly exchange with DEX
// deposit supply_amount collateral to cdp treasury
<T as Trait>::Treasury::deposit_system_collateral(currency_id, supply_amount);
// exchange with Dex by cdp treasury
<T as Trait>::Treasury::swap_collateral_to_stable(currency_id, supply_amount, target);
// refund remain collateral to who
let refund_collateral_amount = collateral_balance - supply_amount;
if !refund_collateral_amount.is_zero() {
<T as Trait>::Currency::deposit(currency_id, &who, refund_collateral_amount).expect("never failed");
}
} else {
// deposit collateral_balance collateral to cdp treasury
<T as Trait>::Treasury::deposit_system_collateral(currency_id, collateral_balance);
// create collateral auctions by cdp treasury
<T as Trait>::Treasury::create_collateral_auctions(currency_id, collateral_balance, target, who.clone());
}
Self::deposit_event(RawEvent::LiquidateUnsafeCdp(
currency_id,
who,
collateral_balance,
bad_debt,
));
Ok(())
}
}
impl<T: Trait> RiskManager<T::AccountId, CurrencyIdOf<T>, AmountOf<T>, T::DebitAmount> for Module<T> {
fn check_position_adjustment(
account_id: &T::AccountId,
currency_id: CurrencyIdOf<T>,
collateral_amount: AmountOf<T>,
debit_amount: T::DebitAmount,
) -> DispatchResult {
let mut debit_balance = <loans::Module<T>>::debits(currency_id, account_id).0;
let mut collateral_balance = <loans::Module<T>>::collaterals(account_id, currency_id);
// calculate new debit balance and collateral balance after position adjustment
let collateral_balance_adjustment =
TryInto::<BalanceOf<T>>::try_into(collateral_amount.abs()).map_err(|_| Error::<T>::AmountConvertFailed)?;
if collateral_amount.is_positive() {
collateral_balance = collateral_balance
.checked_add(&collateral_balance_adjustment)
.ok_or(Error::<T>::BalanceOverflow)?;
} else {
collateral_balance = collateral_balance
.checked_sub(&collateral_balance_adjustment)
.ok_or(Error::<T>::BalanceOverflow)?;
}
let debit_balance_adjustment = TryInto::<T::DebitBalance>::try_into(debit_amount.abs())
.map_err(|_| Error::<T>::DebitAmountConvertFailed)?;
if debit_amount.is_positive() {
debit_balance = debit_balance
.checked_add(&debit_balance_adjustment)
.ok_or(Error::<T>::BalanceOverflow)?;
} else {
debit_balance = debit_balance
.checked_sub(&debit_balance_adjustment)
.ok_or(Error::<T>::BalanceOverflow)?;
}
let debit_value = DebitExchangeRateConvertor::<T>::convert((currency_id, debit_balance));
if !debit_value.is_zero() {
// check the required collateral ratio
let feed_price = <T as Trait>::PriceSource::get_price(T::GetStableCurrencyId::get(), currency_id)
.ok_or(Error::<T>::InvalidFeedPrice)?;
let collateral_ratio =
Self::calculate_collateral_ratio(currency_id, collateral_balance, debit_balance, feed_price);
if let Some(required_collateral_ratio) = Self::required_collateral_ratio(currency_id) {
ensure!(
collateral_ratio >= required_collateral_ratio,
Error::<T>::BelowRequiredCollateralRatio
);
}
// check the liquidation ratio
let liquidation_ratio = if let Some(ratio) = Self::liquidation_ratio(currency_id) {
ratio
} else {
T::DefaultLiquidationRatio::get()
};
ensure!(collateral_ratio >= liquidation_ratio, Error::<T>::BelowLiquidationRatio);
// check the minimum_debit_value
ensure!(
debit_value >= T::MinimumDebitValue::get(),
Error::<T>::RemainDebitValueTooSmall,
);
}
Ok(())
}
fn check_debit_cap(currency_id: CurrencyIdOf<T>, debit_amount: T::DebitAmount) -> DispatchResult {
let mut total_debit_balance = <loans::Module<T>>::total_debits(currency_id);
let debit_balance_adjustment = TryInto::<T::DebitBalance>::try_into(debit_amount.abs())
.map_err(|_| Error::<T>::DebitAmountConvertFailed)?;
if debit_amount.is_positive() {
total_debit_balance = total_debit_balance
.checked_add(&debit_balance_adjustment)
.ok_or(Error::<T>::BalanceOverflow)?;
} else {
total_debit_balance = total_debit_balance
.checked_sub(&debit_balance_adjustment)
.ok_or(Error::<T>::BalanceOverflow)?;
}
ensure!(
!Self::exceed_debit_value_cap(currency_id, total_debit_balance),
Error::<T>::ExceedDebitValueHardCap
);
Ok(())
}
}
impl<T: Trait> EmergencyShutdown for Module<T> {
fn on_emergency_shutdown() {
Self::emergency_shutdown();
}
}
#[allow(deprecated)]
impl<T: Trait> frame_support::unsigned::ValidateUnsigned for Module<T> {
type Call = Call<T>;
fn validate_unsigned(call: &Self::Call) -> TransactionValidity {
if let Call::liquidate(currency_id, who) = call {
// check cdp is unsafe
if !Self::is_unsafe_cdp(*currency_id, &who) {
return InvalidTransaction::Stale.into();
}
Ok(ValidTransaction {
priority: TransactionPriority::max_value(),
requires: vec![],
provides: vec![],
longevity: 64_u64,
propagate: true,
})
} else {
InvalidTransaction::Call.into()
}
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct AutomaticLiquidationValidation<T: Trait + Send + Sync>(rstd::marker::PhantomData<T>);
impl<T: Trait + Send + Sync> AutomaticLiquidationValidation<T> {
pub fn new() -> Self {
Self(rstd::marker::PhantomData)
}
}
impl<T: Trait + Send + Sync> rstd::fmt::Debug for AutomaticLiquidationValidation<T> {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut rstd::fmt::Formatter) -> rstd::fmt::Result {
write!(f, "AutomaticLiquidationValidation")
}
#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut rstd::fmt::Formatter) -> rstd::fmt::Result {
Ok(())
}
}
impl<T: Trait + Send + Sync> SignedExtension for AutomaticLiquidationValidation<T> {
const IDENTIFIER: &'static str = "AutomaticLiquidationValidation";
type AccountId = T::AccountId;
type Call = <T as Trait>::Call;
type AdditionalSigned = ();
type DispatchInfo = DispatchInfo;
type Pre = ();
fn additional_signed(&self) -> rstd::result::Result<(), TransactionValidityError> {
Ok(())
}
fn validate(
&self,
_who: &Self::AccountId,
call: &Self::Call,
_info: Self::DispatchInfo,
_len: usize,
) -> TransactionValidity {
let call = match call.is_sub_type() {
Some(call) => call,
None => return Ok(ValidTransaction::default()),
};
match call {
Call::<T>::liquidate(currency_id, account_id) => {
// check cdp is unsafe
if !<Module<T>>::is_unsafe_cdp(*currency_id, account_id) {
InvalidTransaction::Stale.into()
} else {
let mut valid_tx = ValidTransaction::default();
valid_tx.priority = TransactionPriority::max_value();
Ok(valid_tx)
}
}
_ => Ok(ValidTransaction::default()),
}
}
}