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

improve: Add external config #1362

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
a68d5e7
improve: Add external config
evaldofelipe Jan 8, 2025
b0adb9d
add test route
evaldofelipe Jan 8, 2025
0ea8765
update env
evaldofelipe Jan 8, 2025
76ceda7
test
evaldofelipe Jan 8, 2025
366fdec
test
evaldofelipe Jan 8, 2025
c6bc7a4
update env
evaldofelipe Jan 8, 2025
a4a28ff
add pre-build env
evaldofelipe Jan 8, 2025
b231bc1
test
evaldofelipe Jan 8, 2025
7b6b27b
test
evaldofelipe Jan 8, 2025
5a42a29
update
evaldofelipe Jan 8, 2025
13fc109
update test
evaldofelipe Jan 9, 2025
8b93e40
fix
evaldofelipe Jan 9, 2025
7d754e0
test
evaldofelipe Jan 9, 2025
937b85c
update
evaldofelipe Jan 9, 2025
7e2e8bc
test
evaldofelipe Jan 9, 2025
4a26327
test
evaldofelipe Jan 9, 2025
e12c12d
change path
evaldofelipe Jan 9, 2025
51e3dcb
update test
evaldofelipe Jan 9, 2025
0f831e2
fix export
evaldofelipe Jan 9, 2025
9bb75a8
update
evaldofelipe Jan 9, 2025
8c90d4c
update
evaldofelipe Jan 10, 2025
851eb04
test
evaldofelipe Jan 10, 2025
6c3c929
test
evaldofelipe Jan 10, 2025
e5686ea
test tmp
evaldofelipe Jan 10, 2025
c1d6c20
udpate
evaldofelipe Jan 10, 2025
387e441
test
evaldofelipe Jan 10, 2025
ad5591f
test
evaldofelipe Jan 10, 2025
e480a7c
test
evaldofelipe Jan 10, 2025
919e9b8
test
evaldofelipe Jan 10, 2025
51529fe
test
evaldofelipe Jan 10, 2025
6bba313
test
evaldofelipe Jan 10, 2025
d704522
testt
evaldofelipe Jan 10, 2025
fc0cfdf
update
evaldofelipe Jan 10, 2025
4928c36
update
evaldofelipe Jan 10, 2025
085bc05
test
evaldofelipe Jan 10, 2025
cc57876
test
evaldofelipe Jan 10, 2025
47ac5b1
test
evaldofelipe Jan 10, 2025
fc07c1f
test
evaldofelipe Jan 10, 2025
13b235e
update
evaldofelipe Jan 10, 2025
c081d02
update test
evaldofelipe Jan 10, 2025
3207e3e
test
evaldofelipe Jan 10, 2025
8715c0f
test
evaldofelipe Jan 13, 2025
2293c1d
test
evaldofelipe Jan 14, 2025
ab82534
update
evaldofelipe Jan 14, 2025
6e04148
test
evaldofelipe Jan 14, 2025
6080af6
test
evaldofelipe Jan 14, 2025
2c1014c
chore: you're welcome
james-a-morris Jan 15, 2025
ec70f38
test update
mrice32 Jan 15, 2025
b253998
test
mrice32 Jan 15, 2025
f7a6404
Update available-routes.ts
mrice32 Jan 15, 2025
22bd246
Update available-routes.ts
mrice32 Jan 15, 2025
653d944
read dotenv file manually
mrice32 Jan 15, 2025
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ stats.*
/blob-report/
/playwright/.cache/
.cache-synpress

# ENV
output_api.env
output.env
output.log
125 changes: 13 additions & 112 deletions api/available-routes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { VercelResponse } from "@vercel/node";
import { object, assert, Infer, optional, string } from "superstruct";
import {
getLogger,
applyMapFilter,
validAddress,
positiveIntStr,
ENABLED_ROUTES,
handleErrorCondition,
DISABLED_CHAINS_FOR_AVAILABLE_ROUTES,
DISABLED_TOKENS_FOR_AVAILABLE_ROUTES,
} from "./_utils";
import { object, Infer, optional, string } from "superstruct";
import dotenv from "dotenv";
import { validAddress, positiveIntStr } from "./_utils";
import { TypedVercelRequest } from "./_types";
import fs from "fs";
import path from "path";

