0

I am currently testing around with https://github.com/dappuniversity/price-bot and came to a weird error. The task of the bot is to compare some trading pairs on Uniswap and Kyber network. I think Uniswap works correctly while Kyber network seems to shoot some problems. At first here is the code (shortened):

// SERVER CONFIG
const PORT = process.env.PORT || 5000
const app = express();
const server = http.createServer(app).listen(PORT, () => console.log(`Listening on ${ PORT }`))

// WEB3 CONFIG
const web3 = new Web3(process.env.RPC_URL)

// Uniswap Factory Contract: https://etherscan.io/address/0xc0a47dfe034b400b47bdad5fecda2621de6c4d95#code
const UNISWAP_FACTORY_ABI = [{"name":"NewExchange","inputs":[{"type":"address","name":"token","indexed":true},{"type":"address","name":"exchange","indexed":true}],"anonymous":false,"type":"event"},{"name":"initializeFactory","outputs":[],"inputs":[{"type":"address","name":"template"}],"constant":false,"payable":false,"type":"function","gas":35725},{"name":"createExchange","outputs":[{"type":"address","name":"out"}],"inputs":[{"type":"address","name":"token"}],"constant":false,"payable":false,"type":"function","gas":187911},{"name":"getExchange","outputs":[{"type":"address","name":"out"}],"inputs":[{"type":"address","name":"token"}],"constant":true,"payable":false,"type":"function","gas":715},{"name":"getToken","outputs":[{"type":"address","name":"out"}],"inputs":[{"type":"address","name":"exchange"}],"constant":true,"payable":false,"type":"function","gas":745},{"name":"getTokenWithId","outputs":[{"type":"address","name":"out"}],"inputs":[{"type":"uint256","name":"token_id"}],"constant":true,"payable":false,"type":"function","gas":736},{"name":"exchangeTemplate","outputs":[{"type":"address","name":"out"}],"inputs":[],"constant":true,"payable":false,"type":"function","gas":633},{"name":"tokenCount","outputs":[{"type":"uint256","name":"out"}],"inputs":[],"constant":true,"payable":false,"type":"function","gas":663}]
const UNISWAP_FACTORY_ADDRESS = '0xc0a47dfe034b400b47bdad5fecda2621de6c4d95'
const uniswapFactoryContract = new web3.eth.Contract(UNISWAP_FACTORY_ABI, UNISWAP_FACTORY_ADDRESS)

/ Uniswap Exchange Template: https://etherscan.io/address/0x09cabec1ead1c0ba254b09efb3ee13841712be14#code
const UNISWAP_EXCHANGE_ABI = ....

// Kyber mainnet "Expected Rate": https://etherscan.io/address/0x65bF64Ff5f51272f729BDcD7AcFB00677ced86Cd#readContract
const KYBER_RATE_ABI =....

const KYBER_RATE_ADDRESS = /*`0x96B610046D63638d970E6243151311d8827D69a5`*/'0x65bF64Ff5f51272f729BDcD7AcFB00677ced86Cd'
const kyberRateContract = new web3.eth.Contract(KYBER_RATE_ABI, KYBER_RATE_ADDRESS)

async function checkPair(args) {
  const { inputTokenSymbol, inputTokenAddress, outputTokenSymbol, outputTokenAddress, inputAmount } = args

  const exchangeAddress = await uniswapFactoryContract.methods.getExchange(outputTokenAddress).call()
  console.log(`Exchange for output token found: ${exchangeAddress}`)
  const exchangeContract = new web3.eth.Contract(UNISWAP_EXCHANGE_ABI, exchangeAddress)
  console.log(`Exchange Contract : ${exchangeContract}`)

  const uniswapResult = await exchangeContract.methods.getEthToTokenInputPrice(inputAmount).call()
  console.log(`Uniswaps result: : ${uniswapResult}`)
  let kyberResult = await kyberRateContract.methods.getExpectedRate(inputTokenAddress, outputTokenAddress, inputAmount).call()
  console.log(`Kyber result: ${kyberResult}`)

  console.table([{
    'Input Token': inputTokenSymbol,
    'Output Token': outputTokenSymbol,
    'Input Amount': web3.utils.fromWei(inputAmount, 'Ether'),
    'Uniswap Return': web3.utils.fromWei(uniswapResult, 'Ether'),
    'Kyber Expected Rate': web3.utils.fromWei(kyberResult.expectedRate, 'Ether'),
    'Kyber Min Return': web3.utils.fromWei(kyberResult.slippageRate, 'Ether'),
    'Timestamp': moment().tz('America/Chicago').format(),
  }])
}

let priceMonitor
let monitoringPrice = false

async function monitorPrice() {
  if(monitoringPrice) {
    return
  }

  console.log("Checking prices...")
  monitoringPrice = true

  try {

    // ADD YOUR CUSTOM TOKEN PAIRS HERE!!!

    await checkPair({
      inputTokenSymbol: 'ETH',
      inputTokenAddress: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
      outputTokenSymbol: 'MKR',
      outputTokenAddress: '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2',
      inputAmount: web3.utils.toWei('0.03', 'ETHER')
    })

    await checkPair({
      inputTokenSymbol: 'ETH',
      inputTokenAddress: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
      outputTokenSymbol: 'KNC',
      outputTokenAddress: '0xdd974d5c2e2928dea5f71b9825b8b646686bd200',
      inputAmount: web3.utils.toWei('1', 'ETHER')
    })

   // ... some more ...


  } catch (error) {
    console.error(error)
    monitoringPrice = false
    clearInterval(priceMonitor)
    return
  }

  monitoringPrice = false
}

// Check markets every n seconds
const POLLING_INTERVAL = process.env.POLLING_INTERVAL || 3000 // 3 Seconds
priceMonitor = setInterval(async () => { await monitorPrice() }, POLLING_INTERVAL)

Now here is the output:

Listening on 5000
Checking prices...
Exchange for output token found: 0x2C4Bd064b998838076fa341A83d007FC2FA50957
Exchange Contract : [object Object]
Uniswaps result: : 50931593641169419
Kyber result: [object Object]
┌─────────┬─────────────┬──────────────┬──────────────┬────────────────────────┬──────────────────────┬────────────────────────┬─────────────────────────────┐
│ (index) │ Input Token │ Output Token │ Input Amount │     Uniswap Return     │ Kyber Expected Rate  │    Kyber Min Return    │          Timestamp          │
├─────────┼─────────────┼──────────────┼──────────────┼────────────────────────┼──────────────────────┼────────────────────────┼─────────────────────────────┤
│    0    │    'ETH'    │    'MKR'     │    '0.03'    │ '0.050931593641169419' │ '1.6977197880389806' │ '1.646788194397811182' │ '2023-03-06T23:37:16-06:00' │
└─────────┴─────────────┴──────────────┴──────────────┴────────────────────────┴──────────────────────┴────────────────────────┴─────────────────────────────┘
Exchange for output token found: 0x49c4f9bc14884f6210F28342ceD592A633801a8b
Exchange Contract : [object Object]
Uniswaps result: : 1110441629411142678154
Error: Returned error: execution reverted

looks like the first pair is working (even the rate difference between Uniswap and kyber seems weird) but at the second pair it breaks. I tried to find and use different contract addresses but these right here are looking legit. Is there something I overlook? Are the contract addresses wrong or is it something else? I would be very very grateful if someone could help me, thanks in advance!

0

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.