Rust资产转换与交易支付插件pallet-asset-conversion-tx-payment的使用:实现多链资产互换与手续费支付功能
pallet-asset-conversion-tx-payment
资产转换交易支付Pallet
该pallet允许包含它的运行时(runtime)使用除链原生代币以外的资产支付交易费用。
概述
通过扩展交易来包含一个可选的AssetId
(默认None
表示使用原生代币),该pallet实现了用指定资产支付交易费用的功能。它需要一个类似pallet-transaction-payment
的OnChargeAssetTransaction
实现。内置的AssetConversionAdapter
(实现了OnChargeAssetTransaction
)通过将pallet-transaction-payment
计算出的手续费转换成所需资产来确定费用金额。
集成
该pallet封装了FRAME的交易支付pallet并作为其替代品。这意味着你应在construct_runtime
宏中同时包含这两个pallet,但只需包含该pallet的TransactionExtension
(ChargeAssetTxPayment
)。
许可证: Apache-2.0
完整示例代码
// 在runtime/src/lib.rs中的集成示例
use frame_support::{
construct_runtime,
pallet_prelude::*,
traits::Currency,
};
// 引入必要的pallet
pub use pallet_asset_conversion_tx_payment;
pub use pallet_transaction_payment;
// 配置交易支付pallet
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = pallet_asset_conversion_tx_payment::AssetConversionAdapter<Self>;
// 其他配置...
}
// 配置资产转换交易支付pallet
impl pallet_asset_conversion_tx_payment::Config for Runtime {
type Event = Event;
type OnChargeAssetTransaction = pallet_asset_conversion_tx_payment::AssetConversionAdapter<Self>;
// 其他配置...
}
construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
// 包含原生交易支付pallet
TransactionPayment: pallet_transaction_payment,
// 包含资产转换交易支付pallet
AssetConversionTxPayment: pallet_asset_conversion_tx_payment,
// 其他pallet...
}
);
// 在runtime/src/lib.rs中实现交易扩展
impl frame_system::offchain::CreateTransaction<Runtime, UncheckedExtrinsic> for Runtime {
type Public = <Signature as Verify>::Signer;
type Signature = Signature;
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: Call,
public: Self::Public,
account: AccountId,
nonce: Index,
) -> Option<(Call, <UncheckedExtrinsic as ExtrinsicT>::SignaturePayload)> {
// 使用资产转换交易支付扩展
let tip = 0.into();
let asset_id = None; // None表示使用原生代币,Some(asset_id)表示使用指定资产
let extra: pallet_asset_conversion_tx_payment::ChargeAssetTxPayment<Runtime> =
pallet_asset_conversion_tx_payment::ChargeAssetTxPayment::from(tip, asset_id);
Some((call, (account, nonce, extra)))
}
}
更完整的集成示例
// runtime/src/lib.rs
#![cfg_attr(not(feature = "std"), no_std)]
use sp_std::prelude::*;
use frame_support::{
construct_runtime, parameter_types,
traits::{Currency, Imbalance, OnUnbalanced},
weights::{Weight, DispatchClass},
};
use frame_system::EnsureRoot;
use pallet_transaction_payment::CurrencyAdapter;
// 定义原生代币类型
pub type Balance = u128;
pub type Amount = i128;
parameter_types! {
pub const ExistentialDeposit: Balance = 1;
}
impl pallet_balances::Config for Runtime {
type Balance = Balance;
type DustRemoval = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type MaxLocks = ();
type WeightInfo = ();
}
parameter_types! {
pub const TransactionByteFee: Balance = 1;
pub const OperationalFeeMultiplier: u8 = 5;
}
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = pallet_asset_conversion_tx_payment::AssetConversionAdapter<Self>;
type TransactionByteFee = TransactionByteFee;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = IdentityFee<Balance>;
type FeeMultiplierUpdate = ();
}
impl pallet_asset_conversion_tx_payment::Config for Runtime {
type Event = Event;
type OnChargeAssetTransaction = pallet_asset_conversion_tx_payment::AssetConversionAdapter<Self>;
}
construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: frame_system,
Balances: pallet_balances,
TransactionPayment: pallet_transaction_payment,
AssetConversionTxPayment: pallet_asset_conversion_tx_payment,
}
);
// 实现交易扩展
impl frame_system::offchain::CreateTransaction<Runtime, UncheckedExtrinsic> for Runtime {
type Public = <Signature as Verify>::Signer;
type Signature = Signature;
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: Call,
public: Self::Public,
account: AccountId,
nonce: Index,
) -> Option<(Call, <UncheckedExtrinsic as ExtrinsicT>::SignaturePayload)> {
let tip = 0.into();
// 使用资产ID 1作为示例
let asset_id = Some(1);
let extra: pallet_asset_conversion_tx_payment::ChargeAssetTxPayment<Runtime> =
pallet_asset_conversion_tx_payment::ChargeAssetTxPayment::from(tip, asset_id);
Some((call, (account, nonce, extra)))
}
}
安装
在项目目录中运行以下Cargo命令:
cargo add pallet-asset-conversion-tx-payment
或者在Cargo.toml中添加以下行:
pallet-asset-conversion-tx-payment = "23.0.0"
1 回复