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

Commit 98d044e

Browse files
authored
Merge pull request #39 from jborseth/margin-endpoints
Add methods for margin endpoints
2 parents 221e7e6 + bdbbcd4 commit 98d044e

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,39 @@ authedClient.getFills(callback);
240240
authedClient.getFills({'before': 3000}, callback);
241241
```
242242

243+
* [`getFundings`](https://docs.gdax.com/#list-fundings)
244+
```javascript
245+
authedClient.getFundings({}, callback);
246+
```
247+
248+
* [`repay`](https://docs.gdax.com/#repay)
249+
```javascript
250+
var params = {
251+
'amount': '2000.00',
252+
'currency': 'USD'
253+
};
254+
authedClient.repay(params, callback);
255+
```
256+
257+
* [`marginTransfer`](https://docs.gdax.com/#margin-transfer)
258+
```javascript
259+
var params =
260+
'margin_profile_id': '45fa9e3b-00ba-4631-b907-8a98cbdf21be',
261+
'type': 'deposit',
262+
'currency': 'USD',
263+
'amount': 2
264+
};
265+
authedClient.marginTransfer(params, callback);
266+
```
267+
268+
* [`closePosition`](https://docs.gdax.com/#close)
269+
```javascript
270+
var params = {
271+
'repay_only': false
272+
};
273+
authedClient.closePosition(params, callback);
274+
```
275+
243276
* [`deposit`, `withdraw`](https://docs.gdax.com/#list-fills)
244277
```javascript
245278
// Deposit to your Exchange USD account from your Coinbase USD account.

lib/clients/authenticated.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,44 @@ _.assign(AuthenticatedClient.prototype, new function() {
267267
return prototype.get.call(self, ['fills'], opts, callback);
268268
};
269269

270+
prototype.getFundings = function(callback) {
271+
var self = this;
272+
return prototype.get.call(self, ['funding'], callback);
273+
};
274+
275+
prototype.repay = function(params, callback) {
276+
var self = this;
277+
_.forEach(['amount', 'currency'], function(param) {
278+
if (params[param] === undefined) {
279+
throw "`opts` must include param `" + param + "`";
280+
}
281+
});
282+
var opts = { 'body': params };
283+
return prototype.post.call(self, ['funding/repay'], opts, callback);
284+
};
285+
286+
prototype.marginTransfer = function(params, callback) {
287+
var self = this;
288+
_.forEach(['margin_profile_id', 'type', 'currency', 'amount'], function(param) {
289+
if (params[param] === undefined) {
290+
throw "`opts` must include param `" + param + "`";
291+
}
292+
});
293+
var opts = { 'body': params };
294+
return prototype.post.call(self, ['profiles/margin-transfer'], opts, callback);
295+
};
296+
297+
prototype.closePosition = function(params, callback) {
298+
var self = this;
299+
_.forEach(['repay_only'], function(param) {
300+
if (params[param] === undefined) {
301+
throw "`opts` must include param `" + param + "`";
302+
}
303+
});
304+
var opts = { 'body': params };
305+
return prototype.post.call(self, ['position/close'], opts, callback);
306+
};
307+
270308
prototype.deposit = function(params, callback) {
271309
var self = this;
272310
params.type = 'deposit';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gdax",
3-
"version": "0.3.2",
3+
"version": "0.4.2",
44
"author": "Coinbase",
55
"bugs": "https://github.com/coinbase/gdax-node/issues",
66
"contributors": [

tests/authenticated.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,102 @@ test('get fills', function(done) {
305305
});
306306
});
307307

308+
test('get fundings', function(done) {
309+
var expectedResponse = [{
310+
"id": "280c0a56-f2fa-4d3b-a199-92df76fff5cd",
311+
"order_id": "280c0a56-f2fa-4d3b-a199-92df76fff5cd",
312+
"profile_id": "d881e5a6-58eb-47cd-b8e2-8d9f2e3ec6f6",
313+
"amount": "545.2400000000000000",
314+
"status": "outstanding",
315+
"created_at": "2017-03-18T00:34:34.270484Z",
316+
"currency": "USD",
317+
"repaid_amount": "532.7580047716682500"
318+
}];
319+
320+
nock(EXCHANGE_API_URL)
321+
.get('/funding')
322+
.reply(200, expectedResponse);
323+
324+
authClient.getFundings(function(err, resp, data) {
325+
assert.ifError(err);
326+
assert.deepEqual(data, expectedResponse);
327+
328+
nock.cleanAll();
329+
done();
330+
});
331+
});
332+
333+
test('repay', function(done) {
334+
var params = {
335+
"amount" : 10000,
336+
"currency": 'USD'
337+
};
338+
339+
nock(EXCHANGE_API_URL)
340+
.post('/funding/repay', params)
341+
.reply(200, {});
342+
343+
authClient.repay(params, function(err, resp, data) {
344+
assert.ifError(err);
345+
346+
nock.cleanAll();
347+
done();
348+
});
349+
});
350+
351+
test('margin transfer', function(done) {
352+
var params = {
353+
"margin_profile_id": "45fa9e3b-00ba-4631-b907-8a98cbdf21be",
354+
"type": "deposit",
355+
"currency": "USD",
356+
"amount": 2
357+
};
358+
var expectedResponse = {
359+
"created_at": "2017-01-25T19:06:23.415126Z",
360+
"id": "80bc6b74-8b1f-4c60-a089-c61f9810d4ab",
361+
"user_id": "521c20b3d4ab09621f000011",
362+
"profile_id": "cda95996-ac59-45a3-a42e-30daeb061867",
363+
"margin_profile_id": "45fa9e3b-00ba-4631-b907-8a98cbdf21be",
364+
"type": "deposit",
365+
"amount": "2",
366+
"currency": "USD",
367+
"account_id": "23035fc7-0707-4b59-b0d2-95d0c035f8f5",
368+
"margin_account_id": "e1d9862c-a259-4e83-96cd-376352a9d24d",
369+
"margin_product_id": "BTC-USD",
370+
"status": "completed",
371+
"nonce": 25
372+
};
373+
374+
nock(EXCHANGE_API_URL)
375+
.post('/profiles/margin-transfer', params)
376+
.reply(200, expectedResponse);
377+
378+
authClient.marginTransfer(params, function(err, resp, data) {
379+
assert.ifError(err);
380+
assert.deepEqual(data, expectedResponse);
381+
382+
nock.cleanAll();
383+
done();
384+
});
385+
});
386+
387+
test('close position', function(done) {
388+
var params = {
389+
"repay_only" : false
390+
};
391+
392+
nock(EXCHANGE_API_URL)
393+
.post('/position/close', params)
394+
.reply(200, {});
395+
396+
authClient.closePosition(params, function(err, resp, data) {
397+
assert.ifError(err);
398+
399+
nock.cleanAll();
400+
done();
401+
});
402+
});
403+
308404
test('deposit', function(done) {
309405
var transfer = {
310406
"amount" : 10480,

0 commit comments

Comments
 (0)