Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Commit

Permalink
Simplify OrderbookSync constructor to take auth object
Browse files Browse the repository at this point in the history
...instead of full instance of AuthenticatedClient
  • Loading branch information
rmm5t committed Dec 25, 2017
1 parent 9cc86d1 commit 481193e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
20 changes: 15 additions & 5 deletions lib/orderbook_sync.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
const WebsocketClient = require('./clients/websocket.js');
const AuthenticatedClient = require('./clients/authenticated.js');
const PublicClient = require('./clients/public.js');
const Orderbook = require('./orderbook.js');
const Utils = require('./utilities.js');

// Orderbook syncing
class OrderbookSync extends WebsocketClient {
constructor(
productIDs,
apiURI = 'https://api.gdax.com',
websocketURI = 'wss://ws-feed.gdax.com',
authenticatedClient = null,
auth = null,
{ heartbeat = false } = {}
) {
super(productIDs, websocketURI, authenticatedClient, { heartbeat });
super(productIDs, websocketURI, auth, { heartbeat });
this.apiURI = apiURI;
this.authenticatedClient = authenticatedClient;
this.auth = Utils.checkAuth(auth);

this._queues = {}; // []
this._sequences = {}; // -1
this._public_clients = {};
this.books = {};

if (this.auth.secret) {
this._authenticatedClient = new AuthenticatedClient(
this.auth.key,
this.auth.secret,
this.auth.passphrase
);
}

this.productIDs.forEach(productID => {
this._queues[productID] = [];
this._sequences[productID] = -2;
Expand Down Expand Up @@ -55,8 +65,8 @@ class OrderbookSync extends WebsocketClient {
const bookLevel = 3;
const args = { level: bookLevel };

if (this.authenticatedClient) {
this.authenticatedClient
if (this._authenticatedClient) {
this._authenticatedClient
.getProductOrderBook(args, productID)
.then(onData.bind(this))
.catch(onError.bind(this));
Expand Down
19 changes: 6 additions & 13 deletions tests/orderbook_sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@ suite('OrderbookSync', () => {
});
});

test('passes authentication details to websocket (with AuthenticatedClient)', done => {
test('passes authentication details to websocket', done => {
const server = testserver(++port, () => {
const authClient = new Gdax.AuthenticatedClient(
'suchkey',
'suchsecret',
'muchpassphrase'
);
new Gdax.OrderbookSync(
'BTC-USD',
EXCHANGE_API_URL,
'ws://localhost:' + port,
authClient
{ key: 'suchkey', secret: 'suchsecret', passphrase: 'muchpassphrase' }
);
});

Expand Down Expand Up @@ -89,7 +84,7 @@ suite('OrderbookSync', () => {
});
});

test('emits a message event (with AuthenticatedClient)', done => {
test('emits a message event (with auth)', done => {
nock(EXCHANGE_API_URL)
.get('/products/BTC-USD/book?level=3')
.times(2)
Expand All @@ -99,12 +94,11 @@ suite('OrderbookSync', () => {
});

const server = testserver(++port, () => {
const authClient = new Gdax.AuthenticatedClient('key', 'secret', 'pass');
const orderbookSync = new Gdax.OrderbookSync(
'BTC-USD',
EXCHANGE_API_URL,
'ws://localhost:' + port,
authClient
{ key: 'key', secret: 'secret', passphrase: 'pass' }
);
orderbookSync.on('message', data => {
assert.deepEqual(data, {
Expand Down Expand Up @@ -147,18 +141,17 @@ suite('OrderbookSync', () => {
});
});

test('emits an error event on error (with AuthenticatedClient)', done => {
test('emits an error event on error (with auth)', done => {
nock(EXCHANGE_API_URL)
.get('/products/BTC-USD/book?level=3')
.replyWithError('whoops');

const server = testserver(++port, () => {
const authClient = new Gdax.AuthenticatedClient('key', 'secret', 'pass');
const orderbookSync = new Gdax.OrderbookSync(
'BTC-USD',
EXCHANGE_API_URL,
'ws://localhost:' + port,
authClient
{ key: 'key', secret: 'secret', passphrase: 'pass' }
);

orderbookSync.on('message', () =>
Expand Down

0 comments on commit 481193e

Please sign in to comment.