const BinanceKit = require('./binance-kit') const HttpKit = require('../../kit/http-kit') const TimeKit = require("../../kit/time-kit"); const HTTPRequest = require('request') const isDev = process.env.USER === 'skyfffire' const proxy = 'http://localhost:9080' class BinanceSpot { static BASE_URL = 'https://api.binance.com' static TRADE_TYPE = { MARKET: 'MARKET' } static TRADE_SIDE = { BUY: 'buy', SELL: 'sell' } static PRICE = { MARKET: -1 } static NETWORK = { BSC: 'BSC' } static BASE_TOKEN = { BUSD: 'BUSD' } constructor(apiKey, secretKey) { this.apiKey = apiKey this.secretKey = secretKey } static async realPrice(symbol='BNBUSDT', side=BinanceSpot.TRADE_SIDE.BUY) { const url = `${BinanceSpot.BASE_URL}/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]) } } static async exchangeInfo(symbols=['BNBBUSD', 'BTCBUSD']) { const url = `${BinanceSpot.BASE_URL}/api/v3/exchangeInfo?symbols=${BinanceKit.buildExchangeInfoSymbols(symbols)}` const { data: rst } = await HttpKit.get(url) return rst } async accountInfo() { const url = `${BinanceSpot.BASE_URL}/api/v3/account` let timestamp = new Date().getTime() let data = { timestamp: timestamp } let signature = BinanceKit.createSignature(this.secretKey, data) let finalQueryURL = BinanceKit.toFinalQueryURL(url, data, signature) let headers = { 'X-MBX-APIKEY': this.apiKey } const { data: rst } = await HttpKit.get(finalQueryURL, headers) return rst } async withdraw(amount, coin=BinanceSpot.BASE_TOKEN.BUSD, address='0xCfB3Cb29EeAE464ABD44E35d53e12c555705e824', network=BinanceSpot.NETWORK.BSC) { const url = `${BinanceSpot.BASE_URL}/sapi/v1/capital/withdraw/apply` let timestamp = new Date().getTime() let data = { timestamp: timestamp, coin: coin, address: address, amount: amount, network: network } let signature = BinanceKit.createSignature(this.secretKey, data) let finalQueryURL = BinanceKit.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='BTCUSDT', price, side, quantity, quoteOrderQty, type=BinanceSpot.TRADE_TYPE.MARKET) { const url = `${BinanceSpot.BASE_URL}/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 = BinanceKit.createSignature(this.secretKey, data) let finalQueryURL = BinanceKit.toFinalQueryURL(url, data, signature) let headers = { 'X-MBX-APIKEY': this.apiKey } const params = { url: finalQueryURL, method: 'POST', headers: headers, proxy: isDev ? proxy : undefined } 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 TimeKit.sleep(10) } return rst } async buy(symbol, price, amount, type=BinanceSpot.TRADE_TYPE.MARKET) { return await this.takeOrder( symbol, price, BinanceSpot.TRADE_SIDE.BUY, amount, 0, type) } async sell(symbol, price, amount, type=BinanceSpot.TRADE_TYPE.MARKET) { return await this.takeOrder( symbol, price, BinanceSpot.TRADE_SIDE.SELL, amount, 0, type) } } module.exports = BinanceSpot