Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove honzon global paramter #2071

Merged
merged 6 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 29 additions & 56 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ pub mod module {
collateral_type: CurrencyId,
new_total_debit_value: Balance,
},
/// The global interest rate per sec for all types of collateral updated.
GlobalInterestRatePerSecUpdated { new_global_interest_rate_per_sec: Rate },
}

/// Mapping from collateral type to its exchange rate of debit units and
Expand All @@ -295,13 +293,6 @@ pub mod module {
#[pallet::getter(fn debit_exchange_rate)]
pub type DebitExchangeRate<T: Config> = StorageMap<_, Twox64Concat, CurrencyId, ExchangeRate, OptionQuery>;

/// Global interest rate per sec for all types of collateral
///
/// GlobalInterestRatePerSec: Rate
#[pallet::storage]
#[pallet::getter(fn global_interest_rate_per_sec)]
pub type GlobalInterestRatePerSec<T: Config> = StorageValue<_, Rate, ValueQuery>;

/// Mapping from collateral type to its risk management params
///
/// CollateralParams: CurrencyId => RiskManagementParams
Expand All @@ -328,7 +319,6 @@ pub mod module {
Option<Ratio>,
Balance,
)>,
pub global_interest_rate_per_sec: Rate,
}

#[pallet::genesis_build]
Expand All @@ -355,7 +345,6 @@ pub mod module {
);
},
);
GlobalInterestRatePerSec::<T>::put(self.global_interest_rate_per_sec);
}
}

Expand Down Expand Up @@ -443,22 +432,6 @@ pub mod module {
Ok(())
}

/// Update global parameters related to risk management of CDP
///
/// The dispatch origin of this call must be `UpdateOrigin`.
///
/// - `global_interest_rate_per_sec`: global interest rate per sec.
#[pallet::weight((<T as Config>::WeightInfo::set_global_params(), DispatchClass::Operational))]
#[transactional]
pub fn set_global_params(origin: OriginFor<T>, global_interest_rate_per_sec: Rate) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
GlobalInterestRatePerSec::<T>::put(global_interest_rate_per_sec);
Self::deposit_event(Event::GlobalInterestRatePerSecUpdated {
new_global_interest_rate_per_sec: global_interest_rate_per_sec,
});
Ok(())
}

