1inch.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const HttpKit = require('../../kit/http-kit.js')
  2. const Config = require('../../config/config.js')
  3. // const SimpleWeb3 = require("./simple-web3");
  4. const chainID = 56
  5. class OneInch {}
  6. OneInch.price = async function(fromTokenAddress, toTokenAddress, amount, fromTokenDecimals) {
  7. fromTokenAddress = fromTokenAddress.toLowerCase()
  8. toTokenAddress = toTokenAddress.toLowerCase()
  9. amount = amount * (10 ** fromTokenDecimals)
  10. const url = `https://api.1inch.exchange/v3.0/${chainID}/quote?fromTokenAddress=${fromTokenAddress}&amount=${amount}` +
  11. `&toTokenAddress=${toTokenAddress}`
  12. const { data: rst } = await HttpKit.get(url)
  13. if (!rst || !rst.toToken || !rst.toTokenAmount) {
  14. return rst
  15. }
  16. const toTokenAmount = parseFloat(rst.toTokenAmount) / (10 ** rst.toToken.decimals)
  17. const fromTokenAmount = amount / (10 ** fromTokenDecimals)
  18. rst.price = fromTokenAmount / toTokenAmount
  19. Config.estimatedGas = rst.estimatedGas
  20. if (toTokenAddress.toLowerCase() === '0xe9e7cea3dedca5984780bafc599bd69add087d56') {
  21. rst.price = 1 / rst.price
  22. }
  23. return rst.price
  24. }
  25. OneInch.buildSwapTx = async function(fromTokenAddress, toTokenAddress, fromTokenDecimals, amount, fromAddress,
  26. gas = Config.estimatedGas, slippage = 1) {
  27. amount = amount * (10 ** fromTokenDecimals)
  28. const url = `https://api.1inch.exchange/v3.0/${chainID}/swap?fromTokenAddress=${fromTokenAddress}&toTokenAddress=${toTokenAddress}`
  29. + `&amount=${amount}&fromAddress=${fromAddress}&slippage=${slippage}`
  30. const { data: rst } = await HttpKit.get(url, {
  31. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4464.0 Safari/537.36 Edg/91.0.852.0'
  32. })
  33. if (rst && rst.toToken && rst.fromToken) {
  34. const fromTokenAmount = parseFloat(rst.fromTokenAmount) / (10 ** rst.fromToken.decimals)
  35. const toTokenAmount = parseFloat(rst.toTokenAmount) / (10 ** rst.toToken.decimals)
  36. rst.price = fromTokenAmount / toTokenAmount
  37. }
  38. return rst
  39. }
  40. // OneInch.swap = async function(txObject) {
  41. // const txCount = await SimpleWeb3.web3.eth.getTransactionCount(PrivateConfig.address)
  42. // txObject.nonce = SimpleWeb3.web3.utils.toHex(txCount)
  43. // // txObject.gasPrice = SimpleWeb3.web3.utils.toHex(txObject.gasPrice)
  44. // txObject.gasPrice = '0x165a0bc00'
  45. // txObject.gas = SimpleWeb3.web3.utils.toHex(txObject.gas)
  46. // txObject.value = SimpleWeb3.web3.utils.toHex(txObject.value)
  47. //
  48. // await SimpleWeb3.sendTransaction(txObject)
  49. // }
  50. //
  51. // OneInch.Start = async function(toToken) {
  52. // while (true) {
  53. // toToken.alive = true
  54. //
  55. // // 如果BNB连续变化maxRefreshBNBTimes次,则1INCH或其他交易所可能已经出bug。
  56. // if (Config.refreshBNBTimes >= Config.maxRefreshBNBTimes) {
  57. // console.log(`机器人判定交易所可能出了bug,失败次数:${Config.refreshBNBTimes},为保护BNB,已停止交易。`)
  58. // break
  59. // }
  60. //
  61. // try {
  62. // const tokenSymbol = toToken.symbol
  63. // const baseToken = Config.baseToken
  64. // if (!baseToken.balance) {
  65. // await MyKit.sleep(1000)
  66. //
  67. // continue
  68. // }
  69. // const binancePairSymbol = tokenSymbol + baseToken.symbol
  70. // const amount = Config.baseTokenAmount < baseToken.balance ? Config.baseTokenAmount : baseToken.balance
  71. // const OneInchPrice = await OneInch.price(baseToken.contract, baseToken.decimals, amount, toToken.contract)
  72. // const binancePrice = await bs.price(binancePairSymbol)
  73. // let distance = MyKit._N(100 * ((binancePrice / OneInchPrice) - 1.001), 4)
  74. // let profit = MyKit._N(amount * distance / 100 - Config.charge, 4)
  75. // let time = MyKit.getTimeByMillisecond(new Date().getTime())
  76. //
  77. // // debug
  78. // if (Config.debug && profit > 0) {
  79. // console.log(`[${time}, ${binancePairSymbol}] D价格: ${OneInchPrice},C价格: ${binancePrice},利润:${profit}。`)
  80. // }
  81. //
  82. // // 一次判断
  83. // if (profit > Config.profitLimit) {
  84. // // 二次判断,并获取交易tx
  85. // const swapRST = await OneInch.swap(baseToken.contract, toToken.contract, baseToken.decimals, amount)
  86. // if (!swapRST) {
  87. // continue
  88. // }
  89. //
  90. // distance = MyKit._N(100 * ((binancePrice / swapRST.price) - 1.001), 4)
  91. // profit = MyKit._N(amount * distance / 100 - Config.charge, 4)
  92. // if (!baseToken.isSwap && profit > Config.profitLimit) {
  93. // time = MyKit.getTimeByMillisecond(new Date().getTime())
  94. //
  95. // // gas容错
  96. // swapRST.tx.gas = parseInt((parseInt(swapRST.tx.gas) * 1.5)) + ''
  97. //
  98. // // 如果利润大于5%,手续费*2抢单
  99. // if (distance > 20) {
  100. // console.log('[利润大于20%]手续费*1.5执行抢单逻辑.')
  101. // swapRST.tx.gasPrice = parseInt((parseInt(swapRST.tx.gasPrice) * 1.5)) + ''
  102. // }
  103. //
  104. // console.log(`[${time}, ${tokenSymbol}]判定通过, 利润率:${distance}%, 预估利润:${profit}${baseToken.symbol}, 目标利润:${Config.profitLimit}${baseToken.symbol}.\n`)
  105. // console.log(`DEFI价格: ${swapRST.price}, CEFI价格: ${binancePrice}\n\n`)
  106. // await wallet.transferByTX1Inch(swapRST.tx)
  107. // }
  108. // }
  109. //
  110. // await MyKit.sleep(5000)
  111. // } catch (e) {
  112. // console.log(e)
  113. // }
  114. // }
  115. // }
  116. module.exports = OneInch