bl3p
Version:
BL3P API
296 lines (250 loc) • 8.07 kB
JavaScript
/**
* NodeJS BL3P API package
* @author Roy van Zanten <roy@bitonic.nl>
* Please check out the BL3P-API documentation on Github: <https://github.com/BitonicNL/bl3p-api>
*/
//Load required modules
var request = require('request');
var querystring = require('querystring');
var crypto = require('crypto');
var websocket_client = require('ws');
//Define global variables
var auth_rest_url = 'https://api.bl3p.eu';
var public_websocket_url = 'wss://api.bl3p.eu/1/BTCEUR';
var version = '1';
var public_api_key;
var private_api_key;
var bl3p_auth;
var bl3p_public;
//Export Bl3p module
module.exports = {
Bl3pAuth : Bl3pAuth,
orderbook : orderbook,
trades : trades,
};
/**
* Bl3pAuth constructor
* @method Bl3p
* @param {string} public_key [private key of api-key]
* @param {string} private_key [public key of api-key]
*/
function Bl3pAuth(public_key, private_key){
if(!public_key || !private_key){
return false;
}else{
public_api_key = public_key;
private_api_key = private_key;
}
bl3p_auth = this;
}
/**
* Bl3p API caller
* @method bl3p_call
* @param {string} call_path [path to call]
* @param {object} params [parameters to pass]
* @param {Function} callback [function to call when the call is finished]
*/
Bl3pAuth.prototype.api_call = function(call_path, params, callback){
var errors = [];
if(typeof callback != 'function'){
errors.push('No callback defined.');
}
if(typeof call_path == 'undefined'){
errors.push('No call_path defined.');
}
if(errors.length == 0){
var post_data = querystring.stringify(params);
var body = call_path + '\0' + post_data;
var decoded_priv = new Buffer(private_api_key, 'base64');
var hmac_digest = crypto.createHmac('sha512', decoded_priv).update(body).digest('binary');
var signed_api_key = new Buffer(hmac_digest, 'binary').toString('base64');
var options = {
host: auth_rest_url,
port: 443,
method: 'POST',
headers : {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data),
'Rest-Key': public_api_key,
'Rest-Sign': signed_api_key
},
form : params,
url : auth_rest_url + '/' + version + '/' + call_path
};
request.post(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(false, body);
}else{
callback(error, false);
}
});
}else{
console.log('Errors: ' + errors);
}
}
/**
* Create an order
* @method add_order
* @param {int} amount [amount to order]
* @param {string} type [description]
* @param {int} price [limit price]
* @param {string} fee_currency [currency of the tradefee]
* @param {int} amount_funds [max spend amount (eur)]
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.add_order = function(amount, type, price, fee_currency, amount_funds, callback){
var params = {amount_int : amount, type : type, fee_currency : fee_currency};
if(amount_funds){
params.amount_funds_int = amount_funds;
}
if(price){
params.price_int = price;
}
bl3p_auth.api_call('BTCEUR/money/order/add', params, callback);
}
/**
* [Get information of an specific order]
* @method order_info
* @param {int} order_id [id of the order you want information about]
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.order_info = function(order_id, callback){
var params = {order_id : order_id};
bl3p_auth.api_call('BTCEUR/money/order/result', params, callback);
}
/**
* Get all the active orders
* @method active_orders
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.active_orders = function(callback){
bl3p_auth.api_call('BTCEUR/money/orders', false, callback);
}
/**
* Cancel an order
* @method cancel_order
* @param {int} order_id [id of the order that needs to be cancelled]
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.cancel_order = function(order_id, callback){
var params = {order_id : order_id};
bl3p_auth.api_call('BTCEUR/money/order/cancel', params, callback);
}
/**
* Get the full orderbook depth
* @method active_orders
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.full_depth = function(callback){
bl3p_auth.api_call('BTCEUR/money/depth/full', false, callback);
}
/**
* Get the full orderbook depth
* @method active_orders
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.new_deposit_address = function(callback){
bl3p_auth.api_call('BTCEUR/money/new_deposit_address', false, callback);
}
/**
* Get the last deposit address
* @method last_deposit_address
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.last_deposit_address = function(callback){
bl3p_auth.api_call('BTCEUR/money/deposit_address', false, callback);
}
/**
* Get the last 1000 trades
* @method last_1000_trades
* @param {int} trade_id [returns the 1000 trades that are made after specified trade_id]
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.last_1000_trades = function(trade_id, callback){
if(trade_id){
var params = {trade_id : trade_id};
bl3p_auth.api_call('BTCEUR/money/trades/fetch', params, callback);
}else{
bl3p_auth.api_call('BTCEUR/money/trades/fetch', false, callback);
}
}
/**
* Get the transaction history of your account
* @method wallet_history
* @param {string} currency [returns transaction history of specified currency]
* @param {int} page [the result is split into groups, with this param you can define which page you want to return]
* @param {timestamp} date_from [show records that arent newer then specified date]
* @param {timestamp} date_to [show records that arent older then specified date]
* @param {string} type [type of record]
* @param {int} recs_per_page [amount of records that are returned for each page, default(50)]
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.wallet_history = function(currency, page, date_from, date_to, type, recs_per_page, callback){
var params = {
currency : currency,
};
if(page){
params.page = page;
}
if(date_from){
params.date_from = date_from;
}
if(date_to){
params.date_to = date_to;
}
if(type){
params.type = type;
}
if(recs_per_page){
params.recs_per_page = recs_per_page;
}
bl3p_auth.api_call('GENMKT/money/wallet/history', params, callback);
}
/**
* Get your balance and information about your account.
* @method account_info
* @param {Function} [callback function]
*/
Bl3pAuth.prototype.account_info = function(callback){
bl3p_auth.api_call('GENMKT/money/info', false, callback);
}
/**
* Initiate a withdraw
* @method withdraw
* @param {string} type [EUR or BTC]
* @param {int} amount [amount to withdraw]
* @param {string} account [specify the account_id if it regards an EUR withdraw or an bitcoin address if the withdraw regards a BTC withdraw]
* @param {Function} callback [callback function]
*/
Bl3pAuth.prototype.withdraw = function(type, amount, account, callback){
var params = {type : type, amount_int : amount};
if(type == 'EUR'){
params.account_id = account;
}else if(type == 'BTC'){
params.address = account;
}
bl3p_auth.api_call('BTCEUR/money/withdraw', params, callback);
}
function websocket_connect(call_path, callback){
bl3p_public = new websocket_client(call_path, {port:443});
if(bl3p_public){
bl3p_public.on('open', function(connection) {
console.log('Connected to: ' + call_path);
});
bl3p_public.on('error', function(error){
callback(error, false);
});
bl3p_public.on('message', function(message) {
callback(false, message);
});
return true;
}else{
return false;
}
}
function trades(callback){
return websocket_connect(public_websocket_url + '/trades', callback);
}
function orderbook(callback){
return websocket_connect(public_websocket_url + '/orderbook', callback);
}