Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update transaction fee xpallet #561

Merged
merged 20 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
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.

4 changes: 2 additions & 2 deletions primitives/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub type RpcVoteWeight<Weight> = RpcU128<Weight>;

/// A helper struct for handling u128 serialization/deserialization of RPC.
/// See https://github.com/polkadot-js/api/issues/2464 for details (shit!).
#[derive(Eq, PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
#[derive(Eq, PartialEq, Copy, Clone, Debug, Serialize, Deserialize, Default)]
pub struct RpcU128<T: Display + FromStr>(#[serde(with = "self::serde_num_str")] T);

impl<T: Display + FromStr> From<T> for RpcU128<T> {
impl<T: Display + FromStr + Default> From<T> for RpcU128<T> {
fn from(value: T) -> Self {
RpcU128(value)
}
Expand Down
14 changes: 9 additions & 5 deletions runtime/chainx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,8 +1323,12 @@ impl_runtime_apis! {
TransactionPayment::query_info(uxt, len)
}
}
fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> pallet_transaction_payment::FeeDetails<Balance> {
todo!("Migrate ChainX query_fee_details")
fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> pallet_transaction_payment_rpc_runtime_api::FeeDetails<Balance> {
//todo!("Migrate ChainX query_fee_details")
//In https://github.com/paritytech/substrate/blob/master/frame/transaction-payment/rpc/runtime-api/src/lib.rs , the function(query_fee_details)
//in trait(TransactionPaymentApi) returns the result FeeDetails which has been defined in frame before , so there is no need to take
// care of the property extra_fee,maybe the function here is useless..
TransactionPayment::query_fee_details(uxt, len)
}
}

Expand All @@ -1334,11 +1338,11 @@ impl_runtime_apis! {
len: u32,
) -> xpallet_transaction_fee::FeeDetails<Balance> {
if let Some(extra_fee) = ChargeExtraFee::has_extra_fee(&uxt.function) {
let details = XTransactionFee::query_fee_details(uxt, len);
let partial_details = XTransactionFee::query_fee_details(uxt, len);
xpallet_transaction_fee::FeeDetails {
extra_fee,
final_fee: details.final_fee + extra_fee,
..details
final_fee: partial_details.final_fee + extra_fee,
..partial_details
}
} else {
XTransactionFee::query_fee_details(uxt, len)
Expand Down
2 changes: 1 addition & 1 deletion xpallets/assets/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
C::Api: XAssetsRuntimeApi<Block, AccountId, Balance>,
Block: BlockT,
AccountId: Clone + Display + Codec,
Balance: Clone + Copy + Display + FromStr + Codec + Zero,
Balance: Clone + Copy + Display + FromStr + Codec + Zero + Default,
{
fn assets_by_account(
&self,
Expand Down
4 changes: 2 additions & 2 deletions xpallets/dex/spot/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ where
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: XSpotRuntimeApi<Block, AccountId, Balance, BlockNumber, Price>,
AccountId: Codec,
Balance: Codec + Display + FromStr,
Balance: Codec + Display + FromStr + Default,
BlockNumber: Codec,
Price: Codec + Display + FromStr,
Price: Codec + Display + FromStr + Default,
{
fn trading_pairs(
&self,
Expand Down
2 changes: 1 addition & 1 deletion xpallets/gateway/common/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: XGatewayCommonRuntimeApi<Block, AccountId, Balance>,
AccountId: Codec + Send + Sync + 'static,
Balance: Codec + Display + FromStr + Send + Sync + 'static + From<u64>,
Balance: Codec + Display + FromStr + Send + Sync + 'static + From<u64> + Default,
{
fn bound_addrs(
&self,
Expand Down
4 changes: 2 additions & 2 deletions xpallets/mining/asset/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ where
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: XMiningAssetRuntimeApi<Block, AccountId, Balance, MiningWeight, BlockNumber>,
AccountId: Codec,
Balance: Codec + Display + FromStr,
MiningWeight: Codec + Display + FromStr,
Balance: Codec + Display + FromStr + Default,
MiningWeight: Codec + Display + FromStr + Default,
BlockNumber: Codec,
{
fn mining_assets(
Expand Down
4 changes: 2 additions & 2 deletions xpallets/mining/staking/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ where
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: XStakingRuntimeApi<Block, AccountId, Balance, VoteWeight, BlockNumber>,
AccountId: Codec + Ord,
Balance: Codec + Display + FromStr,
VoteWeight: Codec + Display + FromStr,
Balance: Codec + Display + FromStr + Default,
VoteWeight: Codec + Display + FromStr + Default,
BlockNumber: Codec,
{
fn validators(
Expand Down
1 change: 1 addition & 0 deletions xpallets/transaction-fee/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sp-runtime = "3.0.0"

# Substrate pallets
pallet-transaction-payment-rpc = "3.0.0"
pallet-transaction-payment = { version = "3.0.0", default-features = false }

xp-rpc = { path = "../../../primitives/rpc" }

Expand Down
2 changes: 1 addition & 1 deletion xpallets/transaction-fee/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sp_runtime::traits::{MaybeDisplay, MaybeFromStr};
pub use xpallet_transaction_fee::{FeeDetails, InclusionFee};

sp_api::decl_runtime_apis! {
pub trait XTransactionFeeApi<Balance> where
pub trait XTransactionFeeApi<Balance: Default> where
Balance: Codec + MaybeDisplay + MaybeFromStr,
{
fn query_fee_details(uxt: Block::Extrinsic, len: u32) -> FeeDetails<Balance>;
Expand Down
30 changes: 23 additions & 7 deletions xpallets/transaction-fee/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
Block: BlockT,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: XTransactionFeeRuntimeApi<Block, Balance>,
Balance: Codec + MaybeDisplay + MaybeFromStr,
Balance: Codec + MaybeDisplay + MaybeFromStr + Default,
{
fn query_fee_details(
&self,
Expand All @@ -68,12 +68,28 @@ where

api.query_fee_details(&at, uxt, encoded_len)
.map(|fee_details| FeeDetails {
inclusion_fee: fee_details.inclusion_fee.map(|fee| InclusionFee {
base_fee: fee.base_fee.into(),
len_fee: fee.len_fee.into(),
adjusted_weight_fee: fee.adjusted_weight_fee.into(),
}),
tip: fee_details.tip.into(),
// inclusion_fee: fee_details.inclusion_fee.map(|fee| InclusionFee {
// base_fee: fee.base_fee.into(),
// len_fee: fee.len_fee.into(),
// adjusted_weight_fee: fee.adjusted_weight_fee.into(),
// }),
// tip: fee_details.tip.into(),
// partial_details: {inclusion_fee: fee_details.partial_details.inclusion_fee.map(|fee| InclusionFee {
// base_fee: fee.base_fee.into(),
// len_fee: fee.len_fee.into(),
// adjusted_weight_fee: fee.adjusted_weight_fee.into(),
// }),
// tip: fee_details.partial_details.tip.into()},
partial_details: pallet_transaction_payment::FeeDetails {
inclusion_fee: fee_details.partial_details.inclusion_fee.map(|fee| {
pallet_transaction_payment::InclusionFee {
base_fee: fee.base_fee.into(),
len_fee: fee.len_fee.into(),
adjusted_weight_fee: fee.adjusted_weight_fee.into(),
}
}),
tip: fee_details.partial_details.tip.into(),
},
extra_fee: fee_details.extra_fee.into(),
final_fee: fee_details.final_fee.into(),
})
Expand Down
27 changes: 17 additions & 10 deletions xpallets/transaction-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

mod types;

alannotnerd marked this conversation as resolved.
Show resolved Hide resolved
use codec::DecodeLimit;
use sp_std::prelude::*;

use frame_support::{
decl_event, decl_module,
decl_event, decl_module, pallet,
traits::Get,
weights::{
DispatchClass, DispatchInfo, GetDispatchInfo, Pays, PostDispatchInfo, Weight,
Expand All @@ -28,7 +29,8 @@ use sp_runtime::{
FixedPointNumber, FixedPointOperand,
};

pub use self::types::{FeeDetails, InclusionFee};
pub use self::types::FeeDetails;
pub use pallet_transaction_payment::InclusionFee;

type BalanceOf<T> = <<T as pallet_transaction_payment::Config>::OnChargeTransaction as pallet_transaction_payment::OnChargeTransaction<T>>::Balance;
alannotnerd marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -123,6 +125,7 @@ where

// the adjustable part of the fee.
let unadjusted_weight_fee = Self::weight_to_fee(weight);

let multiplier = pallet_transaction_payment::Module::<T>::next_fee_multiplier();
// final adjusted weight fee.
let adjusted_weight_fee = multiplier.saturating_mul_int(unadjusted_weight_fee);
Expand All @@ -134,19 +137,23 @@ where
.saturating_add(tip);

FeeDetails {
inclusion_fee: Some(InclusionFee {
base_fee,
len_fee: fixed_len_fee,
adjusted_weight_fee,
}),
tip,
partial_details: pallet_transaction_payment::FeeDetails {
inclusion_fee: Some(pallet_transaction_payment::InclusionFee {
base_fee,
len_fee: fixed_len_fee,
adjusted_weight_fee,
}),
tip,
},
extra_fee: 0u32.into(),
final_fee: total,
}
} else {
FeeDetails {
inclusion_fee: None,
tip,
partial_details: pallet_transaction_payment::FeeDetails {
inclusion_fee: None,
tip,
},
extra_fee: 0u32.into(),
final_fee: tip,
}
Expand Down
53 changes: 27 additions & 26 deletions xpallets/transaction-fee/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ use serde::{Deserialize, Serialize};

use sp_runtime::RuntimeDebug;

/// The base fee and adjusted weight and length fees constitute the _inclusion fee,_ which is
/// the minimum fee for a transaction to be included in a block.
///
/// ```ignore
/// inclusion_fee = base_fee + len_fee + [targeted_fee_adjustment * weight_fee];
/// ```
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct InclusionFee<Balance> {
/// This is the minimum amount a user pays for a transaction. It is declared
/// as a base _weight_ in the runtime and converted to a fee using `WeightToFee`.
pub base_fee: Balance,
/// The length fee, the amount paid for the encoded length (in bytes) of the transaction.
pub len_fee: Balance,
/// - `targeted_fee_adjustment`: This is a multiplier that can tune the final fee based on
/// the congestion of the network.
/// - `weight_fee`: This amount is computed based on the weight of the transaction. Weight
/// accounts for the execution time of a transaction.
///
/// adjusted_weight_fee = targeted_fee_adjustment * weight_fee
pub adjusted_weight_fee: Balance,
}
// replace by the struct InclusionFee in substrate frame
// /// The base fee and adjusted weight and length fees constitute the _inclusion fee,_ which is
// /// the minimum fee for a transaction to be included in a block.
// ///
// /// ```ignore
// /// inclusion_fee = base_fee + len_fee + [targeted_fee_adjustment * weight_fee];
// /// ```
// #[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
// #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
// #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
// pub struct InclusionFee<Balance> {
// /// This is the minimum amount a user pays for a transaction. It is declared
// /// as a base _weight_ in the runtime and converted to a fee using `WeightToFee`.
// pub base_fee: Balance,
// /// The length fee, the amount paid for the encoded length (in bytes) of the transaction.
// pub len_fee: Balance,
// /// - `targeted_fee_adjustment`: This is a multiplier that can tune the final fee based on
// /// the congestion of the network.
// /// - `weight_fee`: This amount is computed based on the weight of the transaction. Weight
// /// accounts for the execution time of a transaction.
// ///
// /// adjusted_weight_fee = targeted_fee_adjustment * weight_fee
// pub adjusted_weight_fee: Balance,
// }

/// The `final_fee` is composed of:
/// - (Optional) `inclusion_fee`: Only the `Pays::Yes` transaction can have the inclusion fee.
Expand All @@ -41,10 +42,10 @@ pub struct InclusionFee<Balance> {
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct FeeDetails<Balance> {
pub inclusion_fee: Option<InclusionFee<Balance>>,
pub struct FeeDetails<Balance: Default> {
/// Some calls might be charged extra fee besides the essential `inclusion_fee`.
/// use the struct Feedetails in substrate instead of the previous protery inclusion_fee and tip
pub partial_details: pallet_transaction_payment::FeeDetails<Balance>,
liuchengxu marked this conversation as resolved.
Show resolved Hide resolved
pub extra_fee: Balance,
pub tip: Balance,
pub final_fee: Balance,
}