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

Moving pallet-asset-tx-payment from cumulus to substrate #10127

Merged
merged 34 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7eae0c6
Moving `pallet-asset-tx-payment` from cumulus
Oct 30, 2021
f8e7b6b
move pallet-asset-tx-payment into transaction payment directory
apopiak Nov 3, 2021
d22d83a
Merge branch 'master' of github.com:paritytech/substrate into asset-t…
apopiak Nov 3, 2021
78bbed9
cargo +nightly fmt
apopiak Nov 3, 2021
2791253
Adding `pallet-asset-tx-payment` to node runtime
Nov 3, 2021
2de2612
Merge branch 'master' into asset-tx-payment-from-cumulus
Nov 3, 2021
a90cbb6
Updating cargo.lock after merge
Nov 3, 2021
f19b1a2
Merge branch 'asset-tx-payment-from-cumulus' of https://github.com/ge…
Nov 3, 2021
02bf137
forgot this
Nov 3, 2021
74cbc9c
Adding tx-payment signature
Nov 3, 2021
8498007
Missed one more
Nov 3, 2021
1dd51da
`transaction-payment` replaced in`SignedExtension`
Nov 5, 2021
f325b32
Fixing benches
Nov 5, 2021
775bfde
add test to verify that we don't charge on post-dispatch if we didn't…
apopiak Nov 8, 2021
b83eb9e
add (failing) test for asset tx payment of unsigned extrinsics
apopiak Nov 8, 2021
ab19880
fix test by removing debug_assert
apopiak Nov 8, 2021
30a022a
cargo +nightly fmt
apopiak Nov 8, 2021
a5ea79b
Merge branch 'master' into asset-tx-payment-from-cumulus
Nov 8, 2021
d4883ff
typo in `Cargo.lock`
Nov 8, 2021
7d43023
Merge branch 'master' into asset-tx-payment-from-cumulus
Nov 9, 2021
cdb34a2
Object defined twice in lock file
georgesdib Nov 9, 2021
9498aa3
cargo update
Nov 9, 2021
2561622
Merge branch 'paritytech:master' into asset-tx-payment-from-cumulus
georgesdib Nov 10, 2021
e0b877c
remove todo
apopiak Nov 10, 2021
119d560
Apply formatting suggestions from code review
apopiak Nov 10, 2021
8f2770c
Refactoring `post_dispatch` of `asset-tx-payment`
Nov 10, 2021
2d4c777
Merge branch 'master' into asset-tx-payment-from-cumulus
Nov 10, 2021
2f36c4e
Merge branch 'master'
Nov 11, 2021
b622541
Removing redundant `TODO`
Nov 11, 2021
03fa69b
Reverting an accidental bump of `impl-serde`
Nov 11, 2021
6107177
Merge branch 'master' of upsteam
Nov 14, 2021
6b56ab1
Revert unneeded changes to `cargo.lock`
Nov 14, 2021
21d9cc3
Update frame/transaction-payment/asset-tx-payment/src/payment.rs
georgesdib Nov 18, 2021
9d01c8c
Fixing cargo fmt
georgesdib Nov 18, 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
26 changes: 26 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ members = [
"frame/system/rpc/runtime-api",
"frame/timestamp",
"frame/transaction-payment",
"frame/transaction-payment/asset-tx-payment",
"frame/transaction-payment/rpc",
"frame/transaction-payment/rpc/runtime-api",
"frame/transaction-storage",
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ sc-sync-state-rpc = { version = "0.10.0-dev", path = "../../../client/sync-state
frame-system = { version = "4.0.0-dev", path = "../../../frame/system" }
frame-system-rpc-runtime-api = { version = "4.0.0-dev", path = "../../../frame/system/rpc/runtime-api" }
pallet-transaction-payment = { version = "4.0.0-dev", path = "../../../frame/transaction-payment" }
pallet-asset-tx-payment = { version = "4.0.0-dev", path = "../../../frame/transaction-payment/asset-tx-payment/" }
pallet-im-online = { version = "4.0.0-dev", default-features = false, path = "../../../frame/im-online" }

# node-specific dependencies
Expand Down
6 changes: 3 additions & 3 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn create_extrinsic(
)),
frame_system::CheckNonce::<node_runtime::Runtime>::from(nonce),
frame_system::CheckWeight::<node_runtime::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<node_runtime::Runtime>::from(tip),
pallet_asset_tx_payment::ChargeAssetTxPayment::<node_runtime::Runtime>::from(tip, None),
);

