Skip to content

Commit 3510cf4

Browse files
committed
refactor: no more hardcoded contract address nor network config, rely on wagmi and viem
1 parent 5c68726 commit 3510cf4

File tree

9 files changed

+51
-47
lines changed

9 files changed

+51
-47
lines changed

cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"codegen",
1515
"commify",
1616
"commitlint",
17+
"consts",
1718
"COOLDOWN",
1819
"datetime",
1920
"devnet",
@@ -33,6 +34,7 @@
3334
"solhint",
3435
"typechain",
3536
"Unslashed",
37+
"viem",
3638
"wagmi"
3739
],
3840
"ignoreWords": [],

web/src/components/ConnectWallet/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const SwitchChainButton: React.FC = () => {
2222
<Button
2323
isLoading={isLoading}
2424
disabled={isLoading}
25-
text={`Switch to ${SUPPORTED_CHAINS[DEFAULT_CHAIN].chainName}`}
25+
text={`Switch to ${SUPPORTED_CHAINS[DEFAULT_CHAIN].name}`}
2626
onClick={handleSwitch}
2727
/>
2828
);

web/src/consts/chains.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
export const SUPPORTED_CHAINS = {
2-
421613: {
3-
chainName: "Arbitrum Goerli",
4-
nativeCurrency: { name: "ETH", symbol: "ETH", decimals: 18 },
5-
rpcUrls: ["https://goerli-rollup.arbitrum.io/rpc"],
6-
blockExplorerUrls: ["https://goerli.arbiscan.io/"],
7-
},
8-
};
9-
10-
export const DEFAULT_CHAIN = 421613;
1+
import { Chain, arbitrumGoerli, gnosisChiado } from "@wagmi/chains";
112

12-
export const SUPPORTED_CHAINIDS = Object.keys(SUPPORTED_CHAINS).map((x) => parseInt(x));
3+
export const DEFAULT_CHAIN = arbitrumGoerli.id;
134

14-
export const QUERY_CHAINS = {
15-
10200: {
16-
chainName: "Chiado Testnet",
17-
nativeCurrency: { name: "xDAI", symbol: "xDAI", decimals: 18 },
18-
rpcUrls: ["https://rpc.eu-central-2.gateway.fm/v3/gnosis/archival/chiado"],
19-
blockExplorerUrls: ["https://blockscout.chiadochain.net"],
20-
},
5+
export const SUPPORTED_CHAINS: Record<number, Chain> = {
6+
[arbitrumGoerli.id]: arbitrumGoerli,
217
};
8+
export const SUPPORTED_CHAIN_IDS = Object.keys(SUPPORTED_CHAINS);
229

23-
export const QUERY_CHAINIDS = Object.keys(QUERY_CHAINS).map((x) => parseInt(x));
10+
export const QUERY_CHAINS: Record<number, Chain> = {
11+
[gnosisChiado.id]: gnosisChiado,
12+
};
13+
export const QUERY_CHAIN_IDS = Object.keys(QUERY_CHAINS);

web/src/consts/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { version, gitCommitHash, gitCommitShortHash, gitBranch, gitTags, clean }
22

33
export const ONE_BASIS_POINT = 10000n;
44

5-
export const KLEROS_CONTRACT_ADDRESS = "ethereum:0x93ed3fbe21207ec2e8f2d3c3de6e058cb73bc04d";
6-
export const WETH_CONTRACT_ADDRESS = "ethereum:0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
7-
export const PNK_FAUCET_CONTRACT_ADDRESS = "0x05648Ee14941630a649082e0dA5cb80D29cC9002";
8-
95
export const IPFS_GATEWAY = process.env.REACT_APP_IPFS_GATEWAY || "https://cdn.kleros.link";
106

117
export const GIT_BRANCH = gitBranch;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Abi, PublicClient } from "viem";
2+
import { usePublicClient } from "wagmi";
3+
import { GetContractArgs, GetContractResult } from "wagmi/actions";
4+
import { getPinakionV2, pinakionV2ABI, getWeth, getPnkFaucet, wethABI, pnkFaucetABI } from "./contracts/generated";
5+
6+
type Config = Omit<GetContractArgs<Abi, unknown>, "abi" | "address"> & {
7+
chainId?: any;
8+
};
9+
10+
export const useContractAddress = <TAbi extends Abi>(
11+
getter: (c: Config) => GetContractResult<TAbi, PublicClient>
12+
): GetContractResult<TAbi, PublicClient> => {
13+
const publicClient = usePublicClient();
14+
return getter({ walletClient: publicClient });
15+
};
16+
17+
export const usePNKAddress = () => {
18+
return useContractAddress<typeof pinakionV2ABI>(getPinakionV2)?.address;
19+
};
20+
21+
export const useWETHAddress = () => {
22+
return useContractAddress<typeof wethABI>(getWeth)?.address;
23+
};
24+
25+
export const usePNKFaucetAddress = () => {
26+
return useContractAddress<typeof pnkFaucetABI>(getPnkFaucet)?.address;
27+
};

web/src/pages/Courts/CourtDetails/Stats.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styled from "styled-components";
33
import { useParams } from "react-router-dom";
44
import { useCourtDetails, CourtDetailsQuery } from "queries/useCourtDetails";
55
import { useCoinPrice } from "hooks/useCoinPrice";
6-
import { KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS } from "consts/index";
6+
import { usePNKAddress, useWETHAddress } from "hooks/useContractAddress";
77
import { formatETH, formatPNK, formatUnitsWei, formatUSD, isUndefined } from "utils/index";
88
import { calculateSubtextRender } from "utils/calculateSubtextRender";
99
import StatDisplay, { IStatDisplay } from "components/StatDisplay";
@@ -93,15 +93,11 @@ const stats: IStat[] = [
9393
},
9494
];
9595

