Skip to content

Commit

Permalink
feat: proxy endpoints to eth-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeday committed Jul 3, 2024
1 parent 6c8f0ca commit 48afa96
Show file tree
Hide file tree
Showing 27 changed files with 212 additions and 793 deletions.
5 changes: 0 additions & 5 deletions config/get-secret-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export type SecretConfigType = Modify<

cspReportOnly: boolean;

subgraphRequestTimeout: number;

rateLimit: number;
rateLimitTimeFrame: number;
}
Expand Down Expand Up @@ -49,9 +47,6 @@ export const getSecretConfig = (): SecretConfigType => {

cspReportOnly: toBoolean(serverRuntimeConfig.cspReportOnly),

subgraphRequestTimeout:
Number(serverRuntimeConfig.subgraphRequestTimeout) || 5000,

rateLimit: Number(serverRuntimeConfig.rateLimit) || 100,
rateLimitTimeFrame: Number(serverRuntimeConfig.rateLimitTimeFrame) || 60, // 1 minute;
};
Expand Down
16 changes: 10 additions & 6 deletions consts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ export const enum API_ROUTES {
}

const getEthApiOrigin = (path: string) => {
const { hostname, protocol } = new URL(config.rootOrigin);
return protocol + '//' + 'eth-api.' + hostname + path;
return config.ethAPIBasePath + path;
};

