Skip to content

Commit c132303

Browse files
committed
feat: packaging to both CJS and ESM
1 parent 692c83f commit c132303

File tree

8 files changed

+161
-62
lines changed

8 files changed

+161
-62
lines changed

contracts/deployments/contractsViem.ts

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,107 @@
11
import { arbitrum, arbitrumSepolia } from "viem/chains";
2-
import { disputeTemplateRegistryConfig as devnetDtrConfig, klerosCoreConfig as devnetCoreConfig } from "./devnet.viem";
32
import {
4-
disputeTemplateRegistryConfig as mainnetDtrConfig,
5-
klerosCoreNeoConfig as mainnetCoreConfig,
6-
} from "./mainnet.viem";
7-
import { type PublicClient, type WalletClient } from "viem";
3+
klerosCoreConfig as devnetCoreConfig,
4+
sortitionModuleConfig as devnetSortitionConfig,
5+
disputeKitClassicConfig as devnetDkcConfig,
6+
disputeResolverConfig as devnetDrConfig,
7+
disputeTemplateRegistryConfig as devnetDtrConfig,
8+
evidenceModuleConfig as devnetEvidenceConfig,
9+
policyRegistryConfig as devnetPolicyRegistryConfig,
10+
transactionBatcherConfig as devnetBatcherConfig,
11+
chainlinkRngConfig as devnetChainlinkRngConfig,
12+
blockHashRngConfig as devnetBlockHashRngConfig,
13+
pnkConfig as devnetPnkConfig,
14+
klerosCoreSnapshotProxyConfig as devnetSnapshotProxyConfig,
15+
klerosCoreUniversityConfig as devnetCoreUniversityConfig,
16+
sortitionModuleUniversityConfig as devnetSortitionUniversityConfig,
17+
disputeKitClassicUniversityConfig as devnetDkcUniversityConfig,
18+
disputeResolverUniversityConfig as devnetDrUniversityConfig,
19+
klerosCoreUniversityProxyConfig as devnetCoreUniversityProxyConfig,
20+
} from "./devnet.viem";
21+
import { type PublicClient, type WalletClient, getContract } from "viem";
822

