BinanceSpot.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const Tik = require('./kit/BinanceTik')
  2. const HttpKit = require('./kit/HttpKit')
  3. const HTTPRequest = require('request')
  4. const MyKit = require('./kit/MyKit.js')
  5. const isDev = process.env.USER === 'skyfffire'
  6. class BinanceSpot {
  7. constructor (apiKey, secretKey) {
  8. this.baseURL = 'https://api.binance.com'
  9. this.apiKey = apiKey
  10. this.secretKey = secretKey
  11. this.tik = new Tik()
  12. }
  13. async exchangeInfo () {
  14. const url = this.baseURL + '/api/v3/exchangeInfo'
  15. const { data: rst } = await HttpKit.get(url)
  16. return rst
  17. }
  18. async price (symbol='BNBUSDT', side = 'sell') {
  19. const url = `${this.baseURL}/api/v3/depth?symbol=${symbol}&limit=${5}`
  20. const { data: rst } = await HttpKit.get(url)
  21. if (!rst || !rst.bids || !rst.asks || !rst.bids[0] || !rst.asks[0]) {
  22. return 0
  23. }
  24. if (side === 'sell') {
  25. return parseFloat(rst.bids[0][0])
  26. } else {
  27. return parseFloat(rst.asks[0][0])
  28. }
  29. }
  30. async accountInfo () {
  31. const url = this.baseURL + '/api/v3/account'
  32. let timestamp = new Date().getTime()
  33. let data = {
  34. timestamp: timestamp
  35. }
  36. let signature = this.tik.createSignature(this.secretKey, data)
  37. let finalQueryURL = this.tik.toFinalQueryURL(url, data, signature)
  38. let headers = {
  39. 'X-MBX-APIKEY': this.apiKey
  40. }
  41. const { data: rst } = await HttpKit.get(finalQueryURL, headers)
  42. return rst
  43. }
  44. async withdraw (amount= -1,
  45. coin='BUSD',
  46. address='0xCfB3Cb29EeAE464ABD44E35d53e12c555705e824',
  47. network='BSC') {
  48. const url = this.baseURL + '/sapi/v1/capital/withdraw/apply'
  49. let timestamp = new Date().getTime()
  50. let data = {
  51. timestamp: timestamp,
  52. coin: coin,
  53. address: address,
  54. amount: amount,
  55. network: network
  56. }
  57. let signature = this.tik.createSignature(this.secretKey, data)
  58. let finalQueryURL = this.tik.toFinalQueryURL(url, data, signature)
  59. let headers = {
  60. 'X-MBX-APIKEY': this.apiKey
  61. }
  62. const { data: rst } = await HttpKit.post(finalQueryURL, data, headers)
  63. return rst
  64. }
  65. async takeOrder (symbol='BTC_USDT',
  66. price=-1,
  67. side='BUY',
  68. quantity=0.001,
  69. quoteOrderQty=10,
  70. type='MARKET') {
  71. const url = this.baseURL + '/api/v3/order'
  72. let timestamp = new Date().getTime()
  73. let data = {
  74. timestamp: timestamp,
  75. symbol: symbol,
  76. side: side,
  77. type: type,
  78. recvWindow: 60 * 1000
  79. }
  80. // 区分买卖情况应该分别传入什么数量
  81. if (side === 'BUY') data['quoteOrderQty'] = quoteOrderQty
  82. else if (side === 'SELL') data['quantity'] = quantity
  83. // MARKET状态不传入price
  84. if (type !== 'MARKET') data['price'] = price
  85. let signature = this.tik.createSignature(this.secretKey, data)
  86. let finalQueryURL = this.tik.toFinalQueryURL(url, data, signature)
  87. let headers = {
  88. 'X-MBX-APIKEY': this.apiKey
  89. }
  90. const params = {
  91. url: finalQueryURL,
  92. method: 'POST',
  93. headers: headers
  94. }
  95. if (isDev) {
  96. params.proxy = 'http://localhost:9080'
  97. }
  98. let finish = false
  99. let rst = undefined
  100. HTTPRequest(params, function (error, response) {
  101. // 请求成功的处理逻辑
  102. if (!error) {
  103. rst = JSON.parse(response.body)
  104. } else {
  105. rst = error
  106. }
  107. finish = true
  108. })
  109. while (!finish) {
  110. await MyKit.sleep(500)
  111. }
  112. return rst
  113. }
  114. async buy (symbol, price, amount, type='MARKET') {
  115. return await this.takeOrder(
  116. symbol,
  117. price,
  118. 'BUY',
  119. amount,
  120. 0,
  121. type)
  122. }
  123. async sell (symbol, price, amount, type='MARKET') {
  124. return await this.takeOrder(
  125. symbol,
  126. price,
  127. 'SELL',
  128. amount,
  129. 0,
  130. type)
  131. }
  132. }
  133. module.exports = BinanceSpot