5

I call json-RPC through the terminal and get the data as below.

extrinsics is raw byte, but I'm using polkadot-js(SCALE codec?) to decode it.

But I don't know which method to call.

"block": {
"extrinsics": [
"0x280402000be1da78d37e01","0xd91f..(too long haha)..580"
],
"header": { "digest": { ... }


@polkadot/crypto-util? @polkadot/util? Which module and which method should I use? I want input string(raw data) and get string(json or human data)

please help..

2
  • If you are already using Polkadot JS, would a sufficient answer show you how to extract the extrinsics in the block with Polkadot JS API? Or are you trying to do this manually for some reason?
    – Shawn Tabrizi
    Feb 14, 2022 at 16:35
  • I send json rpc to the polkadot node from my server and get the response value. I'm looking for a separate way to decode on my server. I want Java more than JavaScript :)
    – PaperFrog
    Feb 15, 2022 at 4:24

2 Answers 2

7

As mentioned in the wiki page https://wiki.polkadot.network/docs/build-node-interaction, some return values (like extrinsics) are SCALE encoded and you need to decode them by using the metadata. You can retrieve the metadata :

You can also use sidecar (as mentioned in the same wiki page) to get the information already decoded. The corresponding github page is https://github.com/paritytech/substrate-api-sidecar.

1
  • 1
    I got metadata. I know what this is. But how do you decode with this? Do I put this in the JavaScript code?
    – PaperFrog
    Feb 14, 2022 at 16:35
7

You can use @polkadot-js and do something like this:

const wsProvider = new WsProvider('wss://rpc.polkadot.io');
const api = await ApiPromise.create({ provider: wsProvider });
const exampleBlockNumber = 7200920
const blockHash = await api.rpc.chain.getBlockHash(exampleBlockNumber);
const signedBlock = await api.rpc.chain.getBlock(blockHash);

signedBlock.block.extrinsics.forEach((ex, index) => {
  console.log(index, ex.toHuman());
  const { method: { args, method, section } } = ex;
  console.log(`${section}.${method}(${args.map((a) => a.toString()).join(', ')})`);
});

which will give you output like this:

{isSigned: false, method: {…}}
timestamp.set(1633886766001)
{isSigned: true, method: {…}, era: {…}, nonce: '0', signature: '0x56344710ae648a1da40e2c1a1159c73fdbd7d522af01f1fd…7376ab59fd4c492a711cc64bba48d6ae23762d95176071a0c', …}
balances.transfer(13Hwdzi6jkXtBWTSU5de5gC93hDog23pdqd59SPZ7s9zXVNL, 395536044985)
{isSigned: true, method: {…}, era: {…}, nonce: '0', signature: '0x402484bf077d64a8018463a6ccb824f9487fdad8a186d50b…5ca230746fa10e793f5e42f027b53c065f9beb0472d1ca588', …}
balances.transfer(15YHa58TQ624RNd5agRP1uuaqf6fqjUY5n5wrd6V2Ziu3Qsg, 31835000000)
{isSigned: true, method: {…}, era: {…}, nonce: '6', signature: '0x158457697711485bc9b2b34fb1fbc0d4d2b0d91cf0de9502…eb85f4e03efef3fe58ccf174b827a3da05975af36d7687e07', …}
balances.transfer(15BFDbm97npEgs9g7Cfrn7qLeL1g212qRmB58qA7N5F32Ti9, 28665000000)
{isSigned: true, method: {…}, era: {…}, nonce: '1,697,264', signature: '0x928814bc12625c401f31df31cf3753c0045a770aac7e4d7f…5643cd9346bc07dd921f7c71605f311c783ba3d940d47a68d', …}
utility.batch([{"callIndex":"0x0500","args":{"dest":{"id":"16AhBPuZkS2Ugdx4G2VEBu6fJ734hr1Na3Qtn1PwiYrUdq6o"},"value":198803000000}}, {"callIndex":"0x0500","args":{"dest":{"id":"13QLYLKmVfJBkWVWMhJtnFtZBYFK6KEiGHEwrGVsSJyVRpjd"},"value":749000000000}}, {"callIndex":"0x0500","args":{"dest":{"id":"15aeeDCQrBnh5NhWxvkhQkZGQ6JH3HM3J8kTZBG8Pbmvj8mX"},"value":16600000000}}])

You can find more info here: https://polkadot.js.org/docs/api/cookbook/blocks#how-do-i-view-extrinsic-information

This code will work on the frontend or backend. You can see an example of running this code on node (backend) here: https://github.com/brunopgalvao/node-dot/blob/main/server.js

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.