Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump express from 4.17.1 to 4.17.3 #87

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@babel/preset-react": "^7.8.3",
"@types/chai": "^4.2.9",
"@types/mocha": "^7.0.1",
"@types/node-fetch": "^2.6.2",
"@types/react": "^16.9.22",
"@types/react-dom": "^16.9.5",
"@typescript-eslint/eslint-plugin": ">=2.0.0",
Expand Down Expand Up @@ -59,7 +60,7 @@
"compression": "^1.7.4",
"cors": "^2.8.5",
"edge-server-tools": "^0.2.10",
"express": "^4.17.1",
"express": "^4.17.3",
"morgan": "^1.10.0",
"nano": "10.0.0",
"node-fetch": "^2.6.0",
Expand Down
45 changes: 45 additions & 0 deletions src/coinrankEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import fetch from 'node-fetch'

import { config } from './config'
import { REDIS_COINRANK_KEY_PREFIX } from './constants'
import { asCoingeckoMarkets, CoinrankMarkets, CoinrankRedis } from './types'
import { setAsync, slackMessage } from './utils/dbUtils'
import { logger, snooze } from './utils/utils'

const PAGE_SIZE = 250
const LOOP_DELAY = 45000
const { defaultFiatCode } = config

export const coinrankEngine = async (): Promise<void> => {
logger('Updating Coinrank Cache')
try {
const lastUpdate = new Date().toISOString()
const { uri } = config.providers.coingecko
let markets: CoinrankMarkets = []
for (let page = 1; page <= 8; page++) {
const url = `${uri}/api/v3/coins/markets?vs_currency=USD&page=${page}&per_page=${PAGE_SIZE}&price_change_percentage=1h,24h,7d,14d,30d,1y`
const response = await fetch(url)
if (response.ok === false) {
const text = await response.text()
throw new Error(text)
}
const reply = await response.json()
const marketsPage = asCoingeckoMarkets(reply)
markets = [...markets, ...marketsPage]
}
const data: CoinrankRedis = { lastUpdate, markets }
await setAsync(
`${REDIS_COINRANK_KEY_PREFIX}_${defaultFiatCode}`,
JSON.stringify(data)
)
} catch (e) {
const err: any = e // Weird TS issue causing :any to get removed from above line
const message = `coinrankEngine failure: ${err.message}`
slackMessage(message).catch(e => logger(e))
logger(message)
} finally {
logger('COINRANK ENGINE SNOOZING **********************')
await snooze(LOOP_DELAY)
coinrankEngine().catch(e => logger(e))
}
}
9 changes: 0 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {
COIN_MARKET_CAP_HISTORICAL_API_KEY = '',
SLACK_WEBHOOK_URL = '',
OPEN_EXCHANGE_RATES_API_KEY,
NOMICS_API_KEY,
DEFAULT_FIAT = 'iso:USD'
} = process.env

Expand All @@ -38,10 +37,6 @@ const proivderDefaults = {
uri: 'https://openexchangerates.org',
apiKey: OPEN_EXCHANGE_RATES_API_KEY
},
nomics: {
uri: 'https://api.nomics.com',
apiKey: NOMICS_API_KEY
},
coingecko: {
uri: 'https://api.coingecko.com'
},
Expand Down Expand Up @@ -129,10 +124,6 @@ export const asConfig = asObject({
uri: asString,
apiKey: asString
}),
nomics: asObject({
uri: asString,
apiKey: asString
}),
coingecko: asObject({
uri: asString
}),
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const REDIS_COINRANK_KEY_PREFIX = 'coinrank'
118 changes: 116 additions & 2 deletions src/exchangeRateRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { mul } from 'biggystring'
import { asArray, asMaybe, asObject, asOptional, asString } from 'cleaners'
import express from 'express'
import nano from 'nano'
import fetch from 'node-fetch'
import promisify from 'promisify-node'

