2

i have a pallet that prints extrinsic data (bytes and hex) of every extrinsic inside every block, how can i print it like an human readable object ("from", "to" ecc..)?

that's my pallet code:

#[frame_support::pallet]
pub mod pallet_custom {
    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;
    use sp_runtime::generic::UncheckedExtrinsic;
    
    #[pallet::pallet]
    pub struct Pallet<T>(_);

    #[pallet::config]
    pub trait Config: frame_system::Config {}


    #[pallet::call]
    impl<T: Config> Pallet<T> {
        // Aggiungi le funzioni di chiamata qui
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_finalize(block_number: BlockNumberFor<T>) {
            log::info!("🇮🇹 on_finalize | Block number is {:?}", block_number);

            let total_block_transactions_count = frame_system::Pallet::<T>::extrinsic_count();
            log::info!("🇮🇹 on_finalize | Extrinsic count is {:?}", total_block_transactions_count);

            for i in 0..total_block_transactions_count {
                let transactions_data = frame_system::Pallet::<T>::extrinsic_data(i);
                log::info!("🇮🇹 on_finalize | Extrinsic data {:?} is {:?}", i, transactions_data);
              
                // convert the transaction data to hex string
                let transactions_data_hex = hex::encode(transactions_data.clone());
                log::info!("🇮🇹 on_finalize | Extrinsic data hex {:?} is {:?}", i, transactions_data_hex);

            

            }
        }

        fn offchain_worker(block_number: BlockNumberFor<T>) {
            log::info!("🇮🇹 offchain_worker | Block number is {:?}", block_number);
        }
    }
}

I hope to read something like:

{from: "0x94..", to: "0x4a9...", value: 0, data: "0x39c...."}

i receive something like:

0xe50184f24ff3a9cf04c71dbc94d0b566f7a27b94566cac48ddbb2036e1a0b045ce62ce71741ef06493cc58a03056adc3026a04ecb471dd3cd433087417721f1d4f716acc03d2cfda9b5cde20dbefaeafa6ce79ef25fbdf00350000000403798d4ba9baf0064ec19eb4f0a1a45785ae9d6dfc130000e8890423c78a

1 Answer 1

1

The question is not very clear which line of code that you expect it to print out because I don't find any code related to your expected output. But I assume these lines are what you expect

// convert the transaction data to hex string
let transactions_data_hex = hex::encode(transactions_data.clone());
log::info!("🇮🇹 on_finalize | Extrinsic data hex {:?} is {:?}", i, transactions_data_hex);

If so, then I don't think hex encoding the byte vector of transaction data is a right way to do. Can you give a try?

let extrinsic = sp_runtime::generic::UncheckedExtrinsic::decode(&mut input);

Reference: https://paritytech.github.io/polkadot-sdk/master/sp_runtime/generic/struct.UncheckedExtrinsic.html#impl-Decode-for-UncheckedExtrinsic%3CAddress,+Call,+Signature,+Extra%3E

The expected output would be

pub struct UncheckedExtrinsic<Address, Call, Signature, Extra>
where
    Extra: SignedExtension,
{
    pub signature: Option<(Address, Signature, Extra)>,
    pub function: Call,
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.