From 05e536fe9b401709c72fe9a1ed5d7c7843ee2a1e Mon Sep 17 00:00:00 2001 From: James Morris Date: Fri, 30 Sep 2022 18:31:19 -0400 Subject: [PATCH 1/7] feat: :sparkles: crate a mapping of string to numeric chain id --- src/utils/constants.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/utils/constants.ts b/src/utils/constants.ts index faa550dce..d3a91493d 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -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])]) +) as Record; + /* Colors and Media Queries section */ export const BREAKPOINTS = { tabletMin: 550, From cd55d82d513aa7e619d3c55e3591250949da8fde Mon Sep 17 00:00:00 2001 From: James Morris Date: Fri, 30 Sep 2022 18:31:53 -0400 Subject: [PATCH 2/7] feat: :sparkles: add fns to resolve canonical name --- src/utils/config.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/utils/config.ts b/src/utils/config.ts index ff187f2c5..8401e9037 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -139,6 +139,21 @@ export class ConfigClient { constants.isSupportedChainId(chainId) && this.spokeChains.has(chainId) ); }; + getSupportedCanonicalNameAsChainId = (canonicalName: string) => { + // 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; + }; + resolveChainIdFromNumericOrCanonical = (chainIdOrCanonical?: string) => { + return chainIdOrCanonical + ? this.getSupportedCanonicalNameAsChainId(chainIdOrCanonical) ?? + Number(chainIdOrCanonical) + : Number(chainIdOrCanonical); + }; // 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( From 9e689dfcdbecb4adb1674a3ee20dfd2debf6913c Mon Sep 17 00:00:00 2001 From: James Morris Date: Fri, 30 Sep 2022 18:32:27 -0400 Subject: [PATCH 3/7] improve: enable canonical name linking in from/to --- src/components/CoinSelection/useCoinSelection.tsx | 2 +- src/hooks/useSendForm.tsx | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/CoinSelection/useCoinSelection.tsx b/src/components/CoinSelection/useCoinSelection.tsx index 38d0bc18a..3d9645be8 100644 --- a/src/components/CoinSelection/useCoinSelection.tsx +++ b/src/components/CoinSelection/useCoinSelection.tsx @@ -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); diff --git a/src/hooks/useSendForm.tsx b/src/hooks/useSendForm.tsx index a9e1a3b8a..3af7332cb 100644 --- a/src/hooks/useSendForm.tsx +++ b/src/hooks/useSendForm.tsx @@ -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 ); From 6fad86d5cac3c1751619cbbce6a552135d962699 Mon Sep 17 00:00:00 2001 From: James Morris Date: Mon, 3 Oct 2022 09:13:39 -0400 Subject: [PATCH 4/7] fix: remove typecast --- src/utils/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/constants.ts b/src/utils/constants.ts index d3a91493d..893adf9d8 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -38,7 +38,7 @@ export const CanonicalChainName = Object.fromEntries( Object.entries(ChainId) .filter((v) => Number.isNaN(Number(v[0]))) .map((v) => [v[0].toLowerCase(), Number(v[1])]) -) as Record; +); /* Colors and Media Queries section */ export const BREAKPOINTS = { From f10db643615b188fbd39d9ad28c80202c5639156 Mon Sep 17 00:00:00 2001 From: James Morris Date: Mon, 3 Oct 2022 09:34:19 -0400 Subject: [PATCH 5/7] improve: hoist undefined logic into calling fn --- src/utils/config.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/utils/config.ts b/src/utils/config.ts index 8401e9037..60d3ac4c6 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -139,7 +139,9 @@ export class ConfigClient { constants.isSupportedChainId(chainId) && this.spokeChains.has(chainId) ); }; - getSupportedCanonicalNameAsChainId = (canonicalName: string) => { + 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 @@ -149,10 +151,10 @@ export class ConfigClient { : undefined; }; resolveChainIdFromNumericOrCanonical = (chainIdOrCanonical?: string) => { - return chainIdOrCanonical - ? this.getSupportedCanonicalNameAsChainId(chainIdOrCanonical) ?? - Number(chainIdOrCanonical) - : Number(chainIdOrCanonical); + return ( + this.getSupportedCanonicalNameAsChainId(chainIdOrCanonical) ?? + Number(chainIdOrCanonical) + ); }; // returns token list in order specified by constants, but adds in token address for the chain specified getTokenList(chainId?: number): TokenList { From 2c79bf89e3d08620a068ed888e2776be313eecd6 Mon Sep 17 00:00:00 2001 From: James Morris Date: Fri, 7 Oct 2022 18:34:51 -0400 Subject: [PATCH 6/7] improve: add conditional --- src/utils/config.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/utils/config.ts b/src/utils/config.ts index 60d3ac4c6..2d4d309b8 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -151,10 +151,11 @@ export class ConfigClient { : undefined; }; resolveChainIdFromNumericOrCanonical = (chainIdOrCanonical?: string) => { - return ( - this.getSupportedCanonicalNameAsChainId(chainIdOrCanonical) ?? - Number(chainIdOrCanonical) - ); + 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 { From e0d9671544967f277f21f00f9812db2e7738b942 Mon Sep 17 00:00:00 2001 From: James Morris Date: Fri, 7 Oct 2022 18:40:48 -0400 Subject: [PATCH 7/7] improve: add doc comment --- src/utils/config.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/config.ts b/src/utils/config.ts index 2d4d309b8..a3fc7b402 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -150,6 +150,11 @@ export class ConfigClient { ? 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)