I’ve made a function to get swap prices of the Sushiswap rates using getReserves() function. As I have learned, in order to get the current swap price of 2 tokens, I need to divide the amount of reserves of the first token by the amount of reserves. But when I try to use this function::Listen
I’ve made a function to get swap prices of the Sushiswap rates using getReserves() function. As I have learned, in order to get the current swap price of 2 tokens, I need to divide the amount of reserves of the first token by the amount of reserves. But when I try to use this function I get the call revert exception (method="getReserves()", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.4.0)
error.
But the strange thing is that when I do the same function on wETH token, I get all the results I need – the actual price.
const runBot = async () => { const sushiFactory = new ethers.Contract( '0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac', UniswapV2Factory.abi, wallet, ); const uniswapFactory = new ethers.Contract( '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f', UniswapV2Factory.abi, wallet, ); const usdcAddress = '0x6b175474e89094c44da98b954eedeac495271d0f'; const wethAddress = '0x6b3595068778dd592e39a122f4f5a5cf09c90fe2'; const sushi_address = '0x6b3595068778dd592e39a122f4f5a5cf09c90fe2' const bnb_address = '0xB8c77482e45F1F44dE1745F52C74426C631bDD52' const usdc_address = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' const eth_address = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' const dai_address = '0x6b175474e89094c44da98b954eedeac495271d0f' const btc_address = '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599' let sushiEthDai; let uniswapEthDai; const loadPairs = async () => { sushiEthDai = new ethers.Contract( await sushiFactory.getPair(btc_address, dai_address), UniswapV2Pair.abi, wallet, ); uniswapEthDai = new ethers.Contract( await uniswapFactory.getPair(btc_address, dai_address), UniswapV2Pair.abi, wallet, ); }; await loadPairs(); provider.on('block', async (blockNumber) => { try { console.log(blockNumber); const sushiReserves = await sushiEthDai.getReserves(); const uniswapReserves = await uniswapEthDai.getReserves(); const reserve0Sushi = Number(ethers.utils.formatUnits(sushiReserves[0], 18)); const reserve1Sushi = Number(ethers.utils.formatUnits(sushiReserves[1], 18)); const reserve0Uni = Number(ethers.utils.formatUnits(uniswapReserves[0], 18)); const reserve1Uni = Number(ethers.utils.formatUnits(uniswapReserves[1], 18)); const priceUniswap = reserve0Uni / reserve1Uni; const priceSushiswap = reserve0Sushi / reserve1Sushi; const shouldStartEth = priceUniswap < priceSushiswap; const spread = Math.abs((priceSushiswap / priceUniswap - 1) * 100); console.log(`SUSHI PRICE ${reserve0Sushi / reserve1Sushi}`) } catch (err) { console.error(err); } }) } runBot()