Skip to content

Commit

Permalink
improve: allow canonical linking in deep-link (#336)
Browse files Browse the repository at this point in the history
* feat: ✨ crate a mapping of string to numeric chain id

* feat: ✨ add fns to resolve canonical name

* improve: enable canonical name linking in from/to

* fix: remove typecast

* improve: hoist undefined logic into calling fn

* improve: add conditional

* improve: add doc comment
  • Loading branch information
james-a-morris authored Oct 10, 2022
1 parent 9011e8e commit c9d393b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
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 @@ -141,6 +141,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

2 comments on commit c9d393b

@vercel
Copy link

@vercel vercel bot commented on c9d393b Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

goerli-frontend-v2 – ./

goerli-frontend-v2.vercel.app
goerli-frontend-v2-uma.vercel.app
goerli-frontend-v2-git-master-uma.vercel.app

@vercel
Copy link

@vercel vercel bot commented on c9d393b Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.