Use Web3.js to Query Amount of Staked Tokens in Autofarm
- Works for PancakeBunny farm too!
You need node.js installed
Supply necessary environment variables. You are free to do this how ever. In development, you may create a
.envfile at the root of this project.
Sample content of
BLOCKCHAIN_NODE="https://bsc-dataseed4.binance.org/" BSCSCAN_API_KEY="YOUR_BSC_API_KEY" # you can get one at https://bscscan.com/myapikey OWNER_WALLET_ADDRESS="YOUR_WALLET_ADDRESS"
config.jsaccording to what farms you are participating in.
The only tricky part is in finding the farm's PID for whatever token you're farming. I haven't explored the best way to obtain the PID yet except to brute force it. You can use the
stakedWantTokensfunction and manually go from 1 -> x and compare the output of the balance of your staked tokens and what you see on Autofarm's UI aka
https://autofarm.network/. If you don't want to use the codebase here to figure it out, you can also interact with the smart contract at
https://bscscan.com/address/0x0895196562c7868c5be92459fae7f877ed450452#readContract-> Contract -> Read Contract and scroll down to look for the
To run this code with
npm run start
As mentioned above, you are likely going to have to brute force or figure a better way to find the PIDs of your farm.
Here are the PIDs I discovered thus far (21 Feb 2021):
This is just a very simple repository to show you how you can interact with Smart Contracts using Web3 and a hosted full node. The code is somewhat specific to Autofarm (BSC) but you can adapt the code to work with ETH as well.
- Retrieve price either through a price data api provider (simplest) or querying the relevant liquidity pool smart contract and dividing the ratios to get a price (more complex and may not be accurate in the case of low activity liquidity pool with little to no arbitrageurs)
- In the case of staked LP tokens, query pool contract to find out the percentage ownership of the pool and the total locked paired assets and figure out how much of each token you own.