migrate current plugins to ccxt
This commit is contained in:
parent
680131fbb8
commit
a881527a1f
3 changed files with 142 additions and 0 deletions
28
lib/plugins/common/cctx.js
Normal file
28
lib/plugins/common/cctx.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
const _ = require('lodash/fp')
|
||||
|
||||
const CRYPTO = {
|
||||
bitstamp: ['BTC', 'ETH', 'LTC', 'BCH'],
|
||||
itbit: ['BTC', 'ETH', 'LTC', 'BCH'],
|
||||
kraken: ['BTC', 'ETH', 'LTC', 'DASH', 'ZEC', 'BCH'],
|
||||
coinbase: ['BTC', 'ETH', 'LTC', 'BCH', 'ZEC', 'DASH']
|
||||
}
|
||||
|
||||
const FIAT = {
|
||||
bitstamp: ['USD', 'EUR'],
|
||||
itbit: ['USD'],
|
||||
kraken: ['USD', 'EUR']
|
||||
}
|
||||
|
||||
module.exports = { verifyCurrencies }
|
||||
|
||||
function verifyCurrencies (exchangeName, fiatCode, cryptoCode) {
|
||||
if (!_.includes(cryptoCode, CRYPTO[exchangeName])) {
|
||||
throw new Error('Unsupported crypto: ' + cryptoCode)
|
||||
}
|
||||
if (!(exchangeName === 'coinbase')) { // coinbase is only used for ticker and it's expected to support most of the fiat
|
||||
if (!_.includes(fiatCode, FIAT[exchangeName])) {
|
||||
throw new Error('Unsupported fiat: ' + fiatCode)
|
||||
}
|
||||
}
|
||||
return cryptoCode + '/' + fiatCode
|
||||
}
|
||||
57
lib/plugins/exchange/cctx/cctx.js
Normal file
57
lib/plugins/exchange/cctx/cctx.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
var ccxt = require('ccxt')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
const _ = require('lodash/fp')
|
||||
const common = require('../../common/cctx')
|
||||
|
||||
function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName) {
|
||||
try {
|
||||
const exchange = setUpExchange(account, exchangeName)
|
||||
const symbol = common.verifyCurrencies(exchangeName, fiatCode, cryptoCode)
|
||||
const amount = coinUtils.toUnit(cryptoAtoms, cryptoCode).toFixed(8)
|
||||
if (exchangeName === 'itbit') {
|
||||
return exchange.fetchOrderBook(symbol)
|
||||
.then(orderBook => {
|
||||
return exchange.createOrder(symbol, 'limit', side, amount, calculatePrice(side, amount, orderBook), { walletId: account.walletId })
|
||||
})
|
||||
}
|
||||
return exchange.createOrder(symbol, 'market', side, amount)
|
||||
} catch (e) {
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
function setUpExchange (account, exchangeName) {
|
||||
// map given credentials to cctx properties
|
||||
if (!_.includes(exchangeName, ccxt.exchanges)) {
|
||||
throw new Error(`Exchange ${exchangeName} not supported by ccxt.`)
|
||||
}
|
||||
|
||||
switch (exchangeName) {
|
||||
case 'itbit':
|
||||
if (!account.clientKey || !account.clientSecret || !account.userId || !account.walletId)
|
||||
throw new Error('Must provide user ID, wallet ID, client key, and client secret')
|
||||
return new ccxt[exchangeName](_.mapKeys((key) => { return key === 'clientKey' ? 'apiKey' : key === 'clientSecret' ? 'secret' : key === 'userId' ? 'uid' : key }, _.omit(['walletId'], account)))
|
||||
case 'kraken':
|
||||
if (!account.apiKey || !account.privateKey)
|
||||
throw new Error('Must provide key and private key')
|
||||
return new ccxt[exchangeName](_.mapKeys((key) => { return key === 'privateKey' ? 'secret' : key }, account))
|
||||
case 'bitstamp':
|
||||
if (!account.key || !account.secret || !account.clientId)
|
||||
throw new Error('Must provide key, secret and client ID')
|
||||
return new ccxt[exchangeName](_.mapKeys((key) => { return key === 'key' ? 'apiKey' : key === 'clientId' ? 'uid' : key }, account))
|
||||
default:
|
||||
throw new Error(`Exchange ${exchangeName} not supported.`)
|
||||
}
|
||||
}
|
||||
|
||||
function calculatePrice (side, amount, orderBook) {
|
||||
const book = side === 'buy' ? 'asks' : 'bids'
|
||||
let collected = 0.0
|
||||
for (const entry of orderBook[book]) {
|
||||
collected += parseFloat(entry[1])
|
||||
if (collected >= amount) return parseFloat(entry[0])
|
||||
}
|
||||
throw new Error('Insufficient market depth')
|
||||
}
|
||||
|
||||
module.exports = { trade }
|
||||
57
lib/plugins/ticker/cctx/cctx.js
Normal file
57
lib/plugins/ticker/cctx/cctx.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
const ccxt = require('ccxt')
|
||||
const BN = require('../../../bn')
|
||||
const axios = require('axios')
|
||||
const _ = require('lodash/fp')
|
||||
const common = require('../../common/cctx')
|
||||
|
||||
function ticker (exchangeName, fiatCode, cryptoCode) {
|
||||
const exchange = new ccxt[exchangeName]()
|
||||
|
||||
if (fiatCode === 'EUR' || fiatCode === 'USD' || exchange.id === 'coinbase') {
|
||||
return getCurrencyRates(exchange, fiatCode, cryptoCode)
|
||||
}
|
||||
|
||||
return axios.get('https://bitpay.com/rates')
|
||||
.then(response => {
|
||||
try {
|
||||
const fxRates = response.data.data
|
||||
const usdRate = findCurrencyRates(fxRates, 'USD')
|
||||
const fxRate = findCurrencyRates(fxRates, fiatCode).div(usdRate)
|
||||
|
||||
return getCurrencyRates(exchange, 'USD', cryptoCode)
|
||||
.then(res => ({
|
||||
rates: {
|
||||
ask: res.rates.ask.times(fxRate),
|
||||
bid: res.rates.bid.times(fxRate)
|
||||
}
|
||||
}))
|
||||
} catch (e) {
|
||||
return Promise.reject(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getCurrencyRates (exchange, fiatCode, cryptoCode) {
|
||||
try {
|
||||
if (exchange.has['fetchTicker']) {
|
||||
const symbol = common.verifyCurrencies(exchange.id, fiatCode, cryptoCode)
|
||||
return exchange.fetchTicker(symbol)
|
||||
.then(res => ({
|
||||
rates: {
|
||||
ask: BN(res.ask),
|
||||
bid: BN(res.bid)
|
||||
}
|
||||
}))
|
||||
}
|
||||
} catch (e) {
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
function findCurrencyRates (fxRates, fiatCode) {
|
||||
const rates = _.find(_.matchesProperty('code', fiatCode), fxRates)
|
||||
if (!rates || !rates.rate) throw new Error(`Unsupported currency: ${fiatCode}`)
|
||||
return BN(rates.rate.toString())
|
||||
}
|
||||
|
||||
module.exports = { ticker }
|
||||
Loading…
Add table
Add a link
Reference in a new issue