|
1 | | -import { defineConfig } from "@wagmi/cli"; |
2 | | -import { react, actions } from "@wagmi/cli/plugins"; |
3 | | -import { arbitrumGoerli } from "wagmi/chains"; |
4 | | -import KlerosCore from "@kleros/kleros-v2-contracts/deployments/arbitrumGoerli/KlerosCore.json" assert { type: "json" }; |
5 | | -import PNK from "@kleros/kleros-v2-contracts/deployments/arbitrumGoerli/PNK.json" assert { type: "json" }; |
6 | | -import DisputeKitClassic from "@kleros/kleros-v2-contracts/deployments/arbitrumGoerli/DisputeKitClassic.json" assert { type: "json" }; |
7 | | -import PolicyRegistry from "@kleros/kleros-v2-contracts/deployments/arbitrumGoerli/PolicyRegistry.json" assert { type: "json" }; |
8 | | -import IArbitrableV2 from "@kleros/kleros-v2-contracts/artifacts/src/arbitration/interfaces/IArbitrableV2.sol/IArbitrableV2.json" assert { type: "json" }; |
| 1 | +import { Config, ContractConfig, defineConfig } from "@wagmi/cli"; |
| 2 | +import { readdir, readFile } from "fs/promises"; |
| 3 | +import { parse, join } from "path"; |
| 4 | +import { Abi } from "viem"; |
| 5 | +import { Chain } from "@wagmi/chains"; |
| 6 | +import dotenv from "dotenv"; |
9 | 7 | import IHomeGateway from "@kleros/kleros-v2-contracts/artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" }; |
10 | | -import PNKFaucet from "@kleros/kleros-v2-contracts/deployments/arbitrumGoerli/PNKFaucet.json" assert { type: "json" }; |
11 | | - |
12 | | -export default defineConfig({ |
13 | | - out: "src/hooks/contracts/generated.ts", |
14 | | - contracts: [ |
15 | | - { |
16 | | - name: "KlerosCore", |
17 | | - address: { |
18 | | - [arbitrumGoerli.id]: KlerosCore.address as `0x{string}`, |
19 | | - }, |
20 | | - abi: KlerosCore.abi, |
21 | | - }, |
22 | | - { |
23 | | - name: "DisputeKitClassic", |
24 | | - address: { |
25 | | - [arbitrumGoerli.id]: DisputeKitClassic.address as `0x{string}`, |
26 | | - }, |
27 | | - abi: DisputeKitClassic.abi, |
28 | | - }, |
29 | | - { |
30 | | - name: "PNK", |
31 | | - address: { |
32 | | - [arbitrumGoerli.id]: PNK.address as `0x{string}`, |
33 | | - }, |
34 | | - abi: PNK.abi, |
35 | | - }, |
36 | | - { |
37 | | - name: "PolicyRegistry", |
38 | | - address: { |
39 | | - [arbitrumGoerli.id]: PolicyRegistry.address as `0x{string}`, |
| 8 | +import IArbitrableV2 from "@kleros/kleros-v2-contracts/artifacts/src/arbitration/interfaces/IArbitrableV2.sol/IArbitrableV2.json" assert { type: "json" }; |
| 9 | + |
| 10 | +dotenv.config(); |
| 11 | + |
| 12 | +type ArtifactPartial = { |
| 13 | + abi: Abi; |
| 14 | +}; |
| 15 | + |
| 16 | +const getAbi = (artifact: any) => { |
| 17 | + return (artifact as ArtifactPartial).abi; |
| 18 | +}; |
| 19 | + |
| 20 | +const readArtifacts = async (viemChainName: string, hardhatChainName?: string) => { |
| 21 | + const chains = await import("wagmi/chains"); |
| 22 | + const chain = chains[viemChainName] as Chain; |
| 23 | + if (!chain) { |
| 24 | + throw new Error(`Viem chain ${viemChainName} not found`); |
| 25 | + } |
| 26 | + |
| 27 | + const directoryPath = `../contracts/deployments/${hardhatChainName ?? viemChainName}`; |
| 28 | + const files = await readdir(directoryPath); |
| 29 | + |
| 30 | + const results: ContractConfig[] = []; |
| 31 | + for (const file of files) { |
| 32 | + const { name, ext } = parse(file); |
| 33 | + if (ext === ".json") { |
| 34 | + const filePath = join(directoryPath, file); |
| 35 | + const fileContent = await readFile(filePath, "utf-8"); |
| 36 | + const jsonContent = JSON.parse(fileContent); |
| 37 | + results.push({ |
| 38 | + name: name, |
| 39 | + address: { |
| 40 | + [chain.id]: jsonContent.address as `0x{string}`, |
| 41 | + }, |
| 42 | + abi: jsonContent.abi, |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | + return results; |
| 47 | +}; |
| 48 | + |
| 49 | +const getConfig = async (): Promise<Config> => { |
| 50 | + const deployment = process.env["REACT_APP_DEPLOYMENT"] ?? "testnet"; |
| 51 | + |
| 52 | + let viemNetwork; |
| 53 | + let hardhatNetwork; |
| 54 | + switch (deployment) { |
| 55 | + case "devnet": |
| 56 | + viemNetwork = "arbitrumGoerli"; |
| 57 | + hardhatNetwork = "arbitrumGoerliDevnet"; |
| 58 | + break; |
| 59 | + case "testnet": |
| 60 | + viemNetwork = "arbitrumGoerli"; |
| 61 | + hardhatNetwork = "arbitrumGoerli"; |
| 62 | + break; |
| 63 | + case "mainnet": |
| 64 | + viemNetwork = "arbitrum"; |
| 65 | + hardhatNetwork = "arbitrum"; |
| 66 | + break; |
| 67 | + default: |
| 68 | + throw new Error(`Unknown deployment ${deployment}`); |
| 69 | + } |
| 70 | + |
| 71 | + const deploymentContracts = await readArtifacts(viemNetwork, hardhatNetwork); |
| 72 | + |
| 73 | + return { |
| 74 | + out: "src/hooks/contracts/generated.ts", |
| 75 | + contracts: [ |
| 76 | + ...deploymentContracts, |
| 77 | + { |
| 78 | + name: "IHomeGateway", |
| 79 | + abi: getAbi(IHomeGateway), |
40 | 80 | }, |
41 | | - abi: PolicyRegistry.abi, |
42 | | - }, |
43 | | - { |
44 | | - name: "IArbitrableV2", |
45 | | - abi: IArbitrableV2.abi, |
46 | | - }, |
47 | | - { |
48 | | - name: "IHomeGateway", |
49 | | - abi: IHomeGateway.abi, |
50 | | - }, |
51 | | - { |
52 | | - name: "PNKFaucet", |
53 | | - address: { |
54 | | - [arbitrumGoerli.id]: PNKFaucet.address as `0x{string}`, |
| 81 | + { |
| 82 | + name: "IArbitrableV2", |
| 83 | + abi: getAbi(IArbitrableV2), |
55 | 84 | }, |
56 | | - abi: PNKFaucet.abi, |
57 | | - }, |
58 | | - ], |
59 | | - plugins: [react(), actions()], |
60 | | -}); |
| 85 | + ], |
| 86 | + }; |
| 87 | +}; |
| 88 | + |
| 89 | +export default defineConfig(getConfig); |
0 commit comments