1+ import { L2_CHAINS , TESTNET_CHAINS } from '@buildeross/constants'
12import { Box , Button , ButtonProps , Flex , PopUp , Text } from '@buildeross/zord'
23import { useConnectModal } from '@rainbow-me/rainbowkit'
3- import { useCallback , useRef , useState } from 'react'
4+ import Link from 'next/link'
5+ import { useCallback , useMemo , useRef , useState } from 'react'
46import { Icon } from 'src/components/Icon'
57import { useChainStore } from 'src/stores/useChainStore'
68import { useAccount , useBalance , useSwitchChain } from 'wagmi'
79
10+ const INSUFFICIENT_BALANCE_ERROR =
11+ 'Insufficient balance. Add ETH to your wallet (via bridging or exchange) to complete the transaction.'
12+
813export type ContractButtonProps = Omit < ButtonProps , 'onClick' | 'type' | 'ref' > & {
914 // Accept an optional click event; callers may also pass a 0-arg handler.
1015 handleClick : ( e ?: any ) => void | Promise < void >
@@ -24,6 +29,12 @@ export const ContractButton = ({
2429 chainId : appChain . id ,
2530 } )
2631
32+ const shouldShowBridgeLink = useMemo ( ( ) => {
33+ const isL2 = L2_CHAINS . includes ( appChain . id )
34+ const isTestnet = TESTNET_CHAINS . some ( ( chain ) => chain . id === appChain . id )
35+ return isL2 && ! isTestnet && userBalance ?. value === 0n
36+ } , [ appChain . id , userBalance ?. value ] )
37+
2738 const { openConnectModal } = useConnectModal ( )
2839 const { switchChain } = useSwitchChain ( )
2940 const [ buttonError , setButtonError ] = useState < string | null > ( null )
@@ -43,7 +54,7 @@ export const ContractButton = ({
4354
4455 if ( ! userAddress ) return openConnectModal ?.( )
4556 if ( userBalance ?. value === 0n ) {
46- setButtonError ( 'Insufficient balance' )
57+ setButtonError ( INSUFFICIENT_BALANCE_ERROR )
4758 return
4859 }
4960 if ( userChain ?. id !== appChain . id ) {
@@ -103,7 +114,17 @@ export const ContractButton = ({
103114 < Icon id = "warning" size = "md" fill = "negative" />
104115 < Box >
105116 < Text variant = "paragraph-sm" color = "text2" >
106- { buttonError }
117+ { buttonError === INSUFFICIENT_BALANCE_ERROR && shouldShowBridgeLink ? (
118+ < >
119+ Insufficient balance. Add ETH to your wallet via{ ' ' }
120+ < Link href = "/bridge" style = { { textDecoration : 'underline' } } >
121+ bridging
122+ </ Link > { ' ' }
123+ or exchange to complete the transaction.
124+ </ >
125+ ) : (
126+ buttonError
127+ ) }
107128 </ Text >
108129 </ Box >
109130 </ Flex >
0 commit comments