const AvailableRoutesQueryParamsSchema = object({
originToken: optional(validAddress()),
Expand All @@ -21,112 +15,19 @@ const AvailableRoutesQueryParamsSchema = object({
destinationTokenSymbol: optional(string()),
});

let envPath = path.join(process.cwd(), 'output_api.env');
let envFile = fs.readFileSync(envPath, "utf-8");
dotenv.populate(process.env, dotenv.parse(envFile));

type AvailableRoutesQueryParams = Infer<
typeof AvailableRoutesQueryParamsSchema
>;

const handler = async (
{ query }: TypedVercelRequest<AvailableRoutesQueryParams>,
response: VercelResponse
) => {
const logger = getLogger();
logger.debug({
at: "Routes",
message: "Query data",
query,
const handler = async (_: any, response: VercelResponse) => {
response.status(200).json({
test: process.env.GIT_ENV_EXPORTED,
});
try {
assert(query, AvailableRoutesQueryParamsSchema);

const {
originToken,
destinationToken,
originChainId,
destinationChainId,
originTokenSymbol,
destinationTokenSymbol,
} = query;

const enabledRoutes = applyMapFilter(
ENABLED_ROUTES.routes,
// Filter out elements from the request query parameters
(route: {
originToken: string;
originChainId: number;
destinationChainId: number;
destinationToken: string;
fromTokenSymbol: string;
toTokenSymbol: string;
isNative: boolean;
}) =>
![route.originChainId, route.destinationChainId].some((chainId) =>
DISABLED_CHAINS_FOR_AVAILABLE_ROUTES.includes(String(chainId))
) &&
!DISABLED_TOKENS_FOR_AVAILABLE_ROUTES.some(
(s) => s.toUpperCase() === route.fromTokenSymbol.toUpperCase()
) &&
(!originToken ||
originToken.toLowerCase() === route.originToken.toLowerCase()) &&
(!originChainId || originChainId === String(route.originChainId)) &&
(!destinationChainId ||
destinationChainId === String(route.destinationChainId)) &&
(!destinationToken ||
destinationToken.toLowerCase() ===
route.destinationToken.toLowerCase()) &&
(!originTokenSymbol ||
originTokenSymbol.toUpperCase() ===
route.fromTokenSymbol.toUpperCase()) &&
(!destinationTokenSymbol ||
destinationTokenSymbol.toUpperCase() ===
route.toTokenSymbol.toUpperCase()),
// Create a mapping of enabled routes to a route with the destination token resolved.
(route) => ({
originChainId: route.fromChain,
originToken: route.fromTokenAddress,
destinationChainId: route.toChain,
fromTokenSymbol: route.fromTokenSymbol,
toTokenSymbol: route.toTokenSymbol,
destinationToken: route.toTokenAddress,
isNative: route.isNative,
})
).map((route) => ({
originChainId: route.originChainId,
originToken: route.originToken,
destinationChainId: route.destinationChainId,
destinationToken: route.destinationToken,
originTokenSymbol: route.fromTokenSymbol,
destinationTokenSymbol: route.toTokenSymbol,
isNative: route.isNative,
}));

// Two different explanations for how `stale-while-revalidate` works:

// https://vercel.com/docs/concepts/edge-network/caching#stale-while-revalidate
// This tells our CDN the value is fresh for 6 hours. If a request is repeated within the next 6 hours,
// the previously cached value is still fresh. The header x-vercel-cache present in the response will show the
// value HIT. If the request is repeated up to 6 hours later, the cached value will be stale but
// still render. In the background, a revalidation request will be made to populate the cache with a fresh value.
// x-vercel-cache will have the value STALE until the cache is refreshed.

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
// The response is fresh for 6 hours. After 6 hours it becomes stale, but the cache is allowed to reuse it
// for any requests that are made in the following 6 hours, provided that they revalidate the response in the background.
// Revalidation will make the cache be fresh again, so it appears to clients that it was always fresh during
// that period — effectively hiding the latency penalty of revalidation from them.
// If no request happened during that period, the cache became stale and the next request will revalidate normally.
logger.debug({
at: "Routes",
message: "Response data",
responseJson: enabledRoutes,
});
response.setHeader(
"Cache-Control",
"s-maxage=21600, stale-while-revalidate=21600"
);
response.status(200).json(enabledRoutes);
} catch (error: unknown) {
return handleErrorCondition("available-routes", response, logger, error);
}
return;
};

export default handler;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
"scripts": {
"start": "export REACT_APP_GIT_COMMIT_HASH=$(git rev-parse HEAD) && vite",
"dev": "export REACT_APP_GIT_COMMIT_HASH=$(git rev-parse HEAD) && vite --port $PORT --host",
"build": "yarn prebuild && export REACT_APP_GIT_COMMIT_HASH=$(git rev-parse HEAD) && tsc && vite build",
"build": "yarn prebuild-env && yarn prebuild && echo $GIT_ENV_EXPORTED && export REACT_APP_GIT_COMMIT_HASH=$(git rev-parse HEAD) && tsc && vite build",
"prebuild": "tsx scripts/pre-build.ts",
"prebuild-env": "./scripts/pre-build-env.sh",
"analyze": "yarn build && rollup-plugin-visualizer --open ./bundle-size-analysis.json",
"test": "export REACT_APP_GIT_COMMIT_HASH=$(git rev-parse HEAD) && jest --env jsdom src",
"serve": "vite preview --port 3000",
Expand Down
22 changes: 22 additions & 0 deletions scripts/pre-build-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -o errexit
set -o nounset

BASE_URL="${GIT_ENV_REPO}/across/frontend/outputs/"

if [ -n "${GH_TOKEN}" ]; then

files=(output.env output_api.env)

for file in "${files[@]}"; do
echo "Downloading $file..."
curl -H "Authorization: token ${GH_TOKEN}" -L "${BASE_URL}${file}" -o "./${file}"
cp ./${file} ./api/${file}
cat ./${file}
done
echo "All files downloaded."

else
echo "No env exported"
fi
3 changes: 2 additions & 1 deletion scripts/pre-build.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "./remote-configs/utils";

dotenv.config({
path: [".env.local", ".env.production", ".env"],
path: [".env.local", ".env.production", ".env", "output.env"],
});

const remoteConfigs = {
Expand Down Expand Up @@ -105,6 +105,7 @@ const remoteConfigs = {

(async () => {
console.log("Running pre-build script to fetch remote config files...");
console.log(process.env.GIT_ENV_EXPORTED);

try {
for (const [configType, { fetchFn, localFilePath }] of Object.entries(
Expand Down