price-monitor.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 PriceMonitorConfig = require('./config/price-monitor-config')
  7. const showPrices = function(context, task) {
  8. const logger = task.logger
  9. const fileLogger = task.fileLogger
  10. const tokenMap = context.tokenMap
  11. const titles = ['pair', '1inch', 'binance', 'diff', 'percentage']
  12. const colWidth = 30
  13. const padTitles = titles.map((title) => {
  14. return title.padEnd(colWidth, ' ')
  15. })
  16. const titleLine = (() => {
  17. let titleLine = ''
  18. for (const padTitle of padTitles) {
  19. titleLine = titleLine.concat('|').concat(' ').concat(padTitle)
  20. }
  21. return titleLine.concat('|')
  22. })()
  23. const splitLint = ''.padEnd((colWidth + 2) * titles.length + 1, '-')
  24. console.clear()
  25. console.info(splitLint)
  26. console.info(titleLine)
  27. console.info(splitLint)
  28. Object.keys(tokenMap).forEach((tokenHash) => {
  29. const token = tokenMap[tokenHash]
  30. const pair = token.exchange.pair
  31. const OneInchPrice = token.OneInchPrice
  32. const BinancePrice = token.BinancePrice
  33. const DiffPrice = token.DiffPrice
  34. const percentage = NumKit.getSubFloat((DiffPrice / OneInchPrice) * 100, 2)
  35. const cols = [pair.toString(), OneInchPrice.toString(), BinancePrice.toString(), DiffPrice.toString(), percentage.toString()]
  36. const padCols = cols.map((col) => {
  37. return col.padEnd(colWidth, ' ')
  38. })
  39. const infoLine = (() => {
  40. let infoLine = ''
  41. for (const padCol of padCols) {
  42. infoLine = infoLine.concat('|').concat(' ').concat(padCol)
  43. }
  44. return infoLine.concat('|')
  45. })()
  46. // 价格非法的就不输出了
  47. if ((() => {
  48. return !OneInchPrice || !BinancePrice
  49. })()) return
  50. // 需要输出到文件的打印逻辑
  51. if ((() => {
  52. return percentage > PriceMonitorConfig.percentageLimit
  53. })()) {
  54. fileLogger.info(infoLine)
  55. }
  56. console.info(infoLine)
  57. })
  58. console.log(splitLint)
  59. logger.info('')
  60. }
  61. const onTickFun = async function() {
  62. const task = this
  63. const context = task.context
  64. const tokenMap = context.tokenMap
  65. const tokenContractAddressList = Object.keys(tokenMap)
  66. // 搜集所有价格数据
  67. for (const tokenHash of tokenContractAddressList) {
  68. const toToken = tokenMap[tokenHash]
  69. const lotSize = toToken.exchange.lotSize
  70. const fromIerc20Token = Config.baseIerc20Token
  71. const amount = Config.baseTokenAmount
  72. const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), lotSize)
  73. const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), lotSize)
  74. toToken.OneInchPrice = OneInchPrice
  75. toToken.BinancePrice = BinancePrice
  76. toToken.DiffPrice = BinancePrice - OneInchPrice
  77. if ((() => {
  78. return Math.abs(toToken.DiffPrice) < Math.pow(10, -lotSize)
  79. })()) {
  80. toToken.DiffPrice = 0
  81. } {
  82. toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, lotSize)
  83. }
  84. }
  85. // 绘制帧
  86. showPrices(context, task)
  87. }
  88. const priceMonitor = new OneTask('PriceMonitor',5 * 1000, OneTask.baseInit, onTickFun)
  89. priceMonitor.Start()