0

i try to subscribe a account change event by code follow document https://polkadot.js.org/docs/api/start/api.query.subs

  const unsub = api.query.system.account(address, (acc) => console.log);

it tells the unsub will be a UnsubscribePromise type. but it actully return Promise<Codec> type.

how can i get the UnsubscribePromise Type to unsubscribe it.

update

i wrote a demo code

deom code

and try to compile it.

➜  sub git:(master) pnpm build

> [email protected] build /srv/tmp/sub
> tsc && vite build

src/pok/pok.ts:6:53 - error TS7006: Parameter 'acc' implicitly has an 'any' type.

6     const unsub = await api.query.system.account('',acc =>console.log);
                                                      ~~~

src/pok/pok.ts:10:5 - error TS2349: This expression is not callable.
  Type 'Codec' has no call signatures.

10     unsub()
       ~~~~~


Found 2 errors in the same file, starting at: src/pok/pok.ts:6

 ELIFECYCLE  Command failed with exit code 2.

it tells me Type 'Codec' has no call signatures.

update 2

i make a workspace on gitpod

demo workspace

it can show the error message on terminel tab.

2 Answers 2

3

You are trying to use TypeScript results (static code analysis) without having augmentation applied. See the JS API TS augmentation FAQ on how to apply the correct types for your environment.

Effectively while the runtime is correctly decorated in all cases, for TypeScript you need to apply the augmentation overrides for your specific chain to help at coding-analysis time - here it doesn't have access to the on-chain metadata. Since all chains are different (different methods, arguments, etc.) for the static analysis the editors needs a bit of help to "pick the right one".

An example of applying augmentation for TS -

// apply augmentation first, before any usage
import "@polkadot/api-augment"

// normal imports
import { ApiPromise, WsProvider } from "@polkadot/api"

// do stuff
async function main () {
    const api = await ApiPromise.create({
        provider: new WsProvider("wss://kusama-rpc.polkadot.io")
    })

    // hu is VoidFn
    // hdr is Header
    const hu = await api.rpc.chain.subscribeAllHeads((hdr) =>
        console.log(hdr.number.toString())
    )

    // qu is VoidFn
    // acc is FrameSystemAccountInfo
    const qu = await api.query.system.account("", (acc) =>
        console.log(acc.consumers.toNumber())
    )

    // unsubscribe
    qu()
    hu()
}
5
  • i applied explicitly but pnpm build also failed
    – kula
    Jul 6, 2022 at 0:02
  • This is the correct answer for TS augmentation.
    – Jaco
    Jul 6, 2022 at 17:36
  • But the result of my implementation according to the document here is still wrong @jaco. the code is github.com/kulame/api-test-4450/blob/master/index.ts and it raise errror index.ts:22:2 - error TS2349: This expression is not callable. Type 'Codec' has no call signatures.
    – kula
    Jul 7, 2022 at 2:28
  • Added an example to my post above (small adaptation of my test code with comments as to what is in the editor visible and in the runtime - all due to the augmentation being applied first)
    – Xiu Hung
    Jul 7, 2022 at 7:41
  • I understand what you mean. My confusion now is that it cannot compile through tsc. my demo code also append "import "@polkadot/api-augment"" before any usage
    – kula
    Jul 7, 2022 at 13:52
1

you need to await to get the unsub fn.

const unsub = await api.query.system.account(address, (acc) => console.log);

For example:

const address = ""
const unsub = await api.query.system.account(address, (acc) => console.log);
console.log(unsub)

will print

()=>l.unsubscribe()

Thanks!

4

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .