1.0.1 • Published 5 years ago

coinswitch v1.0.1

Weekly downloads
57
License
MIT
Repository
github
Last release
5 years ago

coinswitch NPM Version node Dependency Status JavaScript Style Guide

Browser and Node.js Coinswitch.co API client

Easily exchange assets with few lines of code.

Install

npm install --save coinswitch

Usage

const Coinswitch = require('coinswitch')

;(async () => {

  const cs = new Coinswitch({
    apiKey: '<YOUR API KEY>',
    userIP: '1.1.1.1'
  })

  const coinsList = await cs.getCoins()  
  console.log(coinsList)
  /*
  [
    {
        "symbol": "btc",
        "name": "Bitcoin",
        "isActive": true
    },
    {
        "symbol": "ltc",
        "name": "Litecoin",
        "isActive": true
    },
    {
        "symbol": "bch",
        "name": "Bitcoin Cash",
        "isActive": true
    },
  	...
  ]
  */
})()

Make an exchange order

const cs = new Coinswitch({
  apiKey: '<YOUR API KEY>',
  userIP: '1.1.1.1'
})

const {
  offerReferenceId,
  depositCoinAmount,
  destinationCoinAmount
} = await cs.generateOffer('btc', 'ltc', 0.03)

const {
  orderId,
  exchangeAddress: { address }
} = await cs.makeOrder({
  depositCoin: 'btc',
  destinationCoin: 'ltc',
  depositCoinAmount,
  offerReferenceId,
  userReferenceId: 'test-user',
  destinationAddress: { address: 'LXdmzmqSALB1DbJyA43b6prQCXKn1J6SdV' },
  refundAddress: { address: '1KcuzqzcvkpY3K8eoNgmKbqhFvoeQXAoPa' }
})

console.log(`
=========
Order ID: ${orderId}
Deposit: BTC (${depositCoinAmount})
Receive: LTC (${destinationCoinAmount})
Exchange Address: ${address}
=========
`)

NB: Addresses must be specified as a JS Object like {address: "...", tag: "..."}

Methods

Get your api-key from the coinswitch API page.

  • new CoinSwitch(<Object>): The class constructor requires an Object like { apiKey: "...", userIP: "..."}.
Method NameReturnDescription
.version()StringReturn API version being used
.getCoins()ArrayGet list of supported coins as obj {symbol, name, isActive}
.isCoinActive(<symbol>)BooleanGiven a symbol (ex. btc) return true/false wether the coin is active or not
.getDestinationCoins(<symbol>)ArrayReturn list of available destination coins for the one provided
.getDepositCoins(<symbol>)ArrayReturn list of depositable coins for the one provided
.getExchangeLimit(<symbol>, <symbol>)ObjectGet exchange limits for a depositCoin and destinationCoin
.generateOffer(<symbol>, <symbol>, <number>)ObjectGenerate an exchange offer for a coin pair
.makeOrder(<object>*)ObjectCreates a new tx for an offer. It will return transaction object with an order_id field to track the tx status.
.getOrder(<string>)ObjectGet status of the given Order ID string.

* makeOrder input object sample:

{
  depositCoin: 'btc',
  destinationCoin: 'ltc',
  depositCoinAmount: 0.03,
  offerReferenceId: "...", // get it from generateOffer(..)
  userReferenceId: 'test-user',
  destinationAddress: { address: 'LXdmzmqSALB1DbJyA43b6prQCXKn1J6SdV' },
  refundAddress: { address: '1KcuzqzcvkpY3K8eoNgmKbqhFvoeQXAoPa' }
}

Example

For more examples look at the unit test/ folder.

Test

npm test

Debug

To enable debug set the env var DEBUG=coinswitch

License

MIT