Skip to content

Instantly share code, notes, and snippets.

@JacobThaDev
Last active March 1, 2022 07:05
Show Gist options
  • Save JacobThaDev/dc19c58691716c85ae88169bcf00ce43 to your computer and use it in GitHub Desktop.
Save JacobThaDev/dc19c58691716c85ae88169bcf00ce43 to your computer and use it in GitHub Desktop.
How to get the value of a token.
const Web3 = require("web3");
const web3 = new Web3(new Web3.providers.HttpProvider("https://bsc-dataseed1.binance.org:443"));
const mini_abi = [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}];
const aggregator = [{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"description","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint80","name":"_roundId","type":"uint80"}],"name":"getRoundData","outputs":[{"internalType":"uint80","name":"roundId","type":"uint80"},{"internalType":"int256","name":"answer","type":"int256"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"updatedAt","type":"uint256"},{"internalType":"uint80","name":"answeredInRound","type":"uint80"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"latestRoundData","outputs":[{"internalType":"uint80","name":"roundId","type":"uint80"},{"internalType":"int256","name":"answer","type":"int256"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"updatedAt","type":"uint256"},{"internalType":"uint80","name":"answeredInRound","type":"uint80"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}];
/**
* @return the real-time price of a token
*/
async function getPrice(contract, pool_address, bnb_price) {
// the address of the pair. In this case we're using wbnb pair, so we need wbnb address.
let wbnb_addr = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c";
// get the amount of bnb stored in the LP
let wbnb_balance = await getBalance(pool_address, wbnb);
// get the amount of tokens stored in the LP
let token_balance = await getBalance(pool_address, contract);
return wbnb_balance / token_balance * bnb_price;
}
/**
* @return the balance of a token in a given wallet address.
*/
async function getBalance(wallet, token_contract, decimals = null) {
let call = await new web3.eth.call({
to: token_contract, // contract address
data: "0x70a08231000000000000000000000000"+wallet.replace("0x", "")
});
decimals = decimals ? decimals : await getDecimals(contract);
return parseInt(call) / 10 ** decimals;
}
/**
* @return integer the number of decimals for the contract
*/
async function getDecimals(address) {
let contract = new web3.eth.Contract(mini_abi, address);
let decimals = await contract.methods.decimals().call();
return decimals;
}
/**
* @return the latest price of bnb
*/
async function getBnbPrice() {
try {
let feed = new web3.eth.Contract(aggregator, "0x0567F2323251f0Aab15c8dFb1967E4e8A7D42aeE");
let round = await feed.methods.latestRoundData().call();
let decimals = await feed.methods.decimals().call();
let rounded = (round[1] / 10 ** decimals).toFixed(2)
return parseFloat(rounded);
} catch(err) {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment