Statemint runtimes to accept sufficient assets as xcm fee payment (#1278)

* point to my branch

* girazoki-add-TakeFirstAssetTrader-to-utility

* Commit lock

* point at custom branch

* add new trader to statemine runtimes

* compiles

* Back to master

* Update last tomls

* Imports up

* remove non-needing imports

* FMT

* log messages properly

* Use TakeRevenue instead of HandleCredit

* Introduce xcm fee handler

* check total supply in tests

* FMT

* fix test

* Start decoupling balance calculation into different traits

* Make traits a bit more generic

* PR suggestions

* add import

* import well

* Place xcmfeesassethandler into parachains common

* fix tests

* config parameters

* Min amount to fee receiver

* Make minimum amount for block author to be at least the ED

* Doc in AssetFeeAsExistentialDepositMultiplier

* saturating sub

* make sure we dont enter twice

* FMT

* fmt again

* adapt tests

* Add doc and struct for weight refund

* Doc

* More doc

* PR suggestions

* store all info related to asset payment as multiasset

* return AssetNotFound instead of TooExpensive

* Use asset transactor to deposit fee

* uninstall from statemint

* R for RUntime and CON for BalanceConverter

* Rework logic to avoid unnecesary match and error

* Rework ED check, also in case of refund

* rework typo

* In case refund makes drop below ED, just refund the difference

* fix test westmint

* clone id

* move test imports to preamble

* move test imports to preamble

* test-utils with builderS

* lock file updated

* remove unused imports

Co-authored-by: Stephen Shelton <steve@brewcraft.org>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: joepetrowski <joe@parity.io>
This commit is contained in:
girazoki
2022-08-03 18:04:13 +02:00
committed by GitHub
parent acc409cd0a
commit 4d04eebb44
16 changed files with 1209 additions and 11 deletions
@@ -0,0 +1,45 @@
[package]
name = "asset-test-utils"
version = "1.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
description = "Statemint parachain runtime"
[dependencies]
# Substrate
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
pallet-session = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
# Cumulus
pallet-collator-selection = { path = "../../../../pallets/collator-selection", default-features = false }
parachains-common = { path = "../../../common", default-features = false }
[dev-dependencies]
hex-literal = "0.3.4"
[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
[features]
default = [ "std" ]
std = [
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-collator-selection/std",
"pallet-session/std",
"parachains-common/std",
"sp-consensus-aura/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
@@ -0,0 +1,134 @@
use frame_support::traits::GenesisBuild;
use sp_std::marker::PhantomData;
use frame_support::traits::OriginTrait;
use parachains_common::AccountId;
use sp_consensus_aura::AURA_ENGINE_ID;
use sp_core::Encode;
use sp_runtime::{Digest, DigestItem};
pub type BalanceOf<Runtime> = <Runtime as pallet_balances::Config>::Balance;
pub type AccountIdOf<Runtime> = <Runtime as frame_system::Config>::AccountId;
pub type ValidatorIdOf<Runtime> = <Runtime as pallet_session::Config>::ValidatorId;
pub type SessionKeysOf<Runtime> = <Runtime as pallet_session::Config>::Keys;
// Basic builder based on balances, collators and pallet_sessopm
pub struct ExtBuilder<
Runtime: frame_system::Config + pallet_balances::Config + pallet_session::Config,
> {
// endowed accounts with balances
balances: Vec<(AccountIdOf<Runtime>, BalanceOf<Runtime>)>,
// collators to test block prod
collators: Vec<AccountIdOf<Runtime>>,
// keys added to pallet session
keys: Vec<(AccountIdOf<Runtime>, ValidatorIdOf<Runtime>, SessionKeysOf<Runtime>)>,
_runtime: PhantomData<Runtime>,
}
impl<Runtime: frame_system::Config + pallet_balances::Config + pallet_session::Config> Default
for ExtBuilder<Runtime>
{
fn default() -> ExtBuilder<Runtime> {
ExtBuilder { balances: vec![], collators: vec![], keys: vec![], _runtime: PhantomData }
}
}
impl<Runtime: frame_system::Config + pallet_balances::Config + pallet_session::Config>
ExtBuilder<Runtime>
{
pub fn with_balances(
mut self,
balances: Vec<(AccountIdOf<Runtime>, BalanceOf<Runtime>)>,
) -> Self {
self.balances = balances;
self
}
pub fn with_collators(mut self, collators: Vec<AccountIdOf<Runtime>>) -> Self {
self.collators = collators;
self
}
pub fn with_session_keys(
mut self,
keys: Vec<(AccountIdOf<Runtime>, ValidatorIdOf<Runtime>, SessionKeysOf<Runtime>)>,
) -> Self {
self.keys = keys;
self
}
pub fn build(self) -> sp_io::TestExternalities
where
Runtime:
pallet_collator_selection::Config + pallet_balances::Config + pallet_session::Config,
ValidatorIdOf<Runtime>: From<AccountIdOf<Runtime>>,
{
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
pallet_balances::GenesisConfig::<Runtime> { balances: self.balances.into() }
.assimilate_storage(&mut t)
.unwrap();
pallet_collator_selection::GenesisConfig::<Runtime> {
invulnerables: self.collators.clone().into(),
candidacy_bond: Default::default(),
desired_candidates: Default::default(),
}
.assimilate_storage(&mut t)
.unwrap();
pallet_session::GenesisConfig::<Runtime> { keys: self.keys }
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
frame_system::Pallet::<Runtime>::set_block_number(1u32.into());
});
ext
}
}
pub struct RuntimeHelper<Runtime>(PhantomData<Runtime>);
/// Utility function that advances the chain to the desired block number.
/// If an author is provided, that author information is injected to all the blocks in the meantime.
impl<Runtime: frame_system::Config> RuntimeHelper<Runtime>
where
AccountIdOf<Runtime>:
Into<<<Runtime as frame_system::Config>::Origin as OriginTrait>::AccountId>,
{
pub fn run_to_block(n: u32, author: Option<AccountId>) {
while frame_system::Pallet::<Runtime>::block_number() < n.into() {
// Set the new block number and author
match author {
Some(ref author) => {
let pre_digest = Digest {
logs: vec![DigestItem::PreRuntime(AURA_ENGINE_ID, author.encode())],
};
frame_system::Pallet::<Runtime>::reset_events();
frame_system::Pallet::<Runtime>::initialize(
&(frame_system::Pallet::<Runtime>::block_number() + 1u32.into()),
&frame_system::Pallet::<Runtime>::parent_hash(),
&pre_digest,
);
},
None => {
frame_system::Pallet::<Runtime>::set_block_number(
frame_system::Pallet::<Runtime>::block_number() + 1u32.into(),
);
},
}
}
}
pub fn root_origin() -> <Runtime as frame_system::Config>::Origin {
<Runtime as frame_system::Config>::Origin::root()
}
pub fn origin_of(
account_id: AccountIdOf<Runtime>,
) -> <Runtime as frame_system::Config>::Origin {
<Runtime as frame_system::Config>::Origin::signed(account_id.into())
}
}