Browse Source

加入lotSize

龚成明 2 years ago
parent
commit
a44284ffbf
4 changed files with 35 additions and 27 deletions
  1. 0 14
      libs/binance/binance-kit.js
  2. 27 7
      libs/one-task.js
  3. 7 5
      libs/token.js
  4. 1 1
      scripts/one-pro.js

+ 0 - 14
libs/binance/binance-kit.js

@@ -38,20 +38,6 @@ module.exports = class BinanceKit {
     return `${buffer}]`
   }
 
-  static parsePriceTickFilterMap(pairInfoList) {
-    if (pairInfoList === undefined) throw new Error('exchange info symbols is undefined!')
-
-    const filterMap = {}
-
-    pairInfoList.forEach(function (pairInfo) {
-      const priceTickFilter = pairInfo.filters.filter(filterObj => filterObj.filterType === 'PRICE_FILTER')[0]
-
-      filterMap[pairInfo.symbol] = Math.log10(1 / parseFloat(priceTickFilter.tickSize))
-    })
-
-    return filterMap
-  }
-
   static parseBalancesToAccountAssetMap(balances) {
     const accountAssetMap = {}
 

+ 27 - 7
libs/one-task.js

@@ -2,25 +2,45 @@ const OneTask = require('./task')
 const Context = require("./context");
 const Config = require("../config/config");
 const BinanceSpot = require("./binance/binance-spot");
-const BinanceKit = require("./binance/binance-kit");
 const IERC20 = require("./web3/ierc20-token");
 const Token = require("./token");
 
 OneTask.baseInit = async function() {
   this.context = new Context()
 
-  // 获取priceTickFilter
-  const priceTickFilterMap = await (async () => {
-    const pairs = Object.values(Config.tokenMapping).map(coin => `${coin}${Config.baseIerc20Token.symbol}` )
-    const exchangeInfo = await BinanceSpot.exchangeInfo(pairs)
-    return BinanceKit.parsePriceTickFilterMap(exchangeInfo.symbols)
+  const pairs = Object.values(Config.tokenMapping).map(coin => `${coin}${Config.baseIerc20Token.symbol}` )
+  const exchangeInfo = await BinanceSpot.exchangeInfo(pairs)
+
+  // 解析priceTickFilter
+  const priceTickFilterMap = (() => {
+    const filterMap = {}
+
+    exchangeInfo.symbols.forEach(function(pairInfo) {
+      const filter = pairInfo.filters.filter(filterObj => filterObj.filterType === 'PRICE_FILTER')[0]
+
+      filterMap[pairInfo.symbol] = Math.log10(1 / parseFloat(filter.tickSize))
+    })
+
+    return filterMap
+  })()
+  // 解析lotSizeFilter
+  const lotSizeFilterMap = (() => {
+    const filterMap = {}
+
+    exchangeInfo.symbols.forEach(function(pairInfo) {
+      const filter = pairInfo.filters.filter(filterObj => filterObj.filterType === 'LOT_SIZE')[0]
+
+      filterMap[pairInfo.symbol] = Math.log10(1 / parseFloat(filter.stepSize))
+    })
+
+    return filterMap
   })()
 
   // 初始化IERC20的token
   await IERC20.batchInit(this.context, Object.keys(Config.tokenMapping))
 
   // 初始化本地token,绑定binance与IERC20
-  await Token.batchInit(this.context, Object.keys(Config.tokenMapping), priceTickFilterMap)
+  await Token.batchInit(this.context, Object.keys(Config.tokenMapping), priceTickFilterMap, lotSizeFilterMap)
 }
 
 module.exports = OneTask

+ 7 - 5
libs/token.js

@@ -5,30 +5,32 @@ module.exports = class Token {
   exchange = {}
   ierc20 = {}
 
-  constructor(exchangeSymbol, exchangePriceTick, exchangePair, ierc20Decimals) {
+  constructor(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals) {
     this.exchange.symbol = exchangeSymbol
     this.exchange.priceTick = exchangePriceTick
+    this.exchange.lotSize = exchangeLotSize
     this.exchange.pair = exchangePair
 
     this.ierc20.decimals = ierc20Decimals
   }
 
-  static async init(context, tokenHash, priceTickFilterMap) {
+  static async init(context, tokenHash, priceTickFilterMap, lotSizeFilterMap) {
     // token初始化
     const ierc20Decimals = parseInt(await IERC20.getDecimals(tokenHash))
     const exchangeSymbol = Config.tokenMapping[tokenHash]
     const exchangePair = `${exchangeSymbol}${Config.baseIerc20Token.symbol}`
     const exchangePriceTick = priceTickFilterMap[exchangePair]
+    const exchangeLotSize = lotSizeFilterMap[exchangePair]
 
-    context.tokenMap[tokenHash] = new Token(exchangeSymbol, exchangePriceTick, exchangePair, ierc20Decimals)
+    context.tokenMap[tokenHash] = new Token(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals)
   }
 
-  static async batchInit(context, ierc20TokenAddressList, priceTickFilterMap) {
+  static async batchInit(context, ierc20TokenAddressList, priceTickFilterMap, lotSizeFilterMap) {
     context.tokenMap = {}
 
     for (const tokenHash of ierc20TokenAddressList) {
       // 初始化token
-      await Token.init(context, tokenHash, priceTickFilterMap)
+      await Token.init(context, tokenHash, priceTickFilterMap, lotSizeFilterMap)
     }
   }
 }

+ 1 - 1
scripts/one-pro.js

@@ -46,7 +46,7 @@ const orderHandler = function(context, task) {
       // 1. 获取base资产数量
       // 2. 下单获取成交数量以及平均价格
       token.orderPrice = token.BinancePrice
-      token.orderAmount = NumKit.getSubFloat(Config.baseTokenAmount / token.orderPrice, token.exchange.priceTick)
+      token.orderAmount = NumKit.getSubFloat(Config.baseTokenAmount / token.orderPrice, token.exchange.lotSize)
 
       // 3. 下单成功后打印日志
       fileLogger.info(`[订单]${pair}, volume: ${Config.baseTokenAmount}, price: ${token.orderPrice}, amount: ${token.orderAmount}.`)