Skip to content

Instantly share code, notes, and snippets.

@master5o1

master5o1/BitNZ.js

Last active Aug 29, 2015
Embed
What would you like to do?
var crypto = require('crypto');
var request = require('request');
module.exports = function BitNZ(username, apiKey, apiSecret) {
var self = this;
var hostname = 'https://bitnz.com';
var api = '/api/0';
var noop = function() {};
var sign = function(obj){
obj = obj || {};
obj.nonce = new Date().getTime() + '' + new Date().getMilliseconds();
obj.signature = crypto.createHmac('sha256', apiSecret)
.update(obj.nonce + username + apiKey)
.digest('hex')
.toUpperCase();
obj.key = apiKey;
return obj;
};
self.sign = sign;
var post = function(action, parameters, callback) {
callback = callback || noop;
console.log('URL', hostname + api + action, parameters);
request.post(hostname + api + action, {
form: parameters,
headers: {
'User-Agent': 'Mozilla/4.0 (compatible; bitNZ node.js client by master5o1)'
}
}, callback);
}
var get = function(action, parameters, callback) {
callback = callback || noop;
var params = [];
Object.keys(parameters).forEach(function(key){
this.push(key + '=' + parameters[key]);
}, params);
request.get(hostname + api + action + '?' + params.join('&'), callback);
};
// Open/Public API Calls:
self.getTicker = function(callback){
return get('/ticker', {}, callback);
};
self.getTrades = function(since, date, callback){
return get('/trades', {
since: since,
'since_date': date
}, callback);
};
self.getOrderBook = function(parmeters, callback){
return get('/orderbook', parameters, callback);
};
// Private API Calls:
self.getBalance = function(callback) {
return post('/private/balance', sign({}), callback);
};
self.openBids = function(callback) {
return post('/private/orders/buy/open', sign({}), callback);
};
self.openAsks = function(callback) {
return post('/private/orders/sell/open', sign({}), callback);
};
self.closedBids = function(skip, limit, callback) {
return post('/private/orders/buy/close', sign({
offset: skip,
limit: limit
}), callback);
};
self.closedAsks = function(skip, limit, callback) {
return post('/private/orders/sell/close', sign({
offset: skip,
limit: limit
}), callback);
};
self.cancelBid = function(id, callback) {
return post('/private/orders/buy/cancel', sign({
id: id
}), callback);
};
self.cancelAsk = function(id, callback) {
return post('/private/orders/sell/cancel', sign({
id: id
}), callback);
};
self.createBid = function(amount, price, callback) {
return post('/private/orders/buy/create', sign({
amount: amount,
price: price
}), callback);
};
self.createAsk = function(amount, price, callback) {
return post('/private/orders/sell/create', sign({
amount: amount,
price: price
}), callback);
};
self.depositAddress = function(callback) {
return post('/private/btc/address', sign({}), callback);
};
self.withdraw = function(address, amount, callback) {
return post('/private/btc/withdraw', sign({
address: address,
amount: amount
}), callback);
};
};
var BitNZ = require('./BitNZ.js');
var username = "username"
, apiKey = "key"
, apiSecret = "sekrit";
var bitnz = new BitNZ(username, apiKey, apiSecret);
bitnz.getBalance(function(error, response, body){
console.log(response.statusCode);
if (!error && response.statusCode === 200) {
console.log(body);
}
});
bitnz.getTicker(function(error, response, body){
console.log(response.statusCode);
if (!error && response.statusCode === 200) {
console.log(body);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment