Skip to content

Commit a6b0dee

Browse files
committed
feat: generate the wagmi config dynamically by reading the deployment artifacts from all the chains
1 parent ffdb0c2 commit a6b0dee

File tree

1 file changed

+78
-10
lines changed

1 file changed

+78
-10
lines changed

contracts/wagmi.config.ts

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { readdir, readFile } from "fs/promises";
2+
import { parse, join } from "path";
13
import { Config, ContractConfig, defineConfig } from "@wagmi/cli";
2-
import { arbitrumGoerli, gnosisChiado } from "wagmi/chains";
3-
import HomeGatewayToGnosis from "@kleros/kleros-v2-contracts/deployments/arbitrumGoerli/HomeGatewayToGnosis.json" assert { type: "json" };
4-
import IHomeGateway from "@kleros/kleros-v2-contracts/artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
54
import { Abi } from "viem";
5+
import { Chain } from "@wagmi/chains";
6+
import IHomeGateway from "@kleros/kleros-v2-contracts/artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
67

78
type ArtifactPartial = {
89
abi: Abi;
@@ -12,17 +13,84 @@ const getAbi = (artifact: any) => {
1213
return (artifact as ArtifactPartial).abi;
1314
};
1415

16+
const readArtifacts = async (viemChainName: string, hardhatChainName?: string) => {
17+
const chains = await import("wagmi/chains");
18+
const chain = chains[viemChainName] as Chain;
19+
if (!chain) {
20+
throw new Error(`Viem chain ${viemChainName} not found`);
21+
}
22+
23+
const directoryPath = `./deployments/${hardhatChainName ?? viemChainName}`;
24+
const files = await readdir(directoryPath);
25+
26+
const results: ContractConfig[] = [];
27+
for (const file of files) {
28+
const { name, ext } = parse(file);
29+
if (ext === ".json") {
30+
const filePath = join(directoryPath, file);
31+
const fileContent = await readFile(filePath, "utf-8");
32+
const jsonContent = JSON.parse(fileContent);
33+
results.push({
34+
name: name,
35+
address: {
36+
[chain.id]: jsonContent.address as `0x{string}`,
37+
},
38+
abi: jsonContent.abi,
39+
});
40+
}
41+
}
42+
return results;
43+
};
44+
45+
// Group contracts by name and merge the address dictionary
46+
function merge(arr1: ContractConfig[], arr2: ContractConfig[]) {
47+
const mergedArr: ContractConfig[] = [...arr1];
48+
for (const contract of arr2) {
49+
const index = mergedArr.findIndex((c) => c.name === contract.name);
50+
if (index === -1) {
51+
mergedArr.push(contract);
52+
} else {
53+
mergedArr[index] = {
54+
...mergedArr[index],
55+
address: {
56+
...(mergedArr[index].address as Record<number, `0x${string}`>),
57+
...(contract.address as Record<number, `0x${string}`>),
58+
},
59+
};
60+
}
61+
}
62+
return mergedArr;
63+
}
64+
1565
const getConfig = async (): Promise<Config> => {
66+
const arbitrumGoerliContracts = await readArtifacts("arbitrumGoerli");
67+
arbitrumGoerliContracts.forEach((c) => console.log("✔ Found arbitrumGoerli artifact: %s", c.name));
68+
let contracts = arbitrumGoerliContracts;
69+
70+
const chiadoContracts = await readArtifacts("gnosisChiado", "chiado"); // renaming the Hardhat network improves this but breaks many other scripts
71+
chiadoContracts.forEach((c) => console.log("✔ Found chiado artifact: %s", c.name));
72+
contracts = merge(contracts, chiadoContracts);
73+
74+
const goerliContracts = await readArtifacts("goerli");
75+
goerliContracts.forEach((c) => console.log("✔ Found goerli artifact: %s", c.name));
76+
contracts = merge(contracts, goerliContracts);
77+
78+
const arbitrumContracts = await readArtifacts("arbitrum");
79+
arbitrumContracts.forEach((c) => console.log("✔ Found arbitrum artifact: %s", c.name));
80+
contracts = merge(contracts, arbitrumContracts);
81+
82+
const gnosisContracts = await readArtifacts("gnosis", "gnosischain");
83+
gnosisContracts.forEach((c) => console.log("✔ Found gnosis artifact: %s", c.name));
84+
contracts = merge(contracts, gnosisContracts);
85+
86+
const mainnetContracts = await readArtifacts("mainnet");
87+
mainnetContracts.forEach((c) => console.log("✔ Found mainnet artifact: %s", c.name));
88+
contracts = merge(contracts, mainnetContracts);
89+
1690
return {
1791
out: "viem/generated.ts",
1892
contracts: [
19-
{
20-
name: "HomeGatewayToGnosis",
21-
address: {
22-
[arbitrumGoerli.id]: HomeGatewayToGnosis.address as `0x{string}`,
23-
},
24-
abi: getAbi(HomeGatewayToGnosis),
25-
},
93+
...contracts,
2694
{
2795
name: "IHomeGateway",
2896
abi: getAbi(IHomeGateway),

0 commit comments

Comments
 (0)