Skip to content

Commit

Permalink
Update instances of fromToken to resolve potential undefined errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboydiamonds committed Nov 7, 2023
1 parent f5c23ef commit 843858d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const InputContainer = () => {
)?.balance

useEffect(() => {
if (fromToken && fromToken.decimals[fromChainId]) {
if (fromToken && fromToken?.decimals[fromChainId]) {
setShowValue(fromValue)
}

Expand Down Expand Up @@ -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])
Expand Down
39 changes: 21 additions & 18 deletions packages/synapse-interface/pages/state-managed-bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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]
),
Expand All @@ -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 })
}
Expand All @@ -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 })
Expand Down Expand Up @@ -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
Expand All @@ -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 }
)

Expand Down Expand Up @@ -447,7 +450,7 @@ const StateManagedBridge = () => {
const successToastContent = (
<div>
<div>
Successfully initiated bridge from {fromToken.symbol} on{' '}
Successfully initiated bridge from {fromToken?.symbol} on{' '}
{originChainName} to {toToken.symbol} on {destinationChainName}
</div>
<ExplorerToastLink
Expand Down
2 changes: 1 addition & 1 deletion packages/synapse-interface/slices/bridge/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default function Updater(): null {
userInputExists
? debouncedToTokensFromValue
: getDefaultBridgeAmount(fromToken),
fromToken.decimals[fromChainId]
fromToken?.decimals[fromChainId]
),
}
}
Expand Down

0 comments on commit 843858d

Please sign in to comment.