Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTC-e V3 Public API update #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
node-btc-e
=====

An unofficial node.js client for the [btc-e trade api](https://btc-e.com/api/documentation) including v2 public api methods(depth, fee, ticker, and trades).

## Installation

node-btc-e is available as `btc-e` on npm.

```
npm install btc-e
```
An unofficial node.js client for the [btc-e trade api](https://btc-e.com/api/documentation)
Updated to work with BTC-e V3 Public API

## Usage

```javascript
var BTCE = require('btc-e'),
var BTCE = require('./btc-e.js'),
btceTrade = new BTCE("YourApiKey", "YourSecret"),
// No need to provide keys if you're only using the public api methods.
btcePublic = new BTCE();
Expand All @@ -40,11 +33,11 @@ When passed as a hash, the following options are supported:
* timeout - The timeout to use when making requests, defaults to 5 seconds
* nonce - A nonce generation function ([Custom nonce generation](#custom-nonce-generation))
* tapi_url - The base url to use when making trade api requests, defaults to `https://btc-e.com/tapi`
* public_url - The base url to use when making public api requests, defaults to `https://btc-e.com/api/2/`
* public_url - The base url to use when making public api requests, defaults to `https://btc-e.com/api/3/`
* strict_ssl - `true` by default, but can be set to `false` if desired, such as if btc-e has problems with their SSL certificate again.

```javascript
var BTCE = require('btc-e'),
var BTCE = require('./btc-e'),
HttpsAgent = require('agentkeepalive').HttpsAgent,
btceTrade = new BTCE("YourApiKey", "YourSecret", {
agent: new HttpsAgent()
Expand All @@ -58,7 +51,7 @@ By default the module generates a nonce based on the current timestamp in second
btc-e expects every nonce given to be greater than the previous one for each api key you have, this presents a big problem when trying to do multiple async calls with the same api key since there is no guarantee that the first api call will be processed before the second one and so on. Chaining calls synchronously(take a look at promises with [q.js](https://github.com/kriskowal/q) for help with that) or using multiple clients, each with their own API key are the only way around that problem.

```javascript
var BTCE = require('btc-e'),
var BTCE = require('./btc-e'),
fs = require('fs'),
currentNonce = fs.existsSync("nonce.json") ? JSON.parse(fs.readFileSync("nonce.json")) : 0,
// Provide a nonce generation function as the third parameter if desired.
Expand All @@ -81,9 +74,8 @@ btce.getInfo(function(err, info) {
});
```

## Reference

A method-by-method [reference](https://github.com/pskupinski/node-btc-e/wiki/API-Reference) is available on the wiki.
## Credits
All credits goes to [pskupinski](https://github.com/pskupinski/node-btc-e), as initial creator.

## License

Expand Down
35 changes: 23 additions & 12 deletions btc-e.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var request = require('request'),

var BTCE = function(apiKey, secret, options) {
this.url = 'https://btc-e.com/tapi';
this.publicApiUrl = 'https://btc-e.com/api/2/';
this.publicApiUrl = 'https://btc-e.com/api/3/';
this.timeout = 5000;
this.apiKey = apiKey;
this.secret = secret;
Expand Down Expand Up @@ -98,10 +98,20 @@ BTCE.prototype.makeRequest = function(method, params, callback) {
}, callback);
};

BTCE.prototype.makePublicApiRequest = function(pair, method, callback) {
this._sendRequest({
url: this.publicApiUrl + pair + '/' + method
BTCE.prototype.makePublicApiRequest = function(pair, limit, method, callback) {

var url = this.publicApiUrl + method;
if (pair) {
url += '/' + pair;
}
if (limit) {
url += "?limit=" + limit;
}

this._sendRequest({
url: url
}, callback);

};

BTCE.prototype.getInfo = function(callback) {
Expand Down Expand Up @@ -152,20 +162,21 @@ BTCE.prototype.cancelOrder = function(paramsOrOrderId, callback) {
this.makeRequest('CancelOrder', input, callback);
};

BTCE.prototype.ticker = function(pair, callback) {
this.makePublicApiRequest(pair, 'ticker', callback);
BTCE.prototype.info = function ( callback) {
this.makePublicApiRequest(false, false, 'info', callback);
};

BTCE.prototype.trades = function(pair, callback) {
this.makePublicApiRequest(pair, 'trades', callback);
BTCE.prototype.ticker = function(pair, callback) {
this.makePublicApiRequest(pair, false, 'ticker', callback);
};

BTCE.prototype.depth = function(pair, callback) {
this.makePublicApiRequest(pair, 'depth', callback);
BTCE.prototype.trades = function(pair, limit, callback) {
this.makePublicApiRequest(pair, limit, 'trades', callback);
};

BTCE.prototype.fee = function(pair, callback) {
this.makePublicApiRequest(pair, 'fee', callback);
BTCE.prototype.depth = function(pair, limit, callback) {
this.makePublicApiRequest(pair, limit, 'depth', callback);
};


module.exports = BTCE;
26 changes: 26 additions & 0 deletions test-public-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var BTCE = require('./btc-e.js'),
btcePublic = new BTCE();


btcePublic.info(function(err, data) {
console.log("Test Info Method");
console.log(err, data);
});


btcePublic.ticker("btc_usd", function(err, data) {
console.log("Test Ticker Method");
console.log(err, data);
});


btcePublic.depth("btc_usd", 20, function(err, data) {
console.log("Test Depth Method");
console.log(err, data);
});


btcePublic.trades("btc_usd", 20, function(err, data) {
console.log("Test Trades Method");
console.log(err, data);
});