From 843858d4271541c17a2c8326ace337625ee6e660 Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Tue, 7 Nov 2023 12:54:25 -0800 Subject: [PATCH] Update instances of fromToken to resolve potential undefined errors --- .../BridgeTransactionButton.tsx | 2 +- .../StateManagedBridge/InputContainer.tsx | 4 +- .../pages/state-managed-bridge/index.tsx | 39 ++++++++++--------- .../slices/bridge/updater.tsx | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/synapse-interface/components/StateManagedBridge/BridgeTransactionButton.tsx b/packages/synapse-interface/components/StateManagedBridge/BridgeTransactionButton.tsx index 0e53ec0e72..e548846629 100644 --- a/packages/synapse-interface/components/StateManagedBridge/BridgeTransactionButton.tsx +++ b/packages/synapse-interface/components/StateManagedBridge/BridgeTransactionButton.tsx @@ -78,7 +78,7 @@ export const BridgeTransactionButton = ({ let buttonProperties const fromTokenDecimals: number | undefined = - fromToken && fromToken.decimals[fromChainId] + fromToken && fromToken?.decimals[fromChainId] const fromValueBigInt = useMemo(() => { return fromTokenDecimals ? stringToBigInt(fromValue, fromTokenDecimals) : 0 diff --git a/packages/synapse-interface/components/StateManagedBridge/InputContainer.tsx b/packages/synapse-interface/components/StateManagedBridge/InputContainer.tsx index 3664cc389d..04c2ec5e8b 100644 --- a/packages/synapse-interface/components/StateManagedBridge/InputContainer.tsx +++ b/packages/synapse-interface/components/StateManagedBridge/InputContainer.tsx @@ -45,7 +45,7 @@ export const InputContainer = () => { )?.balance useEffect(() => { - if (fromToken && fromToken.decimals[fromChainId]) { + if (fromToken && fromToken?.decimals[fromChainId]) { setShowValue(fromValue) } @@ -76,7 +76,7 @@ export const InputContainer = () => { const onMaxBalance = useCallback(() => { dispatch( updateFromValue( - formatBigIntToString(balance, fromToken.decimals[fromChainId]) + formatBigIntToString(balance, fromToken?.decimals[fromChainId]) ) ) }, [balance, fromChainId, fromToken]) diff --git a/packages/synapse-interface/pages/state-managed-bridge/index.tsx b/packages/synapse-interface/pages/state-managed-bridge/index.tsx index b3ce45da0a..e2f69f874d 100644 --- a/packages/synapse-interface/pages/state-managed-bridge/index.tsx +++ b/packages/synapse-interface/pages/state-managed-bridge/index.tsx @@ -134,7 +134,7 @@ const StateManagedBridge = () => { fromToken && toToken && fromToken?.decimals[fromChainId] && - stringToBigInt(debouncedFromValue, fromToken.decimals[fromChainId]) > 0n + stringToBigInt(debouncedFromValue, fromToken?.decimals[fromChainId]) > 0n ) { console.log('trying to set bridge quote') getAndSetBridgeQuote() @@ -192,7 +192,7 @@ const StateManagedBridge = () => { toChainId, fromToken.addresses[fromChainId], toToken.addresses[toChainId], - stringToBigInt(debouncedFromValue, fromToken.decimals[fromChainId]) + stringToBigInt(debouncedFromValue, fromToken?.decimals[fromChainId]) ) // console.log(`[getAndSetQuote] fromChainId`, fromChainId) @@ -211,28 +211,31 @@ const StateManagedBridge = () => { const toValueBigInt = BigInt(maxAmountOut.toString()) ?? 0n - const originTokenDecimals = fromToken.decimals[fromChainId] + const originTokenDecimals = fromToken?.decimals[fromChainId] const adjustedFeeAmount = BigInt(feeAmount) < - stringToBigInt(`${debouncedFromValue}`, fromToken.decimals[fromChainId]) + stringToBigInt( + `${debouncedFromValue}`, + fromToken?.decimals[fromChainId] + ) ? BigInt(feeAmount) : BigInt(feeAmount) / powBigInt(10n, BigInt(18 - originTokenDecimals)) const isUnsupported = AcceptedChainId[fromChainId] ? false : true const allowance = - fromToken.addresses[fromChainId] === zeroAddress || + fromToken?.addresses[fromChainId] === zeroAddress || address === undefined || isUnsupported ? 0n : await getErc20TokenAllowance({ address, chainId: fromChainId, - tokenAddress: fromToken.addresses[fromChainId] as Address, + tokenAddress: fromToken?.addresses[fromChainId] as Address, spender: routerAddress, }) - if (fromToken.addresses[fromChainId] !== zeroAddress && address) { + if (fromToken?.addresses[fromChainId] !== zeroAddress && address) { dispatch( updateSingleTokenAllowance({ chainId: fromChainId, @@ -275,9 +278,9 @@ const StateManagedBridge = () => { exchangeRate: calculateExchangeRate( stringToBigInt( debouncedFromValue, - fromToken.decimals[fromChainId] + fromToken?.decimals[fromChainId] ) - BigInt(adjustedFeeAmount), - fromToken.decimals[fromChainId], + fromToken?.decimals[fromChainId], toValueBigInt, toToken.decimals[toChainId] ), @@ -293,7 +296,7 @@ const StateManagedBridge = () => { ) toast.dismiss(quoteToast) - const message = `Route found for bridging ${debouncedFromValue} ${fromToken.symbol} on ${CHAINS_BY_ID[fromChainId]?.name} to ${toToken.symbol} on ${CHAINS_BY_ID[toChainId]?.name}` + const message = `Route found for bridging ${debouncedFromValue} ${fromToken?.symbol} on ${CHAINS_BY_ID[fromChainId]?.name} to ${toToken.symbol} on ${CHAINS_BY_ID[toChainId]?.name}` console.log(message) quoteToast = toast(message, { duration: 3000 }) } @@ -311,7 +314,7 @@ const StateManagedBridge = () => { } else if (!toToken) { message = 'Please select a destination token' } else { - message = `No route found for bridging ${debouncedFromValue} ${fromToken.symbol} on ${CHAINS_BY_ID[fromChainId]?.name} to ${toToken.symbol} on ${CHAINS_BY_ID[toChainId]?.name}` + message = `No route found for bridging ${debouncedFromValue} ${fromToken?.symbol} on ${CHAINS_BY_ID[fromChainId]?.name} to ${toToken.symbol} on ${CHAINS_BY_ID[toChainId]?.name}` } console.log(message) quoteToast = toast(message, { duration: 3000 }) @@ -393,22 +396,22 @@ const StateManagedBridge = () => { bridgeQuote.routerAddress, fromChainId, toChainId, - fromToken.addresses[fromChainId as keyof Token['addresses']], - stringToBigInt(debouncedFromValue, fromToken.decimals[fromChainId]), + fromToken?.addresses[fromChainId as keyof Token['addresses']], + stringToBigInt(debouncedFromValue, fromToken?.decimals[fromChainId]), bridgeQuote.quotes.originQuery, bridgeQuote.quotes.destQuery ) const payload = - fromToken.addresses[fromChainId as keyof Token['addresses']] === + fromToken?.addresses[fromChainId as keyof Token['addresses']] === zeroAddress || - fromToken.addresses[fromChainId as keyof Token['addresses']] === '' + fromToken?.addresses[fromChainId as keyof Token['addresses']] === '' ? { data: data.data, to: data.to, value: stringToBigInt( debouncedFromValue, - fromToken.decimals[fromChainId] + fromToken?.decimals[fromChainId] ), } : data @@ -418,7 +421,7 @@ const StateManagedBridge = () => { const originChainName = CHAINS_BY_ID[fromChainId]?.name const destinationChainName = CHAINS_BY_ID[toChainId]?.name pendingPopup = toast( - `Bridging from ${fromToken.symbol} on ${originChainName} to ${toToken.symbol} on ${destinationChainName}`, + `Bridging from ${fromToken?.symbol} on ${originChainName} to ${toToken.symbol} on ${destinationChainName}`, { id: 'bridge-in-progress-popup', duration: Infinity } ) @@ -447,7 +450,7 @@ const StateManagedBridge = () => { const successToastContent = (