price-monitor.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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
  18. const BinancePrice = token.BinancePrice
  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 || PrevOneInchPrice === OneInchPrice || PrevBinancePrice === 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
  31. })()
  32. table.showLine(rows, isLogToFile ? fileLogger : undefined)
  33. })
  34. table.printEndLine(logger)
  35. }
  36. const onTickFun = async function() {
  37. const task = this
  38. const context = task.context
  39. const tokenMap = context.tokenMap
  40. const tokenContractAddressList = Object.keys(tokenMap)
  41. // 搜集所有价格数据
  42. for (const tokenHash of tokenContractAddressList) {
  43. const toToken = tokenMap[tokenHash]
  44. const priceTick = toToken.exchange.priceTick
  45. const fromIerc20Token = Config.baseIerc20Token
  46. const amount = Config.baseTokenAmount
  47. const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), priceTick)
  48. const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), priceTick)
  49. toToken.OneInchPrice = OneInchPrice
  50. toToken.BinancePrice = BinancePrice
  51. toToken.DiffPrice = BinancePrice - OneInchPrice
  52. if ((() => {
  53. return Math.abs(toToken.DiffPrice) < Math.pow(10, -priceTick)
  54. })()) {
  55. toToken.DiffPrice = 0
  56. } {
  57. toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, priceTick)
  58. }
  59. }
  60. // 绘制帧
  61. showPrices(context, task)
  62. // 赋予prev价格值
  63. Object.keys(tokenMap).forEach((tokenHash) => {
  64. const token = tokenMap[tokenHash]
  65. token.PrevOneInchPrice = token.OneInchPrice
  66. token.PrevBinancePrice = token.BinancePrice
  67. })
  68. }
  69. const priceMonitor = new OneTask('PriceMonitor',5 * 1000, OneTask.baseInit, onTickFun)
  70. priceMonitor.Start()