let raw_payload = node_runtime::SignedPayload::from_raw(
Expand Down Expand Up @@ -725,15 +725,15 @@ mod tests {
let check_era = frame_system::CheckEra::from(Era::Immortal);
let check_nonce = frame_system::CheckNonce::from(index);
let check_weight = frame_system::CheckWeight::new();
let payment = pallet_transaction_payment::ChargeTransactionPayment::from(0);
let tx_payment = pallet_asset_tx_payment::ChargeAssetTxPayment::from(0, None);
let extra = (
check_spec_version,
check_tx_version,
check_genesis,
check_era,
check_nonce,
check_weight,
payment,
tx_payment,
);
let raw_payload = SignedPayload::from_raw(
function,
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pallet-treasury = { version = "4.0.0-dev", default-features = false, path = "../
pallet-utility = { version = "4.0.0-dev", default-features = false, path = "../../../frame/utility" }
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-payment" }
pallet-transaction-payment-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" }
pallet-asset-tx-payment = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-payment/asset-tx-payment/" }
pallet-transaction-storage = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-storage" }
pallet-uniques = { version = "4.0.0-dev", default-features = false, path = "../../../frame/uniques" }
pallet-vesting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/vesting" }
Expand Down
19 changes: 17 additions & 2 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

//! Some configurable implementations as associated type for the substrate runtime.

use crate::{Authorship, Balances, NegativeImbalance};
use frame_support::traits::{Currency, OnUnbalanced};
use crate::{AccountId, Assets, Authorship, Balances, NegativeImbalance, Runtime};
use frame_support::traits::{
fungibles::{Balanced, CreditOf},
Currency, OnUnbalanced,
};
use pallet_asset_tx_payment::HandleCredit;

pub struct Author;
impl OnUnbalanced<NegativeImbalance> for Author {
Expand All @@ -27,6 +31,17 @@ impl OnUnbalanced<NegativeImbalance> for Author {
}
}

/// A `HandleCredit` implementation that naively transfers the fees to the block author.
/// Will drop and burn the assets in case the transfer fails.
pub struct CreditToBlockAuthor;
impl HandleCredit<AccountId, Assets> for CreditToBlockAuthor {
fn handle_credit(credit: CreditOf<AccountId, Assets>) {
let author = pallet_authorship::Pallet::<Runtime>::author();
// Drop the result which will trigger the `OnDrop` of the imbalance in case of error.
let _ = Assets::resolve(&author, credit);
}
}

#[cfg(test)]
mod multiplier_tests {
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
Expand Down
17 changes: 13 additions & 4 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub use sp_runtime::BuildStorage;

/// Implementations of some helper traits passed into runtime modules as associated types.
pub mod impls;
use impls::Author;
use impls::{Author, CreditToBlockAuthor};

/// Constant values used within the runtime.
pub mod constants;
Expand Down Expand Up @@ -432,6 +432,14 @@ impl pallet_transaction_payment::Config for Runtime {
TargetedFeeAdjustment<Self, TargetBlockFullness, AdjustmentVariable, MinimumMultiplier>;
}

impl pallet_asset_tx_payment::Config for Runtime {
type Fungibles = Assets;
type OnChargeAssetTransaction = pallet_asset_tx_payment::FungiblesAdapter<
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
CreditToBlockAuthor,
>;
}

parameter_types! {
pub const MinimumPeriod: Moment = SLOT_DURATION / 2;
}
Expand Down Expand Up @@ -969,7 +977,7 @@ where
frame_system::CheckEra::<Runtime>::from(era),
frame_system::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
apopiak marked this conversation as resolved.
Show resolved Hide resolved
pallet_asset_tx_payment::ChargeAssetTxPayment::<Runtime>::from(tip, None),
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
Expand Down Expand Up @@ -1168,7 +1176,7 @@ parameter_types! {

impl pallet_assets::Config for Runtime {
type Event = Event;
type Balance = u64;
type Balance = u128;
type AssetId = u32;
type Currency = Balances;
type ForceOrigin = EnsureRoot<AccountId>;
Expand Down Expand Up @@ -1257,6 +1265,7 @@ construct_runtime!(
Indices: pallet_indices,
Balances: pallet_balances,
TransactionPayment: pallet_transaction_payment,
georgesdib marked this conversation as resolved.
Show resolved Hide resolved
AssetTxPayment: pallet_asset_tx_payment,
ElectionProviderMultiPhase: pallet_election_provider_multi_phase,
Staking: pallet_staking,
Session: pallet_session,
Expand Down Expand Up @@ -1315,7 +1324,7 @@ pub type SignedExtra = (
frame_system::CheckEra<Runtime>,
frame_system::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
pallet_asset_tx_payment::ChargeAssetTxPayment<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;
Expand Down
1 change: 1 addition & 0 deletions bin/node/test-runner-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test-runner = { path = "../../../test-utils/test-runner" }
frame-system = { path = "../../../frame/system" }
frame-benchmarking = { path = "../../../frame/benchmarking" }
pallet-transaction-payment = { path = "../../../frame/transaction-payment" }
pallet-asset-tx-payment = { path = "../../../frame/transaction-payment/asset-tx-payment/" }

node-runtime = { path = "../runtime" }
node-primitives = { path = "../primitives" }
Expand Down
2 changes: 1 addition & 1 deletion bin/node/test-runner-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl ChainInfo for NodeTemplateChainInfo {
frame_system::Pallet::<Self::Runtime>::account_nonce(from),
),
frame_system::CheckWeight::<Self::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Self::Runtime>::from(0),
pallet_asset_tx_payment::ChargeAssetTxPayment::<Self::Runtime>::from(0, None),
)
}
}
Expand Down
1 change: 1 addition & 0 deletions bin/node/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/c
frame-system = { version = "4.0.0-dev", path = "../../../frame/system" }
substrate-test-client = { version = "2.0.0", path = "../../../test-utils/client" }
pallet-transaction-payment = { version = "4.0.0-dev", path = "../../../frame/transaction-payment" }
pallet-asset-tx-payment = { version = "4.0.0-dev", path = "../../../frame/transaction-payment/asset-tx-payment/" }
sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" }
sp-timestamp = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/timestamp" }
sp-block-builder = { version = "4.0.0-dev", path = "../../../primitives/block-builder" }
Expand Down
2 changes: 1 addition & 1 deletion bin/node/testing/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn signed_extra(nonce: Index, extra_fee: Balance) -> SignedExtra {
frame_system::CheckEra::from(Era::mortal(256, 0)),
frame_system::CheckNonce::from(nonce),
frame_system::CheckWeight::new(),
pallet_transaction_payment::ChargeTransactionPayment::from(extra_fee),
pallet_asset_tx_payment::ChargeAssetTxPayment::from(extra_fee, None),
)
}

Expand Down
55 changes: 55 additions & 0 deletions frame/transaction-payment/asset-tx-payment/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[package]
name = "pallet-asset-tx-payment"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "pallet to manage transaction payments in assets"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
# Substrate dependencies
sp-core = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/core" }
sp-io = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/io" }
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/runtime" }
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/std" }

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

# Other dependencies
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.126", optional = true }

[dev-dependencies]
smallvec = "1.7.0"
serde_json = "1.0.68"

sp-storage = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/storage" }

pallet-assets = { version = "4.0.0-dev", path = "../../assets" }
pallet-authorship = { version = "4.0.0-dev", path = "../../authorship" }
pallet-balances = { version = "4.0.0-dev", path = "../../balances" }


[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-std/std",
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
"sp-io/std",
"sp-core/std",
"pallet-transaction-payment/std",
]
try-runtime = ["frame-support/try-runtime"]
21 changes: 21 additions & 0 deletions frame/transaction-payment/asset-tx-payment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# pallet-asset-tx-payment

## Asset Transaction Payment Pallet

This pallet allows runtimes that include it to pay for transactions in assets other than the
native token of the chain.

### Overview
It does this by extending transactions to include an optional `AssetId` that specifies the asset
to be used for payment (defaulting to the native token on `None`). It expects an
[`OnChargeAssetTransaction`] implementation analogously to [`pallet-transaction-payment`]. The
included [`FungiblesAdapter`] (implementing [`OnChargeAssetTransaction`]) determines the fee
amount by converting the fee calculated by [`pallet-transaction-payment`] into the desired
asset.

### 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
pallet's [`SignedExtension`] ([`ChargeAssetTxPayment`]).

License: Apache-2.0
Loading