Skip to content

Commit 746860a

Browse files
authored
refactor: move contract artifact generation to generated package (#4405)
Moves ownership of contract TypeScript artifact generation from the contracts package to the generated package, improving logical separation and encapsulation. Previously, the contracts package owned both Solidity compilation (via Foundry) and TypeScript artifact generation (via typechain). This PR separates these concerns so that: - **contracts package**: Owns Solidity source and compilation only - **generated package**: Owns TypeScript artifact generation and typechain tooling
1 parent 0decffc commit 746860a

File tree

7 files changed

+24
-23
lines changed

7 files changed

+24
-23
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@
329329
{
330330
"label": "BuildContractTypes",
331331
"type": "shell",
332-
"command": "yarn workspace @towns-protocol/contracts build-types",
332+
"command": "yarn workspace @towns-protocol/generated build-types",
333333
"isBackground": true,
334334
"problemMatcher": [],
335335
"presentation": {

packages/contracts/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "0.0.387",
44
"packageManager": "[email protected]",
55
"scripts": {
6-
"build-types": "bash scripts/build-contract-types.sh",
76
"clean": "forge clean",
87
"compile": "forge build",
98
"format": "yarn prettier:write",
@@ -36,14 +35,12 @@
3635
"@openzeppelin/merkle-tree": "^1.0.8",
3736
"@prb/test": "^0.6.4",
3837
"@towns-protocol/prettier-config": "workspace:^",
39-
"@typechain/ethers-v5": "^11.1.2",
4038
"@wagmi/cli": "^2.2.0",
4139
"forge-std": "github:foundry-rs/forge-std#v1.10.0",
4240
"prettier": "^3.5.3",
4341
"prettier-plugin-solidity": "^1.4.2",
4442
"solhint": "^5.0.5",
45-
"solidity-bytes-utils": "^0.8.4",
46-
"typechain": "^8.3.2"
43+
"solidity-bytes-utils": "^0.8.4"
4744
},
4845
"files": [
4946
"docs/**/*",

packages/generated/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"build": "node ./scripts/prepare.js",
8+
"build-types": "bash scripts/build-contract-types.sh",
89
"make-config": "node ./scripts/make-config.js"
910
},
1011
"dependencies": {
@@ -13,7 +14,9 @@
1314
"ethers": "^5.8.0"
1415
},
1516
"devDependencies": {
16-
"@towns-protocol/contracts": "workspace:^"
17+
"@towns-protocol/contracts": "workspace:^",
18+
"@typechain/ethers-v5": "^11.1.2",
19+
"typechain": "^8.3.2"
1720
},
1821
"files": [
1922
"config/**/*",

packages/contracts/scripts/build-contract-types.sh renamed to packages/generated/scripts/build-contract-types.sh

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
#!/bin/bash
1+
#!/bin/bash
22
set -ueo pipefail
33
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")"
44
cd ..
55

6-
# Assume contracts and generated are siblings under the same parent directory
7-
PARENT_DIR="$(dirname "$(pwd)")"
8-
ABI_DIR="$PARENT_DIR/generated/dev/abis"
9-
TYPINGS_DIR="$PARENT_DIR/generated/dev/typings"
6+
# Contracts package is a sibling directory
7+
CONTRACTS_DIR="../contracts"
8+
ABI_DIR="./dev/abis"
9+
TYPINGS_DIR="./dev/typings"
1010

11-
forge build
11+
# Run forge build in contracts directory (needs foundry.toml)
12+
(cd "$CONTRACTS_DIR" && forge build)
1213

1314
CONTRACT_INTERFACES="(IDiamond|IDiamondCut|IArchitect|Architect|ILegacyArchitect|MockLegacyArchitect|IProxyManager|IPausable|IEntitlementsManager|EntitlementsManager|IChannel|Channels|IRoles|Roles|IMulticall|IRuleEntitlement|IRuleEntitlementV2|IWalletLink|WalletLink|INodeRegistry|NodeRegistry|IOperatorRegistry|OperatorRegistry|IStreamRegistry|StreamRegistry|OwnableFacet|TokenPausableFacet|UserEntitlement|SpaceOwner|MockERC721A|MembershipFacet|IMembershipMetadata|Member|IBanning|IPricingModules|ICrossChainEntitlement|MockEntitlementGated|PrepayFacet|IERC721AQueryable|IEntitlementDataQueryable|PlatformRequirementsFacet|IERC721A|INodeOperator|ISpaceDelegation|IEntitlementChecker|IERC5267|ICreateSpace|IDropFacet|DropFacet|ITownsPoints|ITipping|IReview|ITreasury|ISwapRouter|ISwapFacet|IAppRegistry|IAppAccount|ISimpleApp|ITownsApp|Towns|RewardsDistributionV2|MainnetDelegation|GuardianFacet|SubscriptionModuleFacet|IAppInstaller|IAppFactory)"
1415

1516
# Clean typings to avoid stale factories/interfaces lingering between runs
1617
rm -rf "$TYPINGS_DIR"
17-
yarn typechain --target=ethers-v5 "out/**/?${CONTRACT_INTERFACES}.json" --out-dir "$TYPINGS_DIR"
18+
yarn typechain --target=ethers-v5 "$CONTRACTS_DIR/out/**/?${CONTRACT_INTERFACES}.json" --out-dir "$TYPINGS_DIR"
1819

1920
# Clean abis to avoid stale abi files lingering between runs
2021
rm -rf "$ABI_DIR"
2122
mkdir -p "$ABI_DIR"
22-
cp -a out/{Diamond,DiamondCutFacet,Architect,MockLegacyArchitect,ProxyManager,IPausable,EntitlementsManager,Channels,Roles,IMulticall,OwnableFacet,WalletLink,MockWalletLink,NodeRegistry,OperatorRegistry,StreamRegistry,TokenPausableFacet,IRuleEntitlement,UserEntitlement,SpaceOwner,MockERC721A,MembershipFacet,IMembershipMetadata,Member,MockRiverRegistry,IBanning,IPricingModules,ICrossChainEntitlement,MockCrossChainEntitlement,MockEntitlementGated,PrepayFacet,IERC721AQueryable,IEntitlementDataQueryable,PlatformRequirementsFacet,IERC721A,INodeOperator,ISpaceDelegation,IEntitlementChecker,IEntitlementGated,IERC5267,ICreateSpace,DropFacet,ITownsPoints,ITipping,IReview,ITreasury,ISwapRouter,ISwapFacet,IAppRegistry,IAppAccount,ISimpleApp,ITownsApp,Towns,RewardsDistributionV2,MainnetDelegation,GuardianFacet,SubscriptionModuleFacet,IAppInstaller,IAppFactory}.sol/*.abi.json "$ABI_DIR"
23+
cp -a $CONTRACTS_DIR/out/{Diamond,DiamondCutFacet,Architect,MockLegacyArchitect,ProxyManager,IPausable,EntitlementsManager,Channels,Roles,IMulticall,OwnableFacet,WalletLink,MockWalletLink,NodeRegistry,OperatorRegistry,StreamRegistry,TokenPausableFacet,IRuleEntitlement,UserEntitlement,SpaceOwner,MockERC721A,MembershipFacet,IMembershipMetadata,Member,MockRiverRegistry,IBanning,IPricingModules,ICrossChainEntitlement,MockCrossChainEntitlement,MockEntitlementGated,PrepayFacet,IERC721AQueryable,IEntitlementDataQueryable,PlatformRequirementsFacet,IERC721A,INodeOperator,ISpaceDelegation,IEntitlementChecker,IEntitlementGated,IERC5267,ICreateSpace,DropFacet,ITownsPoints,ITipping,IReview,ITreasury,ISwapRouter,ISwapFacet,IAppRegistry,IAppAccount,ISimpleApp,ITownsApp,Towns,RewardsDistributionV2,MainnetDelegation,GuardianFacet,SubscriptionModuleFacet,IAppInstaller,IAppFactory}.sol/*.abi.json "$ABI_DIR"
2324

2425
# Copy the json abis to TS files for type inference
2526
for file in $ABI_DIR/*.abi.json; do
@@ -29,12 +30,12 @@ done
2930

3031
# Generate contract hash for this artifact build
3132
echo "Generating contract hash..."
32-
SRC_HASH=$(git rev-parse HEAD:./src 2>/dev/null || echo "")
33+
SRC_HASH=$(git rev-parse HEAD:$CONTRACTS_DIR/src 2>/dev/null || echo "")
3334
BUILD_SCRIPT_HASH=$(git rev-parse HEAD:./scripts/build-contract-types.sh 2>/dev/null || echo "")
3435

3536
if [ -n "$SRC_HASH" ] && [ -n "$BUILD_SCRIPT_HASH" ]; then
3637
CONTRACTS_HASH="$SRC_HASH:$BUILD_SCRIPT_HASH"
37-
echo "$CONTRACTS_HASH" > "$PARENT_DIR/generated/dev/.contracts-hash"
38+
echo "$CONTRACTS_HASH" > "./dev/.contracts-hash"
3839
echo "Contract hash: $CONTRACTS_HASH"
3940
else
4041
echo "Warning: Failed to generate contract hash (not in git repo or src not found)"

packages/generated/scripts/prepare.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function getContractsHash() {
4949
}).trim();
5050

5151
const buildScriptHash = execSync('git rev-parse HEAD:./scripts/build-contract-types.sh', {
52-
cwd: contractsDir,
52+
cwd: packageRoot,
5353
encoding: 'utf8',
5454
stdio: 'pipe'
5555
}).trim();
@@ -167,7 +167,7 @@ function generateArtifacts() {
167167
}
168168
}
169169

170-
const buildScript = resolve(contractsDir, 'scripts/build-contract-types.sh');
170+
const buildScript = resolve(packageRoot, 'scripts/build-contract-types.sh');
171171

172172
if (!existsSync(buildScript)) {
173173
throw new Error(`Build script not found at ${buildScript}`);
@@ -180,7 +180,7 @@ function generateArtifacts() {
180180
};
181181

182182
execSync(`bash ${buildScript}`, {
183-
cwd: contractsDir,
183+
cwd: packageRoot,
184184
stdio: 'inherit',
185185
env: envWithFoundry
186186
});

scripts/publish-to-npm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fi
4545

4646
# Generate contract types for publishing
4747
echo "Generating contract types for publishing..."
48-
yarn workspace @towns-protocol/contracts build-types
48+
yarn workspace @towns-protocol/generated build-types
4949
exit_status_contracts=$?
5050

5151
if [ $exit_status_contracts -ne 0 ]; then

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10165,7 +10165,6 @@ __metadata:
1016510165
"@prb/test": ^0.6.4
1016610166
"@towns-protocol/diamond": ^0.6.3
1016710167
"@towns-protocol/prettier-config": "workspace:^"
10168-
"@typechain/ethers-v5": ^11.1.2
1016910168
"@uniswap/permit2": "github:towns-protocol/permit2#v1.0.0"
1017010169
"@wagmi/cli": ^2.2.0
1017110170
account-abstraction: "github:eth-infinitism/account-abstraction#v0.7.0"
@@ -10177,7 +10176,6 @@ __metadata:
1017710176
solady: ^0.1.24
1017810177
solhint: ^5.0.5
1017910178
solidity-bytes-utils: ^0.8.4
10180-
typechain: ^8.3.2
1018110179
languageName: unknown
1018210180
linkType: soft
1018310181

@@ -10267,7 +10265,9 @@ __metadata:
1026710265
"@ethersproject/abi": ^5.8.0
1026810266
"@ethersproject/providers": ^5.8.0
1026910267
"@towns-protocol/contracts": "workspace:^"
10268+
"@typechain/ethers-v5": ^11.1.2
1027010269
ethers: ^5.8.0
10270+
typechain: ^8.3.2
1027110271
languageName: unknown
1027210272
linkType: soft
1027310273

@@ -12437,7 +12437,7 @@ __metadata:
1243712437
source-map-support: ^0.5.19
1243812438
table: ^6.8.0
1243912439
typescript: ^4.3.5
12440-
checksum: bdca754160deb9b34a6b668bbcb80477c1c596cdaa90ed4966e0b557f32d26f1d973033a468dca016ad8e5d909c68f15859408208b70602bdee0619c6208bd02
12440+
checksum: 8be8f2e1f8dbdb9c35edddf1f919f6c09fe764e45ad5733c0d449869e6c9241de6eac9710576d025bb3934f3bb97d017198a7c5f31b88a68aee0dbc898e1de97
1244112441
languageName: node
1244212442
linkType: hard
1244312443

0 commit comments

Comments
 (0)