fix: sanity checks on ccxt responses

This commit is contained in:
Rafael Taranto 2023-10-31 18:25:59 +00:00
parent 7b96ed966a
commit 3fe3fed203

View file

@ -8,6 +8,14 @@ const RETRIES = 2
const tickerObjects = {}
// This is probably fixed on upstream ccxt
// but we need to udpate node to get on the latest version
const sanityCheckRates = (ask, bid, tickerName) => {
if (new BN(0).eq(ask) || new BN(0).eq(bid)) {
throw new Error(`Failure fetching rates for ${tickerName}`)
}
}
function ticker (fiatCode, cryptoCode, tickerName) {
if (!tickerObjects[tickerName]) {
tickerObjects[tickerName] = new ccxt[tickerName]({
@ -45,12 +53,15 @@ function getCurrencyRates (ticker, fiatCode, cryptoCode) {
}
const symbol = buildMarket(fiatCode, cryptoCode, ticker.id)
return ticker.fetchTicker(symbol)
.then(res => ({
rates: {
ask: new BN(res.ask),
bid: new BN(res.bid)
.then(res => {
sanityCheckRates(res.ask, res.bid, cryptoCode)
return {
rates: {
ask: new BN(res.ask),
bid: new BN(res.bid)
}
}
}))
})
} catch (e) {
return Promise.reject(e)
}