Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/apps/staking/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions packages/apps/staking/src/hooks/useKVStore.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
46 changes: 23 additions & 23 deletions packages/apps/staking/src/hooks/useStake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();

Expand All @@ -23,13 +23,19 @@ export const useStake = () => {
);
const [stakingData, setStakingData] = useState<StakerInfo | null>(null);
const [tokenBalance, setTokenBalance] = useState<number>(0);
const [browserProvider, setBrowserProvider] =
useState<ethers.BrowserProvider | null>(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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Loading