UNPKG

bitxplus

Version:

A simple wrapper for the BitX API.

264 lines (227 loc) 6.47 kB
'use strict'; var https = require('https'), path = require('path'), querystring = require('querystring'), config = require(path.join(__dirname, '..', 'package')), url = require('url'), util = require('util'); var basePath = '/api/1/', extend = util._extend; function BitX(keyId, keySecret, options) { if (!(this instanceof BitX)) { return new BitX(keyId, keySecret, options); } if (typeof keyId === 'string') { this.auth = keyId + ':' + keySecret; } else { options = keyId; } options = options || {}; this.hostname = options.hostname || 'api.mybitx.com'; this.port = options.port || 443; this.ca = options.ca; this.pair = options.pair || 'XBTZAR'; this.headers = { 'Accept': 'application/json', 'Accept-Charset': 'utf-8', 'User-Agent': 'node-bitx v' + config.version }; } BitX.prototype._request = function(method, resourcePath, data, callback) { data = querystring.stringify(data); if (method === 'GET') { if (data) { resourcePath += '?' + data; } } else if (method === 'POST') { this.headers['Content-Type'] = 'application/x-www-form-urlencoded'; this.headers['Content-Length'] = Buffer.byteLength(data); } var options = { headers: this.headers, hostname: this.hostname, path: url.resolve(basePath, resourcePath), port: this.port, auth: this.auth, method: method }; if (this.ca) { options.ca = this.ca; options.agent = new https.Agent(options); } var req = https.request(options); req.on('response', function(res) { var response = ''; res.setEncoding('utf8'); res.on('data', function(data) { response += data; }); res.on('end', function() { if (res.statusCode !== 200) { return callback(new Error('BitX error ' + res.statusCode + ': ' + response)); } try { response = JSON.parse(response); } catch (err) { return callback(err); } if (response.error) { return callback(new Error(response.error)); } return callback(null, response); }); }); req.on('error', function(err) { callback(err); }); if (method === 'POST' && data) { req.write(data); } req.end(); }; BitX.prototype.getTicker = function(options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { pair: this.pair }; this._request('GET', 'ticker', extend(defaults, options), callback); }; BitX.prototype.getAllTickers = function(callback) { this._request('GET', 'tickers', null, callback); }; BitX.prototype.getOrderBook = function(options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { pair: this.pair }; this._request('GET', 'orderbook', extend(defaults, options), callback); }; BitX.prototype.getTrades = function(options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { pair: this.pair }; this._request('GET', 'trades', extend(defaults, options), callback); }; BitX.prototype.getOrderList = function(options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { pair: this.pair }; this._request('GET', 'listorders', extend(defaults, options), callback); }; BitX.prototype.getLimits = function(callback) { console.log('node-bitx warning: BitX.getLimits is deprecated. Please use BitX.getBalance instead.'); this._request('GET', 'BTCZAR/getlimits', null, callback); }; BitX.prototype.stopOrder = function(orderId, callback) { var body = { order_id: orderId }; this._request('POST', 'stoporder', body, callback); }; BitX.prototype.postBuyOrder = function(volume, price, callback) { var body = { type: 'BID', volume: volume, price: price, pair: this.pair }; this._request('POST', 'postorder', body, callback); }; BitX.prototype.postSellOrder = function(volume, price, callback) { var body = { type: 'ASK', volume: volume, price: price, pair: this.pair }; this._request('POST', 'postorder', body, callback); }; BitX.prototype.getOrder = function(id, callback) { this._request('GET', 'orders/' + id, null, callback); }; BitX.prototype.postMarketOrder = function(pair, type, options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { pair: pair, type: type }; this._request('POST', 'marketorder', extend(defaults, options), callback); }; BitX.prototype.send = function(amount, currency, address, options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { amount: amount, currency: currency, address: address }; this._request('POST', 'send', extend(defaults, options), callback); }; BitX.prototype.getBalance = function(asset, callback) { if (typeof asset === 'function') { callback = asset; asset = null; } this._request('GET', 'balance', asset ? {asset: asset} : null, callback); }; BitX.prototype.getFundingAddress = function(asset, options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { asset: asset }; this._request('GET', 'funding_address', extend(defaults, options), callback); }; BitX.prototype.createFundingAddress = function(asset, callback) { this._request('POST', 'funding_address', {asset: asset}, callback); }; BitX.prototype.getTransactions = function(asset, options, callback) { if (typeof options === 'function') { callback = options; options = null; } var defaults = { asset: asset, offset: 0, limit: 10 }; this._request('GET', 'transactions', extend(defaults, options), callback); }; BitX.prototype.getWithdrawals = function(callback) { this._request('GET', 'withdrawals/', null, callback); }; BitX.prototype.getWithdrawal = function(id, callback) { this._request('GET', 'withdrawals/' + id, null, callback); }; BitX.prototype.requestWithdrawal = function(type, amount, callback) { var options = { type: type, amount: amount }; this._request('POST', 'withdrawals/', options, callback); }; BitX.prototype.cancelWithdrawal = function(id, callback) { this._request('DELETE', 'withdrawals/' + id, null, callback); }; module.exports = BitX;