Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,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'
117 changes: 116 additions & 1 deletion src/exchangeRateRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +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 {
asCoinrankReq,
asExchangeRateResponse,
CoinrankRedis,
CoinrankReq
} from './types'
import { asExtendedReq } from './utils/asExtendedReq'
import { DbDoc, hmgetAsync } from './utils/dbUtils'
import { DbDoc, getAsync, hmgetAsync, setAsync } from './utils/dbUtils'
import {
addIso,
fromCode,
Expand All @@ -19,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 @@ -301,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 @@ -346,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))
119 changes: 0 additions & 119 deletions src/providers/nomics.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/rates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
fallbackConstantRates,
zeroRates
} from './providers/hardcodedProviders'
import { nomics } from './providers/nomics'
import { openExchangeRates } from './providers/openExchangeRates'
import { wazirx } from './providers/wazirx'
import {
Expand Down Expand Up @@ -136,7 +135,6 @@ const getRatesFromProviders = async (
coingecko,
coincap,
coinMarketCap,
nomics,
compound,
fallbackConstantRates,
currencyConverter,
Expand Down
Loading