| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- const Tik = require('./kit/BinanceTik')
- const HttpKit = require('./kit/HttpKit')
- const HTTPRequest = require('request')
- const MyKit = require('./kit/MyKit.js')
- const isDev = process.env.USER === 'skyfffire'
- class BinanceSpot {
- constructor (apiKey, secretKey) {
- this.baseURL = 'https://api.binance.com'
- this.apiKey = apiKey
- this.secretKey = secretKey
- this.tik = new Tik()
- }
- async exchangeInfo () {
- const url = this.baseURL + '/api/v3/exchangeInfo'
- const { data: rst } = await HttpKit.get(url)
- return rst
- }
- async price (symbol='BNBUSDT', side = 'sell') {
- const url = `${this.baseURL}/api/v3/depth?symbol=${symbol}&limit=${5}`
- const { data: rst } = await HttpKit.get(url)
- if (!rst || !rst.bids || !rst.asks || !rst.bids[0] || !rst.asks[0]) {
- return 0
- }
- if (side === 'sell') {
- return parseFloat(rst.bids[0][0])
- } else {
- return parseFloat(rst.asks[0][0])
- }
- }
- async accountInfo () {
- const url = this.baseURL + '/api/v3/account'
- let timestamp = new Date().getTime()
- let data = {
- timestamp: timestamp
- }
- let signature = this.tik.createSignature(this.secretKey, data)
- let finalQueryURL = this.tik.toFinalQueryURL(url, data, signature)
- let headers = {
- 'X-MBX-APIKEY': this.apiKey
- }
- const { data: rst } = await HttpKit.get(finalQueryURL, headers)
- return rst
- }
- async withdraw (amount= -1,
- coin='BUSD',
- address='0xCfB3Cb29EeAE464ABD44E35d53e12c555705e824',
- network='BSC') {
- const url = this.baseURL + '/sapi/v1/capital/withdraw/apply'
- let timestamp = new Date().getTime()
- let data = {
- timestamp: timestamp,
- coin: coin,
- address: address,
- amount: amount,
- network: network
- }
- let signature = this.tik.createSignature(this.secretKey, data)
- let finalQueryURL = this.tik.toFinalQueryURL(url, data, signature)
- let headers = {
- 'X-MBX-APIKEY': this.apiKey
- }
- const { data: rst } = await HttpKit.post(finalQueryURL, data, headers)
- return rst
- }
- async takeOrder (symbol='BTC_USDT',
- price=-1,
- side='BUY',
- quantity=0.001,
- quoteOrderQty=10,
- type='MARKET') {
- const url = this.baseURL + '/api/v3/order'
- let timestamp = new Date().getTime()
- let data = {
- timestamp: timestamp,
- symbol: symbol,
- side: side,
- type: type,
- recvWindow: 60 * 1000
- }
- // 区分买卖情况应该分别传入什么数量
- if (side === 'BUY') data['quoteOrderQty'] = quoteOrderQty
- else if (side === 'SELL') data['quantity'] = quantity
- // MARKET状态不传入price
- if (type !== 'MARKET') data['price'] = price
- let signature = this.tik.createSignature(this.secretKey, data)
- let finalQueryURL = this.tik.toFinalQueryURL(url, data, signature)
- let headers = {
- 'X-MBX-APIKEY': this.apiKey
- }
- const params = {
- url: finalQueryURL,
- method: 'POST',
- headers: headers
- }
- if (isDev) {
- params.proxy = 'http://localhost:9080'
- }
- let finish = false
- let rst = undefined
- HTTPRequest(params, function (error, response) {
- // 请求成功的处理逻辑
- if (!error) {
- rst = JSON.parse(response.body)
- } else {
- rst = error
- }
- finish = true
- })
- while (!finish) {
- await MyKit.sleep(500)
- }
- return rst
- }
- async buy (symbol, price, amount, type='MARKET') {
- return await this.takeOrder(
- symbol,
- price,
- 'BUY',
- amount,
- 0,
- type)
- }
- async sell (symbol, price, amount, type='MARKET') {
- return await this.takeOrder(
- symbol,
- price,
- 'SELL',
- amount,
- 0,
- type)
- }
- }
- module.exports = BinanceSpot
|