收取交易费用并将其存储在锅中 代码示例

问题描述

想要在使用转账的过程中收取更高的交易费(5%)并存入罐子账户?我怎样才能做到这一点?是否需要修改平衡托盘的传递函数

https://github.com/paritytech/substrate/blob/master/frame/balances/src/lib.rs#L257

另外,我如何收取额外的交易费用,需要一些帮助来编写代码

解决方法

费用由 pallett-transaction-paymentChargeTransactionPayment 收取,SingedExtension 是收取交易付款的 {{3}}。

如果您想在每次转移时收取 x%,您可以为 pallet-balances 创建一个新的签名扩展,它会拦截 Call::transfer 并收取一些额外费用。

请注意,如果您将两者都包含在运行时中,则这可能与 ChargeTransactionPayment 发生冲突。

代码示例

这不会按原样编译;只是告诉你方向。

pub struct TakeFivePercent<T>(PhantomData<T>);
impl<T: Config> SignedExtension for TakeFivePercent<T>  {
    const IDENTIFIER: &'static str = "TakeFivePercent";
    type AccountId = T::AccountId;
    type Call = T::Call;
    type AdditionalSigned = ();
    
    fn additional_signed(&self) -> sp_std::result::Result<(),TransactionValidityError> { Ok(()) }

    fn validate(
        &self,who: &Self::AccountId,call: &Self::Call,info: &DispatchInfoOf<Self::Call>,len: usize,) -> TransactionValidity {
        match call { 
              Self::Call(Call::Transfer(dest,amount)) => {
                    // withdraw 5% of amount from dest
              }
        }
    }

    // note that we won't implement `pre_dispatch` and let it auto-impl
    // to valiadte.
    // https://crates.parity.io/src/sp_runtime/traits.rs.html#744-874
}

最后,请注意,您需要将此添加到顶级运行时文件中已签名扩展的元组中,construct_runtime! 所在的位置。

pub type SignedExtensions = (
    Foo,Bar,..,TakeFivePercent<Runtime>    
)