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

Cdp Treasury Bench update #1609

Merged
merged 39 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3711f51
benches
ferrell-code Nov 12, 2021
be7cdc3
Merge remote-tracking branch 'origin/master' into fer-bench-cdp
Nov 12, 2021
fb6bccf
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 12, 2021
4713d79
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 12, 2021
26b5930
update verify
ferrell-code Nov 12, 2021
f248063
Merge branch 'fer-bench-cdp' of https://github.com/AcalaNetwork/Acala…
ferrell-code Nov 12, 2021
e01d60f
update
ferrell-code Nov 12, 2021
907d63d
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 12, 2021
994ead6
cdp treasury benches
ferrell-code Nov 12, 2021
e0a1eab
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 13, 2021
9cab584
adjust liquidate_by_auction
ferrell-code Nov 15, 2021
e4d3a9c
Merge branch 'fer-bench-cdp' of https://github.com/AcalaNetwork/Acala…
ferrell-code Nov 15, 2021
fe102b0
Merge remote-tracking branch 'origin/master' into fer-cdp-treasury-bench
Nov 15, 2021
42be60d
update extrinsic
ferrell-code Nov 15, 2021
4584406
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 15, 2021
dcb9828
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 15, 2021
1def781
Merge remote-tracking branch 'origin/master' into fer-bench-cdp
Nov 15, 2021
538ca12
fix
ferrell-code Nov 15, 2021
757130f
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 15, 2021
25a90eb
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 15, 2021
d0e3745
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 15, 2021
ac57cdf
update to refund weight
ferrell-code Nov 16, 2021
5143308
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 16, 2021
9fa8fd2
Update modules/cdp-treasury/src/lib.rs
ferrell-code Nov 16, 2021
c772649
Merge remote-tracking branch 'origin/master' into fer-cdp-treasury-bench
Nov 16, 2021
436bc1b
compile
ferrell-code Nov 16, 2021
dd493e0
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 16, 2021
bee248b
Merge remote-tracking branch 'upstream/fer-bench-cdp' into fer-cdp-tr…
ferrell-code Nov 16, 2021
7e82321
Merge branch 'fer-cdp-treasury-bench' of https://github.com/AcalaNetw…
ferrell-code Nov 16, 2021
b92d8ac
update cdp_engine benchmarking
ferrell-code Nov 16, 2021
fbff344
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 16, 2021
3c012c2
fix tests
ferrell-code Nov 16, 2021
7c083a1
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 16, 2021
d5db31f
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 16, 2021
67526e1
Merge branch 'fer-cdp-treasury-bench' of https://github.com/AcalaNetw…
ferrell-code Nov 16, 2021
66ec327
integration test
ferrell-code Nov 16, 2021
8a0a3e6
cargo run --release --color=never --bin=acala --features=runtime-benc…
Nov 16, 2021
837cc83
add field name to LiquidationStrategy
ferrell-code Nov 16, 2021
48f2e1f
Merge branch 'fer-cdp-treasury-bench' of https://github.com/AcalaNetw…
ferrell-code Nov 16, 2021
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
28 changes: 17 additions & 11 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ type ChangeBalance = Change<Balance>;
/// Liquidation strategy available
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub enum LiquidationStrategy {
/// Liquidation CDP's collateral by create collateral auction
Auction,
/// Liquidation CDP's collateral by create collateral auction, auction_count
/// being the number of new auctions created
Auction { auction_count: u32 },
/// Liquidation CDP's collateral by swap with DEX
ferrell-code marked this conversation as resolved.
Show resolved Hide resolved
Exchange,
}
Expand Down Expand Up @@ -396,18 +397,18 @@ pub mod module {
///
/// - `currency_id`: CDP's collateral type.
/// - `who`: CDP's owner.
#[pallet::weight(<T as Config>::WeightInfo::liquidate_by_dex())]
#[pallet::weight(<T as Config>::WeightInfo::liquidate_by_auction(<T as Config>::CDPTreasury::max_auction()))]
#[transactional]
pub fn liquidate(
origin: OriginFor<T>,
currency_id: CurrencyId,
who: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
ensure_none(origin)?;
let who = T::Lookup::lookup(who)?;
ensure!(!T::EmergencyShutdown::is_shutdown(), Error::<T>::AlreadyShutdown);
Self::liquidate_unsafe_cdp(who, currency_id)?;
Ok(())
let consumed_weight: Weight = Self::liquidate_unsafe_cdp(who, currency_id)?;
Ok(Some(consumed_weight).into())
}

/// Settle CDP has debit after system shutdown
Expand Down Expand Up @@ -907,7 +908,7 @@ impl<T: Config> Pallet<T> {
}

// liquidate unsafe cdp
pub fn liquidate_unsafe_cdp(who: T::AccountId, currency_id: CurrencyId) -> DispatchResult {
pub fn liquidate_unsafe_cdp(who: T::AccountId, currency_id: CurrencyId) -> Result<Weight, DispatchError> {
let Position { collateral, debit } = <LoansOf<T>>::positions(currency_id, &who);

// ensure the cdp is unsafe
Expand Down Expand Up @@ -968,25 +969,30 @@ impl<T: Config> Pallet<T> {
}

// if cannot liquidate by swap, create collateral auctions by cdp treasury
<T as Config>::CDPTreasury::create_collateral_auctions(
let created_auctions = <T as Config>::CDPTreasury::create_collateral_auctions(
currency_id,
collateral,
target_stable_amount,
who.clone(),
true,
)?;

Ok(LiquidationStrategy::Auction)
Ok(LiquidationStrategy::Auction {
auction_count: created_auctions,
})
})()?;

Self::deposit_event(Event::LiquidateUnsafeCDP(
currency_id,
who,
collateral,
bad_debt_value,
liquidation_strategy,
liquidation_strategy.clone(),
));
Ok(())
match liquidation_strategy {
LiquidationStrategy::Auction { auction_count } => Ok(T::WeightInfo::liquidate_by_auction(auction_count)),
LiquidationStrategy::Exchange => Ok(T::WeightInfo::liquidate_by_dex()),
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/cdp-engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ fn liquidate_unsafe_cdp_by_collateral_auction() {
ALICE,
100,
50,
LiquidationStrategy::Auction,
LiquidationStrategy::Auction { auction_count: 1 },
)));
assert_eq!(CDPTreasuryModule::debit_pool(), 50);
assert_eq!(Currencies::free_balance(BTC, &ALICE), 900);
Expand Down Expand Up @@ -528,7 +528,7 @@ fn liquidate_unsafe_cdp_by_collateral_auction_when_limited_by_slippage() {
ALICE,
100,
50,
LiquidationStrategy::Auction,
LiquidationStrategy::Auction { auction_count: 1 },
)));

