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

Network fee distribution and Incentive Pools #2153

Draft
wants to merge 58 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
979cc54
fee distributionn framework
zqhxuyuan May 23, 2022
5e67844
do_set_rate
zqhxuyuan May 24, 2022
143de0a
xcm fee
zqhxuyuan May 24, 2022
f9fcd8c
income source as paramter
zqhxuyuan May 24, 2022
bac9f23
mandala tx fee to collator
zqhxuyuan May 25, 2022
e93bde1
mandala init genesis fees
zqhxuyuan May 25, 2022
9d27a84
fees mock test
zqhxuyuan May 26, 2022
e82485f
merge master
zqhxuyuan May 27, 2022
8538d0c
dispatch call test
zqhxuyuan May 27, 2022
149a2f5
use PoolPercent as parameters
zqhxuyuan Jun 9, 2022
cede726
fix clippy
zqhxuyuan Jun 9, 2022
74c45c2
check rate
zqhxuyuan Jun 9, 2022
cb22815
tx fee allocation refactor
zqhxuyuan Jun 9, 2022
b034472
fix clippy and genesis use u32 tuple
zqhxuyuan Jun 10, 2022
c98a4cf
fix test
zqhxuyuan Jun 10, 2022
7d81546
rename on_fee_deposit
zqhxuyuan Jun 13, 2022
e6eb9bd
add DealTxFeesWithAccount
zqhxuyuan Jun 13, 2022
6558c9e
extract distribution_fees
zqhxuyuan Jun 13, 2022
eec6dc9
add dex for distribution treasury to incentive
zqhxuyuan Jun 14, 2022
2eb5e13
distribution fee test
zqhxuyuan Jun 14, 2022
388a759
distribut incentive dex test
zqhxuyuan Jun 14, 2022
92eef59
add threshold and Period
zqhxuyuan Jun 14, 2022
9d9faa8
rename to AllocationPeriod
zqhxuyuan Jun 14, 2022
0226b92
Changed Homa momdule's staking reward to go to OnFeeDeposit configure…
Jun 14, 2022
d72d8d5
merge master
zqhxuyuan Jun 14, 2022
2c7211c
update to v0.9.22
zqhxuyuan Jun 14, 2022
94a393f
benchmark
zqhxuyuan Jun 14, 2022
7314463
build_pool_percents helper method
zqhxuyuan Jun 15, 2022
c768270
fix benchmark
zqhxuyuan Jun 15, 2022
2f575ef
move HomaTreasuryPalletId to common
zqhxuyuan Jun 15, 2022
d112816
Acala XcmFeeToTreasury
zqhxuyuan Jun 15, 2022
74e5909
remove AccountId from XcmFeeToTreasury
zqhxuyuan Jun 16, 2022
2973996
merge master and update to v0.9.23
zqhxuyuan Jun 16, 2022
f609940
Changed the interest accumulation so the generated stable currencies …
Jun 16, 2022
cec2f74
xcm fee go to NetworkTreasuryPool instead TreasuryAccount
zqhxuyuan Jun 16, 2022
02b4ff4
Merge branch 'fee_distribution' of ssh://github.com/AcalaNetwork/Acal…
Jun 16, 2022
fc1b802
Pulled from the latest from the branch
Jun 16, 2022
a45260c
Fixed acala integration test build problem
Jun 16, 2022
679a97b
Updated a unit test
Jun 17, 2022
ca5f62c
add some comments
zqhxuyuan Jun 17, 2022
49aa1d6
Merge branch 'fee_distribution' of https://github.com/AcalaNetwork/Ac…
zqhxuyuan Jun 17, 2022
19877f1
dev genesis config to Karura/Acala
zqhxuyuan Jun 20, 2022
e4c4923
FeesConfig
zqhxuyuan Jun 20, 2022
0a4aa80
fix clippy
zqhxuyuan Jun 20, 2022
b2c50f6
Merge remote-tracking branch 'origin/master' into fee_distribution
zqhxuyuan Jun 20, 2022
30122aa
merge master
zqhxuyuan Jun 20, 2022
351a8b5
cargo run --profile production --color=never --bin=acala --features=r…
Jun 20, 2022
af7347a
runtime upgrade
zqhxuyuan Jun 20, 2022
c89715c
Merge branch 'fee_distribution' of https://github.com/AcalaNetwork/Ac…
zqhxuyuan Jun 20, 2022
57444e5
cargo run --profile production --color=never --bin=acala --features=r…
Jun 20, 2022
d79e376
cargo run --profile production --color=never --bin=acala --features=r…
Jun 20, 2022
74b5b6f
Merge remote-tracking branch 'origin/master' into fee_distribution
Jun 20, 2022
8a21a4a
cargo run --release --color=never --bin=acala --features=runtime-benc…
Jun 20, 2022
db24f7e
update runtime WeightInfo
zqhxuyuan Jun 21, 2022
c17c11f
add DistributeTxFees to Karura/Acala
zqhxuyuan Jun 21, 2022
117f791
add post_upgrade assert
zqhxuyuan Jun 21, 2022
d0e6454
Liquidation penalty now goes to OnFeeDeposit, and the same amount of …
Jun 21, 2022
d5707e5
Revert "Liquidation penalty now goes to OnFeeDeposit, and the same am…
Jul 3, 2022
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
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions modules/cdp-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
support = { package = "module-support", path = "../support", default-features = false }
loans = { package = "module-loans", path = "../loans", default-features = false }
module-fees = { path = "../fees", default-features = false }
primitives = { package = "acala-primitives", path = "../../primitives", default-features = false }
rand_chacha = { version = "0.2", default-features = false }
nutsfinance-stable-asset = { version = "0.1.0", default-features = false, path = "../../ecosystem-modules/stable-asset/lib/stable-asset", package = "nutsfinance-stable-asset" }
Expand Down Expand Up @@ -46,6 +47,7 @@ std = [
"sp-std/std",
"support/std",
"loans/std",
"module-fees/std",
"primitives/std",
"orml-utilities/std",
]
Expand Down
17 changes: 12 additions & 5 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use frame_system::{
};
use orml_traits::{Change, GetByKey, MultiCurrency};
use orml_utilities::OffchainErr;
use primitives::{Amount, Balance, CurrencyId, Position};
use primitives::{Amount, Balance, CurrencyId, IncomeSource, Position};
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
ChaChaRng,
Expand All @@ -56,8 +56,8 @@ use sp_runtime::{
};
use sp_std::prelude::*;
use support::{
CDPTreasury, CDPTreasuryExtended, DEXManager, EmergencyShutdown, ExchangeRate, Price, PriceProvider, Rate, Ratio,
RiskManager, Swap, SwapLimit,
CDPTreasury, CDPTreasuryExtended, DEXManager, EmergencyShutdown, ExchangeRate, OnFeeDeposit, Price, PriceProvider,
Rate, Ratio, RiskManager, Swap, SwapLimit,
};

