Search code examples
substratepolkadot

How could I configure the reward for a substrate Aura validator?


Now that the PoA is running with multiple Aura validators in my substrate-node-template. How could I configure that reward amount or value for my Aura validators?

enter image description here


Solution

  • I was able to make it work using this code.

    // Substrate and Polkadot dependencies
    use frame_support::{
        derive_impl, parameter_types, ord_parameter_types,
        traits::{
            fungible::{Balanced, Credit},
        OnUnbalanced,
        },
    };
    
    
    
    /// Logic for the author to get a portion of fees.
    pub struct ToAuthor<R>(core::marker::PhantomData<R>);
    impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for ToAuthor<R>
    where
        R: pallet_balances::Config + pallet_authorship::Config,
        <R as frame_system::Config>::AccountId: From<AccountId>,
        <R as frame_system::Config>::AccountId: Into<AccountId>,
    {
        fn on_nonzero_unbalanced(
            amount: Credit<<R as frame_system::Config>::AccountId, pallet_balances::Pallet<R>>,
        ) {
            if let Some(author) = <pallet_authorship::Pallet<R>>::author() {
                let _ = <pallet_balances::Pallet<R>>::resolve(&author, amount);
            }
        }
    }
    
    parameter_types! {
        pub FeeMultiplier: Multiplier = Multiplier::one();
    }
    
    impl pallet_transaction_payment::Config for Runtime {
        type RuntimeEvent = RuntimeEvent;
        type OnChargeTransaction = FungibleAdapter<Balances, ToAuthor<Runtime>>;
        type OperationalFeeMultiplier = ConstU8<100>;
        type WeightToFee = IdentityFee<Balance>;
        type LengthToFee = IdentityFee<Balance>;
        type FeeMultiplierUpdate = ConstFeeMultiplier<FeeMultiplier>;
    }
    
    
    
    pub struct AuraAccountAdapter;
    impl frame_support::traits::FindAuthor<AccountId> for AuraAccountAdapter {
        fn find_author<'a, I>(digests: I) -> Option<AccountId>
            where I: 'a + IntoIterator<Item=(frame_support::ConsensusEngineId, &'a [u8])>
        {
            pallet_aura::AuraAuthorId::<Runtime>::find_author(digests).and_then(|k| {
                AccountId::try_from(k.as_ref()).ok()
            })
        }
    }
    
    impl pallet_authorship::Config for Runtime {
        type FindAuthor = AuraAccountAdapter;
        type EventHandler = ();
    }