0

Very sorry for such a newbie question; just don't know where else to ask 🙏

Essentially, I want to programmatically dump from the network who won the most recent election. To do so, I'm issuing an erasStakers(...)[https://polkadot.js.org/docs/substrate/storage/#erasstakersu32-accountid32-palletstakingexposure] RPC call. So far as I understand it, there is no direct RPC API call for erasStakers(...), and one needs to interact with the node through getStorage(...) to learn the election result.

Yet the Polkadot Explorer somehow shows the human-readable output: Polkadot Explorer with an election winners shown

However, when I do in my Nodejs code:

await api.query.staking.erasStakers(635, null)
// => {"total":0,"own":0,"others":[]}

It seems the Polkadot Node just returns an empty object.

I also know about Parity's substrate-debug-kit[https://github.com/paritytech/substrate-debug-kit/blob/master/offline-election/src/subcommands/staking.rs] repo, which queries the Polkadot's node. I believe I could leverage sub_storage::read from that repo, but I can't guess the correct "Prefix" parameter and the return type to get erasStakers; it just says "get_pairs failed", whatever I try.

Is there a way to get either in my NodeJS or in my Rust code something akin to what I see in the picture — a human readable structure representing an erasStakers(...) call?

I'm new to Polkadot, so apologies if I mixed up terms, or misunderstood something trivial — any help would be much appreciated.

2
  • 1
    This is not correct await api.query.staking.erasStakers(635, null) - use .entries, see polkadot.js.org/docs/api/cookbook/… (that is what the apps UI uses above)
    – Jaco
    Commented Apr 27, 2022 at 14:22
  • You don't need a specific RPC call, what the UI is doing is querying the storage directly which is done with state_getStorage, but it requires to pass the storage key encoded which is not friendly to do by hand but your Rust client should be able to do that for you.
    – olanod
    Commented Apr 27, 2022 at 16:29

1 Answer 1

1

As per a comment above, to iterate over a map, you need to use the entries methods, for double maps there is also a cookbook example.

In the case of api.query.staking.erasStakers(...) it returns a specific entry, and only the value. In the case of api.query.staking.erasStakers.entries(...) it returns the full map, will all keys & values for each entry.

(This is obviously a much heavier operation, apart from the fact that the return type is different, since it needs to chunk the values and iterate on the Node RPC side)

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.