0

I'm coding a simple app in node.js to retrieve token prices. I am using router contract '0x10ED43C718714eb63d5aA57B78B54704E256024E' and ethers.js library

Most addresses seem to work correctly, but some return a 'ds-math-sub-underflow' error when trying to use getAmountsIn. E.g. addresses '0xd48474e7444727bf500a32d5abe01943f3a59a64' and '0xA91A840eE787Bb773922f961b6C5cD2A564F535d'

Can someone take a look and give me some suggestions on what I'm doing wrong? Thanks in advance

import { BEP20, IUniswapV2Router02 } from './abi'
import { ethers } from 'ethers'
import getNodeUrl from './utils/getRpcUrl'


const WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
const pancakeRouterV2 = '0x10ED43C718714eb63d5aA57B78B54704E256024E'

let rpcUrl = getNodeUrl()
let provider = new ethers.providers.JsonRpcProvider(rpcUrl);

// let wsProvider = new ethers.providers.WebSocketProvider("wss://bsc-ws-node.nariox.org:443");

// wsProvider.on('block', (blockNumber) => {
//   // console.log('New Block: ' + blockNumber);
// });

const getDecimals = async (token: any) => {
  const contract = new ethers.Contract(token, BEP20, provider);

  let decimals = await contract.decimals().then((units: any) => {
    units = units.toNumber()
    return units
  })

  return decimals
}


const getPriceFromPancake = async ({data}: any) => {

  const token0 = data.token0 || null
  const decimals0 = data.decimals0 || await getDecimals(token0)
  
  const token1 = data.token1 || WBNB
  let decimals1 = data.decimals1 || 18

  decimals1 = Math.pow(10, decimals1)

  const amountIn = (1 * decimals1).toString()
  const amountOut = amountIn

  const amountInLenght = amountIn.length

  const pancakeRouter = new ethers.Contract(pancakeRouterV2, IUniswapV2Router02.abi, provider)

  console.log(decimals0);
  

  let amountsOut = await pancakeRouter.getAmountsOut(amountIn, [token0, token1]).then((result: any) => {
    let price = '0'
    // let divider = 0

    if(decimals0 === 8){
      price = (result[0] / 1e26).toFixed(amountInLenght)
    } 
  
    if(decimals0 === 9){
      price = (result[1] / 1e27).toFixed(amountInLenght)
    } 
  
    if(decimals0 === 18){
      price = (result[1] / 1e18).toFixed(amountInLenght)
    }


    // console.log('Price Out: ' + price);
    // console.log('Price Out USD: ' + parseFloat(price) * 314);

    return price
  }, (reason: any) => {
    console.error('error getAmountsOut', reason.reason);
  })
  
  let amountsIn = await pancakeRouter.getAmountsIn(amountOut, [token1, token0]).then((result: any) => {
      let price = '0'
     
      if(decimals0 === 8){
        price = (result[0] / 1e26).toFixed(amountInLenght)
      } 

      if(decimals0 === 9){
        price = (result[0] / 1e27).toFixed(amountInLenght)
      } 
    
      if(decimals0 === 18){
        price = (result[0] / 1e18).toFixed(amountInLenght)
      } 
    
      // console.log('Price In: ' + price);
      // console.log('Price In USD: ' + parseFloat(price) * 314);

      return price
    }, (reason: any) => {
      console.error('error getAmountsIn', reason.reason);
    })
    
    return {
      amountsOut: amountsOut,
      amountsIn: amountsIn,
      amountsOutUSD: (parseFloat(amountsOut) * 312).toString(),
      amountsInUSD: (parseFloat(amountsIn) * 312).toString(),
    }
}


// Bogged Finance
// const token = '0xB09FE1613fE03E7361319d2a43eDc17422f36B09'

// GEMIT.app (GEMIT) 
// const token = '0xA91A840eE787Bb773922f961b6C5cD2A564F535d'

// BitBook (BBT)
const token = '0xd48474e7444727bf500a32d5abe01943f3a59a64'

getPriceFromPancake({
  data: {
    token0: token,
  }
}).then((result) => {
  console.log(result);
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.