const OneTask = require('../../libs/one-task') const OneInch = require('../../libs/web3/1inch') const BinanceSpot = require('../../libs/binance/binance-spot') const Config = require('../../config/config') const NumKit = require('../../kit/num-kit') const TableKit = require('../../kit/table-kit') const PriceMonitorConfig = require('./config/price-monitor-config') const table = new TableKit(['pair', '1inch', 'binance', 'diff', 'percentage']) const showPrices = function(context, task) { const logger = task.logger const fileLogger = task.fileLogger const tokenMap = context.tokenMap table.printTitles() Object.keys(tokenMap).forEach((tokenHash) => { const token = tokenMap[tokenHash] const pair = token.exchange.pair const OneInchPrice = token.OneInchPrice const BinancePrice = token.BinancePrice const DiffPrice = token.DiffPrice const percentage = NumKit.getSubFloat((DiffPrice / OneInchPrice) * 100, 2) // 价格非法的就不输出了 if ((() => { return !OneInchPrice || !BinancePrice })()) return const rows = [pair.toString(), OneInchPrice.toString(), BinancePrice.toString(), DiffPrice.toString(), percentage.toString()] // 需要输出到文件的打印逻辑 const isLogToFile = (() => { return percentage > PriceMonitorConfig.percentageLimit })() table.showLine(rows, isLogToFile ? fileLogger : undefined) }) table.printEndLine(logger) } const onTickFun = async function() { const task = this const context = task.context const tokenMap = context.tokenMap const tokenContractAddressList = Object.keys(tokenMap) // 搜集所有价格数据 for (const tokenHash of tokenContractAddressList) { const toToken = tokenMap[tokenHash] const lotSize = toToken.exchange.lotSize const fromIerc20Token = Config.baseIerc20Token const amount = Config.baseTokenAmount const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), lotSize) const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), lotSize) toToken.OneInchPrice = OneInchPrice toToken.BinancePrice = BinancePrice toToken.DiffPrice = BinancePrice - OneInchPrice if ((() => { return Math.abs(toToken.DiffPrice) < Math.pow(10, -lotSize) })()) { toToken.DiffPrice = 0 } { toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, lotSize) } } // 绘制帧 showPrices(context, task) } const priceMonitor = new OneTask('PriceMonitor',5 * 1000, OneTask.baseInit, onTickFun) priceMonitor.Start()