price-monitor.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const OneTask = require('../../libs/one-task')
  2. const OneInch = require('../../libs/web3/1inch')
  3. const BinanceSpot = require('../../libs/binance/binance-spot')
  4. const Config = require('../../config/config')
  5. const NumKit = require('../../kit/num-kit')
  6. const TableKit = require('../../kit/table-kit')
  7. const PriceMonitorConfig = require('./config/price-monitor-config')
  8. const table = new TableKit(['pair', '1inch', 'binance', 'diff', 'percentage'])
  9. const showPrices = function(context, task) {
  10. const logger = task.logger
  11. const fileLogger = task.fileLogger
  12. const tokenMap = context.tokenMap
  13. table.printTitles()
  14. Object.keys(tokenMap).forEach((tokenHash) => {
  15. const token = tokenMap[tokenHash]
  16. const pair = token.exchange.pair
  17. const OneInchPrice = token.OneInchPrice ? token.OneInchPrice : NaN
  18. const BinancePrice = token.BinancePrice ? token.BinancePrice : NaN
  19. const PrevOneInchPrice = token.PrevOneInchPrice
  20. const PrevBinancePrice = token.PrevBinancePrice
  21. const DiffPrice = token.DiffPrice
  22. const percentage = NumKit.getSubFloat((DiffPrice / OneInchPrice) * 100, 2)
  23. // 价格非法的就不输出了
  24. // if ((() => {
  25. // return !OneInchPrice || !BinancePrice
  26. // })()) return
  27. const rows = [pair.toString(), OneInchPrice.toString(), BinancePrice.toString(), DiffPrice.toString(), percentage.toString()]
  28. // 需要输出到文件的打印逻辑
  29. const isLogToFile = (() => {
  30. return percentage > PriceMonitorConfig.percentageLimit && (PrevOneInchPrice !== OneInchPrice || PrevBinancePrice !== BinancePrice)
  31. })()
  32. table.showLine(rows, isLogToFile ? fileLogger : undefined)
  33. // 上一帧的数据
  34. if (isLogToFile) {
  35. token.PrevOneInchPrice = token.OneInchPrice
  36. token.PrevBinancePrice = token.BinancePrice
  37. }
  38. })
  39. table.printEndLine(logger)
  40. }
  41. const onTickFun = async function() {
  42. const task = this
  43. const context = task.context
  44. const tokenMap = context.tokenMap
  45. const tokenContractAddressList = Object.keys(tokenMap)
  46. // 搜集所有价格数据
  47. for (const tokenHash of tokenContractAddressList) {
  48. const toToken = tokenMap[tokenHash]
  49. const priceTick = toToken.exchange.priceTick
  50. const fromIerc20Token = Config.baseIerc20Token
  51. const amount = Config.baseTokenAmount
  52. const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), priceTick)
  53. const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), priceTick)
  54. toToken.OneInchPrice = OneInchPrice
  55. toToken.BinancePrice = BinancePrice
  56. toToken.DiffPrice = BinancePrice - OneInchPrice
  57. if ((() => {
  58. return Math.abs(toToken.DiffPrice) < Math.pow(10, -priceTick)
  59. })()) {
  60. toToken.DiffPrice = 0
  61. } {
  62. toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, priceTick)
  63. }
  64. }
  65. // 绘制帧
  66. showPrices(context, task)
  67. }
  68. const priceMonitor = new OneTask('PriceMonitor',5 * 1000, OneTask.baseInit, onTickFun)
  69. priceMonitor.Start()