| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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 PriceMonitorConfig = require('./config/price-monitor-config')
- const showPrices = function(context, task) {
- const logger = task.logger
- const fileLogger = task.fileLogger
- const tokenMap = context.tokenMap
- const titles = ['pair', '1inch', 'binance', 'diff', 'percentage']
- const colWidth = 30
- const padTitles = titles.map((title) => {
- return title.padEnd(colWidth, ' ')
- })
- const titleLine = (() => {
- let titleLine = ''
- for (const padTitle of padTitles) {
- titleLine = titleLine.concat('|').concat(' ').concat(padTitle)
- }
- return titleLine.concat('|')
- })()
- const splitLint = ''.padEnd((colWidth + 2) * titles.length + 1, '-')
- console.clear()
- console.info(splitLint)
- console.info(titleLine)
- console.info(splitLint)
- 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)
- const cols = [pair.toString(), OneInchPrice.toString(), BinancePrice.toString(), DiffPrice.toString(), percentage.toString()]
- const padCols = cols.map((col) => {
- return col.padEnd(colWidth, ' ')
- })
- const infoLine = (() => {
- let infoLine = ''
- for (const padCol of padCols) {
- infoLine = infoLine.concat('|').concat(' ').concat(padCol)
- }
- return infoLine.concat('|')
- })()
- // 价格非法的就不输出了
- if ((() => {
- return !OneInchPrice || !BinancePrice
- })()) return
- // 需要输出到文件的打印逻辑
- if ((() => {
- return percentage > PriceMonitorConfig.percentageLimit
- })()) {
- fileLogger.info(infoLine)
- }
- console.info(infoLine)
- })
- console.log(splitLint)
- logger.info('')
- }
- 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()
|