diff --git a/packages/apps/staking/.env.example b/packages/apps/staking/.env.example index 6108cf5bc5..e9daab498c 100644 --- a/packages/apps/staking/.env.example +++ b/packages/apps/staking/.env.example @@ -2,6 +2,7 @@ VITE_APP_DASHBOARD_API_URL=http://0.0.0.0:5006 VITE_APP_ENVIRONMENT=testnet VITE_APP_SUPPORTED_CHAINS=80002 +VITE_APP_WALLETCONNECT_PROJECT_ID=replace-me # Links to header VITE_HEADER_LINK_DASHBOARD=http://localhost:3004 diff --git a/packages/apps/staking/src/hooks/useKVStore.ts b/packages/apps/staking/src/hooks/useKVStore.ts index d2c4a0d993..24cf987032 100644 --- a/packages/apps/staking/src/hooks/useKVStore.ts +++ b/packages/apps/staking/src/hooks/useKVStore.ts @@ -1,5 +1,5 @@ import { ChainId, IKVStore, KVStoreClient } from '@human-protocol/sdk'; -import { ethers } from 'ethers'; +import { Eip1193Provider, ethers } from 'ethers'; import { useEffect, useState } from 'react'; import { useAccount, useWalletClient } from 'wagmi'; import { SUPPORTED_CHAIN_IDS } from '../constants/chains'; @@ -8,7 +8,7 @@ import { parseErrorMessage } from '../utils/string'; import { getKVStoreData } from '../services/dashboard'; export const useKVStore = () => { - const { address, chainId } = useAccount(); + const { address, chainId, connector } = useAccount(); const { data: walletClient } = useWalletClient(); const { showError, openSnackbar } = useSnackbar(); @@ -36,9 +36,12 @@ export const useKVStore = () => { useEffect(() => { const initStakingClient = async () => { try { - if (walletClient && address) { + if (walletClient && address && connector) { checkSupportedChain(); - const provider = new ethers.BrowserProvider(window.ethereum); + const eeip193Provider = await connector?.getProvider(); + const provider = new ethers.BrowserProvider( + eeip193Provider as Eip1193Provider + ); const signer = await provider.getSigner(); const client = await KVStoreClient.build(signer); @@ -53,7 +56,7 @@ export const useKVStore = () => { initStakingClient(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [walletClient, address, chainId]); + }, [walletClient, address, chainId, connector]); const checkSupportedChain = () => { const isSupportedChain = SUPPORTED_CHAIN_IDS.includes(chainId as ChainId); diff --git a/packages/apps/staking/src/hooks/useStake.ts b/packages/apps/staking/src/hooks/useStake.ts index a2a8b74905..c9bfbc8ca7 100644 --- a/packages/apps/staking/src/hooks/useStake.ts +++ b/packages/apps/staking/src/hooks/useStake.ts @@ -5,7 +5,7 @@ import { StakerInfo, StakingClient, } from '@human-protocol/sdk'; -import { ethers } from 'ethers'; +import { Eip1193Provider, ethers } from 'ethers'; import { useEffect, useState } from 'react'; import { useAccount, useWalletClient } from 'wagmi'; import { useSnackbar } from '../providers/SnackProvider'; @@ -14,7 +14,7 @@ import { formatAmount } from '../utils/units'; import { SUPPORTED_CHAIN_IDS } from '../constants/chains'; export const useStake = () => { - const { address, chainId } = useAccount(); + const { address, chainId, connector } = useAccount(); const { data: walletClient } = useWalletClient(); const { showError, openSnackbar } = useSnackbar(); @@ -23,13 +23,19 @@ export const useStake = () => { ); const [stakingData, setStakingData] = useState(null); const [tokenBalance, setTokenBalance] = useState(0); + const [browserProvider, setBrowserProvider] = + useState(null); useEffect(() => { const initStakingClient = async () => { try { - if (walletClient && address) { + if (walletClient && address && connector) { checkSupportedChain(); - const provider = new ethers.BrowserProvider(window.ethereum); + const eeip193Provider = await connector?.getProvider(); + const provider = new ethers.BrowserProvider( + eeip193Provider as Eip1193Provider + ); + setBrowserProvider(provider); const signer = await provider.getSigner(); const client = await StakingClient.build(signer); @@ -45,7 +51,7 @@ export const useStake = () => { initStakingClient(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [walletClient, address, chainId]); + }, [walletClient, address, chainId, connector]); const checkSupportedChain = () => { const isSupportedChain = SUPPORTED_CHAIN_IDS.includes(chainId as ChainId); @@ -99,18 +105,16 @@ export const useStake = () => { }; const handleStake = async (amount: string) => { + if (!browserProvider) return; + try { checkSupportedChain(); - if (stakingClient && amount) { + if (stakingClient && amount && address) { const weiAmount = ethers.parseUnits(amount, 'ether'); await stakingClient.approveStake(weiAmount); await stakingClient.stake(weiAmount); await fetchStakingData(stakingClient); - await fetchTokenBalance( - new ethers.BrowserProvider(window.ethereum), - address!, - chainId - ); + await fetchTokenBalance(browserProvider, address, chainId); openSnackbar('Stake successful', 'success'); } } catch (error) { @@ -120,17 +124,15 @@ export const useStake = () => { }; const handleUnstake = async (amount: string) => { + if (!browserProvider) return; + try { checkSupportedChain(); - if (stakingClient && amount) { + if (stakingClient && amount && address) { const weiAmount = ethers.parseUnits(amount, 'ether'); await stakingClient.unstake(weiAmount); await fetchStakingData(stakingClient); - await fetchTokenBalance( - new ethers.BrowserProvider(window.ethereum), - address!, - chainId - ); + await fetchTokenBalance(browserProvider, address, chainId); openSnackbar('Unstake successful', 'success'); } } catch (error) { @@ -140,16 +142,14 @@ export const useStake = () => { }; const handleWithdraw = async () => { + if (!browserProvider) return; + try { checkSupportedChain(); - if (stakingClient) { + if (stakingClient && address) { await stakingClient.withdraw(); await fetchStakingData(stakingClient); - await fetchTokenBalance( - new ethers.BrowserProvider(window.ethereum), - address!, - chainId - ); + await fetchTokenBalance(browserProvider, address, chainId); openSnackbar('Withdraw successful', 'success'); } } catch (error) {