AstarNetwork / astar.js

Astar network Typescript SDK
Apache License 2.0
30 stars 9 forks source link

Fetch All Stakers' Addresses from a DApp (DApp Staking) #66

Closed ercole89 closed 6 months ago

ercole89 commented 7 months ago

Is your feature request related to a problem? Please describe. I am working on a project where I need to fetch all addresses that are staking on a specific DApp at a given epoch. I have encountered difficulties in finding the appropriate API to retrieve this information. My current issue is that I am unable to obtain the desired output; I either receive a 'null' response or encounter an error, indicating a potential issue with my API call or a misunderstanding of the correct method to use.

Describe the solution you'd like I am looking for guidance on which API to use to get a list of all addresses that are staking on a specific DApp at a given epoch.

Describe alternatives you've considered So far, I have attempted to use api.query.dappsStaking.contractEraStake(), but this has not yielded the expected results. I am open to suggestions on alternative approaches or methods that might be more suitable for this purpose.

Additional context Here is a snippet of the code I am currently using:

const { ApiPromise } = require('@polkadot/api');
const { WsProvider } = require('@polkadot/rpc-provider');
const { options } = require('@astar-network/astar-api');

async function getStakerAddresses(dappAddress, outputFile) {
    const provider = new WsProvider('wss://shiden.api.onfinality.io/public-ws');
    const api = new ApiPromise(options({ provider }));
    await api.isReady;

    try {
        const currentEra = await api.query.dappsStaking.currentEra().catch((error) => {
            console.error('error', error);
            return null;
        });

        if (currentEra === null) return;
        const staking = await api.query.dappsStaking.contractEraStake({ Evm: dappAddress }, currentEra);
        console.log(staking)
    } catch (error) {
        console.error(error);
    } finally {
        api.disconnect();
    }
}

const dappAddress = '0x....';

getStakerAddresses(dappAddress, outputFile)
    .then(() => console.log('Complete'))
    .catch(error => console.error('Error:', error));

This code is intended to fetch the staking addresses, but it does not work as expected. Any advice on how to properly make this API call or if there is a better way to approach this would be greatly appreciated. Thank you in advance for your time and assistance.

Best regards,

bobo-k2 commented 7 months ago

Hi @ercole89, with current dApp staking pallet it is no way to get list of all stakers for a dApp. I believe, in your case, the best approach would be to create an indexer to collect required data. You can use SubSquid, SubQuery or some other indexer of your choice. There is an SubQuery indexer I wrote a some time ago which might help https://github.com/bobo-k2/dapps-staking-indexer. The indexer should do exactly what you need. Have in mind that the indexer is written for educational purposes and never used in production, so you need to check the code and indexed data