4

I am trying to call api.events.staking.Withdrawn.is in polkadot.js/api.

Docs: https://polkadot.js.org/docs/substrate/events#withdrawnaccountid32-u128

Code sandbox: https://codesandbox.io/s/hardcore-architecture-vrhyqm?file=/index.js

It is not working and causing isssue.

const provider = new WsProvider("wss://kusama-rpc.polkadot.io");
const api = await ApiPromise.create({ provider });
const validator = "CaKh7HmPMXxv22GnLgjEZxWHBUu7y7Twf2k8mP3mxpLYTH4";
const response = await api.events.staking.Withdrawn.is(validator, "50");

Error

2022-05-19 17:41:35        API/INIT: RPC methods not decorated: mmr_generateBatchProof
(node:2206) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
    at Object.is (/sandbox/node_modules/@polkadot/types/cjs/metadata/decorate/events/index.js:44:60)
    at Server.<anonymous> (/sandbox/index.js:10:57)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2206) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2206) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2
  • 1
    Please dont post a link to some external code sandbox. Please include the code snippet and the actual error that is returned in this question. May 19, 2022 at 17:29
  • 1
    I updated question!
    – Heindrick
    May 19, 2022 at 17:42

2 Answers 2

4

The api.event.<pallet>.<event>.is(Event) is not used to create an event, rather it is used to check if an emitted event is of a specific type, returning true if this is the case. Additionally the api.event.<pallet>.<event> exposed the metadata for the event, so the signature looks like this -

interface IsEvent <T extends AnyTuple> {
  readonly meta: EventMetadataLatest;

  is: (event: IEvent<AnyTuple>) => event is IEvent<T>;
}

Assuming you have read query.system.events and you have an Vec<EventRecord>, you can use this to iterate through and check if an event is a specific type. (Alternatively you can always look for the section & method on this event and do string matching)

On the middleware/API side, you never need to create events - these are only emitted by the runtime.

1
  • This syntax api.event.<pallet>.<event>.is(Event), I assume can't be used for custom pallets right? Aug 24 at 5:56
1

Could it be that you are passing the "amount" argument as a 'string' in api.events.staking.Withdrawn.is(validator, "50") ?

According to the docs it should be a u128, or in js, I think it a 'number'.

2
  • Can you send me example u128 type amount value? @CustomBlink
    – Heindrick
    May 19, 2022 at 17:58
  • While not applicable here specifically, it is a good question. The JS API will detect and convert formats as/when needed. So where number inputs are required, it would accept: number, BN, BigInt, string (decimal numbers), hex (0x prefixed) and will do the conversion as required.
    – Jaco
    May 19, 2022 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.