Refactored some exchange code

This commit is contained in:
José Oliveira 2021-01-21 11:08:20 +00:00 committed by Josh Harvey
parent 54a231ab60
commit 134eaaa518
20 changed files with 141 additions and 643 deletions

View file

@ -1,49 +0,0 @@
const axios = require('axios')
const _ = require('lodash/fp')
const BN = require('../../../bn')
const common = require('../../common/bitstamp')
exports.NAME = 'Bitstamp'
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.toString())
}
exports.ticker = function ticker (account, fiatCode, cryptoCode) {
if (fiatCode === 'USD' || fiatCode === 'EUR') {
return getCurrencyRates(fiatCode, cryptoCode)
}
return axios.get('https://bitpay.com/rates')
.then(response => {
const fxRates = response.data.data
const usdRate = findCurrency(fxRates, 'USD')
const fxRate = findCurrency(fxRates, fiatCode).div(usdRate)
return getCurrencyRates('USD', 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/' + market, 'GET')
})
.then(r => ({
rates: {
ask: BN(r.ask),
bid: BN(r.bid)
}
}))
}

View file

@ -1,8 +1,8 @@
const ccxt = require('ccxt')
const BN = require('../../../bn')
const BN = require('../../bn')
const axios = require('axios')
const _ = require('lodash/fp')
const common = require('../../common/ccxt')
const common = require('../common/ccxt')
function ticker (exchangeName, fiatCode, cryptoCode) {
const exchange = new ccxt[exchangeName]()

View file

@ -1,54 +0,0 @@
const _ = require('lodash/fp')
const axios = require('axios')
const BN = require('../../../bn')
function getBuyPrice (obj) {
const currencyPair = obj.currencyPair
return axios({
method: 'get',
url: `https://api.coinbase.com/v2/prices/${currencyPair}/buy`,
headers: {'CB-Version': '2017-07-10'}
})
.then(r => r.data)
}
function getSellPrice (obj) {
const currencyPair = obj.currencyPair
return axios({
method: 'get',
url: `https://api.coinbase.com/v2/prices/${currencyPair}/sell`,
headers: {'CB-Version': '2017-07-10'}
})
.then(r => r.data)
}
function ticker (account, fiatCode, cryptoCode) {
return Promise.resolve()
.then(() => {
if (!_.includes(cryptoCode, ['BTC', 'ETH', 'LTC', 'BCH', 'ZEC', 'DASH'])) {
throw new Error('Unsupported crypto: ' + cryptoCode)
}
})
.then(() => {
const currencyPair = `${cryptoCode}-${fiatCode}`
const promises = [
getBuyPrice({currencyPair}),
getSellPrice({currencyPair})
]
return Promise.all(promises)
})
.then(([buyPrice, sellPrice]) => ({
rates: {
ask: BN(buyPrice.data.amount),
bid: BN(sellPrice.data.amount)
}
}))
}
module.exports = {
ticker
}

View file

@ -1,54 +0,0 @@
const axios = require('axios')
const _ = require('lodash/fp')
const BN = require('../../../bn')
const common = require('../../common/itbit')
exports.NAME = 'itBit'
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.toString())
}
exports.ticker = function ticker (account, fiatCode, cryptoCode) {
if (_.includes(fiatCode, ['USD', 'EUR', 'SGD'])) {
return getCurrencyRates(fiatCode, cryptoCode)
}
return axios.get('https://bitpay.com/api/rates')
.then(response => {
const fxRates = response.data
try {
const usdRate = findCurrency(fxRates, 'USD')
const fxRate = findCurrency(fxRates, fiatCode).div(usdRate)
return getCurrencyRates('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 (fiatCode, cryptoCode) {
try {
const market = common.buildMarket(fiatCode, cryptoCode)
return common.request('GET', '/markets/' + market + '/ticker')
.then(r => ({
rates: {
ask: BN(r.ask),
bid: BN(r.bid)
}
}))
} catch (e) {
return Promise.reject(e)
}
}

View file

@ -1,52 +0,0 @@
const axios = require('axios')
const _ = require('lodash/fp')
const BN = require('../../../bn')
const common = require('../../common/kraken')
exports.NAME = 'Kraken'
exports.SUPPORTED_MODULES = ['ticker']
const PAIRS = common.PAIRS
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.toString())
}
exports.ticker = function ticker (account, fiatCode, cryptoCode) {
if (fiatCode === 'USD' || fiatCode === 'EUR') {
return getCurrencyRates(fiatCode, cryptoCode)
}
return axios.get('https://bitpay.com/api/rates')
.then(response => {
const fxRates = response.data
const usdRate = findCurrency(fxRates, 'USD')
const fxRate = findCurrency(fxRates, fiatCode).div(usdRate)
return getCurrencyRates('USD', cryptoCode)
.then(res => ({
rates: {
ask: res.rates.ask.times(fxRate),
bid: res.rates.bid.times(fxRate)
}
}))
})
}
function getCurrencyRates (fiatCode, cryptoCode) {
const pair = PAIRS[cryptoCode][fiatCode]
return axios.get('https://api.kraken.com/0/public/Ticker?pair=' + pair)
.then(function (response) {
const rates = response.data.result[pair]
return {
rates: {
ask: BN(rates.a[0]),
bid: BN(rates.b[0])
}
}
})
}