export const getReplacementLink = (
apiRoute: API_ROUTES,
): string | undefined => {
export const getReplacementLink = (apiRoute: API_ROUTES): string => {
switch (apiRoute) {
case API_ROUTES.ETH_APR:
return getEthApiOrigin('/v1/protocol/eth/apr/last');
case API_ROUTES.ETH_PRICE:
return getEthApiOrigin('/v1/protocol/eth/price');
case API_ROUTES.TOTALSUPPLY:
case API_ROUTES.SHORT_LIDO_STATS:
return getEthApiOrigin('/v1/protocol/steth/stats');
case API_ROUTES.SMA_STETH_APR:
return getEthApiOrigin('/v1/protocol/steth/apr/sma');
case API_ROUTES.ONEINCH_RATE:
return getEthApiOrigin('/v1/swap/one-inch');
default:
return;
throw new Error(`No replacement link found for route: ${apiRoute}`);
}
};
7 changes: 0 additions & 7 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,10 @@ declare module 'next/config' {
rpcUrls_17000: string | undefined;
ethplorerApiKey: string | undefined;

oneInchApiKey: string | undefined;

cspTrustedHosts: string | undefined;
cspReportUri: string | undefined;
cspReportOnly: string | undefined;

subgraphMainnet: string | undefined;
subgraphGoerli: string | undefined;
subgraphHolesky: string | undefined;
subgraphRequestTimeout: string | undefined;

rateLimit: string;
rateLimitTimeFrame: string;

Expand Down
7 changes: 0 additions & 7 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,10 @@ export default withBundleAnalyzer({
rpcUrls_17000: process.env.EL_RPC_URLS_17000,
ethplorerApiKey: process.env.ETHPLORER_API_KEY,

oneInchApiKey: process.env.ONE_INCH_API_KEY,

cspTrustedHosts: process.env.CSP_TRUSTED_HOSTS,
cspReportUri: process.env.CSP_REPORT_URI,
cspReportOnly: process.env.CSP_REPORT_ONLY,

subgraphMainnet: process.env.SUBGRAPH_MAINNET,
subgraphGoerli: process.env.SUBGRAPH_GOERLI,
subgraphHolesky: process.env.SUBGRAPH_HOLESKY,
subgraphRequestTimeout: process.env.SUBGRAPH_REQUEST_TIMEOUT,

rateLimit: process.env.RATE_LIMIT,
rateLimitTimeFrame: process.env.RATE_LIMIT_TIME_FRAME,

Expand Down
31 changes: 9 additions & 22 deletions pages/api/eth-apr.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Cache } from 'memory-cache';
import { wrapRequest as wrapNextRequest } from '@lidofinance/next-api-wrapper';

import { API } from 'types';
import { config } from 'config';
import {
API_DEFAULT_SUNSET_TIMESTAMP,
API_ROUTES,
getReplacementLink,
} from 'consts/api';
import {
getEthApr,
errorAndCacheDefaultWrappers,
responseTimeMetric,
rateLimit,
Expand All @@ -18,24 +15,7 @@ import {
HttpMethod,
} from 'utilsApi';
import Metrics from 'utilsApi/metrics';

const cache = new Cache<typeof config.CACHE_ETH_APR_KEY, string>();

// Proxy for third-party API.
// Returns eth annual percentage rate
// TODO: delete after viewing grafana
const ethApr: API = async (_, res) => {
const cachedEthApr = cache.get(config.CACHE_ETH_APR_KEY);

if (cachedEthApr) {
res.json(cachedEthApr);
} else {
const ethApr = await getEthApr();
cache.put(config.CACHE_ETH_APR_KEY, ethApr, config.CACHE_ETH_APR_TTL);

res.json(ethApr);
}
};
import { createEthApiProxy } from 'utilsApi/cached-proxy';

export default wrapNextRequest([
httpMethodGuard([HttpMethod.GET]),
Expand All @@ -46,4 +26,11 @@ export default wrapNextRequest([
replacementLink: getReplacementLink(API_ROUTES.ETH_APR),
}),
...errorAndCacheDefaultWrappers,
])(ethApr);
])(
createEthApiProxy({
cacheTTL: config.CACHE_ETH_APR_TTL,
endpoint: '/v1/protocol/eth/apr/last',
ignoreParams: true,
transformData: (data) => data.data.apr.toFixed(1),
}),
);
42 changes: 18 additions & 24 deletions pages/api/eth-price.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
import { Cache } from 'memory-cache';
import {
wrapRequest as wrapNextRequest,
cacheControl,
} from '@lidofinance/next-api-wrapper';

import { config } from 'config';
import { API_ROUTES } from 'consts/api';
import {
getEthPrice,
API_DEFAULT_SUNSET_TIMESTAMP,
API_ROUTES,
getReplacementLink,
} from 'consts/api';
import {
defaultErrorHandler,
responseTimeMetric,
rateLimit,
httpMethodGuard,
HttpMethod,
sunsetBy,
} from 'utilsApi';
import Metrics from 'utilsApi/metrics';
import { API } from 'types';

const cache = new Cache<typeof config.CACHE_ETH_PRICE_KEY, unknown>();

// Proxy for third-party API.
const ethPrice: API = async (req, res) => {
const cachedEthPrice = cache.get(config.CACHE_ETH_PRICE_KEY);

if (cachedEthPrice) {
res.json(cachedEthPrice);
} else {
const ethPrice = await getEthPrice();
cache.put(
config.CACHE_ETH_PRICE_KEY,
{ price: ethPrice },
config.CACHE_ETH_PRICE_TTL,
);

res.json({ price: ethPrice });
}
};
import { createEthApiProxy } from 'utilsApi/cached-proxy';

export default wrapNextRequest([
httpMethodGuard([HttpMethod.GET]),
rateLimit,
responseTimeMetric(Metrics.request.apiTimings, API_ROUTES.ETH_PRICE),
cacheControl({ headers: config.CACHE_ETH_PRICE_HEADERS }),
sunsetBy({
sunsetTimestamp: API_DEFAULT_SUNSET_TIMESTAMP,
replacementLink: getReplacementLink(API_ROUTES.ETH_PRICE),
}),
defaultErrorHandler,
])(ethPrice);
])(
createEthApiProxy({
cacheTTL: config.CACHE_ETH_PRICE_TTL,
endpoint: '/v1/protocol/eth/price',
ignoreParams: true,
}),
);
2 changes: 1 addition & 1 deletion pages/api/lido-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const cache = new Cache<typeof config.CACHE_LIDO_STATS_KEY, unknown>();
// Proxy for third-party API.
// Returns steth token information
// DEPRECATED: In future will be delete!!!
const lidoStats: API = async (req, res) => {
export const lidoStats: API = async (req, res) => {
const cachedLidoStats = cache.get(config.CACHE_LIDO_STATS_KEY);

if (cachedLidoStats) {
Expand Down
29 changes: 2 additions & 27 deletions pages/api/lidostats.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Cache } from 'memory-cache';
import { wrapRequest as wrapNextRequest } from '@lidofinance/next-api-wrapper';

import { config } from 'config';
import { API_DEFAULT_SUNSET_TIMESTAMP, API_ROUTES } from 'consts/api';
import {
getLidoStats,
responseTimeMetric,
errorAndCacheDefaultWrappers,
rateLimit,
Expand All @@ -13,31 +10,9 @@ import {
HttpMethod,
} from 'utilsApi';
import Metrics from 'utilsApi/metrics';
import { API } from 'types';

const cache = new Cache<typeof config.CACHE_LIDO_STATS_KEY, unknown>();

// Proxy for third-party API.
// Returns steth token information
// Mirror of /api/lido-stats
// DEPRECATED: In future will be delete!!!
const lidoStats: API = async (req, res) => {
const cachedLidoStats = cache.get(config.CACHE_LIDO_STATS_KEY);

if (cachedLidoStats) {
res.json(cachedLidoStats);
} else {
const lidoStats = await getLidoStats();
cache.put(
config.CACHE_LIDO_STATS_KEY,
{ data: lidoStats },
config.CACHE_LIDO_STATS_TTL,
);

res.json({ data: lidoStats });
}
};
import lidoStats from './lido-stats';

// Mirror for /lido-stats
export default wrapNextRequest([
httpMethodGuard([HttpMethod.GET]),
rateLimit,
Expand Down
Loading

0 comments on commit 48afa96

Please sign in to comment.