Skip to content

Commit

Permalink
fix chain
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleydon committed Jan 9, 2025
1 parent a03fde5 commit 7ee66af
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
10 changes: 4 additions & 6 deletions apps/app/components/SwitchChainButton.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import {ethChain} from '@/config'
import {LoadingButton} from '@mui/lab'
import {mainnet, sepolia} from 'viem/chains'
import {useAccount, useSwitchChain} from 'wagmi'

const targetChain = process.env.VERCEL_ENV === 'production' ? mainnet : sepolia

const SwitchChainButton = ({children}: {children: React.ReactNode}) => {
const {chain} = useAccount()
const {switchChain, isPending} = useSwitchChain()
if (chain == null || chain.id === targetChain.id) {
if (chain == null || chain.id === ethChain.id) {
return children
}
return (
<LoadingButton
loading={isPending}
fullWidth
color="warning"
onClick={() => switchChain({chainId: targetChain.id})}
onClick={() => switchChain({chainId: ethChain.id})}
>
Switch to {targetChain.name}
Switch to {ethChain.name}
</LoadingButton>
)
}
Expand Down
5 changes: 3 additions & 2 deletions apps/app/components/Web3Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {ethChain} from '@/config'
import {ConnectKitProvider, getDefaultConfig} from 'connectkit'
import type {ReactNode} from 'react'
import type {Chain} from 'viem'
import {
WagmiProvider,
cookieStorage,
cookieToInitialState,
createConfig,
createStorage,
} from 'wagmi'
import {type Chain, sepolia} from 'wagmi/chains'

const chains: [Chain, ...Chain[]] = [sepolia]
const chains: [Chain, ...Chain[]] = [ethChain]

const config = createConfig(
getDefaultConfig({
Expand Down
3 changes: 3 additions & 0 deletions apps/app/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {GraphQLClient} from 'graphql-request'
import {sepolia} from 'viem/chains'

export const PHALA_ENDPOINTS = [
'wss://phala-rpc.dwellir.com',
Expand All @@ -10,3 +11,5 @@ export const subsquidClient = new GraphQLClient(
)

export const WPHA_ASSET_ID = 10000

export const ethChain = sepolia
8 changes: 3 additions & 5 deletions apps/app/hooks/useAutoSwitchChain.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {ethChain} from '@/config'
import {useEffect} from 'react'
import {mainnet, sepolia} from 'viem/chains'
import {useSwitchChain} from 'wagmi'
import {useAccount} from 'wagmi'

const targetChain = process.env.VERCEL_ENV === 'production' ? mainnet : sepolia

export const useAutoSwitchChain = () => {
const {chain} = useAccount()
const {switchChain} = useSwitchChain()
useEffect(() => {
if (chain != null && chain.id !== targetChain.id) {
switchChain({chainId: targetChain.id})
if (chain?.id !== ethChain.id) {
switchChain({chainId: ethChain.id})
}
}, [chain, switchChain])
}

0 comments on commit 7ee66af

Please sign in to comment.