Преглед изворни кода

交易测试完毕,准备对接

龚成明 пре 2 година
родитељ
комит
c308015926
2 измењених фајлова са 27 додато и 7 уклоњено
  1. 6 6
      libs/binance/binance-spot.js
  2. 21 1
      libs/binance/test/binance-spot-test.js

+ 6 - 6
libs/binance/binance-spot.js

@@ -103,8 +103,8 @@ class BinanceSpot {
     }
 
     // 区分买卖情况应该分别传入什么数量
-    if (side === 'BUY') data['quoteOrderQty'] = quoteOrderQty
-    else if (side === 'SELL') data['quantity'] = quantity
+    if (side === BinanceSpot.TRADE_SIDE.BUY) data['quoteOrderQty'] = quoteOrderQty
+    else if (side === BinanceSpot.TRADE_SIDE.SELL) data['quantity'] = quantity
 
     // MARKET状态不传入price
     if (type !== 'MARKET') data['price'] = price
@@ -143,22 +143,22 @@ class BinanceSpot {
     return rst
   }
 
-  async buy(symbol, price, amount, type=BinanceSpot.TRADE_TYPE.MARKET) {
+  async buy(symbol, price, quoteOrderQty, type=BinanceSpot.TRADE_TYPE.MARKET) {
     return await this.takeOrder(
       symbol,
       price,
       BinanceSpot.TRADE_SIDE.BUY,
-      amount,
       0,
+      quoteOrderQty,
       type)
   }
 
-  async sell(symbol, price, amount, type=BinanceSpot.TRADE_TYPE.MARKET) {
+  async sell(symbol, price, quantity, type=BinanceSpot.TRADE_TYPE.MARKET) {
     return await this.takeOrder(
       symbol,
       price,
       BinanceSpot.TRADE_SIDE.SELL,
-      amount,
+      quantity,
       0,
       type)
   }

+ 21 - 1
libs/binance/test/binance-spot-test.js

@@ -30,6 +30,25 @@ async function accountInfoTest(context) {
   logger.info(accountAssetMap)
 }
 
+async function orderTest(context) {
+  logger.info('order info test:')
+
+  const binanceSpot = context.binanceSpot
+
+  const orderRst = await binanceSpot.buy('DOGEBUSD', -1, 15)
+
+  if (orderRst.executedQty) {
+    const cummulativeQuoteQty = orderRst.cummulativeQuoteQty
+    const executedQty = orderRst.executedQty
+    const price = cummulativeQuoteQty / executedQty
+
+    logger.info(`消耗${Config.baseIerc20Token.symbol} ${cummulativeQuoteQty}, 买入${executedQty}, 均价${price}.`)
+    logger.info(await binanceSpot.sell('DOGEBUSD', -1, executedQty))
+  } else {
+    logger.error(orderRst)
+  }
+}
+
 async function main() {
   const context = {}
 
@@ -37,7 +56,8 @@ async function main() {
 
   // await realPriceTest()
   // await exchangeInfoTest()
-  await accountInfoTest(context)
+  // await accountInfoTest(context)
+  // await orderTest(context)
 }
 
 main()