Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Add crypto functionality #125

Merged
merged 2 commits into from
Oct 15, 2021
Merged
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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ FYI [Robinhood's Terms and Conditions](https://brokerage-static.s3.amazonaws.com
* [`tag(tag, callback)`](#tagtag-callback)
* [`popularity(symbol, callback)`](#popularitysymbol-callback)
* [`options_positions`](#options_positions)
* [`get_currency_pairs`](#get_currency_pairs)
* [`get_crypto`](#get_crypto)
* [`options_orders`](#options_orders)
* [Contributors](#contributors)

Expand Down Expand Up @@ -979,6 +981,40 @@ var Robinhood = require('robinhood')(credentials, function() {

Return news about a symbol.

### `get_currency_pairs`

Get crypto - currency pairs

```javascript
var credentials = require("../credentials.js")();
var Robinhood = require('robinhood')(credentials, function() {
Robinhood.get_currency_pairs((err, response, body) => {
if (err) {
console.error(err);
} else {
console.log(body);
}
});
});
```

### `get_crypto`

Get cryptocurrency quote information from symbol

```javascript
var credentials = require("../credentials.js")();
var Robinhood = require('robinhood')(credentials, function() {
Robinhood.get_crypto('DOGE', (err, response, body) => {
if (err) {
console.error(err);
} else {
console.log(body);
}
});
});
```

`Documentation lacking sample response` **Feel like contributing? :)**

# Contributors
Expand All @@ -1000,6 +1036,7 @@ Alejandro U. Alvarez ([@aurbano](https://github.com/aurbano))
* Ryan Hendricks ([@ryanhendricks](https://github.com/ryanhendricks))
* Patrick Michaelsen ([@prmichaelsen](https://github.com/prmichaelsen))
* Joshua Wilborn ([@joshuajwilborn](https://github.com/joshuajwilborn))
* Adrian Veliz ([@aveliz1999](https://github.com/aveliz1999))

------------------

Expand Down
14 changes: 14 additions & 0 deletions robinhood.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ declare namespace robinhood {
*/
url (url: string, callback: request.RequestCallback): void


/**
* Get the Robinhood crypto to currency pairs
* @param callback
*/
get_currency_pairs(callback: request.RequestCallback): void

/**
* Get the quote data for a specified crypto symbol.
* @param symbol
* @param callback
*/
get_crypto(symbol: string, callback: request.RequestCallback): void

/**
*
* @param callback
Expand Down
37 changes: 36 additions & 1 deletion src/robinhood.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function RobinhoodWebApi(opts, callback) {
* +--------------------------------+ */
var _apiUrl = 'https://api.robinhood.com/';

var _currencyPairsUrl = 'https://nummus.robinhood.com/currency_pairs/';

var _options = opts || {},
// Private API Endpoints
_endpoints = {
Expand Down Expand Up @@ -64,7 +66,9 @@ function RobinhoodWebApi(opts, callback) {
sp500_up: 'midlands/movers/sp500/?direction=up',
sp500_down: 'midlands/movers/sp500/?direction=down',
news: 'midlands/news/',
tag: 'midlands/tags/tag/'
tag: 'midlands/tags/tag/',

crypto: 'marketdata/forex/quotes/'
},
_clientId = 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS',
_deviceToken = 'ea9fa5c6-01e0-46c9-8430-5b422c99bd16',
Expand Down Expand Up @@ -680,6 +684,37 @@ function RobinhoodWebApi(opts, callback) {
);
};

api.get_currency_pairs = function(callback) {
return request.get(
{
uri: _currencyPairsUrl,
},
callback
)
}

api.get_crypto = function(symbol, callback) {
return api.get_currency_pairs(function (error, response, body) {
if(error) {
return callback(error, response, body);
}

var assets = JSON.parse(body).results;
var asset = assets.find(a => a.asset_currency.code.toLowerCase() === symbol.toLowerCase());
if(!asset) {
var codes = assets.map(a => a.asset_currency.code);
return callback(new Error("Symbol not found. Only these codes are allowed: " + JSON.stringify(codes)));
}

return _request.get(
{
uri: _apiUrl + _endpoints.crypto + asset.id + '/'
},
callback
);
});
}

_init(_options);

return api;
Expand Down