binance-spot-test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const BinanceSpot = require('../binance-spot')
  2. const BinanceKit = require('../binance-kit')
  3. const logger = require('../../../kit/logger-kit').getLogger('binance-spot-test')
  4. const Config = require('../../../config/config')
  5. const PrivateConfig = require('../../../PrivateConfig')
  6. async function realPriceTest() {
  7. logger.info('real price test:')
  8. logger.info(await BinanceSpot.realPrice())
  9. logger.info('')
  10. }
  11. async function exchangeInfoTest() {
  12. logger.info('exchange info test:')
  13. const pairs = Object.values(Config.tokenMapping).map(coin => `${coin}${Config.baseIerc20Token.symbol}` )
  14. const exchangeInfo = await BinanceSpot.exchangeInfo(pairs)
  15. logger.info(BinanceKit.parsePriceTickFilterMap(exchangeInfo.symbols))
  16. logger.info('')
  17. }
  18. async function accountInfoTest(context) {
  19. logger.info('account info test:')
  20. const binanceSpot = context.binanceSpot
  21. const accountInfoRst = await binanceSpot.accountInfo()
  22. const accountAssetMap = BinanceKit.parseBalancesToAccountAssetMap(accountInfoRst.balances)
  23. logger.info(accountAssetMap)
  24. }
  25. async function orderTest(context) {
  26. logger.info('order info test:')
  27. const binanceSpot = context.binanceSpot
  28. const orderRst = await binanceSpot.buy('HOOKBUSD', -1, Config.baseTokenAmount)
  29. logger.info(orderRst)
  30. if (orderRst.executedQty) {
  31. const cummulativeQuoteQty = orderRst.cummulativeQuoteQty
  32. let executedQty = 0
  33. for (const fill of orderRst.fills) {
  34. executedQty += parseFloat(fill.qty)
  35. // 扣除支付的手续费
  36. if (fill.commissionAsset === 'HOOK') {
  37. executedQty -= parseFloat(fill.commission)
  38. }
  39. }
  40. const price = cummulativeQuoteQty / executedQty
  41. logger.info(`消耗${Config.baseIerc20Token.symbol} ${cummulativeQuoteQty}, 买入${executedQty}, 均价${price}.`)
  42. logger.info(await binanceSpot.sell('HOOKBUSD', -1, executedQty))
  43. } else {
  44. logger.error(orderRst)
  45. }
  46. }
  47. async function main() {
  48. const context = {}
  49. context.binanceSpot = new BinanceSpot(PrivateConfig.binanceAPIKey, PrivateConfig.binanceSecretKey)
  50. // await realPriceTest()
  51. // await exchangeInfoTest()
  52. // await accountInfoTest(context)
  53. await orderTest(context)
  54. }
  55. main()