How to query delegator info for stake pool from command line?

I run a stake pool and am trying to figure out how I can query via command line who the current delegators are in the current epoch and how much active stake they each have.

Is this something I can do with cardano-cli? Or is there another commandline tool I can use?

I’m trying to automate a lottery function for the pool.

2 Likes

I’m interested in this one as well

I would advise you to check out blockfrost.io. They offer a free plan that will help you do what you want, quite easily.

1 Like

Agree on the Blockfrost suggestion. Very easy using curl to automate collection of stats and process using bash, or if you prefer nodejs so you can process the json output and use in your website…

We use them for ADAvault plus some other data from coin gecko, and cncli. Happy to share how.

1 Like

Hey @cyberruss How do you do it ? Thanks

You could also use https://api.koios.rest/:

$ curl "https://api.koios.rest/api/v0/pool_delegators?_pool_bech32=pool1qqenhz258k5k9nyjull6gkzg7s4f3ae8v7qxwpegeef9v0nsy3e&_epoch_no=336"
[{"stake_address":"stake1u8792yuvwnqt5nwsepvkdlswhvfd8vk2ewr8jx3p2sd9vvq4c2ga4","amount":"2355006753647","epoch_no":336},
 {"stake_address":"stake1u9dm6wq6vee44c24qjwquqat4gypl9st6prh60t70zswdug8470jw","amount":"1402452321963","epoch_no":336},
 {"stake_address":"stake1uy04h2grcf84663cxwntrxnk0empdv0f98ugqmyt9z07cfgzxpt0h","amount":"556849187620","epoch_no":336},
…

The epoch number is optional:

$ curl "https://api.koios.rest/api/v0/pool_delegators?_pool_bech32=pool1qqenhz258k5k9nyjull6gkzg7s4f3ae8v7qxwpegeef9v0nsy3e"
[{"stake_address":"stake1u9dm6wq6vee44c24qjwquqat4gypl9st6prh60t70zswdug8470jw","amount":"1405976018602","epoch_no":338},
 {"stake_address":"stake1uy04h2grcf84663cxwntrxnk0empdv0f98ugqmyt9z07cfgzxpt0h","amount":"558248285169","epoch_no":338},
 {"stake_address":"stake1uxrkfrnc79fu5jhyujsr66wqgzdy9d9zm0hu7qev8rsewyc2u4axc","amount":"426635022711","epoch_no":338},
…

@HeptaSean im using code from api blockfrost curl -H “project_id: mainne” https://cardano-mainnet.blockfrost.io/api/v0/pools/poolid/delegators/ --output delegs.json

but only shows the stake address any chance that could show the full address?

You can use https://docs.blockfrost.io/#tag/Cardano-Accounts/paths/~1accounts~1{stake_address}~1addresses/get to get all addresses for a stake address.

There is not really the full address except if you are using single address apps like Nami or CNTOOLS.

2 Likes

@HeptaSean from that code i was abble to get the address i need but on at the time , can i use this code for more than one stake adress at the same time?

thanks for your help!

No, unfortunately not. That may cost some requests. Or you switch to identifying wallets/accounts/users by stake address instead of full address.

ok, @HeptaSean so mostly every epoch pool operators that send air drops for the delegators wallets , seek one by one from the stake address?

also quick question , the rewards for the delegators is send out automatically when the block is minted and only the pools owner reward that need to be retrived later for another wallet for exemple?

Ah, if that is the use case, you could just cache the stake address – receive address association in a database.

The rewards for the delegators and the pool are both distributed automatically by the protocol to the corresponding stake addresses.

So, the pool does not have to do anything to distribute them to the delegators.

Ah, if that is the use case, you could just cache the stake address – receive address association in a database.

How can i do that ? :smiley: :smiley:

Oh, I just assumed that you were already doing that in some programming language.

Do you know/like one?

not realy , there is a easy way to do it , maybe i can folow some walkthrough

You could try this script:

#!/bin/sh

# Things we need:
all_there=true
for cmd in curl jq awk
do
    if ! command -v ${cmd} > /dev/null 2>&1
    then
        echo "Command ${cmd} is required. Please install."
        all_there=false
    fi
done
if [ "${all_there}" != true ]
then
    exit 1
fi
if [ -z "${BLOCKFROST_PROJECT_ID}" ]
then
    echo "BLOCKFROST_PROJECT_ID has to be set:"
    echo "     export BLOCKFROST_PROJECT_ID=\"mainnetXXXXX\""
    exit 1
fi
if [ -z "${1}" ]
then
    echo "First argument has to be the pool ID."
    exit 1
fi

# Get stake addresses and put them in file stake_addresses:
curl -s -H "project_id: ${BLOCKFROST_PROJECT_ID}" \
    https://cardano-mainnet.blockfrost.io/api/v0/pools/${1}/delegators | \
    jq -r '.[] | .address' > stake_addresses

# Get receive addresses and put them in file stake_receive_addresses:
if [ -f stake_receive_addresses ]
then
    mv stake_receive_addresses old_stake_receive_addresses
fi
while read stake_address
do
    if ! grep "^${stake_address}" old_stake_receive_addresses 2> /dev/null
    then
        echo -n "${stake_address} "
        curl -s -H "project_id: ${BLOCKFROST_PROJECT_ID}" \
            https://cardano-mainnet.blockfrost.io/api/v0/accounts/${stake_address}/addresses | \
            jq -r '.[0] | .address'
    fi
done < stake_addresses > stake_receive_addresses

# Put just the receive addresses in file recevive_addresses:
awk '{print $2}' stake_receive_addresses > receive_addresses

As usual with scripts, put it in a text file, make it executable with chmod +x delegators.sh (or however you have called it) and then you can execute it by ./delegators.sh in the same directory, put it somewhere in your $PATH or whatever.

The script wants to have the Blockfrost project ID in an environment variable and the pool ID as first and only argument. It creates a bunch of files in the current directory: stake_addresses with only the stake addresses, stake_receive_addresses with both, stake and receive addresses, and receive_addresses with just the receive addresses.

If stake_receive_addresses is there from a previous run, the stake addresses are searched in that file with grep and curl to Blockfrost is only used if they are not found.

1 Like

Thanks @HeptaSean ,Worked Great .

1 Like

Glad it helped!

I’d normally do something like that in Python, but I hoped that a shell script is closer to what you already know and you can track what’s happening there.

Hey @HeptaSean Thank You. Do you know a way to distribute Native Tokens to Delegators? and there is a way to know from a script if those wallets have a specific NFT and how many of them, similar how hosky do it ?

@HeptaSean Hey , is there a way to modify this code to see in those address if they have a nft in it and how many? maybe using the code https://cardano-mainnet.blockfrost.io/api/v0/assets/{asset}/addresses , and also how much adas each have staked ? Thanks!