Last active
November 30, 2021 17:31
-
-
Save sirsean/1ff776047ab6ec5c52860e322d802b91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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