96-
const coinIdToAddress = {
97-
0: KLEROS_CONTRACT_ADDRESS,
98-
1: WETH_CONTRACT_ADDRESS,
99-
};
100-
10196
const Stats = () => {
10297
const { id } = useParams();
10398
const { data } = useCourtDetails(id);
104-
const { prices: pricesData } = useCoinPrice([KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS]);
99+
const coinIdToAddress = [usePNKAddress(), useWETHAddress()];
100+
const { prices: pricesData } = useCoinPrice(coinIdToAddress);
105101

106102
return (
107103
<StyledCard>

web/src/pages/Courts/CourtDetails/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { formatEther } from "viem";
77
import { useCourtPolicy } from "queries/useCourtPolicy";
88
import { useCourtTree, CourtTreeQuery } from "queries/useCourtTree";
99
import { DEFAULT_CHAIN } from "consts/chains";
10-
import { PNK_FAUCET_CONTRACT_ADDRESS } from "consts/index";
10+
import { usePNKFaucetAddress } from "hooks/useContractAddress";
1111
import { wrapWithToast } from "utils/wrapWithToast";
1212
import { isUndefined } from "utils/index";
1313
import { StyledSkeleton } from "components/StyledSkeleton";
@@ -55,8 +55,9 @@ const CourtDetails: React.FC = () => {
5555
watch: true,
5656
});
5757

58+
const faucetAddress = usePNKFaucetAddress();
5859
const { data: balance } = usePnkBalanceOf({
59-
args: [PNK_FAUCET_CONTRACT_ADDRESS],
60+
args: [faucetAddress],
6061
watch: true,
6162
});
6263
const { data: walletClient } = useWalletClient();

web/src/pages/Dashboard/JurorInfo/JurorRewards.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import React from "react";
22
import styled from "styled-components";
33
import { formatUnits, formatEther } from "viem";
44
import { useAccount } from "wagmi";
5-
import { KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS } from "src/consts/index";
65
import TokenRewards from "./TokenRewards";
76
import WithHelpTooltip from "../WithHelpTooltip";
87
import { isUndefined } from "utils/index";
98
import { useUserQuery, UserQuery } from "queries/useUser";
109
import { useCoinPrice } from "hooks/useCoinPrice";
10+
import { usePNKAddress, useWETHAddress } from "hooks/useContractAddress";
1111

1212
interface IReward {
1313
token: "ETH" | "PNK";
@@ -28,11 +28,6 @@ const tooltipMsg =
2828
"is coherent with the final ruling receive the Juror Rewards composed of " +
2929
"arbitration fees (ETH) + PNK redistribution between jurors.";
3030

31-
const coinIdToAddress = {
32-
0: KLEROS_CONTRACT_ADDRESS,
33-
1: WETH_CONTRACT_ADDRESS,
34-
};
35-
3631
const rewards: IReward[] = [
3732
{
3833
token: "ETH",
@@ -59,7 +54,8 @@ const calculateTotalReward = (coinId: number, data: UserQuery): bigint => {
5954
const Coherency: React.FC = () => {
6055
const { address } = useAccount();
6156
const { data } = useUserQuery(address?.toLowerCase());
62-
const { prices: pricesData } = useCoinPrice([KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS]);
57+
const coinIdToAddress = [usePNKAddress(), useWETHAddress()];
58+
const { prices: pricesData } = useCoinPrice(coinIdToAddress);
6359

6460
return (
6561
<>

web/src/pages/Home/CourtOverview/Stats.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import EthereumIcon from "svgs/icons/ethereum.svg";
88
import PNKRedistributedIcon from "svgs/icons/redistributed-pnk.svg";
99
import JurorIcon from "svgs/icons/user.svg";
1010
import BalanceIcon from "svgs/icons/law-balance.svg";
11-
import { KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS } from "consts/index";
1211
import { formatETH, formatPNK, formatUnitsWei, formatUSD, isUndefined } from "utils/index";
1312
import { calculateSubtextRender } from "utils/calculateSubtextRender";
1413
import { useHomePageContext, HomePageQuery, HomePageQueryDataPoints } from "hooks/useHomePageContext";
1514
import { useCoinPrice } from "hooks/useCoinPrice";
15+
import { usePNKAddress, useWETHAddress } from "hooks/useContractAddress";
1616

1717
const StyledCard = styled(Card)`
1818
width: auto;
@@ -77,14 +77,10 @@ const stats: IStat[] = [
7777
},
7878
];
7979

80-
const coinIdToAddress = {
81-
0: KLEROS_CONTRACT_ADDRESS,
82-
1: WETH_CONTRACT_ADDRESS,
83-
};
84-
8580
const Stats = () => {
8681
const { data } = useHomePageContext();
87-
const { prices: pricesData } = useCoinPrice([KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS]);
82+
const coinIdToAddress = [usePNKAddress(), useWETHAddress()];
83+
const { prices: pricesData } = useCoinPrice(coinIdToAddress);
8884
return (
8985
<StyledCard>
9086
{stats.map(({ title, coinId, getText, getSubtext, color, icon }, i) => {

0 commit comments

Comments
 (0)