Skip to content

Commit

Permalink
fix: switch network before calling a write method
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Oct 10, 2022
1 parent 6b6b9cb commit 63d3239
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/ContractInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useAccount,
useSigner,
useNetwork,
useSwitchNetwork,
useContract,
} from 'wagmi'
import Alert from 'components/Alert'
Expand Down Expand Up @@ -54,9 +55,11 @@ const ContractInfo: React.FC<{ address: string; contract: PolyjuiceContractProps
const { address: addr } = useAccount()
const { data: signer } = useSigner()
const { chain } = useNetwork()
const { switchNetworkAsync } = useSwitchNetwork()
const contract = useContract({
addressOrName: address,
contractInterface: abi,
// FIXME: remove the provider, or it will break the workflow of `switch to the correct network -> send a request` by one click due to inconsistent chain id
signerOrProvider: tabIdx === 2 ? signer : provider,
})

Expand Down Expand Up @@ -122,6 +125,14 @@ const ContractInfo: React.FC<{ address: string; contract: PolyjuiceContractProps

form.setAttribute(LOADING_ATTRIBUTE, 'true')
try {
if (tabIdx === 2 && chain?.id !== targetChain.id) {
if (switchNetworkAsync) {
await switchNetworkAsync(targetChain.id)
} else {
connect({ connector, chainId: targetChain.id })
}
return
}
const result = await method(...params)
if (tabIdx === 2) {
const elm = resInputList[0]
Expand Down

0 comments on commit 63d3239

Please sign in to comment.