-
Notifications
You must be signed in to change notification settings - Fork 197
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
fix: update switch chain logic #1916
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
We should update this code, because |
@@ -105,7 +116,8 @@ export function TransferPanel() { | |||
const { switchNetworkAsync } = useSwitchNetworkWithConfig({ | |||
isSwitchingNetworkBeforeTx: true | |||
}) | |||
const chainId = useChainId() | |||
// do not use `useChainId` because it won't detect chains outside of our wagmi config | |||
const latestChain = useLatest(useNetwork()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because we use it in an async function, we must use useLatest
to ensure it's up-to-date
if (!isTransferAllowed) { | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
additional safeguard
packages/arb-token-bridge-ui/src/components/TransferPanel/TransferPanel.tsx
Outdated
Show resolved
Hide resolved
try { | ||
setTransferring(true) | ||
if (isConnectedToTheWrongChain) { | ||
trackEvent('Switch Network and Transfer', { | ||
type: isTeleportMode | ||
? 'Teleport' | ||
: isDepositMode | ||
? 'Deposit' | ||
: 'Withdrawal', | ||
tokenSymbol: selectedToken?.symbol, | ||
assetType: selectedToken ? 'ERC-20' : 'ETH', | ||
accountType: isSmartContractWallet ? 'Smart Contract' : 'EOA', | ||
network: childChainName, | ||
amount: Number(amount), | ||
amount2: isBatchTransfer ? Number(amount2) : undefined, | ||
version: 2 | ||
}) | ||
await switchNetworkAsync?.(sourceChainId) | ||
} | ||
} catch (error) { | ||
return networkConnectionWarningToast() | ||
} finally { | ||
setTransferring(false) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the chain switching out here so that it is used for all kinds of transfers.
} finally { | ||
setTransferring(false) | ||
} | ||
const sourceChainId = latestNetworks.current.sourceChain.id | ||
|
||
if (!isTransferAllowed) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when user reached here, the connected chain must be the source chain, or else it won't proceed.
const isConnectedToTheWrongChain = chain?.id !== networks.sourceChain.id | ||
|
||
if (!arbTokenBridgeLoaded) { | ||
return false | ||
} | ||
if (!eth) { | ||
return false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These conditions were in the while await Promise loop.
Since the while loop is deleted, they are added here to prevent transfers.
Closes FS-859