6

I am writing typescript code to get block information. I am connected to wss://kusama-rpc.polkadot.io . I am followig official polkadot js api documentation .

I am calling api.rpc.chain.getBlock method get block information, and it returns block information as json :

{
  "header": {
    "parentHash": "0xf292579563eb2f12e7a1571643d5285a072f04694397758cae76b38075daf631",
    "number": 1134,
    "stateRoot": "0x468de0ef831c96f56d518017b18d76a89f35f30371c45866d12c12ca2116a407",
    "extrinsicsRoot": "0x4875f3ab89c2a3c30f5de8be2ac40cfaee02059fd69ea76115550a418db5fcc8",
    "digest": {
      "logs": [
        "0x066175726120d86ae01200000000",
        "0x05617572610101be3d6d596445d3cb3b711da09e22f9f24c283306744657ce397d17ff1dbf9859051def7406cd356b2d3d2add155d76618f6b098de0c4ce6b7620106ec00e1188"
      ]
    }
  },
  "extrinsics": [
    "0x280401000bc0ca26af7001"
  ]
}

How do I get extrinsic details as -

{
    "method": {
        "callIndex": "0x0200",
        "section":"timestamp",
        "method": "set",
        "args" : [
            "1,582,827,870,000"
        ]
    },
    "isSigned": false
}

I am assuming the extrinsic I am getting is encoded, what is the method to to decode it?

Bopsi
  • 2,090
  • 5
  • 36
  • 58

3 Answers3

1

I found parseInt('0x33c395') works great to decode block numbers

Deekor
  • 9,144
  • 16
  • 69
  • 121
0

Those are hex Uint8Array encoded. If using node.js you could simply get rid of the "0x" string and perform a Buffer.from(<U8A>, 'hex')

Rocco Musolino
  • 610
  • 10
  • 22
0

Given that the extrinsic is 0x280401000bc0ca26af7001 Let's decode. First comes the length of the extrinsic encoded by the compact encoding.

0x28 is 0b00101000 i.e. the value is 0b001010 or 10 10-base

0x04 means that it is not signed extrinsic of the 4th version (EXTRINSIC_VERSION), otherwise it would be 0b1000_0000 | EXTRINSIC_VERSION since it is not signed next comes call_data, otherwise there would be signature

0x01 a runtime enum specific value (pallet_index)

0x00 a runtime enum specific value (call_index)

0x0bc0ca26af7001 according to the compact encoding the first byte 0x0b ( 0b0000_1011 ) meas that it is the big-integer encoding according 0b11 two lower bits, the others 0b000010 are the number of the following bytes save the shift of 4, i.e. the number of bytes is 4 + 0b10 = 6 in the LE encoding. Thus one have to swap the 0xc0ca26af7001 bytes according to the byte-order. 0xc0ca26af7001 -> 0x0170af26cac0 i.e. 1583486520000 10-base

unegare
  • 2,197
  • 1
  • 11
  • 25