Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[assets tx payment] configurable default asset #12087

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

27 changes: 20 additions & 7 deletions bin/node/runtime/src/lib.rs
Expand Up @@ -32,9 +32,9 @@ use frame_support::{
pallet_prelude::Get,
parameter_types,
traits::{
AsEnsureOriginWithArg, ConstU128, ConstU16, ConstU32, Currency, EitherOfDiverse,
EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, KeyOwnerProofSystem,
LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote,
tokens::OneToOneBalanceConversion, AsEnsureOriginWithArg, ConstU128, ConstU16, ConstU32,
Currency, EitherOfDiverse, EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter,
KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote,
},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
Expand Down Expand Up @@ -459,13 +459,26 @@ impl pallet_transaction_payment::Config for Runtime {
TargetedFeeAdjustment<Self, TargetBlockFullness, AdjustmentVariable, MinimumMultiplier>;
}

parameter_types! {
pub const UseUserConfiguration: bool = false;
}

type AssetsBalances = pallet_asset_tx_payment::FungiblesAdapter<
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
CreditToBlockAuthor,
>;

impl pallet_asset_tx_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = Assets;
type OnChargeAssetTransaction = pallet_asset_tx_payment::FungiblesAdapter<
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
CreditToBlockAuthor,
>;
type OnChargeAssetTransaction = AssetsBalances;
type UseUserConfiguration = UseUserConfiguration;
type WeightInfo = ();
type ConfigurationOrigin = EnsureRoot<AccountId>;
type PayableCall = RuntimeCall;
type ConfigurationExistentialDeposit = ConstU128<100>;
type BalanceConverter = OneToOneBalanceConversion;
type Lock = Assets;
}

parameter_types! {
Expand Down
52 changes: 51 additions & 1 deletion frame/assets/src/impl_fungibles.rs
Expand Up @@ -252,7 +252,7 @@ impl<T: Config<I>, I: 'static> fungibles::approvals::Mutate<<T as SystemConfig>:
Self::do_approve_transfer(asset, owner, delegate, amount)
}

// Aprove spending tokens from a given account
// Approve spending tokens from a given account
fn transfer_from(
asset: T::AssetId,
owner: &<T as SystemConfig>::AccountId,
Expand Down Expand Up @@ -283,3 +283,53 @@ impl<T: Config<I>, I: 'static> fungibles::roles::Inspect<<T as SystemConfig>::Ac
Asset::<T, I>::get(asset).map(|x| x.freezer)
}
}

// noop hold implementation
// real impl can be next pr
impl<T: Config<I>, I: 'static> fungibles::InspectHold<<T as SystemConfig>::AccountId>
for Pallet<T, I>
{
fn balance_on_hold(_asset: T::AssetId, _who: &<T as SystemConfig>::AccountId) -> Self::Balance {
<_>::default()
}

fn can_hold(
_asset: T::AssetId,
_who: &<T as SystemConfig>::AccountId,
_amount: Self::Balance,
) -> bool {
false
}
}

impl<T: Config<I>, I: 'static> fungibles::MutateHold<<T as SystemConfig>::AccountId>
for Pallet<T, I>
{
fn hold(
_asset: T::AssetId,
_who: &<T as SystemConfig>::AccountId,
_amount: Self::Balance,
) -> DispatchResult {
Err(DispatchError::Other("not implemented"))
}

fn release(
_asset: T::AssetId,
_who: &<T as SystemConfig>::AccountId,
_amount: Self::Balance,
_best_effort: bool,
) -> Result<Self::Balance, DispatchError> {
Err(DispatchError::Other("not implemented"))
}

fn transfer_held(
_asset: Self::AssetId,
_source: &<T as SystemConfig>::AccountId,
_dest: &<T as SystemConfig>::AccountId,
_amount: Self::Balance,
_best_effort: bool,
_on_hold: bool,
) -> Result<Self::Balance, DispatchError> {
Err(DispatchError::Other("not implemented"))
}
}
2 changes: 1 addition & 1 deletion frame/support/src/traits/tokens.rs
Expand Up @@ -27,5 +27,5 @@ pub mod nonfungibles;
pub use imbalance::Imbalance;
pub use misc::{
AssetId, Balance, BalanceConversion, BalanceStatus, DepositConsequence, ExistenceRequirement,
Locker, WithdrawConsequence, WithdrawReasons,
Locker, OneToOneBalanceConversion, WithdrawConsequence, WithdrawReasons,
};
8 changes: 8 additions & 0 deletions frame/support/src/traits/tokens/misc.rs
Expand Up @@ -195,6 +195,14 @@ pub trait BalanceConversion<InBalance, AssetId, OutBalance> {
fn to_asset_balance(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>;
}

pub struct OneToOneBalanceConversion;
impl<Balance, AssetId> BalanceConversion<Balance, AssetId, Balance> for OneToOneBalanceConversion {
type Error = DispatchError;
fn to_asset_balance(balance: Balance, _asset_id: AssetId) -> Result<Balance, Self::Error> {
Ok(balance)
}
}

/// Trait to handle asset locking mechanism to ensure interactions with the asset can be implemented
/// downstream to extend logic of Uniques current functionality.
pub trait Locker<CollectionId, ItemId> {
Expand Down
5 changes: 5 additions & 0 deletions frame/transaction-payment/Cargo.toml
Expand Up @@ -43,3 +43,8 @@ std = [
"sp-std/std",
]
try-runtime = ["frame-support/try-runtime"]

runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
8 changes: 8 additions & 0 deletions frame/transaction-payment/asset-tx-payment/Cargo.toml
Expand Up @@ -21,6 +21,7 @@ sp-std = { version = "4.0.0", default-features = false, path = "../../../primiti

frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../../benchmarking" }
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = ".." }

# Other dependencies
Expand Down Expand Up @@ -53,3 +54,10 @@ std = [
"pallet-transaction-payment/std",
]
try-runtime = ["frame-support/try-runtime"]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-transaction-payment/runtime-benchmarks",
]
2 changes: 2 additions & 0 deletions frame/transaction-payment/asset-tx-payment/README.md
Expand Up @@ -13,6 +13,8 @@ included [`FungiblesAdapter`] (implementing [`OnChargeAssetTransaction`]) determ
amount by converting the fee calculated by [`pallet-transaction-payment`] into the desired
asset.

Optionally allows configure default payment asset per account.

### Integration
This pallet wraps FRAME's transaction payment pallet and functions as a replacement. This means
you should include both pallets in your `construct_runtime` macro, but only include this
Expand Down
38 changes: 38 additions & 0 deletions frame/transaction-payment/asset-tx-payment/src/benchmarking.rs
@@ -0,0 +1,38 @@
// This file is part of Substrate.

// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Benchmarks for AssetTxPayment Pallet

#![cfg(feature = "runtime-benchmarks")]

use super::*;
#[allow(unused_imports)] // benchmarks
use crate::Pallet as AssetTxPayment;
use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_system::RawOrigin;

benchmarks! {
set_payment_asset {
let caller: T::AccountId = whitelisted_caller();
// NOTE: best effort without refactoring to support runtime for tests (need asset_id)
}: _(RawOrigin::Signed(caller.clone()), caller.clone(), <_>::default())
verify {
let actual = <PaymentAssets::<T>>::get(&caller);
assert_eq!(actual, None);
}
impl_benchmark_test_suite!(AssetTxPayment, crate::tests::new_test_ext(), crate::tests::Runtime);
}