Skip to content

Commit 31dd4dc

Browse files
committed
feat: add bridge link in contract button
1 parent c801539 commit 31dd4dc

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

apps/web/src/components/ContractButton.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { L2_CHAINS, TESTNET_CHAINS } from '@buildeross/constants'
12
import { Box, Button, ButtonProps, Flex, PopUp, Text } from '@buildeross/zord'
23
import { 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'
46
import { Icon } from 'src/components/Icon'
57
import { useChainStore } from 'src/stores/useChainStore'
68
import { 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+
813
export 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>

packages/constants/src/chains.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212

1313
type Chains = [Chain, ...Chain[]]
1414

15-
const MAINNET_CHAINS: Chains = [
15+
export const MAINNET_CHAINS: Chains = [
1616
{ ...mainnet, id: CHAIN_ID.ETHEREUM, slug: 'ethereum', icon: '/chains/ethereum.svg' },
1717
{ ...zora, id: CHAIN_ID.ZORA, slug: 'zora', icon: '/chains/zora.png' },
1818
{ ...base, id: CHAIN_ID.BASE, slug: 'base', icon: '/chains/base.svg' },
@@ -24,7 +24,7 @@ const MAINNET_CHAINS: Chains = [
2424
},
2525
]
2626

27-
const TESTNET_CHAINS: Chains = [
27+
export const TESTNET_CHAINS: Chains = [
2828
{ ...sepolia, id: CHAIN_ID.SEPOLIA, slug: 'sepolia', icon: '/chains/ethereum.svg' },
2929
{
3030
...optimismSepolia,

0 commit comments

Comments
 (0)