assert_eq!(DEXModule::get_liquidity_pool(BTC, AUSD), (100, 121));
Expand Down
6 changes: 3 additions & 3 deletions modules/cdp-engine/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait WeightInfo {
fn on_initialize(c: u32) -> Weight;
fn set_collateral_params() -> Weight;
fn set_global_params() -> Weight;
fn liquidate_by_auction() -> Weight;
fn liquidate_by_auction(b: u32) -> Weight;
fn liquidate_by_dex() -> Weight;
fn settle() -> Weight;
}
Expand All @@ -73,7 +73,7 @@ impl<T: frame_system::Config> WeightInfo for AcalaWeight<T> {
(11_000_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn liquidate_by_auction() -> Weight {
fn liquidate_by_auction(_b: u32) -> Weight {
(203_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(28 as Weight))
.saturating_add(T::DbWeight::get().writes(17 as Weight))
Expand Down Expand Up @@ -107,7 +107,7 @@ impl WeightInfo for () {
(11_000_000 as Weight)
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn liquidate_by_auction() -> Weight {
fn liquidate_by_auction(_b: u32) -> Weight {
(203_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(28 as Weight))
.saturating_add(RocksDbWeight::get().writes(17 as Weight))
Expand Down
18 changes: 12 additions & 6 deletions modules/cdp-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use sp_runtime::{
traits::{AccountIdConversion, One, Zero},
ArithmeticError, DispatchError, DispatchResult, FixedPointNumber,
};
use sp_std::convert::TryInto;
use support::{AuctionManager, CDPTreasury, CDPTreasuryExtended, DEXManager, Ratio};

mod mock;
Expand Down Expand Up @@ -177,24 +178,24 @@ pub mod module {
Ok(())
}

#[pallet::weight(T::WeightInfo::auction_collateral())]
#[pallet::weight(T::WeightInfo::auction_collateral(T::MaxAuctionsCount::get()))]
#[transactional]
pub fn auction_collateral(
origin: OriginFor<T>,
currency_id: CurrencyId,
#[pallet::compact] amount: Balance,
#[pallet::compact] target: Balance,
splited: bool,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
<Self as CDPTreasuryExtended<T::AccountId>>::create_collateral_auctions(
let created_auctions = <Self as CDPTreasuryExtended<T::AccountId>>::create_collateral_auctions(
currency_id,
amount,
target,
Self::account_id(),
splited,
)?;
Ok(())
Ok(Some(T::WeightInfo::auction_collateral(created_auctions)).into())
}

/// Update parameters related to collateral auction under specific
Expand Down Expand Up @@ -399,7 +400,7 @@ impl<T: Config> CDPTreasuryExtended<T::AccountId> for Pallet<T> {
target: Balance,
refund_receiver: T::AccountId,
splited: bool,
) -> DispatchResult {
) -> Result<u32, DispatchError> {
ensure!(
Self::total_collaterals_not_in_auction(currency_id) >= amount,
Error::<T>::CollateralNotEnough,
Expand Down Expand Up @@ -451,7 +452,12 @@ impl<T: Config> CDPTreasuryExtended<T::AccountId> for Pallet<T> {
unhandled_collateral_amount = unhandled_collateral_amount.saturating_sub(lot_collateral_amount);
unhandled_target = unhandled_target.saturating_sub(lot_target);
}
Ok(())
let created_auctions: u32 = created_lots.try_into().map_err(|_| ArithmeticError::Overflow)?;
Ok(created_auctions)
}

fn max_auction() -> u32 {
T::MaxAuctionsCount::get()
}
}

Expand Down
6 changes: 3 additions & 3 deletions modules/cdp-treasury/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use sp_std::marker::PhantomData;
/// Weight functions needed for module_cdp_treasury.
pub trait WeightInfo {
fn extract_surplus_to_treasury() -> Weight;
fn auction_collateral() -> Weight;
fn auction_collateral(b: u32) -> Weight;
fn set_expected_collateral_auction_size() -> Weight;
}

Expand All @@ -61,7 +61,7 @@ impl<T: frame_system::Config> WeightInfo for AcalaWeight<T> {
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
fn auction_collateral() -> Weight {
fn auction_collateral(_b: u32) -> Weight {
(2_124_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(204 as Weight))
Expand All @@ -79,7 +79,7 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
fn auction_collateral() -> Weight {
fn auction_collateral(_b: u32) -> Weight {
(2_124_000_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(6 as Weight))
.saturating_add(RocksDbWeight::get().writes(204 as Weight))
Expand Down
4 changes: 3 additions & 1 deletion modules/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ pub trait CDPTreasuryExtended<AccountId>: CDPTreasury<AccountId> {
target: Self::Balance,
refund_receiver: AccountId,
splited: bool,
) -> DispatchResult;
) -> sp_std::result::Result<u32, DispatchError>;

fn max_auction() -> u32;
}

pub trait PriceProvider<CurrencyId> {
Expand Down
31 changes: 17 additions & 14 deletions runtime/acala/src/weights/module_cdp_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! Autogenerated weights for module_cdp_engine
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2021-11-16, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-latest"), DB CACHE: 128

// Executed Command:
Expand All @@ -28,7 +28,7 @@
// --chain=acala-latest
// --steps=50
// --repeat=20
// --pallet=*
// --pallet=module_cdp_engine
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
Expand All @@ -48,33 +48,36 @@ use sp_std::marker::PhantomData;
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> module_cdp_engine::WeightInfo for WeightInfo<T> {
fn on_initialize(c: u32, ) -> Weight {
(32_905_000 as Weight)
// Standard Error: 64_000
.saturating_add((5_373_000 as Weight).saturating_mul(c as Weight))
(34_203_000 as Weight)
// Standard Error: 88_000
.saturating_add((4_625_000 as Weight).saturating_mul(c as Weight))
.saturating_add(T::DbWeight::get().reads(7 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn set_collateral_params() -> Weight {
(55_056_000 as Weight)
(56_501_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn set_global_params() -> Weight {
(19_309_000 as Weight)
(19_612_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn liquidate_by_auction() -> Weight {
(275_178_000 as Weight)
fn liquidate_by_auction(b: u32, ) -> Weight {
(275_714_000 as Weight)
// Standard Error: 118_000
.saturating_add((30_172_000 as Weight).saturating_mul(b as Weight))
.saturating_add(T::DbWeight::get().reads(22 as Weight))
.saturating_add(T::DbWeight::get().writes(16 as Weight))
.saturating_add(T::DbWeight::get().writes(15 as Weight))
.saturating_add(T::DbWeight::get().writes((3 as Weight).saturating_mul(b as Weight)))
}
fn liquidate_by_dex() -> Weight {
(374_192_000 as Weight)
.saturating_add(T::DbWeight::get().reads(25 as Weight))
.saturating_add(T::DbWeight::get().writes(15 as Weight))
(428_772_000 as Weight)
.saturating_add(T::DbWeight::get().reads(30 as Weight))
.saturating_add(T::DbWeight::get().writes(16 as Weight))
}
fn settle() -> Weight {
(165_420_000 as Weight)
(170_663_000 as Weight)
.saturating_add(T::DbWeight::get().reads(13 as Weight))
.saturating_add(T::DbWeight::get().writes(8 as Weight))
}
Expand Down
17 changes: 10 additions & 7 deletions runtime/acala/src/weights/module_cdp_treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! Autogenerated weights for module_cdp_treasury
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2021-11-16, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-latest"), DB CACHE: 128

// Executed Command:
Expand All @@ -28,7 +28,7 @@
// --chain=acala-latest
// --steps=50
// --repeat=20
// --pallet=*
// --pallet=module_cdp_treasury
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
Expand All @@ -47,17 +47,20 @@ use sp_std::marker::PhantomData;
/// Weight functions for module_cdp_treasury.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> module_cdp_treasury::WeightInfo for WeightInfo<T> {
fn auction_collateral() -> Weight {
(66_209_000 as Weight)
fn auction_collateral(b: u32, ) -> Weight {
(51_334_000 as Weight)
// Standard Error: 17_000
.saturating_add((30_152_000 as Weight).saturating_mul(b as Weight))
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(7 as Weight))
.saturating_add(T::DbWeight::get().writes(6 as Weight))
.saturating_add(T::DbWeight::get().writes((3 as Weight).saturating_mul(b as Weight)))
}
ferrell-code marked this conversation as resolved.
Show resolved Hide resolved
fn set_expected_collateral_auction_size() -> Weight {
(20_575_000 as Weight)
(21_773_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn extract_surplus_to_treasury() -> Weight {
(68_325_000 as Weight)
(69_041_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/honzon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn liquidate_cdp() {
AccountId::from(ALICE),
50 * dollar(RELAY_CHAIN_CURRENCY),
250_000 * dollar(USD_CURRENCY),
LiquidationStrategy::Auction,
LiquidationStrategy::Auction { auction_count: 1 },
));

assert!(System::events()
Expand Down
Loading