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: allow canonical linking in deep-link #336

Merged
merged 7 commits into from
Oct 10, 2022
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
2 changes: 1 addition & 1 deletion src/components/CoinSelection/useCoinSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function useCoinSelection() {
try {
// Check if Token exists and amount is convertable to Wei
config.getTokenInfoBySymbol(
Number(params.from),
config.resolveChainIdFromNumericOrCanonical(params.from),
params.asset.toUpperCase()
);
toWeiSafe(params.amount);
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useSendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,9 @@ function useSendFormManager(): SendFormManagerContext {
Because we need the asset's decimal value, you need to define **both** asset and amount for the optional params.
*/
useEffect(() => {
const fromChain = Number(params.from);
const toChain = Number(params.to);
const [fromChain, toChain] = [params.from, params.to].map(
config.resolveChainIdFromNumericOrCanonical
);
const areSupportedChains = [fromChain, toChain].every(
config.isSupportedChainId
);
Expand Down
23 changes: 23 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ export class ConfigClient {
constants.isSupportedChainId(chainId) && this.spokeChains.has(chainId)
);
};
getSupportedCanonicalNameAsChainId = (canonicalName?: string) => {
// Returns undefined if the canonicalName is not defined
if (!canonicalName) return;
// Transform the canonical name to match ChainId key
const modifiedCanonicalName = canonicalName.toLowerCase();
// Attempt to resolve the chainId and return
const resolvedChain = constants.CanonicalChainName[modifiedCanonicalName];
return resolvedChain && this.isSupportedChainId(resolvedChain)
? resolvedChain
: undefined;
};
/**
* This function converts either a chainId or canonical name into a corresponding chainId.
* @param chainIdOrCanonical Either a numeric string, an enumerated canonical name, undefined, or an invalid value.
* @returns The chain ID in the valid case. NaN in the invalid case.
*/
resolveChainIdFromNumericOrCanonical = (chainIdOrCanonical?: string) => {
const asNumeric = Number(chainIdOrCanonical);
return Number.isNaN(asNumeric)
? this.getSupportedCanonicalNameAsChainId(chainIdOrCanonical) ??
Number(chainIdOrCanonical)
: asNumeric;
};
// returns token list in order specified by constants, but adds in token address for the chain specified
getTokenList(chainId?: number): TokenList {
const routeTable = Object.fromEntries(
Expand Down
9 changes: 9 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export enum ChainId {
// Polygon testnet
MUMBAI = 80001,
}

// Maps `ChainId` to an object and inverts the Key/Value
// pair. Ex) { "mainnet": 1 }
export const CanonicalChainName = Object.fromEntries(
Object.entries(ChainId)
.filter((v) => Number.isNaN(Number(v[0])))
.map((v) => [v[0].toLowerCase(), Number(v[1])])
);

/* Colors and Media Queries section */
export const BREAKPOINTS = {
tabletMin: 550,
Expand Down