From a881527a1f7f3e63af824442b6ffb1d731309345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Mon, 18 Jan 2021 10:15:44 +0000 Subject: [PATCH] migrate current plugins to ccxt --- lib/plugins/common/cctx.js | 28 +++++++++++++++ lib/plugins/exchange/cctx/cctx.js | 57 +++++++++++++++++++++++++++++++ lib/plugins/ticker/cctx/cctx.js | 57 +++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 lib/plugins/common/cctx.js create mode 100644 lib/plugins/exchange/cctx/cctx.js create mode 100644 lib/plugins/ticker/cctx/cctx.js diff --git a/lib/plugins/common/cctx.js b/lib/plugins/common/cctx.js new file mode 100644 index 00000000..d0793bf3 --- /dev/null +++ b/lib/plugins/common/cctx.js @@ -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 +} diff --git a/lib/plugins/exchange/cctx/cctx.js b/lib/plugins/exchange/cctx/cctx.js new file mode 100644 index 00000000..91c2153a --- /dev/null +++ b/lib/plugins/exchange/cctx/cctx.js @@ -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 } diff --git a/lib/plugins/ticker/cctx/cctx.js b/lib/plugins/ticker/cctx/cctx.js new file mode 100644 index 00000000..f2ea1d0a --- /dev/null +++ b/lib/plugins/ticker/cctx/cctx.js @@ -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 }