import { config } from './config'
import { REDIS_COINRANK_KEY_PREFIX } from './constants'
import { asReturnGetRate, getExchangeRates } from './rates'
import { hmgetAsync } from './uidEngine'
import {
asCoinrankReq,
asExchangeRateResponse,
CoinrankRedis,
CoinrankReq
} from './types'
import { asExtendedReq } from './utils/asExtendedReq'
import { DbDoc } from './utils/dbUtils'
import { DbDoc, getAsync, hmgetAsync, setAsync } from './utils/dbUtils'
import {
addIso,
fromCode,
Expand All @@ -20,6 +27,8 @@ import {
toIsoPair
} from './utils/utils'

const { defaultFiatCode } = config
const EXPIRE_TIME = 60000
export interface ExchangeRateReq {
currency_pair: string
date: string
Expand Down Expand Up @@ -302,6 +311,109 @@ const sendExchangeRates: express.RequestHandler = (req, res, next): void => {
res.json({ data: exReq.requestedRatesResult.data })
}

const sendCoinranks: express.RequestHandler = async (
req,
res,
next
): Promise<void> => {
const { ratesServerAddress } = config
const now = new Date()
const nowTimestamp = now.getTime()

const exReq = req as ExpressRequest
if (exReq == null) return next(500)

let query: CoinrankReq
try {
query = asCoinrankReq(req.query)
} catch (e) {
res
.status(400)
.send(`Error: ${e instanceof Error ? e.message : 'Unknown error'}`)
return
}
const { fiatCode, start, length } = query

try {
if (start < 1 || start > 2000) {
res
.status(400)
.send(`Invalid start param: ${start}. Must be between 1-2000`)
return
}
if (length < 1 || length > 100) {
res
.status(400)
.send(`Invalid length param: ${length}. Must be between 1-100`)
return
}
const jsonString = await getAsync(
`${REDIS_COINRANK_KEY_PREFIX}_${fiatCode}`
)

let redisResult: CoinrankRedis = JSON.parse(jsonString)

if (fiatCode !== defaultFiatCode) {
const lastUpdated =
redisResult != null ? new Date(redisResult.lastUpdate).getTime() : 0

// If no result in redis or it's out of date
if (redisResult == null || nowTimestamp - lastUpdated > EXPIRE_TIME) {
// Attempt to scale prices by foreign exchange rate

// Get exchange rate
const result = await fetch(
`${ratesServerAddress}/v2/exchangeRate?currency_pair=${defaultFiatCode}_${fiatCode}`
)
const resultJson = await result.json()
const { exchangeRate } = asExchangeRateResponse(resultJson)
const rate = Number(exchangeRate)

// Get USD rankings
const jsonString = await getAsync(
`${REDIS_COINRANK_KEY_PREFIX}_${defaultFiatCode}`
)
redisResult = JSON.parse(jsonString)
let { markets } = redisResult

// Modify the prices, mcap, & volume
markets = markets.map(m => ({
...m,
price: m.price * rate,
marketCap: m.marketCap * rate,
volume24h: m.volume24h * rate
}))
const data = markets.slice(start - 1, start + length)

res.json({ data })

// Update redis cache
const redisData: CoinrankRedis = {
markets,
lastUpdate: now.toISOString()
}
await setAsync(
`${REDIS_COINRANK_KEY_PREFIX}_${fiatCode}`,
JSON.stringify(redisData)
)
return
}
}
if (redisResult == null) {
res.status(400).send(`Unable to get results for fiatCode ${fiatCode}`)
return
}

const { markets } = redisResult
const data = markets.slice(start - 1, start + length)

res.json({ data })
} catch (e) {
const err: any = e
res.status(500).send(err.message)
}
}

// *** ROUTES ***

export const exchangeRateRouterV1 = (): express.Router => {
Expand Down Expand Up @@ -347,6 +459,8 @@ export const exchangeRateRouterV2 = (): express.Router => {
sendExchangeRates
])

router.get('/coinrank', [sendCoinranks])

return router
}

Expand Down
2 changes: 2 additions & 0 deletions src/indexEngines.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setupDatabase } from 'edge-server-tools'

import { coinrankEngine } from './coinrankEngine'
import { config } from './config'
import { ratesEngine } from './ratesEngine'
import { uidEngine } from './uidEngine'
Expand All @@ -11,6 +12,7 @@ async function initDb(): Promise<void> {
await setupDatabase(config.couchUri, ratesDbSetup)
ratesEngine().catch(e => logger('ratesEnginee failure', e))
uidEngine().catch(e => logger('uidEnginee failure', e))
coinrankEngine().catch(e => logger('uidEnginee failure', e))
}

initDb().catch(e => logger('initDbe failure', e))
3 changes: 2 additions & 1 deletion src/providers/coinMarketCap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ export const coinMarketCapAssets = async (): Promise<AssetMap> => {
continue // retry
}
if (response.ok === false) {
throw new Error(response.status)
const text = await response.text()
throw new Error(text)
}

return assetMapReducer(
Expand Down
13 changes: 8 additions & 5 deletions src/providers/coincap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ const currentQuery = async (
const response = await fetch(url, OPTIONS)
const json = asCoincapCurrentResponse(await response.json())
if (response.ok === false) {
const text = await response.text()
logger(
`coincapCurrent returned code ${response.status} for ${codes} at ${date}`
`coincapCurrent returned code ${response.status} for ${codes} at ${date}: ${text}`
)
throw new Error(response.status)
throw new Error(text)
}

// Add to return object
Expand Down Expand Up @@ -105,10 +106,11 @@ const historicalQuery = async (
OPTIONS
)
if (response.ok === false) {
const text = await response.text()
logger(
`coincapHistorical returned code ${response.status} for ${id} at ${date}`
`coincapHistorical returned code ${response.status.toString()} for ${id} at ${date}: ${text}`
)
throw new Error(response.status)
throw new Error(text)
}
const json = asCoincapHistoricalResponse(await response.json())
if (json.data.length === 0)
Expand Down Expand Up @@ -179,7 +181,8 @@ export const coincapAssets = async (): Promise<AssetMap> => {
continue // retry
}
if (response.ok === false) {
throw new Error(response.status)
const text = await response.text()
throw new Error(text)
}
return assetMapReducer(asCoincapAssetResponse(await response.json()).data)
}
Expand Down
Loading