Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updated extrinsics' assetId #5879

Merged
merged 1 commit into from
May 15, 2024
Merged

fix: updated extrinsics' assetId #5879

merged 1 commit into from
May 15, 2024

Conversation

bee344
Copy link
Contributor

@bee344 bee344 commented May 14, 2024

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

@bee344 bee344 requested a review from TarikGul May 14, 2024 23:20
Copy link
Member

@TarikGul TarikGul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🙏

@TarikGul TarikGul merged commit 365fde8 into master May 15, 2024
4 checks passed
@TarikGul TarikGul deleted the anp-extrinsic-fix branch May 15, 2024 01:45
@TarikGul
Copy link
Member

Will be included in mondays release

@polkadot-js-bot
Copy link

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.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot read properties of undefined (reading 'toHuman')
3 participants