add coinbase ticker

This commit is contained in:
Josh Harvey 2017-07-10 12:03:04 +03:00
parent d28b21a6d7
commit 97bab49e15
5 changed files with 177 additions and 6 deletions

View file

@ -1,5 +1,5 @@
const BN = require('../../../bn')
const common = require('../common/bitstamp')
const common = require('../../common/bitstamp')
function ticker (account, fiatCode, cryptoCode) {
return Promise.resolve()

View file

@ -0,0 +1,55 @@
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'])) {
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
}