Browse Source

可批量初始化信息

龚成明 2 năm trước cách đây
mục cha
commit
1b4d5993bb

+ 9 - 10
src/libs/binance/binance-kit.js

@@ -38,19 +38,18 @@ class BinanceTik {
     return `${buffer}]`
   }
 
-  static parseLotSizeFilterMap(exchangeInfoSymbols) {
-    if (exchangeInfoSymbols === undefined) throw new Error('exchange info symbols is undefined!')
+  static parseLotSizeFilterMap(pairInfoList) {
+    if (pairInfoList === undefined) throw new Error('exchange info symbols is undefined!')
 
-    return exchangeInfoSymbols.map(function (exchangeInfoSymbol) {
-      const lotSizeFilter = exchangeInfoSymbol.filters.filter(function (filterObj) {
-        return filterObj.filterType === 'LOT_SIZE'
-      })[0]
+    const filterMap = {}
 
-      return {
-        symbol: exchangeInfoSymbol.symbol,
-        lotSizeDecimals: Math.log10(1 / parseFloat(lotSizeFilter.stepSize))
-      }
+    pairInfoList.forEach(function (pairInfo) {
+      const lotSizeFilter = pairInfo.filters.filter(filterObj => filterObj.filterType === 'LOT_SIZE')[0]
+
+      filterMap[pairInfo.symbol] = Math.log10(1 / parseFloat(lotSizeFilter.stepSize))
     })
+
+    return filterMap
   }
 }
 

+ 28 - 4
src/libs/web3/simple-ierc20.js

@@ -1,14 +1,34 @@
 const SimpleWeb3 = require('./simple-web3')
 const Config = require('../../config/config')
+const BinanceSpot = require('../binance/binance-spot')
+const BinanceKit = require('../binance/binance-kit')
 
 class IERC20 {}
 
-IERC20.batchInit = function (ierc20TokenAddressList) {
+IERC20.batchInit = async function (ierc20TokenAddressList) {
   IERC20.contractMap = {}
+  IERC20.tokenMap = {}
 
-  ierc20TokenAddressList.forEach(function (tokenHash) {
-    IERC20.contractMap[tokenHash] = new SimpleWeb3.web3.eth.Contract(Config.BASE_ABI, tokenHash)
-  })
+  const pairs = Object.values(Config.tokenMap).map(coin => `${coin}${Config.baseToken.symbol}` )
+  const exchangeInfo = await BinanceSpot.exchangeInfo(pairs)
+  const lotSizeFilterMap = BinanceKit.parseLotSizeFilterMap(exchangeInfo.symbols)
+
+  for (const tokenHash of ierc20TokenAddressList) {
+    await IERC20.initTokenByHash(tokenHash, lotSizeFilterMap)
+  }
+}
+
+IERC20.initTokenByHash = async function (tokenHash, lotSizeFilterMap) {
+  // 合约初始化
+  IERC20.contractMap[tokenHash] = new SimpleWeb3.web3.eth.Contract(Config.BASE_ABI, tokenHash)
+
+  // 代币初始化
+  const tokenDecimals = await IERC20.getDecimals(tokenHash)
+  const exchangeSymbol = Config.tokenMap[tokenHash]
+  const exchangePair = `${exchangeSymbol}${Config.baseToken.symbol}`
+  const lotSize = lotSizeFilterMap[exchangePair]
+
+  IERC20.tokenMap[tokenHash] = { decimals: tokenDecimals, symbol: exchangeSymbol, pair: exchangePair, lotSize: lotSize }
 }
 
 IERC20.getTokenName = async function (tokenHash) {
@@ -19,4 +39,8 @@ IERC20.getTokenSymbol = async function (tokenHash) {
   return await IERC20.contractMap[tokenHash].methods.symbol().call()
 }
 
+IERC20.getDecimals = async function (tokenHash) {
+  return await IERC20.contractMap[tokenHash].methods.decimals().call()
+}
+
 module.exports = IERC20

+ 12 - 5
src/libs/web3/test/ierc20-test.js

@@ -3,12 +3,19 @@ const IERC20 = require('../simple-ierc20')
 const Config = require('../../../config/config')
 
 async function main() {
-  IERC20.batchInit(Object.keys(Config.tokenMap))
+  await IERC20.batchInit(Object.keys(Config.tokenMap))
+  console.log(IERC20)
 
-  for (const tokenHash of Object.keys(Config.tokenMap)) {
-    logger.info(await IERC20.getTokenName(tokenHash), await IERC20.getTokenSymbol(tokenHash), Config.tokenMap[tokenHash])
-    logger.info('')
-  }
+  // for (const tokenHash of Object.keys(Config.tokenMap)) {
+  //   const tokenName = await IERC20.getTokenName(tokenHash)
+  //   const tokenSymbol = await IERC20.getTokenSymbol(tokenHash)
+  //   const tokenDecimals = await IERC20.getDecimals(tokenHash)
+  //   const exchangeSymbol = Config.tokenMap[tokenHash]
+  //
+  //
+  //   logger.info(`${tokenName}, ${tokenSymbol}, ${tokenDecimals}, ${exchangeSymbol}`)
+  //   logger.info('')
+  // }
 }
 
 main()