Solidity编译报错:abi.encodeCall未找到或不可见问题咨询
Solidity编译报错:abi.encodeCall未找到的解决方案
你编写的Solidity代码如下:
pragma solidity >=0.8.0 <0.9.0; function encodeCall(address _newOwner, uint _sigRequired) external view returns (bytes memory) { // Typo and type errors will not compile return abi.encodeCall(this._addSigner, (_newOwner, _sigRequired)); }
编译时遇到错误:TypeError: Member "encodeCall" not found or not visible after argument-dependent lookup in abi,且确认ABI库全局可用、Hardhat配置正常,但代码能在Remix中正常编译部署。
问题原因
abi.encodeCall是Solidity 0.8.11版本才新增的内置函数,你的pragma声明允许使用0.8.0到0.9.0之间的任意版本,但如果Hardhat配置的编译器版本低于0.8.11,就会触发该错误。而Remix默认可能使用了0.8.11及以上的编译器版本,因此可以正常编译。
解决方案
- 升级Hardhat编译器版本:
在项目根目录的hardhat.config.js或hardhat.config.ts中,将编译器版本指定为0.8.11到0.9.0之间的版本,示例配置:module.exports = { solidity: "0.8.19", // 选择0.8.11至0.8.20之间的稳定版本 }; - 低版本兼容替代方案:
如果需要兼容0.8.11以下的版本,可使用abi.encodeWithSignature替换abi.encodeCall,修改后的代码如下:
注意:使用function encodeCall(address _newOwner, uint _sigRequired) external view returns (bytes memory) { return abi.encodeWithSignature("_addSigner(address,uint256)", _newOwner, _sigRequired); }abi.encodeWithSignature时,函数签名里的参数类型必须严格匹配(比如uint要写全称uint256),否则会出现编码错误。
内容的提问来源于stack exchange,提问作者Rostyslav Dzhohola
相关产品推荐
相关产品推荐

