polkadot-js / api

Promise and RxJS APIs around Polkadot and Substrate based chains via RPC calls. It is dynamically generated based on what the Substrate runtime provides in terms of metadata.
Apache License 2.0
1.06k stars 342 forks source link

fix: updated extrinsics' `assetId` #5879

Closed bee344 closed 1 month ago

bee344 commented 1 month ago

Description

When decoding a signed extrinsic that didn't contain an assetId to pay the fees and trying to call .toHuman(), it fails. That's caused because assetId is optional and calling .toHuman() on undefined errors. This is fixed by checking whether assetId is defined or not before calling toHuman() and based on that, calling the function or setting the value as null.

Before

Block: 1000001
TypeError: Cannot read properties of undefined (reading 'toHuman')
    at Type.toHuman (/home/bee344/Documentos/parity/tests/node_modules/@polkadot/types/cjs/extrinsic/Extrinsic.js:254:39)
    at /home/bee344/Documentos/parity/tests/build/index.js:25:39
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
/home/bee344/Documentos/parity/tests/node_modules/@polkadot/types/cjs/extrinsic/Extrinsic.js:254
                assetId: this.assetId.toHuman(isExpanded, disableAscii),
                                      ^

TypeError: Cannot read properties of undefined (reading 'toHuman')
    at Type.toHuman (/home/bee344/Documentos/parity/tests/node_modules/@polkadot/types/cjs/extrinsic/Extrinsic.js:254:39)
    at /home/bee344/Documentos/parity/tests/build/index.js:25:39
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.18.2

After

Block: 1000001
{
  isSigned: true,
  method: {
    args: {
      real: '0x0198D3053a69C3f977bB1943bc95A0fFA7777474',
      force_proxy_type: 'AuthorMapping',
      call: [Object]
    },
    method: 'proxy',
    section: 'proxy'
  },
  assetId: null,
  era: { MortalEra: { period: '256', phase: '61' } },
  nonce: '6,774',
  signature: '0xf45193fd3be55f9853d8b11039f8c9758b1ec7230fd908512524dd4d3c0acf072a42b31d39a1e86b864e391a019346d461dc2efc29330a29d58d789b911064df00',
  signer: '0x0fb28A7B28591335a33861cbC3E9B77d9Ab9a889',
  tip: '0'
}

To replicate

import "@polkadot/api-augment";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { Vec } from "@polkadot/types";
import { EventRecord } from "@polkadot/types/interfaces";

(async () => {
  try {
    console.log('Results to-> TypeError: Cannot read properties of undefined (reading \'toHuman\') in >=10.13.1')

    const provider = new WsProvider('wss://moonbeam-rpc.dwellir.com');
    const api = await ApiPromise.create({ provider, throwOnConnect: true });

    for (let blockNumber = 1000000; blockNumber < 1000002; blockNumber++) {

      const hash = await api.rpc.chain.getBlockHash(blockNumber);
      const apiAt = await api.at(hash);

      const allRecords = await apiAt.query.system.events() as unknown as Vec<EventRecord>;

      for (const record of allRecords) {
        record.event.toHuman()
      }

      const signedBlock = await api.rpc.chain.getBlock(hash);

      for (let extrinsicIndex = 0; extrinsicIndex < signedBlock.block.extrinsics.length; extrinsicIndex++) {
        console.log('Block:', blockNumber)
        const extrinsic = signedBlock.block.extrinsics[extrinsicIndex];
        console.log(extrinsic.toHuman())
      }
    }
  } catch (err) {
    console.error(err)
    throw err;
  }
})();

Closes #5875

TarikGul commented 1 month ago

Will be included in mondays release

polkadot-js-bot commented 1 month ago

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.