From 1529958d8a8a4ab0aec4afc659a585d3d3ecb59e Mon Sep 17 00:00:00 2001 From: Dan Armstrong Date: Mon, 8 Sep 2014 15:02:55 -0600 Subject: [PATCH 1/6] Added support for ghash.io methods. --- cexapi.js | 281 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 144 insertions(+), 137 deletions(-) diff --git a/cexapi.js b/cexapi.js index efa2cee..c204639 100644 --- a/cexapi.js +++ b/cexapi.js @@ -5,141 +5,148 @@ var _nonce = ''; var _https = require('https'); var _crypto = require('crypto'); - function create(username, api_key, api_secret) //Set variable - { - _username = username; - _api_key = api_key; - _api_secret = api_secret; - } - - function __signature() //Generate signature - { - string = _nonce + _username + _api_key - hmac = _crypto.createHmac('sha256', _api_secret); - hmac.setEncoding('hex'); - hmac.write(string); - hmac.end(); - var temp = hmac.read(); - return temp.toUpperCase() - } - - function __nonce() //Get timestamp as nonce - { - _nonce = Math.round(new Date().getTime() / 1000); - } - - function __post(url, param, callback) //Send post request via requstify - { - var post_data = ''; - var body = ''; - for (var key in param) - { - post_data += key + '=' + param[key] + '&' - } - if (post_data.length > 2) - { - post_data = post_data.substring(0, post_data.length-1 ); - } - else - { - post_data =''; - } - var request = _https.request({ - hostname:'cex.io', - path : url, - port : 443, - method : 'POST', - headers: {'User-Agent': 'cex.io_node.js_api', - 'Content-Length': post_data.length, - 'Content-Type' : 'application/x-www-form-urlencoded'} - }, function(res){ - res.setEncoding('utf8'); - res.on('data', function(chunk){ - body +=chunk; - }); - res.on('end', function() { - callback(JSON.parse(body)); - }); - });//Return answer as object in callback - request.write(post_data); - request.end(); - return body; - } - - function api_call(method, param, is_private, couple, callback) //Api call - { - var url = '/api/' + method +'/' //generate uri - if (couple == undefined) {var couple = ''} else { - if (couple.length > 5) - { - url = url + couple +'/' } - } - if (param == undefined) {var param = new Object()} //generate param in needed - if (is_private == undefined) {var is_private = 0} - else - { - if (is_private == 1) - { - __nonce(); - param.key = _api_key; - param.signature = __signature(); - param.nonce = _nonce; - } - } - __post(url,param, callback) - } - - function ticker(couple,callback) - { - api_call('ticker', new Object(), 0, couple, callback) - } - - function order_book(couple,callback) - { - api_call('order_book', new Object(), 0, couple, callback) - } +function create(username, api_key, api_secret) //Set variable +{ + _username = username; + _api_key = api_key; + _api_secret = api_secret; +} + +function __signature() //Generate signature +{ + string = _nonce + _username + _api_key + hmac = _crypto.createHmac('sha256', _api_secret); + hmac.setEncoding('hex'); + hmac.write(string); + hmac.end(); + var temp = hmac.read(); + return temp.toUpperCase() +} + +function __nonce() //Get timestamp as nonce +{ + _nonce = Math.round(new Date().getTime() / 1000); +} + +function __post(url, param, callback) //Send post request via requstify +{ - function trade_history(since,couple,callback) - { - param = new Object(); - param.since = since; - api_call('trade_history', param, 0, couple, callback) - } - - function balance(callback) - { - param = new Object(); - api_call('balance', param, 1, '', callback) - } - - function open_orders(couple, callback) - { - api_call('open_orders', new Object(), 1, couple, callback); - } - - function cancel_order(id, callback) - { - param = new Object(); - param.id = id; - api_call('cancel_order', param, 1, '', callback); - } - - function place_order(type, amount, price, couple, callback) - { - param = new Object(); - param.type = type; - param.amount = amount; - param.price = price; - api_call('place_order', param, 1, couple, callback) - } - - - exports.create = create; - exports.api_call = api_call; - exports.ticker = ticker; - exports.order_book = order_book; - exports.trade_history = trade_history; - exports.balance = balance; - exports.open_orders = open_orders; - exports.cancel_order = cancel_order; - exports.place_order = place_order; + console.log(url); + var post_data = ''; + var body = ''; + for (var key in param) { + post_data += key + '=' + param[key] + '&' + } + if (post_data.length > 2) { + post_data = post_data.substring(0, post_data.length - 1); + } + else { + post_data = ''; + } + var request = _https.request({ + hostname: 'cex.io', + path: url, + port: 443, + method: 'POST', + headers: { + 'User-Agent': 'cex.io_node.js_api', + 'Content-Length': post_data.length, + 'Content-Type': 'application/x-www-form-urlencoded' + } + }, function (res) { + res.setEncoding('utf8'); + res.on('data', function (chunk) { + body += chunk; + }); + res.on('end', function () { + console.log(body); + callback(JSON.parse(body)); + }); + });//Return answer as object in callback + request.write(post_data); + request.end(); + return body; +} + +function api_call(method, param, is_private, couple, callback) //Api call +{ + + var url = '/api/' + method + (method.indexOf('ghash.io') > -1 ? '' : '/'); //generate uri + if (couple == undefined) { + var couple = ''; + } else { + if (couple.length > 5) { + url = url + couple + '/'; + } + } + if (param == undefined) { + var param = new Object(); + } //generate param in needed + if (is_private == undefined) { + var is_private = 0; + } + else { + if (is_private == 1) { + if(!_nonce) + __nonce(); + ++_nonce; + param.key = _api_key; + param.signature = __signature(); + param.nonce = _nonce; + } + } + __post(url, param, callback); +} + +function ticker(couple, callback) { + api_call('ticker', {}, 0, couple, callback); +} + +function order_book(couple, callback) { + api_call('order_book', {}, 0, couple, callback); +} + +function trade_history(since, couple, callback) { + api_call('trade_history', {since: since}, 0, couple, callback); +} + +function balance(callback) { + api_call('balance', {}, 1, '', callback); +} + +function open_orders(couple, callback) { + api_call('open_orders', {}, 1, couple, callback); +} + +function cancel_order(id, callback) { + api_call('cancel_order', {id: id}, 1, '', callback); +} + +function place_order(type, amount, price, couple, callback) { + params = { + type: type, + amount: amount, + price: price + }; + api_call('place_order', params, 1, couple, callback); +} + +function hashrate(callback) { + api_call('ghash.io/hashrate', {}, 1, null, callback); +} + +function workers(callback) { + api_call('ghash.io/workers', {}, 1, null, callback); +} + + +exports.create = create; +exports.api_call = api_call; +exports.ticker = ticker; +exports.order_book = order_book; +exports.trade_history = trade_history; +exports.balance = balance; +exports.open_orders = open_orders; +exports.cancel_order = cancel_order; +exports.place_order = place_order; +exports.hashrate = hashrate; From fc2b5ffc3a76426d5c5fb74ce4294b00e80bb16e Mon Sep 17 00:00:00 2001 From: Dan Armstrong Date: Mon, 8 Sep 2014 15:04:05 -0600 Subject: [PATCH 2/6] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a235e8c..d8dbaab 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ api_secret - your API secret code 5. open_orders(couple = 'GHS/BTC') - get open order 6. cancel_order(order_id) - cancel order â„–order_id 7. place_order(ptype = 'buy', amount = 1, price = 1, couple = 'GHS/BTC') - create order +8. hashrate() - get the pool hashrate +9. workers() - get your worker information ``` ####c) Full API documentation: https://cex.io/api From 95457560229c9f189c92c1e5fca727a5538dc08a Mon Sep 17 00:00:00 2001 From: Dan Armstrong Date: Mon, 8 Sep 2014 15:13:54 -0600 Subject: [PATCH 3/6] Export workers --- cexapi.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/cexapi.js b/cexapi.js index c204639..b64bf49 100644 --- a/cexapi.js +++ b/cexapi.js @@ -10,7 +10,7 @@ function create(username, api_key, api_secret) //Set variable _username = username; _api_key = api_key; _api_secret = api_secret; -} +}; function __signature() //Generate signature { @@ -21,12 +21,12 @@ function __signature() //Generate signature hmac.end(); var temp = hmac.read(); return temp.toUpperCase() -} +}; function __nonce() //Get timestamp as nonce { _nonce = Math.round(new Date().getTime() / 1000); -} +}; function __post(url, param, callback) //Send post request via requstify { @@ -66,7 +66,7 @@ function __post(url, param, callback) //Send post request via requstify request.write(post_data); request.end(); return body; -} +}; function api_call(method, param, is_private, couple, callback) //Api call { @@ -96,31 +96,31 @@ function api_call(method, param, is_private, couple, callback) //Api call } } __post(url, param, callback); -} +}; function ticker(couple, callback) { api_call('ticker', {}, 0, couple, callback); -} +}; function order_book(couple, callback) { api_call('order_book', {}, 0, couple, callback); -} +}; function trade_history(since, couple, callback) { api_call('trade_history', {since: since}, 0, couple, callback); -} +}; function balance(callback) { api_call('balance', {}, 1, '', callback); -} +}; function open_orders(couple, callback) { api_call('open_orders', {}, 1, couple, callback); -} +}; function cancel_order(id, callback) { api_call('cancel_order', {id: id}, 1, '', callback); -} +}; function place_order(type, amount, price, couple, callback) { params = { @@ -129,15 +129,15 @@ function place_order(type, amount, price, couple, callback) { price: price }; api_call('place_order', params, 1, couple, callback); -} +}; function hashrate(callback) { api_call('ghash.io/hashrate', {}, 1, null, callback); -} +}; function workers(callback) { api_call('ghash.io/workers', {}, 1, null, callback); -} +}; exports.create = create; @@ -150,3 +150,4 @@ exports.open_orders = open_orders; exports.cancel_order = cancel_order; exports.place_order = place_order; exports.hashrate = hashrate; +exports.workers = workers; From 2f6eb18c8b744a64a6412ee65cacc110a645e8bd Mon Sep 17 00:00:00 2001 From: Dan Armstrong Date: Mon, 8 Sep 2014 15:29:26 -0600 Subject: [PATCH 4/6] Fixed undefined variable assignments. --- cexapi.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cexapi.js b/cexapi.js index b64bf49..375fbe8 100644 --- a/cexapi.js +++ b/cexapi.js @@ -14,8 +14,8 @@ function create(username, api_key, api_secret) //Set variable function __signature() //Generate signature { - string = _nonce + _username + _api_key - hmac = _crypto.createHmac('sha256', _api_secret); + var string = _nonce + _username + _api_key + var hmac = _crypto.createHmac('sha256', _api_secret); hmac.setEncoding('hex'); hmac.write(string); hmac.end(); @@ -140,6 +140,20 @@ function workers(callback) { }; +/*exports = { + create: create, + api_call: api_call, + ticker: ticker, + order_book: order_book, + trade_history: trade_history, + balance: balance, + open_orders: open_orders, + cancel_order: cancel_order, + place_order: place_order, + hashrate: hashrate, + workers: worker +}*/ + exports.create = create; exports.api_call = api_call; exports.ticker = ticker; From 458f4cfed16a2b017aa4c6cb7577da2fad16ae9d Mon Sep 17 00:00:00 2001 From: Dan Armstrong Date: Tue, 9 Sep 2014 10:59:18 -0600 Subject: [PATCH 5/6] Removed console.log statements --- cexapi.js | 50 +++++++++++++------------------------------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/cexapi.js b/cexapi.js index 375fbe8..f55a5e7 100644 --- a/cexapi.js +++ b/cexapi.js @@ -5,15 +5,13 @@ var _nonce = ''; var _https = require('https'); var _crypto = require('crypto'); -function create(username, api_key, api_secret) //Set variable -{ +function create(username, api_key, api_secret) { _username = username; _api_key = api_key; _api_secret = api_secret; }; -function __signature() //Generate signature -{ +function __signature() { var string = _nonce + _username + _api_key var hmac = _crypto.createHmac('sha256', _api_secret); hmac.setEncoding('hex'); @@ -23,15 +21,11 @@ function __signature() //Generate signature return temp.toUpperCase() }; -function __nonce() //Get timestamp as nonce -{ +function __nonce() { _nonce = Math.round(new Date().getTime() / 1000); }; -function __post(url, param, callback) //Send post request via requstify -{ - - console.log(url); +function __post(url, param, callback) { var post_data = ''; var body = ''; for (var key in param) { @@ -59,31 +53,28 @@ function __post(url, param, callback) //Send post request via requstify body += chunk; }); res.on('end', function () { - console.log(body); callback(JSON.parse(body)); }); - });//Return answer as object in callback + }); request.write(post_data); request.end(); return body; }; -function api_call(method, param, is_private, couple, callback) //Api call -{ - +function api_call(method, param, is_private, couple, callback) { var url = '/api/' + method + (method.indexOf('ghash.io') > -1 ? '' : '/'); //generate uri - if (couple == undefined) { - var couple = ''; + if (couple === undefined) { + couple = ''; } else { if (couple.length > 5) { url = url + couple + '/'; } } - if (param == undefined) { - var param = new Object(); + if (param === undefined) { + param = {}; } //generate param in needed - if (is_private == undefined) { - var is_private = 0; + if (is_private === undefined) { + is_private = 0; } else { if (is_private == 1) { @@ -123,7 +114,7 @@ function cancel_order(id, callback) { }; function place_order(type, amount, price, couple, callback) { - params = { + var params = { type: type, amount: amount, price: price @@ -139,21 +130,6 @@ function workers(callback) { api_call('ghash.io/workers', {}, 1, null, callback); }; - -/*exports = { - create: create, - api_call: api_call, - ticker: ticker, - order_book: order_book, - trade_history: trade_history, - balance: balance, - open_orders: open_orders, - cancel_order: cancel_order, - place_order: place_order, - hashrate: hashrate, - workers: worker -}*/ - exports.create = create; exports.api_call = api_call; exports.ticker = ticker; From 9eb241a34e7128559c9d630d7561cd88f4e941c5 Mon Sep 17 00:00:00 2001 From: Dan Armstrong Date: Wed, 10 Sep 2014 22:34:00 -0600 Subject: [PATCH 6/6] Fixed null couple bug --- cexapi.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cexapi.js b/cexapi.js index f55a5e7..983b3f9 100644 --- a/cexapi.js +++ b/cexapi.js @@ -66,7 +66,7 @@ function api_call(method, param, is_private, couple, callback) { if (couple === undefined) { couple = ''; } else { - if (couple.length > 5) { + if (couple != undefined && couple.length > 5) { url = url + couple + '/'; } } @@ -102,7 +102,7 @@ function trade_history(since, couple, callback) { }; function balance(callback) { - api_call('balance', {}, 1, '', callback); + api_call('balance', {}, 1, null, callback); }; function open_orders(couple, callback) { @@ -110,7 +110,7 @@ function open_orders(couple, callback) { }; function cancel_order(id, callback) { - api_call('cancel_order', {id: id}, 1, '', callback); + api_call('cancel_order', {id: id}, 1, null, callback); }; function place_order(type, amount, price, couple, callback) {