1

I am trying to run one of the examples in the Polkadot{.js} documentation and it is not working for me in TypeScript. This much works

  const wsProvider = new WsProvider('wss://rpc.polkadot.io');
  const api = await ApiPromise.create({ provider: wsProvider });
  console.log(api.genesisHash.toHex());

  const ADDR = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE';
  const now = await api.query.timestamp.now();
  console.log(`${now}`);

However, this line does not work:

  const { nonce, data: balance } = await api.query.system.account(ADDR);

Here is the error returned:

   return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/scripts/index.ts:110:11 - error TS2339: Property 'nonce' does not exist on type 'Codec'.

110   const { nonce, data: balance } = await api.query.system.account(ADDR);
              ~~~~~
src/scripts/index.ts:110:18 - error TS2339: Property 'data' does not exist on type 'Codec'.

110   const { nonce, data: balance } = await api.query.system.account(ADDR);
                     ~~~~

I don't know if the problem is that I am running it in TypeScript or something else, but does anyone have any suggestions to make this work or see something that I am doing wrong? Thanks.

1
  • Your TypeScript is not augmented, so it returns a default Codec. See this for the solution - polkadot.js.org/docs/api/…
    – Jaco
    Commented May 9, 2022 at 5:03

1 Answer 1

1

As a workaround, I was able to use the following code to get the nonce and free balance:

  const data = await api.query.system.account(ADDR);
  console.log(JSON.stringify(data));
  console.log(data['nonce'].toHuman());
  console.log(data['data']['free'].toHuman());

Do you think the online example is outdated?

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.