Chainlink KeeperRegistry2.0与Typechain最新版本兼容问题求助
问题:Typechain v8与Chainlink合约兼容报错
在使用Hardhat创建项目并通过Typechain v8.1.1提供TypeScript支持时,运行npx hardhat typechain会在@chainlink/contracts/src/v0.8/vendor/@arbitrum/nitro-contracts/src/precompiles/ArbGasInfo.sol相关生成代码处崩溃,报错如下:
SyntaxError: Identifier expected. (4:18) 2 | /* tslint:disable */ 3 | /* eslint-disable */ > 4 | import type * as 086 from './0.8.6'; | ^ 5 | export type { 086 };
问题根源在于导入Chainlink的KeeperRegistry2.0和KeeperRegistrar2.0合约时,Typechain生成的TypeScript文件中使用了以数字开头的标识符086,这不符合TS语法规范。以下是几种可行的解决方法:
解决方法
1. 排除冲突合约的Typechain生成
修改hardhat.config.ts中的Typechain配置,通过exclude字段排除Arbitrum nitro合约相关路径,只针对自身合约和需要的Chainlink合约生成类型:
import { HardhatUserConfig } from "hardhat/config"; import "@typechain/hardhat"; const config: HardhatUserConfig = { // ...其他配置 typechain: { outDir: "typechain-types", target: "ethers-v5", // 匹配你使用的ethers版本 exclude: [ "**/@arbitrum/nitro-contracts/**/*.sol", // 可添加其他无需生成类型的合约路径 ], }, }; export default config;
2. 降级Typechain到兼容版本
由于Chainlink合约依赖Typechain 5.0.0,降级Typechain到v6.x版本(与v5兼容性更好)可规避版本差异问题:
# 同步降级typechain和对应的hardhat插件 npm install typechain@6 @typechain/hardhat@6 --save-dev
注意确保ethers版本与插件兼容,推荐使用ethers v5。
3. 临时手动修复生成文件
若仅用于临时测试,可在Typechain生成代码后,找到报错文件,将086替换为合法标识符(如v086):
// 修改前 import type * as 086 from './0.8.6'; export type { 086 }; // 修改后 import type * as v086 from './0.8.6'; export type { v086 };
此方法需每次生成后重复操作,不适合长期开发场景。
4. 使用Chainlink官方类型定义
检查是否存在Chainlink官方发布的TypeScript类型包,若有可直接安装使用,无需自行生成:
npm install @chainlink/contracts-ts --save-dev
内容的提问来源于stack exchange,提问作者ivaylo nikolov
相关产品推荐
相关产品推荐

