Skip to content

Instantly share code, notes, and snippets.

@sirsean
Last active November 30, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirsean/1ff776047ab6ec5c52860e322d802b91 to your computer and use it in GitHub Desktop.
Save sirsean/1ff776047ab6ec5c52860e322d802b91 to your computer and use it in GitHub Desktop.
import Web3 from 'web3';
import ERC20ContractABI from './src/abi/erc20.js';
import UniswapV2ABI from './src/abi/uniswap_v2.js';
function basicResponse(err, res) {
return res;
}
function convertDecimals(amount, decimals) {
return amount * Math.pow(10, -1 * decimals);
}
const ronin = new Web3(new Web3.providers.HttpProvider('https://api.roninchain.com/rpc'));
async function getLiquidity(lpContract) {
const blockNumber = await ronin.eth.getBlockNumber(basicResponse);
return Promise.all([
lpContract.methods.token0().call(blockNumber, basicResponse),
lpContract.methods.token1().call(blockNumber, basicResponse),
]).then(([token0Addr, token1Addr]) => {
const token0 = new ronin.eth.Contract(ERC20ContractABI, token0Addr);
const token1 = new ronin.eth.Contract(ERC20ContractABI, token1Addr);
return Promise.all([
lpContract.methods.getReserves().call(blockNumber, basicResponse),
token0.methods.decimals().call(blockNumber, basicResponse),
token0.methods.symbol().call(blockNumber, basicResponse),
token0.methods.totalSupply().call(blockNumber, basicResponse),
token1.methods.decimals().call(blockNumber, basicResponse),
token1.methods.symbol().call(blockNumber, basicResponse),
token1.methods.totalSupply().call(blockNumber, basicResponse),
]);
}).then(([reserves, token0Decimals, token0Symbol, token0Supply, token1Decimals, token1Symbol, token1Supply]) => {
return [
{symbol: token0Symbol, liquidity: convertDecimals(reserves[0], token0Decimals), totalSupply: convertDecimals(token0Supply, token0Decimals)},
{symbol: token1Symbol, liquidity: convertDecimals(reserves[1], token1Decimals), totalSupply: convertDecimals(token1Supply, token1Decimals)},
];
});
}
const slpWethPool = new ronin.eth.Contract(UniswapV2ABI, '0x306a28279d04a47468ed83d55088d0dcd1369294');
const slpLiquidity = await getLiquidity(slpWethPool);
console.log(slpLiquidity);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment