Skip to content

Commit 341d0e5

Browse files
alcercujaybuidl
authored andcommitted
refactor(web/WIP): migrate to wagmi
1 parent 72e0ede commit 341d0e5

File tree

15 files changed

+1960
-130
lines changed

15 files changed

+1960
-130
lines changed

web/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ parcel-bundle-reports
2222
.env.development.local
2323
.env.test.local
2424
.env.production.local
25+
src/hooks/contracts/*
2526

2627
npm-debug.log*
2728
yarn-debug.log*

web/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
"clean": "rm dist/bundle.js",
2828
"start": "parcel",
2929
"start-local": "REACT_APP_SUBGRAPH_ENDPOINT=http://localhost:8000/subgraphs/name/kleros/kleros-v2-core-local parcel",
30-
"build": "parcel build",
30+
"build": "yarn run generate-hooks && yarn run parcel build",
3131
"check-style": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
3232
"check-types": "tsc --noEmit",
33-
"generate": "graphql-codegen"
33+
"generate-gql": "graphql-codegen",
34+
"generate-hooks": "wagmi generate"
3435
},
3536
"devDependencies": {
3637
"@netlify/functions": "^1.4.0",
@@ -67,6 +68,8 @@
6768
"@web3-react/core": "^6.1.9",
6869
"@web3-react/injected-connector": "^6.0.7",
6970
"@web3-react/types": "^6.0.7",
71+
"@web3modal/ethereum": "^2.2.2",
72+
"@web3modal/react": "^2.2.2",
7073
"chart.js": "^3.9.1",
7174
"chartjs-adapter-moment": "^1.0.1",
7275
"core-js": "^3.30.0",
@@ -89,7 +92,8 @@
8992
"react-toastify": "^9.1.2",
9093
"react-use": "^17.4.0",
9194
"styled-components": "^5.3.9",
92-
"swr": "^1.3.0"
95+
"swr": "^1.3.0",
96+
"wagmi": "^0.12.6"
9397
},
9498
"volta": {
9599
"node": "16.18.1",

web/src/components/ConnectButton.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import React from "react";
22
import styled from "styled-components";
3+
import { useAccount, useNetwork } from "wagmi";
4+
import { arbitrumGoerli } from "wagmi/chains";
5+
import { useWeb3Modal } from "@web3modal/react";
36
import { shortenAddress } from "utils/shortenAddress";
47
import { Button } from "@kleros/ui-components-library";
5-
import { useWeb3 } from "hooks/useWeb3";
6-
import { useConnect } from "hooks/useConnect";
7-
import { SUPPORTED_CHAINS } from "consts/chains";
88

99
const AccountDisplay: React.FC = () => {
10-
const { account, chainId } = useWeb3();
11-
const chainName = chainId ? SUPPORTED_CHAINS[chainId].chainName : undefined;
12-
const shortAddress = account ? shortenAddress(account) : undefined;
10+
const { address } = useAccount();
11+
const { chain } = useNetwork();
12+
const shortAddress = address ? shortenAddress(address) : undefined;
1313
return (
1414
<StyledContainer>
15-
<small>{chainName}</small>
15+
<small>{chain?.name}</small>
1616
<label>{shortAddress}</label>
1717
</StyledContainer>
1818
);
@@ -40,12 +40,18 @@ const StyledContainer = styled.div`
4040
`;
4141

4242
const ConnectButton: React.FC = () => {
43-
const { active } = useWeb3();
44-
const { activate, connecting } = useConnect();
45-
return active ? (
43+
const { isConnected } = useAccount();
44+
const { open, setDefaultChain, isOpen } = useWeb3Modal();
45+
setDefaultChain(arbitrumGoerli);
46+
return isConnected ? (
4647
<AccountDisplay />
4748
) : (
48-
<Button disabled={connecting} small text={"Connect"} onClick={activate} />
49+
<Button
50+
disabled={isOpen}
51+
small
52+
text={"Connect"}
53+
onClick={async () => await open({ route: "ConnectWallet" })}
54+
/>
4955
);
5056
};
5157

web/src/context/Web3Provider.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
import React from "react";
2-
import { Web3ReactProvider } from "@web3-react/core";
32
import {
4-
Web3Provider as EthersProvider,
5-
ExternalProvider,
6-
} from "@ethersproject/providers";
3+
EthereumClient,
4+
w3mConnectors,
5+
w3mProvider,
6+
} from "@web3modal/ethereum";
7+
import { Web3Modal } from "@web3modal/react";
8+
import { configureChains, createClient, WagmiConfig } from "wagmi";
9+
import { arbitrumGoerli } from "wagmi/chains";
710

8-
const getLibrary = (provider: ExternalProvider): EthersProvider =>
9-
new EthersProvider(provider);
11+
const chains = [arbitrumGoerli];
12+
const projectId = "6efaa26765fa742153baf9281e218217";
13+
14+
const { provider } = configureChains(chains, [w3mProvider({ projectId })]);
15+
const wagmiClient = createClient({
16+
autoConnect: true,
17+
connectors: w3mConnectors({ projectId, version: 1, chains }),
18+
provider,
19+
});
20+
21+
const ethereumClient = new EthereumClient(wagmiClient, chains);
1022

1123
const Web3Provider: React.FC<{ children: React.ReactNode }> = ({
1224
children,
13-
}) => <Web3ReactProvider {...{ getLibrary }}> {children} </Web3ReactProvider>;
25+
}) => (
26+
<>
27+
<WagmiConfig client={wagmiClient}> {children} </WagmiConfig>
28+
<Web3Modal {...{ projectId, ethereumClient }} />
29+
</>
30+
);
1431

1532
export default Web3Provider;

web/src/hooks/queries/useETHBalance.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

web/src/hooks/useConnectedContract.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useWeb3 } from "hooks/useWeb3";
21
import { JsonRpcProvider } from "@ethersproject/providers";
2+
import { useSigner } from "wagmi";
33
import { getContract, ContractName } from "utils/getContract";
44
import { QUERY_CHAINS } from "consts/chains";
55

@@ -10,10 +10,10 @@ export const useConnectedContract = (
1010
contractAddress?: string,
1111
chain?: number
1212
) => {
13-
const { library } = useWeb3();
1413
const contract = getContract(contractName, contractAddress);
15-
if (library && !chain) {
16-
const connectedContract = contract?.connect(library.getSigner());
14+
const { data: signer } = useSigner();
15+
if (signer && !chain) {
16+
const connectedContract = contract?.connect(signer);
1717
return connectedContract;
1818
} else {
1919
try {

web/src/pages/Cases/CaseDetails/Appeal/Classic/Fund.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { useState } from "react";
22
import styled from "styled-components";
33
import { useParams } from "react-router-dom";
4+
import { useAccount, useBalance } from "wagmi";
45
import { Field, Button } from "@kleros/ui-components-library";
56
import { DisputeKitClassic } from "@kleros/kleros-v2-contracts/typechain-types/src/arbitration/dispute-kits/DisputeKitClassic";
67
import { useConnectedContract } from "hooks/useConnectedContract";
78
import { wrapWithToast } from "utils/wrapWithToast";
89
import { useParsedAmount } from "hooks/useParsedAmount";
9-
import { useETHBalance } from "hooks/queries/useETHBalance";
1010
import {
1111
useLoserSideCountdownContext,
1212
useSelectedOptionContext,
@@ -22,7 +22,12 @@ const Fund: React.FC = () => {
2222
(loserSideCountdown! > 0 ||
2323
(fundedChoices!.length > 0 && !fundedChoices?.includes(winningChoice!)));
2424
const { id } = useParams();
25-
const { data: balance } = useETHBalance();
25+
const { address, isDisconnected } = useAccount();
26+
const { data: balance } = useBalance({
27+
address,
28+
watch: true,
29+
cacheTime: 12_000,
30+
});
2631
const [amount, setAmount] = useState("");
2732
const parsedAmount = useParsedAmount(amount);
2833
const [isSending, setIsSending] = useState(false);
@@ -43,7 +48,12 @@ const Fund: React.FC = () => {
4348
placeholder="Amount to fund"
4449
/>
4550
<StyledButton
46-
disabled={isSending || !balance || parsedAmount.gt(balance)}
51+
disabled={
52+
isDisconnected ||
53+
isSending ||
54+
!balance ||
55+
parsedAmount.gt(balance.value)
56+
}
4757
text={typeof balance === "undefined" ? "Connect to Fund" : "Fund"}
4858
onClick={() => {
4959
if (

web/src/pages/Cases/CaseDetails/Evidence/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState } from "react";
22
import styled from "styled-components";
33
import { useParams } from "react-router-dom";
4-
import { useWeb3 } from "hooks/useWeb3";
4+
import { useAccount } from "wagmi";
55
import { Button, Searchbar } from "@kleros/ui-components-library";
66
import { useEvidenceGroup } from "queries/useEvidenceGroup";
77
import { useEvidences } from "queries/useEvidences";
@@ -13,7 +13,7 @@ const Evidence: React.FC<{ arbitrable?: string }> = ({ arbitrable }) => {
1313
const { id } = useParams();
1414
const { data: evidenceGroup } = useEvidenceGroup(id, arbitrable);
1515
const { data } = useEvidences(evidenceGroup);
16-
const { account } = useWeb3();
16+
const { address } = useAccount();
1717
return (
1818
<Container>
1919
{evidenceGroup && (
@@ -27,7 +27,7 @@ const Evidence: React.FC<{ arbitrable?: string }> = ({ arbitrable }) => {
2727
<StyledButton
2828
small
2929
text="Submit Evidence"
30-
disabled={typeof account === "undefined" || isModalOpen}
30+
disabled={typeof address === "undefined" || isModalOpen}
3131
isLoading={isModalOpen}
3232
onClick={() => setIsModalOpen(true)}
3333
/>

web/src/pages/Cases/CaseDetails/Voting/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import { useParams } from "react-router-dom";
3-
import { useWeb3 } from "hooks/useWeb3";
3+
import { useAccount } from "wagmi";
44
import { useDrawQuery } from "queries/useDrawQuery";
55
import Binary from "./Binary";
66
import VotingHistory from "./VotingHistory";
@@ -10,9 +10,9 @@ const Voting: React.FC<{
1010
arbitrable?: string;
1111
currentPeriodIndex?: number;
1212
}> = ({ arbitrable, currentPeriodIndex }) => {
13-
const { account } = useWeb3();
13+
const { address } = useAccount();
1414
const { id } = useParams();
15-
const { data } = useDrawQuery(account?.toLowerCase(), id);
15+
const { data } = useDrawQuery(address?.toLowerCase(), id);
1616
return data &&
1717
currentPeriodIndex === Periods.vote &&
1818
data.draws?.length > 0 ? (

web/src/pages/Courts/CourtDetails/StakePanel/InputDisplay.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import React, { useState } from "react";
22
import styled from "styled-components";
33
import { useParams } from "react-router-dom";
44
import { utils } from "ethers";
5+
import { useAccount } from "wagmi";
56
import { Button, Field } from "@kleros/ui-components-library";
67

7-
import { useWeb3 } from "hooks/useWeb3";
8-
import { useConnect } from "hooks/useConnect";
98
import { useParsedAmount } from "hooks/useParsedAmount";
109
import { usePNKBalance } from "queries/usePNKBalance";
1110
import { useJurorBalance } from "queries/useJurorBalance";
@@ -26,12 +25,11 @@ const InputDisplay: React.FC<IInputDisplay> = ({
2625
const parsedAmount = useParsedAmount(amount);
2726

2827
const { id } = useParams();
29-
const { account } = useWeb3();
30-
const { data: balance } = usePNKBalance(account);
28+
const { address } = useAccount();
29+
const { data: balance } = usePNKBalance(address);
3130
const parsedBalance = utils.formatEther(balance || 0);
32-
const { data: jurorBalance } = useJurorBalance(account, id);
31+
const { data: jurorBalance } = useJurorBalance(address, id);
3332
const parsedStake = utils.formatEther(jurorBalance?.staked || 0);
34-
const { activate, connecting } = useConnect();
3533
const isStaking = action === ActionType.stake;
3634

3735
return (
@@ -62,7 +60,7 @@ const InputDisplay: React.FC<IInputDisplay> = ({
6260
}
6361
variant="info"
6462
/>
65-
{account ? (
63+
{address ? (
6664
<StakeWithdrawButton
6765
{...{
6866
parsedAmount,
@@ -73,16 +71,14 @@ const InputDisplay: React.FC<IInputDisplay> = ({
7371
}}
7472
/>
7573
) : (
76-
<Button
77-
text="Connect to Stake"
78-
onClick={activate}
79-
disabled={connecting}
80-
/>
74+
<Button text="Connect to Stake" />
8175
)}
8276
</InputArea>
8377
</>
8478
);
8579
};
80+
// onClick={activate}
81+
// disabled={connecting}
8682

8783
export default InputDisplay;
8884

0 commit comments

Comments
 (0)