kraken plugin WIP

This commit is contained in:
Josh Harvey 2016-03-28 00:52:15 +02:00
parent fd4e5b5f34
commit 7be7c3f8e6
3 changed files with 74 additions and 9 deletions

View file

@ -63,8 +63,12 @@ function loadPlugin (name, config) {
try {
plugin = require('lamassu-' + name)
} catch (_) {
throw new Error(name + ' module is not installed. ' +
'Try running \'npm install --save lamassu-' + name + '\' first')
try {
require('plugins/' + name)
} catch (_) {
throw new Error(name + ' module is not installed. ' +
'Try running \'npm install --save lamassu-' + name + '\' first')
}
}
// each plugin MUST implement those
@ -116,8 +120,7 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCoin, currency,
if (!currentName) pluginHandle = null
else { // some plugins may be disabled
var pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] ||
{}
var pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] || {}
if (currency) pluginConfig.currency = currency
@ -448,7 +451,14 @@ function pollRate (cryptoCoin, cb) {
logger.debug('[%s] polling for rates (%s)', cryptoCoin, tickerPlugin.NAME)
var tickerPlugin = tickerPlugins[cryptoCoin]
tickerPlugin.ticker(deviceCurrency, function (err, resRates) {
var currencies = deviceCurrency
if (typeof currencies === 'string') currencies = [currencies]
var tickerF = cryptoCoin === 'BTC'
? async.apply(tickerPlugin.ticker, currencies)
: async.apply(tickerPlugin.ticker, currencies, cryptoCoin)
tickerF(function (err, resRates) {
if (err) {
logger.error(err)
return cb && cb(err)

51
lib/plugins/kraken.js Normal file
View file

@ -0,0 +1,51 @@
require('es6-promise').polyfill()
var axios = require('axios')
var _ = require('lodash')
var BigNumber = require('bignumber.js')
BigNumber.config({DECIMAL_PLACES: 30})
exports.NAME = 'Kraken'
exports.SUPPORTED_MODULES = ['ticker']
// var pluginConfig = {}
// https://bitpay.com/api/rates
exports.config = function config (localConfig) {
// pluginConfig = localConfig
}
function findCurrency (fxRates, currency) {
return new BigNumber(_.find(fxRates, function (r) { return r.code === currency }).rate)
}
exports.ticker = function ticker (currencies, cryptoCoin, callback) {
return axios.get('https://bitpay.com/api/rates')
.then(function (response) {
var fxRates = response.data
return axios.get('https://api.kraken.com/0/public/Ticker?pair=ETHUSD')
.then(function (response2) {
var usdRate = findCurrency(fxRates, 'USD')
var rates = response2.data.result.XETHZUSD
var res = {}
var cryptoCoinFactor = new BigNumber(10).pow(cryptoCoin.unitScale)
currencies.forEach(function (currency) {
var fxRate = findCurrency(fxRates, currency).div(usdRate)
res[currency] = {
ask: fxRate.times(rates.a[0]).div(cryptoCoinFactor),
bid: fxRate.times(rates.b[0]).div(cryptoCoinFactor)
}
})
callback(null, res)
})
})
.catch(callback)
}
exports.ticker(['USD', 'ILS', 'EUR'], {unitScale: 18}, function (err, res) {
if (err) return console.log(err.stack)
console.log(JSON.stringify(res, null, 2))
})

View file

@ -10,23 +10,27 @@
},
"dependencies": {
"async": "~0.2.9",
"axios": "^0.9.1",
"bignumber.js": "^2.3.0",
"bluebird": "^3.3.4",
"bunyan": "~0.22.3",
"es6-promise": "^3.1.2",
"express": "~3.4.7",
"inquirer": "^0.8.0",
"joi": "^5.1.0",
"lamassu-bitcoinaverage": "~1.0.0",
"lamassu-bitcoind": "^1.1.0",
"lamassu-bitgo": "^0.1.3",
"lamassu-bitpay": "~1.0.0",
"lamassu-bitstamp": "^1.0.2",
"lamassu-blockchain": "^1.1.3",
"lamassu-blockcypher": "~0.1.0",
"lamassu-coinapult": "^0.4.5",
"lamassu-coinbase": "^1.0.4",
"lamassu-coindesk": "~1.0.0",
"lamassu-coinfloor": "^0.1.2",
"lamassu-config": "~0.4.0",
"lamassu-identitymind": "^1.0.1",
"lamassu-coinbase": "^1.0.4",
"lamassu-coinapult": "^0.4.5",
"lamassu-coinfloor": "^0.1.2",
"lamassu-bitgo": "^0.1.3",
"lamassu-snapcard": "^0.1.7",
"lodash": "^2.4.1",
"minimist": "0.0.8",