Skip to content

Commit

Permalink
feat: custom bridge routes (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-a-morris authored Dec 10, 2024
1 parent 2a3e5d1 commit dc7d842
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
generalMaintenanceMessage,
stringValueInArray,
getConfig,
chainEndpointToId,
} from "utils";
import lazyWithRetry from "utils/lazy-with-retry";
import { ReactComponent as InfoLogo } from "assets/icons/info.svg";
Expand Down Expand Up @@ -187,6 +188,18 @@ const Routes: React.FC = () => {
search: location.search,
}}
/>
{Object.entries(chainEndpointToId).map(([chainName, chainId]) => (
<Route
key={chainId}
exact
path={`/${chainName}`}
render={() => <Send preferredChainId={chainId} />}
/>
))}
<Route
path="*"
render={() => <NotFound custom404Message="page not found" />}
/>
</Switch>
</Suspense>
<Toast position="top-right" />
Expand Down
6 changes: 6 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ export function getChainInfo(chainId: number): ChainInfo {
return chainInfo;
}

export const chainEndpointToId = Object.fromEntries(
chainInfoList.map((chain) => {
return [chain.name.toLowerCase().replaceAll(" ", ""), chain.chainId];
}, [])
);

// For destination chains with no native ETH support, we will send WETH even if the receiver is an EOA
export const nonEthChains = [
ChainId.POLYGON,
Expand Down
13 changes: 9 additions & 4 deletions src/views/Bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
interchangeableTokensMap,
nonEthChains,
GetBridgeFeesResult,
chainEndpointToId,
} from "utils";
import { SwapQuoteApiResponse } from "utils/serverless-api/prod/swap-quote";

Expand Down Expand Up @@ -146,7 +147,7 @@ const defaultRouteFilter = {
};

export function getInitialRoute(filter: RouteFilter = {}) {
const routeFromQueryParams = getRouteFromQueryParams(filter);
const routeFromUrl = getRouteFromUrl(filter);
const routeFromFilter = findEnabledRoute({
inputTokenSymbol:
filter.inputTokenSymbol ??
Expand All @@ -161,7 +162,7 @@ export function getInitialRoute(filter: RouteFilter = {}) {
...enabledRoutes[0],
type: "bridge",
};
return routeFromQueryParams ?? routeFromFilter ?? defaultRoute;
return routeFromUrl ?? routeFromFilter ?? defaultRoute;
}

export function findEnabledRoute(
Expand Down Expand Up @@ -373,9 +374,12 @@ export function getAllChains() {
});
}

export function getRouteFromQueryParams(overrides?: RouteFilter) {
export function getRouteFromUrl(overrides?: RouteFilter) {
const params = new URLSearchParams(window.location.search);

const preferredToChainId =
chainEndpointToId[window.location.pathname.substring(1)];

const fromChain =
Number(
params.get("from") ??
Expand All @@ -386,7 +390,8 @@ export function getRouteFromQueryParams(overrides?: RouteFilter) {

const toChain =
Number(
params.get("to") ??
preferredToChainId ??
params.get("to") ??
params.get("toChain") ??
params.get("destinationChainId") ??
overrides?.toChain
Expand Down

0 comments on commit dc7d842

Please sign in to comment.