From 79bee894d4b53e72a0e3abbdce3043f697bebc84 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 3 Apr 2017 12:24:30 -0400 Subject: [PATCH 1/2] Add methods for margin endpoints --- README.md | 33 +++++++++++++ lib/clients/authenticated.js | 38 ++++++++++++++ tests/authenticated.js | 96 ++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) diff --git a/README.md b/README.md index 88e09ebf..a2cf2d96 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,39 @@ authedClient.getFills(callback); authedClient.getFills({'before': 3000}, callback); ``` +* [`getFundings`](https://docs.gdax.com/#list-fundings) +```javascript +authedClient.getFundings({}, callback); +``` + +* [`repay`](https://docs.gdax.com/#repay) +```javascript +var params = { + 'amount': '2000.00', + 'currency': 'USD' +}; +authedClient.repay(params, callback); +``` + +* [`marginTransfer`](https://docs.gdax.com/#margin-transfer) +```javascript +var params = + 'margin_profile_id': '45fa9e3b-00ba-4631-b907-8a98cbdf21be', + 'type': 'deposit', + 'currency': 'USD', + 'amount': 2 +}; +authedClient.marginTransfer(params, callback); +``` + +* [`closePosition`](https://docs.gdax.com/#close) +```javascript +var params = { + 'repay_only': false +}; +authedClient.closePosition(params, callback); +``` + * [`deposit`, `withdraw`](https://docs.gdax.com/#list-fills) ```javascript // Deposit to your Exchange USD account from your Coinbase USD account. diff --git a/lib/clients/authenticated.js b/lib/clients/authenticated.js index 0e0602a0..e4510e1f 100644 --- a/lib/clients/authenticated.js +++ b/lib/clients/authenticated.js @@ -267,6 +267,44 @@ _.assign(AuthenticatedClient.prototype, new function() { return prototype.get.call(self, ['fills'], opts, callback); }; + prototype.getFundings = function(callback) { + var self = this; + return prototype.get.call(self, ['funding'], callback); + }; + + prototype.repay = function(params, callback) { + var self = this; + _.forEach(['amount', 'currency'], function(param) { + if (params[param] === undefined) { + throw "`opts` must include param `" + param + "`"; + } + }); + var opts = { 'body': params }; + return prototype.post.call(self, ['funding/repay'], opts, callback); + }; + + prototype.marginTransfer = function(params, callback) { + var self = this; + _.forEach(['margin_profile_id', 'type', 'currency', 'amount'], function(param) { + if (params[param] === undefined) { + throw "`opts` must include param `" + param + "`"; + } + }); + var opts = { 'body': params }; + return prototype.post.call(self, ['profiles/margin-transfer'], opts, callback); + }; + + prototype.closePosition = function(params, callback) { + var self = this; + _.forEach(['repay_only'], function(param) { + if (params[param] === undefined) { + throw "`opts` must include param `" + param + "`"; + } + }); + var opts = { 'body': params }; + return prototype.post.call(self, ['position/close'], opts, callback); + }; + prototype.deposit = function(params, callback) { var self = this; params.type = 'deposit'; diff --git a/tests/authenticated.js b/tests/authenticated.js index a53df067..afb6ec55 100644 --- a/tests/authenticated.js +++ b/tests/authenticated.js @@ -305,6 +305,102 @@ test('get fills', function(done) { }); }); +test('get fundings', function(done) { + var expectedResponse = [{ + "id": "280c0a56-f2fa-4d3b-a199-92df76fff5cd", + "order_id": "280c0a56-f2fa-4d3b-a199-92df76fff5cd", + "profile_id": "d881e5a6-58eb-47cd-b8e2-8d9f2e3ec6f6", + "amount": "545.2400000000000000", + "status": "outstanding", + "created_at": "2017-03-18T00:34:34.270484Z", + "currency": "USD", + "repaid_amount": "532.7580047716682500" + }]; + + nock(EXCHANGE_API_URL) + .get('/funding') + .reply(200, expectedResponse); + + authClient.getFundings(function(err, resp, data) { + assert.ifError(err); + assert.deepEqual(data, expectedResponse); + + nock.cleanAll(); + done(); + }); +}); + +test('repay', function(done) { + var params = { + "amount" : 10000, + "currency": 'USD' + }; + + nock(EXCHANGE_API_URL) + .post('/funding/repay', params) + .reply(200, {}); + + authClient.repay(params, function(err, resp, data) { + assert.ifError(err); + + nock.cleanAll(); + done(); + }); +}); + +test('margin transfer', function(done) { + var params = { + "margin_profile_id": "45fa9e3b-00ba-4631-b907-8a98cbdf21be", + "type": "deposit", + "currency": "USD", + "amount": 2 + }; + var expectedResponse = { + "created_at": "2017-01-25T19:06:23.415126Z", + "id": "80bc6b74-8b1f-4c60-a089-c61f9810d4ab", + "user_id": "521c20b3d4ab09621f000011", + "profile_id": "cda95996-ac59-45a3-a42e-30daeb061867", + "margin_profile_id": "45fa9e3b-00ba-4631-b907-8a98cbdf21be", + "type": "deposit", + "amount": "2", + "currency": "USD", + "account_id": "23035fc7-0707-4b59-b0d2-95d0c035f8f5", + "margin_account_id": "e1d9862c-a259-4e83-96cd-376352a9d24d", + "margin_product_id": "BTC-USD", + "status": "completed", + "nonce": 25 + }; + + nock(EXCHANGE_API_URL) + .post('/profiles/margin-transfer', params) + .reply(200, expectedResponse); + + authClient.marginTransfer(params, function(err, resp, data) { + assert.ifError(err); + assert.deepEqual(data, expectedResponse); + + nock.cleanAll(); + done(); + }); +}); + +test('close position', function(done) { + var params = { + "repay_only" : false + }; + + nock(EXCHANGE_API_URL) + .post('/position/close', params) + .reply(200, {}); + + authClient.closePosition(params, function(err, resp, data) { + assert.ifError(err); + + nock.cleanAll(); + done(); + }); +}); + test('deposit', function(done) { var transfer = { "amount" : 10480, From bdbbcd4ca4ab97fc7aeb8b326487053757a7ca2e Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 3 Apr 2017 15:06:25 -0400 Subject: [PATCH 2/2] Bump version to 0.4.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dbfcd0db..9caaf834 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gdax", - "version": "0.3.2", + "version": "0.4.2", "author": "Coinbase", "bugs": "https://github.com/coinbase/gdax-node/issues", "contributors": [