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

feat: markets proxy #993

Merged
merged 4 commits into from
May 9, 2024
Merged
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
40 changes: 36 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,7 @@ aliases:

- &proxy
assetName: proxy
environment: infra
pulumi-stack: public-infra-us-east-2
pulumi-stack: public-us-east-2
pulumi-dir: proxy/pulumi
api-autoscaling: true
api-replicas: 2
Expand All @@ -700,6 +699,15 @@ aliases:
api-memory-limit: 1Gi
api-memory-request: 500Mi

- &proxy-dev
<<: *proxy
assetName: proxy
environment: dev
pulumi-stack: public-dev-us-east-2
api-replicas: 1
api-max-replicas: 2
api-memory-limit: 500Mi

commands:
precheck:
description: install dependencies
Expand Down Expand Up @@ -1235,12 +1243,12 @@ workflows:
<<: [*thorchain-dev, *only-develop]

- deploy-coinstack-node:
name: deploy proxy
name: deploy proxy develop
organization: TAXISTAKE
pulumi-command: up -f --yes
requires:
- deploy dependencies
<<: [*proxy, *only-develop]
<<: [*proxy-dev, *only-develop]

- deploy-monitoring-pulumi:
name: deploy monitoring pulumi
Expand Down Expand Up @@ -1582,6 +1590,30 @@ workflows:
- approve thorchain
<<: [*thorchain, *only-main]

####### PROXY
- deploy-coinstack-node:
name: preview proxy
organization: TAXISTAKE
pulumi-command: preview
requires:
- validate dependencies
<<: [*proxy, *only-main]

- approve-coinstack:
name: approve proxy
type: approval
requires:
- preview proxy
<<: *only-main

- deploy-coinstack-node:
name: deploy proxy
organization: TAXISTAKE
pulumi-command: up -f --yes
requires:
- approve proxy
<<: [*proxy, *only-main]

deploy-fallback:
jobs:
- lint-and-test-persist:
Expand Down
4 changes: 4 additions & 0 deletions node/proxy/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import swaggerUi from 'swagger-ui-express'
import { middleware } from '@shapeshiftoss/common-api'
import { Logger } from '@shapeshiftoss/logger'
import { RegisterRoutes } from './routes'
import { CoinGecko } from './coingecko'

const PORT = process.env.PORT ?? 3000

Expand Down Expand Up @@ -31,6 +32,9 @@ app.use('/docs', swaggerUi.serve, swaggerUi.setup(undefined, options))

RegisterRoutes(app)

const coingecko = new CoinGecko()
app.get('/api/v1/markets/*', coingecko.handler.bind(coingecko))

// redirect any unmatched routes to docs
app.get('/', async (_, res) => {
res.redirect('/docs')
Expand Down
56 changes: 56 additions & 0 deletions node/proxy/api/src/coingecko.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import axios, { AxiosResponse, isAxiosError } from 'axios'
import { Axios } from 'axios'
import { Request, Response } from 'express'

const COINGECKO_API_KEY = process.env.COINGECKO_API_KEY

if (!COINGECKO_API_KEY) throw new Error('COINGECKO_API_KEY env var not set')

const CACHE_TTL_MS = 60_000

type RequestCache = Partial<Record<string, AxiosResponse>>

export class CoinGecko {
private axiosInstance: Axios
private requestCache: RequestCache

constructor() {
this.requestCache = {}
this.axiosInstance = axios.create({
baseURL: 'https://pro-api.coingecko.com/api/v3/',
headers: {
'x-cg-pro-api-key': COINGECKO_API_KEY,
},
})
}

async handler(req: Request, res: Response): Promise<void> {
const url = req.url.substring('/api/v1/markets/'.length)

const cachedResponse = this.requestCache[req.url]
if (cachedResponse) {
Object.entries(cachedResponse.headers).forEach(([k, v]) => res.set(k, v))
res.set('X-Cache', 'HIT').status(cachedResponse.status).send(cachedResponse.data)
return
}

res.set('X-Cache', 'MISS')

try {
const response = await this.axiosInstance.get(url)
this.requestCache[req.url] = response
Object.entries(response.headers).forEach(([k, v]) => res.set(k, v))
res.status(response.status).send(response.data)
} catch (err) {
if (isAxiosError(err)) {
res.status(err.response?.status ?? 500).send(err.response?.data || 'Internal Server Error')
} else if (err instanceof Error) {
res.status(500).send(err.message || 'Internal Server Error')
} else {
res.status(500).send('Internal Server Error')
}
} finally {
setInterval(() => delete this.requestCache[req.url], CACHE_TTL_MS)
}
}
}
3 changes: 2 additions & 1 deletion node/proxy/sample.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# SECRET ENVIRONMENT VARIABLES
ELLIPTIC_API_KEY=
ELLIPTIC_API_SECRET=
ELLIPTIC_API_SECRET=
COINGECKO_API_KEY=