55 StakerInfo ,
66 StakingClient ,
77} from '@human-protocol/sdk' ;
8- import { ethers } from 'ethers' ;
8+ import { Eip1193Provider , ethers } from 'ethers' ;
99import { useEffect , useState } from 'react' ;
1010import { useAccount , useWalletClient } from 'wagmi' ;
1111import { useSnackbar } from '../providers/SnackProvider' ;
@@ -14,7 +14,7 @@ import { formatAmount } from '../utils/units';
1414import { SUPPORTED_CHAIN_IDS } from '../constants/chains' ;
1515
1616export const useStake = ( ) => {
17- const { address, chainId } = useAccount ( ) ;
17+ const { address, chainId, connector } = useAccount ( ) ;
1818 const { data : walletClient } = useWalletClient ( ) ;
1919 const { showError, openSnackbar } = useSnackbar ( ) ;
2020
@@ -23,13 +23,19 @@ export const useStake = () => {
2323 ) ;
2424 const [ stakingData , setStakingData ] = useState < StakerInfo | null > ( null ) ;
2525 const [ tokenBalance , setTokenBalance ] = useState < number > ( 0 ) ;
26+ const [ browserProvider , setBrowserProvider ] =
27+ useState < ethers . BrowserProvider | null > ( null ) ;
2628
2729 useEffect ( ( ) => {
2830 const initStakingClient = async ( ) => {
2931 try {
30- if ( walletClient && address ) {
32+ if ( walletClient && address && connector ) {
3133 checkSupportedChain ( ) ;
32- const provider = new ethers . BrowserProvider ( window . ethereum ) ;
34+ const eeip193Provider = await connector ?. getProvider ( ) ;
35+ const provider = new ethers . BrowserProvider (
36+ eeip193Provider as Eip1193Provider
37+ ) ;
38+ setBrowserProvider ( provider ) ;
3339 const signer = await provider . getSigner ( ) ;
3440
3541 const client = await StakingClient . build ( signer ) ;
@@ -45,7 +51,7 @@ export const useStake = () => {
4551
4652 initStakingClient ( ) ;
4753 // eslint-disable-next-line react-hooks/exhaustive-deps
48- } , [ walletClient , address , chainId ] ) ;
54+ } , [ walletClient , address , chainId , connector ] ) ;
4955
5056 const checkSupportedChain = ( ) => {
5157 const isSupportedChain = SUPPORTED_CHAIN_IDS . includes ( chainId as ChainId ) ;
@@ -99,18 +105,16 @@ export const useStake = () => {
99105 } ;
100106
101107 const handleStake = async ( amount : string ) => {
108+ if ( ! browserProvider ) return ;
109+
102110 try {
103111 checkSupportedChain ( ) ;
104- if ( stakingClient && amount ) {
112+ if ( stakingClient && amount && address ) {
105113 const weiAmount = ethers . parseUnits ( amount , 'ether' ) ;
106114 await stakingClient . approveStake ( weiAmount ) ;
107115 await stakingClient . stake ( weiAmount ) ;
108116 await fetchStakingData ( stakingClient ) ;
109- await fetchTokenBalance (
110- new ethers . BrowserProvider ( window . ethereum ) ,
111- address ! ,
112- chainId
113- ) ;
117+ await fetchTokenBalance ( browserProvider , address , chainId ) ;
114118 openSnackbar ( 'Stake successful' , 'success' ) ;
115119 }
116120 } catch ( error ) {
@@ -120,17 +124,15 @@ export const useStake = () => {
120124 } ;
121125
122126 const handleUnstake = async ( amount : string ) => {
127+ if ( ! browserProvider ) return ;
128+
123129 try {
124130 checkSupportedChain ( ) ;
125- if ( stakingClient && amount ) {
131+ if ( stakingClient && amount && address ) {
126132 const weiAmount = ethers . parseUnits ( amount , 'ether' ) ;
127133 await stakingClient . unstake ( weiAmount ) ;
128134 await fetchStakingData ( stakingClient ) ;
129- await fetchTokenBalance (
130- new ethers . BrowserProvider ( window . ethereum ) ,
131- address ! ,
132- chainId
133- ) ;
135+ await fetchTokenBalance ( browserProvider , address , chainId ) ;
134136 openSnackbar ( 'Unstake successful' , 'success' ) ;
135137 }
136138 } catch ( error ) {
@@ -140,16 +142,14 @@ export const useStake = () => {
140142 } ;
141143
142144 const handleWithdraw = async ( ) => {
145+ if ( ! browserProvider ) return ;
146+
143147 try {
144148 checkSupportedChain ( ) ;
145- if ( stakingClient ) {
149+ if ( stakingClient && address ) {
146150 await stakingClient . withdraw ( ) ;
147151 await fetchStakingData ( stakingClient ) ;
148- await fetchTokenBalance (
149- new ethers . BrowserProvider ( window . ethereum ) ,
150- address ! ,
151- chainId
152- ) ;
152+ await fetchTokenBalance ( browserProvider , address , chainId ) ;
153153 openSnackbar ( 'Withdraw successful' , 'success' ) ;
154154 }
155155 } catch ( error ) {
0 commit comments