From 0682f26cb0e678502e6af5def6002bb66e982606 Mon Sep 17 00:00:00 2001 From: MananTank Date: Mon, 13 Jan 2025 23:28:37 +0000 Subject: [PATCH] [TOOL-3047] Dashboard: Fix TransactionButton opening "No funds" Modal when quickly clicking it after switching chain (#5941) --- .../src/components/buttons/MismatchButton.tsx | 56 ++++++++++++------- .../components/buttons/TransactionButton.tsx | 4 +- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/apps/dashboard/src/components/buttons/MismatchButton.tsx b/apps/dashboard/src/components/buttons/MismatchButton.tsx index feb18908776..d398d00ec01 100644 --- a/apps/dashboard/src/components/buttons/MismatchButton.tsx +++ b/apps/dashboard/src/components/buttons/MismatchButton.tsx @@ -63,18 +63,18 @@ const GAS_FREE_CHAINS = [ 247253, // Saakuru Testnet ]; -function useNetworkMismatchAdapter(desiredChainId: number) { +function useIsNetworkMismatch(txChainId: number) { const walletChainId = useActiveWalletChain()?.id; if (!walletChainId) { // simply not ready yet, assume false return false; } // otherwise, compare the chain ids - return walletChainId !== desiredChainId; + return walletChainId !== txChainId; } type MistmatchButtonProps = React.ComponentProps & { - desiredChainId: number; + txChainId: number; twAccount: Account | undefined; }; @@ -82,7 +82,7 @@ export const MismatchButton = forwardRef< HTMLButtonElement, MistmatchButtonProps >((props, ref) => { - const { desiredChainId, twAccount, ...buttonProps } = props; + const { txChainId, twAccount, ...buttonProps } = props; const account = useActiveAccount(); const wallet = useActiveWallet(); const activeWalletChain = useActiveWalletChain(); @@ -90,14 +90,15 @@ export const MismatchButton = forwardRef< const { theme } = useTheme(); const client = useThirdwebClient(); const pathname = usePathname(); + const txChain = useV5DashboardChain(txChainId); - const evmBalance = useWalletBalance({ + const txChainBalance = useWalletBalance({ address: account?.address, - chain: activeWalletChain, + chain: txChain, client, }); - const networksMismatch = useNetworkMismatchAdapter(desiredChainId); + const networksMismatch = useIsNetworkMismatch(txChainId); const [isMismatchPopoverOpen, setIsMismatchPopoverOpen] = useState(false); const trackEvent = useTrack(); @@ -130,8 +131,15 @@ export const MismatchButton = forwardRef< ); } + const isBalanceRequired = !GAS_FREE_CHAINS.includes(txChainId); + const notEnoughBalance = - (evmBalance.data?.value || 0n) === 0n && !GAS_FREE_CHAINS.includes(chainId); + (txChainBalance.data?.value || 0n) === 0n && isBalanceRequired; + + const disabled = + buttonProps.disabled || + // if user is about to trigger a transaction on txChain, but txChainBalance is not yet loaded and is required before proceeding + (!networksMismatch && txChainBalance.isPending && isBalanceRequired); return ( <> @@ -150,6 +158,7 @@ export const MismatchButton = forwardRef< diff --git a/apps/dashboard/src/components/buttons/TransactionButton.tsx b/apps/dashboard/src/components/buttons/TransactionButton.tsx index 7dc6588d5b8..567e332f601 100644 --- a/apps/dashboard/src/components/buttons/TransactionButton.tsx +++ b/apps/dashboard/src/components/buttons/TransactionButton.tsx @@ -71,7 +71,7 @@ export const TransactionButton: React.FC = ({ = ({

Approve Transaction

- You will need to approve this transaction in your connected wallet. + Your connected wallet will prompt you to approve this transaction

{showHint && (