davesag / ir-api

A NodeJS Client for Independent Reserve's API
MIT License
6 stars 6 forks source link
api-client bitcoin cryptocurrency-exchanges ethereum independent-reserve

ir-api

A Javascript client for Independent Reserve's API.

Note This is a 3rd Party project and is not developed by, or supported by Independent Reserve.

Features

NPM

Usage

Please familiarise yourself with Independent Reserve's API Documentation

Create an account

You will need an account at Independent Reserve and you will need to generate an API Key and API Secret.

Please use my referral code if you create an account.

Install

NodeJS

npm install axios ir-api

React Native

See below for instructions on using with React Native

The API

The API calls follow the official documentation but start with a lower case letter instead of upper case.

Similarly all returned data will have keys that start with lower case instead of upper case.

Public Methods

const ir = require('ir-api')

const { getValidPrimaryCurrencyCodes } = ir()

getValidPrimaryCurrencyCodes()
  .then(codes => {
    console.log('codes', codes)
  })
  .catch(error => {
    console.error(error)
  })

Private Methods

const ir = require('ir-api')

const { getOpenOrders } = ir('my-api-key', 'my-api-secret')

getOpenOrders()
  .then(data => {
    console.log('data', data)
  })
  .catch(error => {
    console.error(error)
  })

Passing Parameters to Methods

Parameters are passed as an object, so for example

getOpenOrders({
  primaryCurrencyCode: 'Xbt',
  secondaryCurrencyCode: 'Usd'
})
  .then(data => {
    console.log('data', data)
  })
  .catch(error => {
    console.error(error)
  })

Configuring axios

Under the hood the ir-api uses axios as its transport layer with the following defaults:

{
  baseURL: 'https://api.independentreserve.com',
  timeout: 2500
  headers: {
    'Content-Type': 'application/json',
    'User-Agent': 'Independent Reserve Javascript API (github.com/davesag/ir-api)'
  }
}

You can supply your own configuration object to the ir function.

const ir = require('ir-api')

const { getAccounts } = ir('my-api-key', 'my-api-secret', {
  timeout: 500,
  headers: { 'User-Agent': 'My amazing app' }
})

You can supply any configuration options that axios supports, however if you change the baseURL, or Content-Type you will find the API calls stop working, so I don't advise doing that.

That said, if your app needs to run integration tests against a mock IR server (maybe you built one for this purpose) then this is where you'd override the baseURL.

const ir = require('ir-api')

const { getAccounts } = ir('my-api-key', 'my-api-secret', {
  baseURL: 'https://localhost:8080/' // because maybe you are testing against a local mock server
})

Independent Reserve's public API server can be quite slow which is why the timeout is set to 2500 by default. It's much faster if you use an apiKey and apiSecret however.

Default Parameters

using async / await

All methods return a resolved promise so you can safely use async / await

Example Gist

See this gist for an example of using the API to retrieve your IR balance, then get the market rates for each of your coins, convert to Australian Dollars and display a simple ASCII table with the results and a total.

Error Handling

Handling Timeouts

The Independent Reserve API occasionally times out. The client will automatically attempt up to 3 retries of any timed-out idempotent request, with a delay of 250ms on first retry, 500ms on second, and 750ms on third. It will also extend the default timeout on each retried request.

If you still keep seeing timeout errors then you can set a longer base request timeout duration as outlined in the configuration example above. The default timeout is 2500ms.

Validating Cryptocurrency Addresses

This API client does not know in advance which cryptocurrencies are supported by Independent Reserve, and as such it's not possible to compile a complete set of cryptocurrency address format validators.

Developers using this library are encouraged to use the many 3rd party cryptocurrency address validators that already exist, depending on their specific use cases.

Use with React Native

You can use ir-api with React Native but you need to do some prep-work first.

Install Dependencies

With npm

npm i axios crypto-browserify process querystring stream-browserify vm-browserify ir-api

Or with yarn

yarn add axios crypto-browserify process querystring stream-browserify vm-browserify ir-api

Create a ./shim.js File

Create a file called shim.js at the root of your project

/* eslint-disable no-undef */
if (typeof __dirname === 'undefined') global.__dirname = '/'
if (typeof __filename === 'undefined') global.__filename = ''
if (typeof process === 'undefined') {
  global.process = require('process')
} else {
  const bProcess = require('process')
  for (let p in bProcess) {
    if (!(p in process)) {
      process[p] = bProcess[p]
    }
  }
}

process.browser = false
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer

// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__
process.env.NODE_ENV = isDev ? 'development' : 'production'
if (typeof localStorage !== 'undefined') {
  localStorage.debug = isDev ? '*' : ''
}

require('crypto')

Then Add ./shim.js to Your Project

As early in the project as you can, such as in <projectRoot>/index.js, add import './shim'

Modify Your metro.conf.js File

Insert the following resolver config in ./metro.conf.js:

resolver: {
  extraNodeModules: {
    "crypto": require.resolve("crypto-browserify"),
    "stream": require.resolve("stream-browserify"),
    "vm": require.resolve("vm-browserify")
  }
},

Example Mobile App

See github.com/davesag/irMobile

Development

Branches

branch status coverage audit notes
develop CircleCI codecov Vulnerabilities Work in progress
main CircleCI codecov Vulnerabilities Latest stable release

Prerequisites

Initialisation

npm install

Test it

Lint It

npm run lint

Contributing

Please see the contributing notes.

Other Ways to Contribute