Skip to content

Commit

Permalink
fixup! Filter out zeroRates and fallbackConstantRates
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodluckH committed Jul 19, 2022
1 parent a3398cd commit 37ac356
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
75 changes: 73 additions & 2 deletions src/exchangeRateRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import nano from 'nano'
import promisify from 'promisify-node'

import { config } from './config'
import { asReturnGetRate, getExchangeRates } from './rates'
import { hmgetAsync } from './uidEngine'
import {
fallbackConstantRates,
zeroRates
} from './providers/hardcodedProviders'
import {
asReturnGetRate,
AssetMap,
getExchangeRates,
NewRates,
ReturnRate
} from './rates'
import { hgetallAsync, hmgetAsync } from './uidEngine'
import { asExtendedReq } from './utils/asExtendedReq'
import currencyCodeMaps from './utils/currencyCodeMaps.json'
import { DbDoc } from './utils/dbUtils'
import {
addIso,
Expand Down Expand Up @@ -302,6 +313,58 @@ const sendExchangeRates: express.RequestHandler = (req, res, next): void => {
res.json({ data: exReq.requestedRatesResult.data })
}

/**
* Produces an Express request handler to be used as a middleware. The
* handler will get rates from the provider and store them in
* `req.requestedRatesResult.data`.
*
* @remarks
* Currenlty, this function is used to query rates from
* {@link zeroRates} and {@link fallbackConstantRates} providers. The
* returned middleware for `zeroRates` will be used before
* {@link queryRedis} and `fallbackConstantRates` after
* {@link queryExchangeRates} so that
* the rates won't be stored in redis and couchdb.
*
* @param provider The provider function that will return a NewRates
* object
* @returns An Express request handler to be used as a middleware.
*/
const queryRatesFromProvder = (
provider: (
rateObj: ReturnRate[],
currentTime: string,
assetMap: AssetMap
) => NewRates
): express.RequestHandler => async (req, res, next): Promise<void> => {
const rateReq = req as ExpressRequest
if (rateReq?.requestedRatesResult == null) return next(500)

const currentTime = normalizeDate(new Date().toISOString())
if (typeof currentTime !== 'string') throw new Error('malformed date')

let constantCurrencyCodes = await hgetallAsync('constantCurrencyCodes')
if (constantCurrencyCodes == null)
constantCurrencyCodes = JSON.parse(currencyCodeMaps.toString())

const result = provider(
rateReq.requestedRatesResult?.data ?? [],
currentTime,
constantCurrencyCodes
)

for (const date of Object.keys(result)) {
const pairs = result[date]
for (const pair of Object.keys(pairs)) {
rateReq.requestedRatesResult.data.push({
currency_pair: pair,
date,
exchangeRate: pairs[pair]
})
}
}
}

// *** ROUTES ***

export const exchangeRateRouterV1 = (): express.Router => {
Expand All @@ -311,8 +374,10 @@ export const exchangeRateRouterV1 = (): express.Router => {
exchangeRateCleaner,
v1IsoChecker,
v1ExchangeRateIsoAdder,
queryRatesFromProvder(zeroRates),
queryRedis,
queryExchangeRates,
queryRatesFromProvder(fallbackConstantRates),
v1ExchangeRateIsoSubtractor,
sendExchangeRate
])
Expand All @@ -321,8 +386,10 @@ export const exchangeRateRouterV1 = (): express.Router => {
exchangeRatesCleaner,
v1IsoChecker,
v1ExchangeRateIsoAdder,
queryRatesFromProvder(zeroRates),
queryRedis,
queryExchangeRates,
queryRatesFromProvder(fallbackConstantRates),
v1ExchangeRateIsoSubtractor,
sendExchangeRates
])
Expand All @@ -335,15 +402,19 @@ export const exchangeRateRouterV2 = (): express.Router => {

router.get('/exchangeRate', [
exchangeRateCleaner,
queryRatesFromProvder(zeroRates),
queryRedis,
queryExchangeRates,
queryRatesFromProvder(fallbackConstantRates),
sendExchangeRate
])

router.post('/exchangeRates', [
exchangeRatesCleaner,
queryRatesFromProvder(zeroRates),
queryRedis,
queryExchangeRates,
queryRatesFromProvder(fallbackConstantRates),
sendExchangeRates
])

Expand Down
19 changes: 4 additions & 15 deletions src/rates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import { coinMarketCap } from './providers/coinMarketCap'
import { coinmonitor } from './providers/coinmonitor'
import { compound } from './providers/compound'
import { currencyConverter } from './providers/currencyConverter'
import {
fallbackConstantRates,
zeroRates
} from './providers/hardcodedProviders'
import { nomics } from './providers/nomics'
import { openExchangeRates } from './providers/openExchangeRates'
import { wazirx } from './providers/wazirx'
Expand Down Expand Up @@ -114,25 +110,22 @@ const sanitizeNewRates = (
return rateMap
}

const getRatesFromProviders = async (
export const getRatesFromProviders = async (
rateObj: ReturnGetRate,
edgeAssetMap: DbDoc,
excludedProviders: string[] = []
edgeAssetMap: DbDoc
): Promise<ReturnGetRate> => {
const currentTime = normalizeDate(new Date().toISOString())
if (typeof currentTime !== 'string') throw new Error('malformed date')

// Retrieve new rates
const rateProviders = [
zeroRates,
coinmonitor,
wazirx,
coingecko,
coincap,
coinMarketCap,
nomics,
compound,
fallbackConstantRates,
currencyConverter,
openExchangeRates
]
Expand All @@ -141,10 +134,7 @@ const getRatesFromProviders = async (
if (constantCurrencyCodes == null)
({ constantCurrencyCodes = {} } = edgeAssetMap)

const eligibleProviders = rateProviders.filter(
provider => !excludedProviders.includes(provider.name)
)
for (const provider of eligibleProviders) {
for (const provider of rateProviders) {
const remainingRequests = getNullRateArray(rateObj.data)
if (remainingRequests.length === 0) break

Expand Down Expand Up @@ -200,8 +190,7 @@ export const getExchangeRates = async (

const out = await getRatesFromProviders(
{ data, documents: documents.slice(1) },
documents[0],
['zeroRates', 'fallbackConstantRates']
documents[0]
)

const redisPromises: Array<Promise<number>> = []
Expand Down

0 comments on commit 37ac356

Please sign in to comment.