Search code examples
polkadotpolkadot-js

How do i get crowdloan info using PolkaDot JS APIs?


I am able to get account information and chain info through JS API just fine, however the balance does not show KSM locked in crowdloans, they are just not there, so how can i know in which CL the address allocated KSM and how many?

This is what i get from balance:

{
  "account": {
    "nonce": "5",
    "consumers": "2",
    "providers": "1",
    "sufficients": "0",
    "data": {
      "free": "X.xx KSM",
      "reserved": "0",
      "miscFrozen": "X.xxx KSM",
      "feeFrozen": "X.xxx KSM"
    }
  }
}

But neither "reserved" or "frozen" are considering the locked KSM in the auctions.


Solution

  • Crowdloan contributions are transfered out of the account and are (safely) stored in a new account that does not have a private key and only the crowdloan pallet has access to. To get your contribution, you need a ParaId, and the hex encoded account address(es) that you want to query.

    const id: ParaId = ...;
    // note that this must be hex, if you have a ss58 account, try: 
    // `api.createType('AccountId', ss58).toHex()`
    const accounts: string[] = ...;
    const contribution = await api.derive.crowdloan.ownContributions(, accounts);
    

    and the result will be a map of Record<Account, Balance>. Example: https://github.com/polkadot-js/apps/blob/68a423748be49db2636a5cd66f9012706dcabbb4/packages/page-parachains/src/Crowdloan/useContributions.ts#L28

    To get the list of all parachains (in different lifecycles), you can do:

    const allParaIds: ParaId[] = (await api.query.paras.paraLifecycles.entries()).map(([key, _]) => key.args[0]);
    

    which maps to this storage.

    If you want just the ones that have upgraded to a parachain, you can use this storage.