Retrieving staking balances in NEAR protocol

521 Views Asked by At

How to retrieve "total amount staked" and "rewards earned" for a particular Near staking account? Ideally via an API.

1

There are 1 best solutions below

0
On

Call an archival rpc node on the staking pool smart contract with method_name get_account_total_balance, which will give you the staked amount.

example:

fetch('https://archival-rpc.mainnet.near.org', {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body: JSON.stringify({
            'jsonrpc': '2.0',
            'id': 'dontcare',
            'method': 'query',
            'params': {
                request_type: 'call_function',
                //finality: 'final',
                block_id: block_id,
                account_id: stakingpool_id,
                method_name: 'get_account_total_balance',
                args_base64: btoa(JSON.stringify({
                    account_id: account_id
                }))
            }
        })

In order to get the rewards earned, look at the balance for previous epochs ( every 12 hour ) and calculate the difference.