/// Update parameters related to risk management of CDP under specific
/// collateral type
///
Expand Down Expand Up @@ -584,35 +557,36 @@ impl<T: Config> Pallet<T> {
let interval_secs = now_secs.saturating_sub(last_accumulation_secs);

for currency_id in T::CollateralCurrencyIds::get() {
let rate_to_accumulate =
Self::compound_interest_rate(Self::get_interest_rate_per_sec(currency_id), interval_secs);
let total_debits = <LoansOf<T>>::total_positions(currency_id).debit;

if !rate_to_accumulate.is_zero() && !total_debits.is_zero() {
let debit_exchange_rate = Self::get_debit_exchange_rate(currency_id);
let debit_exchange_rate_increment = debit_exchange_rate.saturating_mul(rate_to_accumulate);
let issued_stable_coin_balance = debit_exchange_rate_increment.saturating_mul_int(total_debits);

// issue stablecoin to surplus pool
let res = <T as Config>::CDPTreasury::on_system_surplus(issued_stable_coin_balance);
match res {
Ok(_) => {
// update exchange rate when issue success
let new_debit_exchange_rate =
debit_exchange_rate.saturating_add(debit_exchange_rate_increment);
DebitExchangeRate::<T>::insert(currency_id, new_debit_exchange_rate);
}
Err(e) => {
log::warn!(
target: "cdp-engine",
"on_system_surplus: failed to on system surplus {:?}: {:?}. \
This is unexpected but should be safe",
issued_stable_coin_balance, e
);
if let Ok(interest_rate) = Self::get_interest_rate_per_sec(currency_id) {
let rate_to_accumulate = Self::compound_interest_rate(interest_rate, interval_secs);
let total_debits = <LoansOf<T>>::total_positions(currency_id).debit;

if !rate_to_accumulate.is_zero() && !total_debits.is_zero() {
let debit_exchange_rate = Self::get_debit_exchange_rate(currency_id);
let debit_exchange_rate_increment = debit_exchange_rate.saturating_mul(rate_to_accumulate);
let issued_stable_coin_balance = debit_exchange_rate_increment.saturating_mul_int(total_debits);

// issue stablecoin to surplus pool
let res = <T as Config>::CDPTreasury::on_system_surplus(issued_stable_coin_balance);
match res {
Ok(_) => {
// update exchange rate when issue success
let new_debit_exchange_rate =
debit_exchange_rate.saturating_add(debit_exchange_rate_increment);
DebitExchangeRate::<T>::insert(currency_id, new_debit_exchange_rate);
}
Err(e) => {
log::warn!(
target: "cdp-engine",
"on_system_surplus: failed to on system surplus {:?}: {:?}. \
This is unexpected but should be safe",
issued_stable_coin_balance, e
);
}
}
}
count += 1;
}
count += 1;
}
}

Expand Down Expand Up @@ -777,11 +751,10 @@ impl<T: Config> Pallet<T> {
Self::collateral_params(currency_id).required_collateral_ratio
}

pub fn get_interest_rate_per_sec(currency_id: CurrencyId) -> Rate {
pub fn get_interest_rate_per_sec(currency_id: CurrencyId) -> Result<Rate, DispatchError> {
Self::collateral_params(currency_id)
.interest_rate_per_sec
.unwrap_or_default()
.saturating_add(Self::global_interest_rate_per_sec())
.ok_or_else(|| Error::<T>::InvalidCollateralType.into())
ferrell-code marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn compound_interest_rate(rate_per_sec: Rate, secs: u64) -> Rate {
Expand Down
43 changes: 7 additions & 36 deletions modules/cdp-engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,6 @@ fn get_liquidation_ratio_work() {
});
}

#[test]
fn set_global_params_work() {
ExtBuilder::default().build().execute_with(|| {
System::set_block_number(1);
assert_noop!(
CDPEngineModule::set_global_params(Origin::signed(5), Rate::saturating_from_rational(1, 10000)),
BadOrigin
);
assert_ok!(CDPEngineModule::set_global_params(
Origin::signed(1),
Rate::saturating_from_rational(1, 10000),
));
System::assert_last_event(Event::CDPEngineModule(crate::Event::GlobalInterestRatePerSecUpdated {
new_global_interest_rate_per_sec: Rate::saturating_from_rational(1, 10000),
}));
assert_eq!(
CDPEngineModule::global_interest_rate_per_sec(),
Rate::saturating_from_rational(1, 10000)
);
});
}

#[test]
fn set_collateral_params_work() {
ExtBuilder::default().build().execute_with(|| {
Expand Down Expand Up @@ -1222,20 +1200,13 @@ fn liquidate_unsafe_cdp_of_lp_ausd_dot_and_create_dot_auction() {
#[test]
fn get_interest_rate_per_sec_work() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(CDPEngineModule::get_interest_rate_per_sec(BTC), Rate::zero());
assert_eq!(CDPEngineModule::get_interest_rate_per_sec(DOT), Rate::zero());

assert_ok!(CDPEngineModule::set_global_params(
Origin::signed(1),
Rate::saturating_from_rational(1, 10000),
));
assert_eq!(
assert_noop!(
CDPEngineModule::get_interest_rate_per_sec(BTC),
Rate::saturating_from_rational(1, 10000)
crate::Error::<Runtime>::InvalidCollateralType
);
assert_eq!(
assert_noop!(
CDPEngineModule::get_interest_rate_per_sec(DOT),
Rate::saturating_from_rational(1, 10000)
crate::Error::<Runtime>::InvalidCollateralType
);

assert_ok!(CDPEngineModule::set_collateral_params(
Expand All @@ -1249,11 +1220,11 @@ fn get_interest_rate_per_sec_work() {
));
assert_eq!(
CDPEngineModule::get_interest_rate_per_sec(BTC),
Rate::saturating_from_rational(12, 100000)
Ok(Rate::saturating_from_rational(2, 100000))
);
assert_eq!(
assert_noop!(
CDPEngineModule::get_interest_rate_per_sec(DOT),
Rate::saturating_from_rational(1, 10000)
crate::Error::<Runtime>::InvalidCollateralType
);
});
}
Expand Down
9 changes: 0 additions & 9 deletions modules/cdp-engine/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn on_initialize(c: u32) -> Weight;
fn set_collateral_params() -> Weight;
fn set_global_params() -> Weight;
fn liquidate_by_auction(b: u32) -> Weight;
fn liquidate_by_dex() -> Weight;
fn settle() -> Weight;
Expand All @@ -69,10 +68,6 @@ impl<T: frame_system::Config> WeightInfo for AcalaWeight<T> {
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn set_global_params() -> Weight {
(11_000_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn liquidate_by_auction(_b: u32) -> Weight {
(203_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(28 as Weight))
Expand Down Expand Up @@ -103,10 +98,6 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn set_global_params() -> Weight {
(11_000_000 as Weight)
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn liquidate_by_auction(_b: u32) -> Weight {
(203_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(28 as Weight))
Expand Down
1 change: 0 additions & 1 deletion node/service/src/chain_spec/acala.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ fn acala_genesis(
},
cdp_engine: CdpEngineConfig {
collaterals_params: vec![],
global_interest_rate_per_sec: Default::default(),
},
asset_registry: Default::default(),
evm: Default::default(),
Expand Down
1 change: 0 additions & 1 deletion node/service/src/chain_spec/karura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ fn karura_genesis(
},
cdp_engine: CdpEngineConfig {
collaterals_params: vec![],
global_interest_rate_per_sec: Default::default(),
},
asset_registry: Default::default(),
evm: Default::default(),
Expand Down
8 changes: 0 additions & 8 deletions node/service/src/chain_spec/mandala.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,6 @@ fn testnet_genesis(
10_000_000 * dollar(AUSD),
),
],
global_interest_rate_per_sec: FixedU128::saturating_from_rational(
1_547_126_000u128,
1_000_000_000_000_000_000u128,
), /* 5% APR */
},
asset_registry: AssetRegistryConfig {
assets: vec![
Expand Down Expand Up @@ -627,10 +623,6 @@ fn mandala_genesis(
10_000_000 * dollar(AUSD),
),
],
global_interest_rate_per_sec: FixedU128::saturating_from_rational(
1_547_126_000u128,
1_000_000_000_000_000_000u128,
), /* 5% APR */
},
asset_registry: AssetRegistryConfig {
assets: vec![
Expand Down
9 changes: 0 additions & 9 deletions runtime/acala/src/weights/module_cdp_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl<T: frame_system::Config> module_cdp_engine::WeightInfo for WeightInfo<T> {
// Storage: CdpEngine LastAccumulationSecs (r:1 w:1)
// Storage: EmergencyShutdown IsShutdown (r:1 w:0)
// Storage: CdpEngine CollateralParams (r:4 w:0)
// Storage: CdpEngine GlobalInterestRatePerSec (r:1 w:0)
// Storage: Loans TotalPositions (r:4 w:0)
// Storage: Timestamp Now (r:0 w:1)
fn on_initialize(c: u32, ) -> Weight {
Expand All @@ -67,14 +66,6 @@ impl<T: frame_system::Config> module_cdp_engine::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1)
// Storage: CdpEngine GlobalInterestRatePerSec (r:0 w:1)
fn set_global_params() -> Weight {
(11_584_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1)
// Storage: EmergencyShutdown IsShutdown (r:1 w:0)
// Storage: Loans Positions (r:1 w:1)
// Storage: Prices LockedPrice (r:2 w:0)
Expand Down
9 changes: 7 additions & 2 deletions runtime/integration-tests/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,13 @@ fn cancel_schedule_test() {
None,
5,
));
let council_call = Call::CdpEngine(module_cdp_engine::Call::set_global_params {
global_interest_rate_per_sec: Rate::from(10),
let council_call = Call::CdpEngine(module_cdp_engine::Call::set_collateral_params {
currency_id: RENBTC,
interest_rate_per_sec: Change::NewValue(Some(Rate::saturating_from_rational(1, 100000))),
liquidation_ratio: Change::NewValue(Some(Ratio::saturating_from_rational(5, 2))),
liquidation_penalty: Change::NewValue(Some(Rate::saturating_from_rational(2, 10))),
required_collateral_ratio: Change::NewValue(Some(Ratio::saturating_from_rational(9, 5))),
maximum_total_debit_value: Change::NewValue(10000),
});

assert_ok!(Authority::schedule_dispatch(
Expand Down
9 changes: 0 additions & 9 deletions runtime/karura/src/weights/module_cdp_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl<T: frame_system::Config> module_cdp_engine::WeightInfo for WeightInfo<T> {
// Storage: CdpEngine LastAccumulationSecs (r:1 w:1)
// Storage: EmergencyShutdown IsShutdown (r:1 w:0)
// Storage: CdpEngine CollateralParams (r:4 w:0)
// Storage: CdpEngine GlobalInterestRatePerSec (r:1 w:0)
// Storage: Loans TotalPositions (r:4 w:0)
// Storage: Timestamp Now (r:0 w:1)
fn on_initialize(c: u32, ) -> Weight {
Expand All @@ -67,14 +66,6 @@ impl<T: frame_system::Config> module_cdp_engine::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1)
// Storage: CdpEngine GlobalInterestRatePerSec (r:0 w:1)
fn set_global_params() -> Weight {
(11_124_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1)
// Storage: EmergencyShutdown IsShutdown (r:1 w:0)
// Storage: Loans Positions (r:1 w:1)
// Storage: Prices LockedPrice (r:2 w:0)
Expand Down
3 changes: 0 additions & 3 deletions runtime/mandala/src/benchmarking/cdp_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ runtime_benchmarks! {
Change::NewValue(100_000 * dollar(STABLECOIN))
)

set_global_params {
}: _(RawOrigin::Root, Rate::saturating_from_rational(1, 1000000))

// `liquidate` by_auction
liquidate_by_auction {
let b in 1 .. <Runtime as module_cdp_treasury::Config>::MaxAuctionsCount::get();
Expand Down
6 changes: 0 additions & 6 deletions runtime/mandala/src/weights/module_cdp_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl<T: frame_system::Config> module_cdp_engine::WeightInfo for WeightInfo<T> {
// Storage: CdpEngine LastAccumulationSecs (r:1 w:1)
// Storage: EmergencyShutdown IsShutdown (r:1 w:0)
// Storage: CdpEngine CollateralParams (r:4 w:0)
// Storage: CdpEngine GlobalInterestRatePerSec (r:1 w:0)
// Storage: Loans TotalPositions (r:4 w:0)
// Storage: Timestamp Now (r:0 w:1)
// Storage: CdpEngine DebitExchangeRate (r:1 w:1)
Expand All @@ -70,11 +69,6 @@ impl<T: frame_system::Config> module_cdp_engine::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: CdpEngine GlobalInterestRatePerSec (r:0 w:1)
fn set_global_params() -> Weight {
(9_248_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: EmergencyShutdown IsShutdown (r:1 w:0)
// Storage: Loans Positions (r:1 w:1)
// Storage: Prices LockedPrice (r:2 w:0)
Expand Down