mod mock;
Expand Down Expand Up @@ -186,6 +186,9 @@ pub mod module {
/// Swap
type Swap: Swap<Self::AccountId, Balance, CurrencyId>;

/// Where the fees are go to.
type OnFeeDeposit: OnFeeDeposit<Self::AccountId, CurrencyId, Balance>;

/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -553,8 +556,12 @@ impl<T: Config> Pallet<T> {
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);
// Staking rewards goes to T::OnFeeDeposit
let res = T::OnFeeDeposit::on_fee_deposit(
IncomeSource::HonzonStabilityFee,
T::GetStableCurrencyId::get(),
issued_stable_coin_balance,
);
match res {
Ok(_) => {
// update exchange rate when issue success
Expand Down
35 changes: 25 additions & 10 deletions modules/cdp-engine/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use frame_support::{
traits::{ConstU128, ConstU32, ConstU64, Everything, Nothing},
PalletId,
};
use frame_system::EnsureSignedBy;
use frame_system::{EnsureRoot, EnsureSignedBy};
use orml_traits::parameter_type_with_key;
use primitives::{DexShare, Moment, TokenSymbol, TradingPair};
use sp_core::H256;
Expand Down Expand Up @@ -122,6 +122,7 @@ pub type AdaptedBasicCurrency = orml_currencies::BasicCurrencyAdapter<Runtime, P

parameter_types! {
pub const GetNativeCurrencyId: CurrencyId = ACA;
pub const GetStakingCurrencyId: CurrencyId = DOT;
}

impl orml_currencies::Config for Runtime {
Expand Down Expand Up @@ -305,6 +306,18 @@ parameter_types! {
pub MaxSwapSlippageCompareToOracle: Ratio = Ratio::saturating_from_rational(50, 100);
}

impl module_fees::Config for Runtime {
type Event = Event;
type UpdateOrigin = EnsureRoot<AccountId>;
type Currency = PalletBalances;
type Currencies = Currencies;
type NativeCurrencyId = GetNativeCurrencyId;
type AllocationPeriod = ConstU64<10>;
type DEX = ();
type DexSwapJointList = AlternativeSwapPathJointList;
type WeightInfo = ();
}

impl Config for Runtime {
type Event = Event;
type PriceSource = MockPriceSource;
Expand All @@ -323,6 +336,7 @@ impl Config for Runtime {
type Currency = Currencies;
type DEX = DEXModule;
type Swap = SpecificJointsSwap<DEXModule, AlternativeSwapPathJointList>;
type OnFeeDeposit = Fees;
type WeightInfo = ();
}

Expand All @@ -335,15 +349,16 @@ construct_runtime!(
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Storage, Config, Event<T>},
CDPEngineModule: cdp_engine::{Pallet, Storage, Call, Event<T>, Config, ValidateUnsigned},
CDPTreasuryModule: cdp_treasury::{Pallet, Storage, Call, Config, Event<T>},
Currencies: orml_currencies::{Pallet, Call},
Tokens: orml_tokens::{Pallet, Storage, Event<T>, Config<T>},
LoansModule: loans::{Pallet, Storage, Call, Event<T>},
PalletBalances: pallet_balances::{Pallet, Call, Storage, Event<T>},
DEXModule: dex::{Pallet, Storage, Call, Event<T>, Config<T>},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
System: frame_system,
CDPEngineModule: cdp_engine,
CDPTreasuryModule: cdp_treasury,
Currencies: orml_currencies,
Tokens: orml_tokens,
LoansModule: loans,
PalletBalances: pallet_balances,
DEXModule: dex,
Timestamp: pallet_timestamp,
Fees: module_fees,
}
);

Expand Down
88 changes: 88 additions & 0 deletions modules/cdp-engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use super::*;
use frame_support::{assert_noop, assert_ok};
use mock::{Call as MockCall, Event, *};
use module_fees::PoolPercent;
use orml_traits::MultiCurrency;
use sp_core::offchain::{testing, OffchainDbExt, OffchainWorkerExt, TransactionPoolExt};
use sp_io::offchain;
Expand Down Expand Up @@ -58,6 +59,17 @@ fn setup_default_collateral(currency_id: CurrencyId) {
));
}

fn setup_fees_distribution() {
assert_ok!(Fees::set_income_fee(
Origin::root(),
IncomeSource::HonzonStabilityFee,
vec![PoolPercent {
pool: BOB,
rate: Rate::one(),
}],
));
}

#[test]
fn check_cdp_status_work() {
ExtBuilder::default().build().execute_with(|| {
Expand Down Expand Up @@ -1347,6 +1359,8 @@ fn compound_interest_rate_work() {
#[test]
fn accumulate_interest_work() {
ExtBuilder::default().build().execute_with(|| {
setup_fees_distribution();

assert_ok!(CDPEngineModule::set_collateral_params(
Origin::signed(1),
BTC,
Expand Down Expand Up @@ -1862,3 +1876,77 @@ fn minimal_collateral_works() {
assert_ok!(CDPEngineModule::adjust_position(&ALICE, BTC, 0, 0));
});
}

#[test]
fn accumulated_interest_goes_to_on_fee_deposit() {
ExtBuilder::default().build().execute_with(|| {
setup_fees_distribution();

assert_ok!(CDPEngineModule::set_collateral_params(
Origin::signed(1),
BTC,
Change::NewValue(Some(Rate::saturating_from_rational(1, 100))),
Change::NewValue(Some(Ratio::saturating_from_rational(2, 1))),
Change::NewValue(Some(Rate::zero())),
Change::NewValue(Some(Ratio::saturating_from_rational(2, 1))),
Change::NewValue(10000),
));
assert_ok!(CDPEngineModule::set_collateral_params(
Origin::signed(1),
DOT,
Change::NewValue(Some(Rate::one())),
Change::NewValue(Some(Ratio::saturating_from_rational(2, 1))),
Change::NewValue(Some(Rate::zero())),
Change::NewValue(Some(Ratio::saturating_from_rational(2, 1))),
Change::NewValue(10000),
));
assert_eq!(
CDPEngineModule::get_interest_rate_per_sec(BTC),
Ok(Rate::saturating_from_rational(1, 100))
);
assert_eq!(CDPEngineModule::get_interest_rate_per_sec(DOT), Ok(Rate::one()));

// Treasury starts off empty.
assert_eq!(Currencies::free_balance(AUSD, &BOB), 0);

CDPEngineModule::accumulate_interest(1, 0);

// No debit generates no interest.
assert_eq!(Currencies::free_balance(AUSD, &BOB), 0);

assert_ok!(CDPEngineModule::adjust_position(&ALICE, BTC, 1000, 1000));
assert_ok!(CDPEngineModule::adjust_position(&ALICE, DOT, 1000, 1000));

CDPEngineModule::accumulate_interest(2, 1);
assert_eq!(CDPEngineModule::last_accumulation_secs(), 2);

// Generated interest = debit * debit_exchange_rate * interest_rate
// = 1000 * 0.1 * 0.01 + 1000 * 0.1 * 1 = 101
assert_eq!(Currencies::free_balance(AUSD, &BOB), 101);

assert_eq!(
CDPEngineModule::get_debit_exchange_rate(BTC),
ExchangeRate::saturating_from_rational(101, 1000)
);
assert_eq!(
CDPEngineModule::get_debit_exchange_rate(DOT),
ExchangeRate::saturating_from_rational(2, 10)
);

CDPEngineModule::accumulate_interest(3, 2);
assert_eq!(CDPEngineModule::last_accumulation_secs(), 3);

// Generated interest = debit * debit_exchange_rate * interest_rate
// = 1000 * 0.101 * 0.01 + 1000 * 0.2 * 1 = 201
assert_eq!(Currencies::free_balance(AUSD, &BOB), 302);

assert_eq!(
CDPEngineModule::get_debit_exchange_rate(BTC),
ExchangeRate::saturating_from_rational(10201, 100000)
);
assert_eq!(
CDPEngineModule::get_debit_exchange_rate(DOT),
ExchangeRate::saturating_from_rational(4, 10)
);
});
}
49 changes: 49 additions & 0 deletions modules/fees/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[package]
name = "module-fees"
version = "2.6.3"
authors = ["Acala Developers"]
edition = "2021"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
scale-info = { version = "2.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", optional = true }

frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }

orml-traits = { package = "orml-traits", path = "../../orml/traits", default-features = false }
orml-tokens = { package = "orml-tokens", path = "../../orml/tokens", default-features = false }

support = { package = "module-support", path = "../support", default-features = false }
primitives = { package = "acala-primitives", path = "../../primitives", default-features = false }

paste = "1.0"

[dev-dependencies]
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
orml-tokens = { path = "../../orml/tokens" }
module-currencies = { path = "../../modules/currencies" }
module-dex = { path = "../../modules/dex" }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"scale-info/std",
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
"sp-core/std",
"sp-std/std",
"orml-traits/std",
"orml-tokens/std",
"support/std",
"primitives/std",
]
try-runtime = ["frame-support/try-runtime"]
Loading