'Retrieving staking balances in NEAR protocol
How to retrieve "total amount staked" and "rewards earned" for a particular Near staking account? Ideally via an API.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
