Skip to content

Commit 5223807

Browse files
authored
Add interface IDs directly to Typechain generated factories (#1240)
* feat(interfaces): add interface IDs directly to generated factory classes - Replace generateInterfaceIds.js with addInterfaceIds.ts utility - Add interface IDs as static properties on Typechain factory classes - Export utility functions via src/utils.ts for programmatic use - Add ts-node dependency to run TypeScript build scripts - Remove separate interfaceIds.ts constants file * refactor(interfaces): improve addInterfaceIds code quality Address Copilot review feedback for PR 1240: - Replace magic numbers with named constants for better readability - Replace fragile regex-based JSON parsing with Function constructor for more robust ABI parsing that handles complex TypeScript syntax * refactor(interfaces): address additional code review feedback Add validation and improve code safety: - Add explicit justification comments for Function constructor usage - Add validation to ensure regex replacement succeeds before writing file - Fix ESLint warning by using const instead of let for content variable - Return false and log warning if metadata injection pattern not found * fix(horizon): fix failing testSlash_RoundDown_TokensThawing_Delegation fuzz test The test was failing when fuzzer generated inputs that left less than MIN_DELEGATION shares after undelegating but more than 0. Added constraint to ensure after undelegation either: - All tokens are undelegated (remaining = 0), which is valid, OR - At least MIN_DELEGATION remains, which is the minimum required The root cause: The contract correctly enforces that delegators must either fully exit (0 shares) or maintain at least MIN_DELEGATION shares. The test was missing this constraint, allowing the fuzzer to generate invalid intermediate amounts like 0.95e18 shares remaining. Fixes the intermittent CI failure on fuzzing seed 0xd5262092. * refactor(interfaces): improve error handling and indentation matching - Extract indentation from existing code instead of hardcoding 2 spaces - Track and report processing failures separately from skipped files - Return null from addInterfaceIdToFactory on errors instead of false - Throw error if any files failed processing to fail the build - Change warning to error for pattern match failures * chore: switching to version of fix already on other branch
1 parent 216f8a4 commit 5223807

File tree

9 files changed

+239
-249
lines changed

9 files changed

+239
-249
lines changed

packages/horizon/test/unit/staking/slash/slash.t.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ contract HorizonStakingSlashTest is HorizonStakingTest {
172172
vm.assume(delegationTokensToSlash <= delegationTokens);
173173
vm.assume(delegationTokensToUndelegate <= delegationTokens);
174174
vm.assume(delegationTokensToUndelegate > 0);
175+
vm.assume(
176+
delegationTokensToUndelegate == delegationTokens ||
177+
MIN_DELEGATION <= delegationTokens - delegationTokensToUndelegate
178+
);
175179

176180
resetPrank(users.delegator);
177181
_delegate(users.indexer, subgraphDataServiceAddress, delegationTokens, 0);

packages/interfaces/package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@
1212
"types": "./dist/src/index.d.ts",
1313
"default": "./dist/src/index.js"
1414
},
15+
"./types": {
16+
"types": "./dist/types/index.d.ts",
17+
"default": "./dist/types/index.js"
18+
},
1519
"./types-v5": {
1620
"types": "./dist/types-v5/index.d.ts",
1721
"default": "./dist/types-v5/index.js"
1822
},
1923
"./wagmi": {
2024
"types": "./dist/wagmi/generated.d.ts",
2125
"default": "./dist/wagmi/generated.js"
26+
},
27+
"./utils": {
28+
"types": "./dist/src/utils.d.ts",
29+
"default": "./dist/src/utils.js"
2230
}
2331
},
2432
"files": [
@@ -47,21 +55,22 @@
4755
"prepublishOnly": "pnpm run build"
4856
},
4957
"devDependencies": {
58+
"@ethersproject/abi": "5.7.0",
59+
"@ethersproject/providers": "5.7.2",
5060
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
5161
"@openzeppelin/contracts": "3.4.2",
5262
"@openzeppelin/contracts-upgradeable": "3.4.2",
5363
"@typechain/ethers-v5": "^10.2.1",
5464
"@wagmi/cli": "^2.3.1",
5565
"ethers": "catalog:",
5666
"ethers-v5": "npm:[email protected]",
57-
"@ethersproject/abi": "5.7.0",
58-
"@ethersproject/providers": "5.7.2",
5967
"hardhat": "catalog:",
6068
"hardhat-ignore-warnings": "catalog:",
6169
"markdownlint-cli": "catalog:",
6270
"prettier": "catalog:",
6371
"prettier-plugin-solidity": "catalog:",
6472
"solhint": "catalog:",
73+
"ts-node": "catalog:",
6574
"typechain": "^8.3.2",
6675
"viem": "^2.31.7"
6776
}

packages/interfaces/scripts/build.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ find_files() {
6464
log_info "📦 Compiling contracts with Hardhat..."
6565
pnpm hardhat compile $([[ "$VERBOSE" != "true" ]] && echo "--quiet")
6666

67-
# Step 1.5: Generate interface IDs
68-
node scripts/generateInterfaceIds.js
67+
# Step 1.5: Add interface IDs to generated factory files (only if needed)
68+
missing_ids=$(grep -rL "static readonly interfaceId" types/factories --include="*__factory.ts" 2>/dev/null | wc -l)
69+
70+
if [[ $missing_ids -gt 0 ]]; then
71+
# Slow operation, only run if needed
72+
npx ts-node scripts/utils/addInterfaceIds.ts types/factories
73+
fi
6974

7075
# Step 2: Generate types (only if needed)
7176
log_info "🏗️ Checking type definitions..."

packages/interfaces/scripts/generateInterfaceIds.js

Lines changed: 0 additions & 179 deletions
This file was deleted.

0 commit comments

Comments
 (0)