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

rm runtime upgrade #1757

Merged
merged 2 commits into from
Jan 10, 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
17 changes: 1 addition & 16 deletions modules/transaction-payment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl PriceProvider<CurrencyId> for MockPriceSource {
parameter_types! {
// DO NOT CHANGE THIS VALUE, AS IT EFFECT THE TESTCASES.
pub const FeePoolSize: Balance = 10_000;
pub const SwapBalanceThreshold: Balance = 20;
pub const SwapThreshold: Balance = 20;
pub const TransactionPaymentPalletId: PalletId = PalletId(*b"aca/fees");
pub const TreasuryPalletId: PalletId = PalletId(*b"aca/trsy");
pub KaruraTreasuryAccount: AccountId = TreasuryPalletId::get().into_account();
Expand Down Expand Up @@ -304,21 +304,6 @@ construct_runtime!(
}
);

pub struct MockTransactionPaymentUpgrade;

impl frame_support::traits::OnRuntimeUpgrade for MockTransactionPaymentUpgrade {
fn on_runtime_upgrade() -> Weight {
for asset in FeePoolExchangeTokens::get() {
let _ = <transaction_payment::Pallet<Runtime>>::initialize_pool(
asset,
FeePoolSize::get(),
SwapBalanceThreshold::get(),
);
}
0
}
}

pub struct ExtBuilder {
balances: Vec<(AccountId, CurrencyId, Balance)>,
base_weight: u64,
Expand Down
7 changes: 4 additions & 3 deletions modules/transaction-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#![cfg(test)]

use super::*;
use crate::mock::MockTransactionPaymentUpgrade;
use frame_support::traits::OnRuntimeUpgrade;
use frame_support::{
assert_noop, assert_ok, parameter_types,
weights::{DispatchClass, DispatchInfo, Pays},
Expand Down Expand Up @@ -118,7 +116,10 @@ fn do_runtime_upgrade_and_init_balance() {
false
));

MockTransactionPaymentUpgrade::on_runtime_upgrade();
for asset in crate::mock::FeePoolExchangeTokens::get() {
let _ = Pallet::<Runtime>::initialize_pool(asset, FeePoolSize::get(), crate::mock::SwapThreshold::get());
}
// MockTransactionPaymentUpgrade::on_runtime_upgrade();

vec![AUSD, DOT].iter().for_each(|token| {
let ed = (<Currencies as MultiCurrency<AccountId>>::minimum_balance(token.clone())).unique_saturated_into();
Expand Down
36 changes: 2 additions & 34 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,13 +1099,6 @@ impl module_transaction_pause::Config for Runtime {
parameter_types! {
// Sort by fee charge order
pub DefaultFeeSwapPathList: Vec<Vec<CurrencyId>> = vec![vec![AUSD, DOT, ACA], vec![DOT, ACA], vec![LDOT, DOT, ACA]];
// Initial fee pool size. one extrinsic=0.0025 ACA, one block=100 extrinsics.
// 20 blocks trigger an swap, so total balance=0.0025*100*20=5 ACA
pub FeePoolSize: Balance = 5 * dollar(ACA);
// one extrinsic fee=0.0025ACA, one block=100 extrinsics, threshold=0.25+0.1=0.35ACA
pub SwapBalanceThreshold: Balance = Ratio::saturating_from_rational(35, 100).saturating_mul_int(dollar(ACA));
// tokens used as fee charge. the token should have corresponding dex swap pool enabled.
pub FeePoolExchangeTokens: Vec<CurrencyId> = vec![AUSD, DOT];
}

type NegativeImbalance = <Balances as PalletCurrency<AccountId>>::NegativeImbalance;
Expand Down Expand Up @@ -1878,33 +1871,8 @@ pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
TransactionPaymentUpgrade,
>;

pub struct TransactionPaymentUpgrade;
impl frame_support::traits::OnRuntimeUpgrade for TransactionPaymentUpgrade {
fn on_runtime_upgrade() -> Weight {
let initial_rates = FeePoolExchangeTokens::get();
if initial_rates.is_empty() {
0
} else {
for asset in initial_rates {
let _ = <module_transaction_payment::Pallet<Runtime>>::initialize_pool(
asset,
FeePoolSize::get(),
SwapBalanceThreshold::get(),
);
}
<Runtime as frame_system::Config>::BlockWeights::get().max_block
}
}
}
pub type Executive =
frame_executive::Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllPallets, ()>;

#[cfg(not(feature = "disable-runtime-api"))]
impl_runtime_apis! {
Expand Down
67 changes: 19 additions & 48 deletions runtime/integration-tests/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::setup::*;
use frame_support::traits::OnRuntimeUpgrade;
use frame_support::weights::{DispatchClass, DispatchInfo, Pays, Weight};
use karura_runtime::{
FeePoolSize, KarPerSecondAsBased, KaruraTreasuryAccount, KsmPerSecond, NativeTokenExistentialDeposit,
TransactionPaymentPalletId,
KarPerSecondAsBased, KaruraTreasuryAccount, KsmPerSecond, NativeTokenExistentialDeposit, TransactionPaymentPalletId,
};
use module_transaction_payment::TransactionFeePoolTrader;
use sp_runtime::traits::SignedExtension;
Expand All @@ -32,9 +30,20 @@ use sp_runtime::{
use xcm_builder::FixedRateOfFungible;
use xcm_executor::{traits::*, Assets, Config};

#[cfg(feature = "with-karura-runtime")]
fn init_charge_fee_pool() {
for asset in InitialTokenFeePool::get() {
let _ = <module_transaction_payment::Pallet<Runtime>>::initialize_pool(
asset.0,
asset.1,
SwapBalanceThreshold::get(),
);
}
}

#[cfg(feature = "with-karura-runtime")]
#[test]
fn runtime_upgrade_initial_pool_works() {
fn initial_charge_fee_pool_works() {
ExtBuilder::default()
.balances(vec![
(AccountId::from(ALICE), KAR, 100000 * dollar(KAR)),
Expand All @@ -50,7 +59,7 @@ fn runtime_upgrade_initial_pool_works() {
let pool_size = FeePoolSize::get();

// upgrade takes no effect
MockRuntimeUpgrade::on_runtime_upgrade();
init_charge_fee_pool();
assert_eq!(Currencies::free_balance(KAR, &treasury_account), ed);
assert_eq!(Currencies::free_balance(KAR, &fee_account1), 0);

Expand Down Expand Up @@ -92,7 +101,7 @@ fn runtime_upgrade_initial_pool_works() {
0,
false
));
MockRuntimeUpgrade::on_runtime_upgrade();
init_charge_fee_pool();
assert_eq!(Currencies::free_balance(KAR, &treasury_account), ed + pool_size);
vec![KSM, KUSD].iter().for_each(|token| {
let ed =
Expand Down Expand Up @@ -209,7 +218,7 @@ fn trader_works() {
0,
false
));
MockRuntimeUpgrade::on_runtime_upgrade();
init_charge_fee_pool();
assert_eq!(Currencies::free_balance(KAR, &treasury_account), ed);
assert_eq!(Currencies::free_balance(KAR, &fee_account1), pool_size);
assert_eq!(Currencies::free_balance(KSM, &fee_account1), ksm_ed);
Expand Down Expand Up @@ -272,7 +281,7 @@ fn charge_transaction_payment_and_threshold_works() {
0,
false
));
MockRuntimeUpgrade::on_runtime_upgrade();
init_charge_fee_pool();
assert_eq!(Currencies::free_balance(KAR, &treasury_account), native_ed);
assert_eq!(Currencies::free_balance(KAR, &sub_account1), pool_size);
assert_eq!(Currencies::free_balance(KSM, &sub_account1), ksm_ed);
Expand Down Expand Up @@ -368,8 +377,8 @@ fn charge_transaction_payment_and_threshold_works() {
let kar1 = Currencies::free_balance(KAR, &sub_account1);
let ksm1 = Currencies::free_balance(KSM, &sub_account1);
assert_eq!(ksm_ed + ksm_exchange_rate.saturating_mul_int(fee), ksm1);
assert_eq!(kar1 > kar2, true);
assert_eq!(ksm2 > ksm1, true);
assert!(kar1 > kar2);
assert!(ksm2 > ksm1);

// next tx use the new rate to calculate the fee to be transfer.
let new_rate: Ratio = module_transaction_payment::Pallet::<Runtime>::token_exchange_rate(KSM).unwrap();
Expand All @@ -386,41 +395,3 @@ fn charge_transaction_payment_and_threshold_works() {
assert_eq!(new_rate.saturating_mul_int(fee), ksm2 - ksm1);
});
}

#[cfg(feature = "with-acala-runtime")]
#[test]
fn acala_dex_disable_works() {
use acala_runtime::{
AcalaTreasuryAccount, FeePoolSize, NativeTokenExistentialDeposit, TransactionPaymentPalletId,
TransactionPaymentUpgrade,
};

ExtBuilder::default().build().execute_with(|| {
let treasury_account = AcalaTreasuryAccount::get();
let fee_account1: AccountId = TransactionPaymentPalletId::get().into_sub_account(DOT);
let fee_account2: AccountId = TransactionPaymentPalletId::get().into_sub_account(AUSD);
let ed = NativeTokenExistentialDeposit::get();
let pool_size = FeePoolSize::get();

assert_ok!(Currencies::update_balance(
Origin::root(),
MultiAddress::Id(treasury_account.clone()),
ACA,
pool_size.saturating_mul(3).unique_saturated_into(),
));
assert_eq!(Currencies::free_balance(ACA, &treasury_account), ed + pool_size * 3);
vec![DOT, AUSD].iter().for_each(|token| {
let ed = (<Currencies as MultiCurrency<AccountId>>::minimum_balance(token.clone())).unique_saturated_into();
assert_ok!(Currencies::update_balance(
Origin::root(),
MultiAddress::Id(treasury_account.clone()),
token.clone(),
ed,
));
});

TransactionPaymentUpgrade::on_runtime_upgrade();
assert_eq!(Currencies::free_balance(ACA, &fee_account1), 0);
assert_eq!(Currencies::free_balance(ACA, &fee_account2), 0);
});
}
36 changes: 14 additions & 22 deletions runtime/integration-tests/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,23 @@ mod mandala_imports {
pub use karura_imports::*;
#[cfg(feature = "with-karura-runtime")]
mod karura_imports {
pub use frame_support::parameter_types;
use frame_support::weights::Weight;
pub use frame_support::{parameter_types, weights::Weight};
pub use karura_runtime::{
constants::parachains, create_x2_parachain_multilocation, get_all_module_accounts, AcalaOracle, AccountId,
AssetRegistry, AuctionManager, Authority, AuthoritysOriginId, Balance, Balances, BlockNumber, Call, CdpEngine,
CdpTreasury, CreateClassDeposit, CreateTokenDeposit, Currencies, CurrencyId, CurrencyIdConvert,
DataDepositPerByte, DefaultExchangeRate, Dex, EmergencyShutdown, Event, EvmAccounts, ExistentialDeposits,
FeePoolSize, FinancialCouncil, Get, GetNativeCurrencyId, Homa, HomaXcm, Honzon, IdleScheduler, KarPerSecond,
FinancialCouncil, Get, GetNativeCurrencyId, Homa, HomaXcm, Honzon, IdleScheduler, KarPerSecond,
KaruraFoundationAccounts, KsmPerSecond, KusamaBondingDuration, KusdPerSecond, Loans, MaxTipsOfPriority,
MinimumDebitValue, MultiLocation, NativeTokenExistentialDeposit, NetworkId, NftPalletId, OneDay, Origin,
OriginCaller, ParachainAccount, ParachainInfo, ParachainSystem, PolkadotXcm, Proxy, ProxyType, Ratio,
RelayChainBlockNumberProvider, Runtime, Scheduler, Session, SessionManager, SevenDays, SwapBalanceThreshold,
System, Timestamp, TipPerWeightStep, TokenSymbol, Tokens, TreasuryPalletId, Utility, Vesting, XTokens,
XcmConfig, XcmExecutor, EVM, NFT,
RelayChainBlockNumberProvider, Runtime, Scheduler, Session, SessionManager, SevenDays, System, Timestamp,
TipPerWeightStep, TokenSymbol, Tokens, TreasuryPalletId, Utility, Vesting, XTokens, XcmConfig, XcmExecutor,
EVM, NFT,
};
pub use primitives::TradingPair;
pub use runtime_common::{calculate_asset_ratio, cent, dollar, millicent, KAR, KSM, KUSD, LKSM};
pub use sp_runtime::traits::AccountIdConversion;
pub use sp_runtime::{traits::AccountIdConversion, FixedPointNumber};

parameter_types! {
pub EnabledTradingPairs: Vec<TradingPair> = vec![
Expand All @@ -111,26 +110,19 @@ mod karura_imports {
);

parameter_types! {
pub TokenExchangeRates: Vec<(CurrencyId, Balance)> = vec![
// Initial fee pool size. one extrinsic=0.0025 KAR, one block=100 extrinsics.
// 20 blocks trigger an swap, so total balance=0.0025*100*20=5 KAR
pub FeePoolSize: Balance = 5 * dollar(KAR);
// one extrinsic fee=0.0025KAR, one block=100 extrinsics, threshold=0.25+0.1=0.35KAR
pub SwapBalanceThreshold: Balance = Ratio::saturating_from_rational(35, 100).saturating_mul_int(dollar(KAR));
// tokens used as fee charge. the token should have corresponding dex swap pool enabled.
pub InitialTokenFeePool: Vec<(CurrencyId, Balance)> = vec![
(KSM, FeePoolSize::get()),
(KUSD, FeePoolSize::get()),
// this one is to simulate not enough native asset so wouldn't take effect
(LKSM, NativeTokenExistentialDeposit::get() - 1),
];
}

pub struct MockRuntimeUpgrade;
impl frame_support::traits::OnRuntimeUpgrade for MockRuntimeUpgrade {
fn on_runtime_upgrade() -> Weight {
for asset in TokenExchangeRates::get() {
let _ = <module_transaction_payment::Pallet<Runtime>>::initialize_pool(
asset.0,
asset.1,
SwapBalanceThreshold::get(),
);
}
0
}
}
}

#[cfg(feature = "with-acala-runtime")]
Expand Down
36 changes: 2 additions & 34 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,13 +1116,6 @@ parameter_types! {
vec![LKSM, KSM, KAR],
vec![BNC, KUSD, KSM, KAR],
];
// Initial fee pool size. one extrinsic=0.0025 KAR, one block=100 extrinsics.
// 20 blocks trigger an swap, so total balance=0.0025*100*20=5 KAR
pub FeePoolSize: Balance = 5 * dollar(KAR);
// one extrinsic fee=0.0025KAR, one block=100 extrinsics, threshold=0.25+0.1=0.35KAR
pub SwapBalanceThreshold: Balance = Ratio::saturating_from_rational(35, 100).saturating_mul_int(dollar(KAR));
// tokens used as fee charge. the token should have corresponding dex swap pool enabled.
pub FeePoolExchangeTokens: Vec<CurrencyId> = vec![KUSD, KSM, LKSM, BNC];
}

type NegativeImbalance = <Balances as PalletCurrency<AccountId>>::NegativeImbalance;
Expand Down Expand Up @@ -1997,33 +1990,8 @@ pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
TransactionPaymentUpgrade,
>;

pub struct TransactionPaymentUpgrade;
impl frame_support::traits::OnRuntimeUpgrade for TransactionPaymentUpgrade {
fn on_runtime_upgrade() -> Weight {
let initial_rates = FeePoolExchangeTokens::get();
if initial_rates.is_empty() {
0
} else {
for asset in initial_rates {
let _ = <module_transaction_payment::Pallet<Runtime>>::initialize_pool(
asset,
FeePoolSize::get(),
SwapBalanceThreshold::get(),
);
}
<Runtime as frame_system::Config>::BlockWeights::get().max_block
}
}
}
pub type Executive =
frame_executive::Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllPallets, ()>;

#[cfg(not(feature = "disable-runtime-api"))]
impl_runtime_apis! {
Expand Down
6 changes: 3 additions & 3 deletions runtime/mandala/src/benchmarking/transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

use super::utils::set_balance;
use crate::{
dollar, AccountId, Balance, Currencies, CurrencyId, Dex, Event, FeePoolSize, GetNativeCurrencyId,
GetStableCurrencyId, NativeTokenExistentialDeposit, Origin, Runtime, System, TransactionPayment, TreasuryPalletId,
dollar, AccountId, Balance, Currencies, CurrencyId, Dex, Event, GetNativeCurrencyId, GetStableCurrencyId,
NativeTokenExistentialDeposit, Origin, Runtime, System, TransactionPayment, TreasuryPalletId,
};
use frame_benchmarking::{account, whitelisted_caller};
use frame_support::traits::OnFinalize;
Expand Down Expand Up @@ -91,7 +91,7 @@ runtime_benchmarks! {
let sub_account: AccountId = <Runtime as module_transaction_payment::Config>::PalletId::get().into_sub_account(STABLECOIN);
let native_ed: Balance = <Currencies as MultiCurrency<AccountId>>::minimum_balance(NATIVECOIN);
let stable_ed: Balance = <Currencies as MultiCurrency<AccountId>>::minimum_balance(STABLECOIN);
let pool_size: Balance = FeePoolSize::get();
let pool_size: Balance = native_ed * 50;
let swap_threshold: Balance = native_ed * 2;

let path = vec![STABLECOIN, NATIVECOIN];
Expand Down
Loading