9-
export const Cores = {
10-
BASE: "BASE",
11-
NEO: "NEO",
12-
UNIVERSITY: "UNIVERSITY",
23+
const deployments = {
24+
devnet: {
25+
chainId: arbitrumSepolia.id,
26+
},
27+
university: {
28+
chainId: arbitrumSepolia.id,
29+
},
30+
testnet: {
31+
chainId: arbitrumSepolia.id,
32+
},
33+
mainnetNeo: {
34+
chainId: arbitrum.id,
35+
},
1336
} as const;
1437

15-
export type Core = (typeof Cores)[keyof typeof Cores];
38+
type DeploymentName = keyof typeof deployments;
39+
40+
type ContractConfig = {
41+
address: Record<number, `0x${string}`>;
42+
abi: readonly any[];
43+
};
44+
45+
type ContractConfigs = {
46+
klerosCore?: {
47+
address: `0x${string}`;
48+
abi: readonly any[];
49+
};
50+
};
51+
52+
function getAddress(config: ContractConfig, chainId: number): `0x${string}` {
53+
const address = config.address[chainId];
54+
if (!address) throw new Error(`No address found for chainId ${chainId}`);
55+
return address;
56+
}
57+
58+
export const getConfigs = ({ deployment }: { deployment: DeploymentName }): ContractConfigs => {
59+
const { chainId } = deployments[deployment];
60+
let contractConfigs: ContractConfigs = {};
61+
switch (deployment) {
62+
case "devnet":
63+
contractConfigs = {
64+
klerosCore: {
65+
address: getAddress(devnetCoreConfig, chainId),
66+
abi: devnetCoreConfig.abi,
67+
},
68+
};
69+
break;
70+
default:
71+
throw new Error(`Unsupported deployment: ${deployment}`);
72+
}
73+
return contractConfigs;
74+
};
1675

1776
export const getContracts = ({
1877
publicClient,
1978
walletClient,
20-
chainId,
21-
coreType,
79+
deployment,
2280
}: {
2381
publicClient: PublicClient;
2482
walletClient?: WalletClient;
25-
chainId: number;
26-
coreType: Core;
83+
deployment: DeploymentName;
2784
}) => {
28-
throw new Error("Not implemented");
29-
switch (chainId) {
30-
case arbitrum.id:
31-
return {
32-
disputeTemplateRegistry: {
33-
address: mainnetDtrConfig.address[chainId],
34-
publicClient,
35-
walletClient,
36-
abi: mainnetDtrConfig.abi,
37-
},
38-
klerosCore: {
39-
address: mainnetCoreConfig.address[chainId],
40-
publicClient,
41-
walletClient,
42-
abi: mainnetCoreConfig.abi,
43-
},
44-
};
45-
case arbitrumSepolia.id:
46-
return {
47-
disputeTemplateRegistry: {
48-
address: devnetDtrConfig.address[chainId],
49-
publicClient,
50-
walletClient,
51-
abi: devnetDtrConfig.abi,
52-
},
53-
klerosCore: {
54-
address: devnetCoreConfig.address[chainId],
55-
publicClient,
56-
walletClient,
57-
abi: devnetCoreConfig.abi,
58-
},
59-
};
85+
const clientConfig = {
86+
client: {
87+
public: publicClient,
88+
wallet: walletClient,
89+
},
90+
};
91+
let klerosCore;
92+
switch (deployment) {
93+
case "devnet":
94+
const contractConfigs = getConfigs({ deployment });
95+
if (!contractConfigs.klerosCore) throw new Error("KlerosCore config not found");
96+
klerosCore = getContract({
97+
...contractConfigs.klerosCore,
98+
...clientConfig,
99+
});
100+
break;
60101
default:
61-
throw new Error(`Unsupported chainId: ${chainId}`);
102+
throw new Error(`Unsupported deployment: ${deployment}`);
62103
}
104+
return {
105+
klerosCore,
106+
};
63107
};
64-
65-
// TODO: do the same for Viem ?

contracts/deployments/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ export * as devnetViem from "./devnet.viem";
88
export * as mainnetViem from "./mainnet.viem";
99
export * as testnetViem from "./testnet.viem";
1010

11+
// Typechain-types
12+
export * from "../typechain-types";
13+
14+
// Contracts getters
1115
export { getContracts as getContractsEthers } from "./contractsEthers";
1216
export { getContracts as getContractsViem } from "./contractsViem";

contracts/package.json

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,33 @@
22
"name": "@kleros/kleros-v2-contracts",
33
"version": "0.8.1",
44
"description": "Smart contracts for Kleros version 2",
5-
"main": "typechain-types/index.ts",
5+
"main": "./dist/cjs/index.js",
6+
"module": "./dist/esm/index.js",
7+
"types": "./dist/types/index.d.ts",
8+
"exports": {
9+
".": {
10+
"types": "./dist/types/index.d.ts",
11+
"import": "./dist/esm/index.js",
12+
"require": "./dist/cjs/index.js",
13+
"default": "./dist/esm/index.js"
14+
}
15+
},
16+
"files": [
17+
"types",
18+
"esm",
19+
"cjs",
20+
"arbitration",
21+
"gateway",
22+
"kleros-v1",
23+
"libraries",
24+
"proxy",
25+
"rng",
26+
"token",
27+
"utils",
28+
"typechain-types",
29+
"deployments",
30+
"index.ts"
31+
],
632
"repository": "git@github.com:kleros/kleros-v2.git",
733
"homepage": "https://github.com/kleros/kleros-v2/tree/master/contracts#readme",
834
"author": "Kleros",
@@ -64,10 +90,10 @@
6490
"release:minor": "scripts/publish.sh minor",
6591
"release:major": "scripts/publish.sh major",
6692
"tenderly-verify": "hardhat tenderly:verify",
67-
"build:all": "yarn rimraf ./dist && yarn build:cjs && yarn build:esm && yarn build:types",
68-
"build:cjs": "tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
69-
"build:esm": "tsc --project tsconfig.json --module es2020 --outDir ./dist/esm && echo '{\"type\": \"module\"}' > ./dist/esm/package.json",
70-
"build:types": "tsc --project tsconfig.json --module esnext --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap"
93+
"build:all": "rm -rf ./dist && yarn build:cjs && yarn build:esm && yarn build:types",
94+
"build:cjs": "tsc --project tsconfig-release.json --module CommonJS --moduleResolution Node --outDir ./dist/cjs && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
95+
"build:esm": "tsc --project tsconfig-release.json --module NodeNext --moduleResolution NodeNext --outDir ./dist/esm && echo '{\"type\": \"module\"}' > ./dist/esm/package.json",
96+
"build:types": "tsc --project tsconfig-release.json --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap"
7197
},
7298
"devDependencies": {
7399
"@defi-wonderland/natspec-smells": "^1.1.5",

contracts/scripts/publish.sh

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
3+
shopt -s extglob
24

35
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
46

@@ -20,31 +22,57 @@ trap _finally EXIT
2022

2123
#--------------------------------------
2224

23-
yarn version $1
25+
if [[ "$PWD" != */contracts ]]; then
26+
echo "Error: This script must be run from the contracts directory"
27+
exit 1
28+
fi
29+
30+
# Recompile the contracts
31+
yarn clean
32+
yarn build
33+
34+
# Rebuild the typechain without mocks
35+
rm -rf artifacts/src/**/*[mM]ock*
36+
find artifacts/src -name "*.dbg.json" -type f -delete
37+
rm -rf typechain-types
38+
yarn typechain --out-dir typechain-types --glob 'artifacts/src/**/*.json' --target ethers-v6
2439

40+
# Generate the viem artifacts
2541
yarn viem:generate-devnet
2642
yarn viem:generate-testnet
2743
yarn viem:generate-mainnet
44+
45+
# Generate the Hardhat artifacts
2846
yarn export:devnet
2947
yarn export:testnet
3048
yarn export:mainnet
3149

50+
# Build the dist
3251
rm -rf dist
3352
mkdir dist
53+
yarn build:all
3454

35-
yarn tsc --project tsconfig-release.json --outDir ./dist
55+
# Copy the README and contracts
3656
cp -pr README.md src/ dist/
57+
58+
# Remove unwanted files
3759
rm -rf dist/config
3860
rm -rf dist/deploy
3961
rm -rf dist/scripts
4062
rm -rf dist/test
63+
rm -rf dist/**/mock
64+
rm -rf dist/**/*Mock*
4165
rm -rf dist/hardhat.config*
4266
rm -rf dist/deployments/**/solcInputs
4367
rm -rf dist/deployments/localhost
4468
rm -rf dist/deployments/hardhat
4569
rm -rf dist/deployments/hardhat.viem.ts
4670
jq 'del(.scripts.prepare)' package.json > dist/package.json
4771

72+
# Bump the version
73+
yarn version $1
74+
75+
# Publish the package
4876
cd dist
49-
npm publish
77+
npm publish --dry-run
5078
cd -

contracts/tsconfig-release.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"declaration": true
55
},
66
"include": [
7-
"./src",
87
"./typechain-types",
98
"./deployments"
109
],

contracts/wagmi.config.devnet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Config, defineConfig } from "@wagmi/cli";
2-
import IHomeGateway from "@kleros/kleros-v2-contracts/artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
2+
import IHomeGateway from "./artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
33
import { getAbi, readArtifacts, merge } from "./scripts/wagmiHelpers";
44

55
const getConfig = async (): Promise<Config> => {

contracts/wagmi.config.mainnet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Config, defineConfig } from "@wagmi/cli";
2-
import IHomeGateway from "@kleros/kleros-v2-contracts/artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
2+
import IHomeGateway from "./artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
33
import { getAbi, readArtifacts, merge } from "./scripts/wagmiHelpers";
44

55
const getConfig = async (): Promise<Config> => {

contracts/wagmi.config.testnet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Config, defineConfig } from "@wagmi/cli";
2-
import IHomeGateway from "@kleros/kleros-v2-contracts/artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
2+
import IHomeGateway from "./artifacts/src/gateway/interfaces/IHomeGateway.sol/IHomeGateway.json" assert { type: "json" };
33
import { getAbi, readArtifacts, merge } from "./scripts/wagmiHelpers";
44

55
const getConfig = async (): Promise<Config> => {

0 commit comments

Comments
 (0)