0

On our substrate node using a storage double map. We are trying to subscribe to an entries query. something like

const unsub = await api.query.system.balances.entries(ADDR, (res) => {
  console.log(res);
});

It works and gets the entries on the first call but it is not updating / running the callback when the storageMap is updated. is it not possible to subscribe to the entries of a storage map?

1 Answer 1

0

The code you referenced does not work for me. I get the following error:

  await api.query.system.balances.entries(ADDR, (res) => {
                                  ^
TypeError: Cannot read properties of undefined (reading 'entries')

You have the correct format for entries. For example:

const referenda = await api.query.referenda.referendumInfoFor.entries();
referenda.forEach(async (referendum) => {
    if (referendum[1].toHuman()['Ongoing']) {
        const referendumIndex = referendum[0].args[0].toNumber();
        console.log("Referendum:", referendumIndex);
        console.log(referendum[1].toJSON());
    }
});

Depending on your chain, the balances of your accounts are actually stored in FRAME system pallet.

Perhaps this is what you are trying to achieve:

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

export const subscribeBalances = async () => {
  const provider = new WsProvider(ENDPOINT);
  const api = await ApiPromise.create({ provider });

  api.query.system.account(ADDR, (balance) => {
    console.log(`Balance for account ${account}: ${balance.data.free}`);
  });
};

Or depending on what you're trying to achieve you can also listen for events from the balances pallet. For example:

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.