diff --git a/substrate/bin/node-template/runtime/src/lib.rs b/substrate/bin/node-template/runtime/src/lib.rs index f4372af525da5b367d531bc1b2a5d3958d24c027..baba5d9b05e592d861efe71ad3fee8c59ccf0820 100644 --- a/substrate/bin/node-template/runtime/src/lib.rs +++ b/substrate/bin/node-template/runtime/src/lib.rs @@ -477,6 +477,12 @@ impl_runtime_apis! { ) -> pallet_transaction_payment::FeeDetails<Balance> { TransactionPayment::query_fee_details(uxt, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi<Block, Balance, RuntimeCall> @@ -494,6 +500,12 @@ impl_runtime_apis! { ) -> pallet_transaction_payment::FeeDetails<Balance> { TransactionPayment::query_call_fee_details(call, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } #[cfg(feature = "runtime-benchmarks")] diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 1f85d118ea5348830eed89c3d2d6f5bdc7fca124..0121f1ee3a8caae80921f125f4f1ae45c2dec27f 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2120,6 +2120,12 @@ impl_runtime_apis! { fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> FeeDetails<Balance> { TransactionPayment::query_fee_details(uxt, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentCallApi<Block, Balance, RuntimeCall> @@ -2131,6 +2137,12 @@ impl_runtime_apis! { fn query_call_fee_details(call: RuntimeCall, len: u32) -> FeeDetails<Balance> { TransactionPayment::query_call_fee_details(call, len) } + fn query_weight_to_fee(weight: Weight) -> Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> Balance { + TransactionPayment::length_to_fee(length) + } } impl pallet_mmr::primitives::MmrApi< diff --git a/substrate/frame/transaction-payment/rpc/runtime-api/src/lib.rs b/substrate/frame/transaction-payment/rpc/runtime-api/src/lib.rs index 10fd2a9e61fc18f55b6d3314fbdca96de078cb18..760f9e3693417d7df932fab2bb2ea61d0b958f1e 100644 --- a/substrate/frame/transaction-payment/rpc/runtime-api/src/lib.rs +++ b/substrate/frame/transaction-payment/rpc/runtime-api/src/lib.rs @@ -25,7 +25,7 @@ use sp_runtime::traits::MaybeDisplay; pub use pallet_transaction_payment::{FeeDetails, InclusionFee, RuntimeDispatchInfo}; sp_api::decl_runtime_apis! { - #[api_version(2)] + #[api_version(3)] pub trait TransactionPaymentApi<Balance> where Balance: Codec + MaybeDisplay, { @@ -33,9 +33,11 @@ sp_api::decl_runtime_apis! { fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance, sp_weights::OldWeight>; fn query_info(uxt: Block::Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance>; fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails<Balance>; + fn query_weight_to_fee(weight: sp_weights::Weight) -> Balance; + fn query_length_to_fee(length: u32) -> Balance; } - #[api_version(2)] + #[api_version(3)] pub trait TransactionPaymentCallApi<Balance, Call> where Balance: Codec + MaybeDisplay, @@ -46,5 +48,11 @@ sp_api::decl_runtime_apis! { /// Query fee details of a given encoded `Call`. fn query_call_fee_details(call: Call, len: u32) -> FeeDetails<Balance>; + + /// Query the output of the current `WeightToFee` given some input. + fn query_weight_to_fee(weight: sp_weights::Weight) -> Balance; + + /// Query the output of the current `LengthToFee` given some input. + fn query_length_to_fee(length: u32) -> Balance; } } diff --git a/substrate/frame/transaction-payment/src/lib.rs b/substrate/frame/transaction-payment/src/lib.rs index 13adbf89c4270b78ee93f5a5c115c1a567f6d105..fb4381c5242c6e1d474ce5eac264dc23f5216158 100644 --- a/substrate/frame/transaction-payment/src/lib.rs +++ b/substrate/frame/transaction-payment/src/lib.rs @@ -612,11 +612,14 @@ where } } - fn length_to_fee(length: u32) -> BalanceOf<T> { + /// Compute the length portion of a fee by invoking the configured `LengthToFee` impl. + pub fn length_to_fee(length: u32) -> BalanceOf<T> { T::LengthToFee::weight_to_fee(&Weight::from_ref_time(length as u64)) } - fn weight_to_fee(weight: Weight) -> BalanceOf<T> { + /// Compute the unadjusted portion of the weight fee by invoking the configured `WeightToFee` + /// impl. Note that the input `weight` is capped by the maximum block weight before computation. + pub fn weight_to_fee(weight: Weight) -> BalanceOf<T> { // cap the weight to the maximum defined in runtime, otherwise it will be the // `Bounded` maximum of its data type, which is not desired. let capped_weight = weight.min(T::BlockWeights::get().max_block);