Skip to content

Commit

Permalink
[Fix] wagmi switch chain claim error (#41)
Browse files Browse the repository at this point in the history
* check switch chain

* bump version

* fix lint and prettier

* bump
  • Loading branch information
BrettCleary authored Dec 18, 2024
1 parent 309bed4 commit 65a44e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperplay/quests-ui",
"version": "0.1.8",
"version": "0.1.9",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 22 additions & 3 deletions src/components/RewardWrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { createPublicClient, http } from 'viem'
import { useAccount, useConnect, useSwitchChain, useWriteContract } from 'wagmi'
import {
useAccount,
useConfig,
useConnect,
useSwitchChain,
useWriteContract
} from 'wagmi'
import { injected } from 'wagmi/connectors'
import { ConfirmClaimModal } from '../ConfirmClaimModal'
import styles from './index.module.scss'
Expand Down Expand Up @@ -58,6 +64,7 @@ export function RewardWrapper({
const { t: tOriginal } = useTranslation()
const account = useAccount()
const { connectAsync } = useConnect()
const config = useConfig()

// Context
const {
Expand Down Expand Up @@ -214,15 +221,27 @@ export function RewardWrapper({

let address: `0x${string}` | undefined

if (account.address) {
/**
* handles https://github.com/HyperPlay-Gaming/product-management/issues/801
* Sometimes wagmi does not establish a connection but useAccount returns the address.
* We need to check that the switch chain method exists before proceeding with claiming.
*/
let connectionHasSwitchChain = false
if (config.state.current) {
const currentConnection = config.state.connections.get(
config.state.current
)
connectionHasSwitchChain = !!currentConnection?.connector.switchChain
}

if (account.address && connectionHasSwitchChain) {
address = account.address
} else {
onShowMetaMaskPopup?.()
logInfo('connecting to wallet...')
const { accounts } = await connectAsync({ connector: injected() })
address = accounts[0]
}

if (!address) {
throw Error('no address found when trying to mint')
}
Expand Down

0 comments on commit 65a44e8

Please sign in to comment.