| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 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 ? token.OneInchPrice : NaN
- const BinancePrice = token.BinancePrice ? token.BinancePrice : NaN
- const PrevOneInchPrice = token.PrevOneInchPrice
- const PrevBinancePrice = token.PrevBinancePrice
- 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 && (PrevOneInchPrice !== OneInchPrice && PrevBinancePrice !== BinancePrice)
- })()
- table.showLine(rows, isLogToFile ? fileLogger : undefined)
- // 上一帧的数据
- if (isLogToFile) {
- token.PrevOneInchPrice = token.OneInchPrice
- token.PrevBinancePrice = token.BinancePrice
- }
- })
- 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 priceTick = toToken.exchange.priceTick
- const fromIerc20Token = Config.baseIerc20Token
- const amount = Config.baseTokenAmount
- const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), priceTick)
- const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), priceTick)
- toToken.OneInchPrice = OneInchPrice
- toToken.BinancePrice = BinancePrice
- toToken.DiffPrice = BinancePrice - OneInchPrice
- if ((() => {
- return Math.abs(toToken.DiffPrice) < Math.pow(10, -priceTick)
- })()) {
- toToken.DiffPrice = 0
- } {
- toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, priceTick)
- }
- }
- // 绘制帧
- showPrices(context, task)
- }
- const priceMonitor = new OneTask('PriceMonitor',5 * 1000, OneTask.baseInit, onTickFun)
- priceMonitor.Start()
|