Add QuadrigaCX exchange and ticker (#176)
* Add QuadrigaCX exchange and ticker * Clean up code from QuadrigaCX
This commit is contained in:
parent
147db125dd
commit
bcf336741e
7 changed files with 238 additions and 12 deletions
91
lib/plugins/common/quadrigacx.js
Normal file
91
lib/plugins/common/quadrigacx.js
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
const axios = require('axios')
|
||||
const crypto = require('crypto')
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const API_ENDPOINT = 'https://api.quadrigacx.com/v2'
|
||||
|
||||
let counter = -1
|
||||
let lastTimestamp = Date.now()
|
||||
|
||||
function pad (num) {
|
||||
const asString = num.toString(10)
|
||||
if (num < 10) return '00' + asString
|
||||
if (num < 100) return '0' + asString
|
||||
return asString
|
||||
}
|
||||
|
||||
function generateNonce () {
|
||||
const timestamp = Date.now()
|
||||
if (timestamp !== lastTimestamp) counter = -1
|
||||
lastTimestamp = timestamp
|
||||
counter = (counter + 1) % 1000
|
||||
return timestamp.toString(10) + pad(counter)
|
||||
}
|
||||
|
||||
function authRequest (config, path, data) {
|
||||
if (!config.key || !config.secret || !config.clientId) {
|
||||
const err = new Error('Must provide key, secret and client ID')
|
||||
return Promise.reject(err)
|
||||
}
|
||||
|
||||
data = data || {}
|
||||
|
||||
const nonce = generateNonce()
|
||||
const msg = [nonce, config.clientId, config.key].join('')
|
||||
|
||||
const signature = crypto
|
||||
.createHmac('sha256', Buffer.from(config.secret))
|
||||
.update(msg)
|
||||
.digest('hex')
|
||||
.toLowerCase()
|
||||
|
||||
const signedData = _.merge(data, {
|
||||
key: config.key,
|
||||
signature,
|
||||
nonce
|
||||
})
|
||||
|
||||
return request(path, 'POST', signedData)
|
||||
}
|
||||
|
||||
function buildMarket (fiatCode, cryptoCode) {
|
||||
if (!_.includes(cryptoCode, ['BTC', 'ETH', 'LTC', 'BCH'])) {
|
||||
throw new Error(`Unsupported crypto: ${cryptoCode}`)
|
||||
}
|
||||
|
||||
if (!_.includes(fiatCode, ['USD', 'CAD'])) {
|
||||
throw new Error(`Unsupported fiat: ${fiatCode}`)
|
||||
}
|
||||
|
||||
let market = `${cryptoCode.toLowerCase()}_${fiatCode.toLowerCase()}`
|
||||
|
||||
if (fiatCode === 'USD' && cryptoCode !== 'BTC') {
|
||||
throw new Error(`Unsupported market: ${market}`)
|
||||
}
|
||||
|
||||
return market
|
||||
}
|
||||
|
||||
function request (path, method, data) {
|
||||
const options = {
|
||||
method,
|
||||
data,
|
||||
url: API_ENDPOINT + path,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/4.0 (compatible; Lamassu client)',
|
||||
'Content-Type': 'application/json; charset=utf-8'
|
||||
}
|
||||
}
|
||||
|
||||
return axios(options)
|
||||
.then(r => {
|
||||
if (r.data.error) throw new Error(r.data.error.message)
|
||||
return r.data
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildMarket,
|
||||
authRequest,
|
||||
request
|
||||
}
|
||||
28
lib/plugins/exchange/quadrigacx/quadrigacx.js
Normal file
28
lib/plugins/exchange/quadrigacx/quadrigacx.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
const common = require('../../common/quadrigacx')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
||||
function buy (account, cryptoAtoms, fiatCode, cryptoCode) {
|
||||
return trade('buy', account, cryptoAtoms, fiatCode, cryptoCode)
|
||||
}
|
||||
|
||||
function sell (account, cryptoAtoms, fiatCode, cryptoCode) {
|
||||
return trade('sell', account, cryptoAtoms, fiatCode, cryptoCode)
|
||||
}
|
||||
|
||||
function trade (type, account, cryptoAtoms, fiatCode, cryptoCode) {
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
const market = common.buildMarket(fiatCode, cryptoCode)
|
||||
const options = {
|
||||
book: market,
|
||||
amount: coinUtils.toUnit(cryptoAtoms, cryptoCode).toFixed(8)
|
||||
}
|
||||
|
||||
return common.authRequest(account, '/' + type, options)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buy,
|
||||
sell
|
||||
}
|
||||
49
lib/plugins/ticker/quadrigacx/quadrigacx.js
Normal file
49
lib/plugins/ticker/quadrigacx/quadrigacx.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
const axios = require('axios')
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const BN = require('../../../bn')
|
||||
const common = require('../../common/quadrigacx')
|
||||
|
||||
exports.NAME = 'QuadrigaCX'
|
||||
exports.SUPPORTED_MODULES = ['ticker']
|
||||
|
||||
function findCurrency (fxRates, fiatCode) {
|
||||
const rates = _.find(_.matchesProperty('code', fiatCode), fxRates)
|
||||
if (!rates || !rates.rate) throw new Error(`Unsupported currency: ${fiatCode}`)
|
||||
return BN(rates.rate)
|
||||
}
|
||||
|
||||
exports.ticker = function ticker (account, fiatCode, cryptoCode) {
|
||||
if (fiatCode === 'USD' && cryptoCode === 'BTC' || fiatCode === 'CAD') {
|
||||
return getCurrencyRates(fiatCode, cryptoCode)
|
||||
}
|
||||
|
||||
return axios.get('https://bitpay.com/api/rates')
|
||||
.then(response => {
|
||||
const fxRates = response.data
|
||||
const cadRate = findCurrency(fxRates, 'CAD')
|
||||
const fxRate = findCurrency(fxRates, fiatCode).div(cadRate)
|
||||
|
||||
return getCurrencyRates('CAD', cryptoCode)
|
||||
.then(res => ({
|
||||
rates: {
|
||||
ask: res.rates.ask.times(fxRate),
|
||||
bid: res.rates.bid.times(fxRate)
|
||||
}
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
function getCurrencyRates (fiatCode, cryptoCode) {
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
const market = common.buildMarket(fiatCode, cryptoCode)
|
||||
return common.request(`/ticker?book=${market}`, 'GET')
|
||||
})
|
||||
.then(r => ({
|
||||
rates: {
|
||||
ask: BN(r.ask),
|
||||
bid: BN(r.bid)
|
||||
}
|
||||
}))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue