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

Commit

Permalink
Merge pull request #39 from jborseth/margin-endpoints
Browse files Browse the repository at this point in the history
Add methods for margin endpoints
  • Loading branch information
CjS77 authored Apr 3, 2017
2 parents 221e7e6 + bdbbcd4 commit 98d044e
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions lib/clients/authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
96 changes: 96 additions & 0 deletions tests/authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 98d044e

Please sign in to comment.