You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Hardhat编译Solidity合约时出现参数数量错误

问题解决:Solidity 0.5.0下ERC20合约编译错误

问题描述

编译以下Solidity智能合约代码时出现参数不匹配错误:

pragma solidity 0.5.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/ownership/Ownable.sol";
 
 
contract GToken is ERC20, Ownable {
     
    constructor() ERC20("GToken", "GT") public {
    _mint(msg.sender, 50 * (10**18));
}
}

错误信息:

contracts/GToken.sol:10:19: TypeError: Wrong argument count for modifier invocation: 2 arguments given but expected 0.
    constructor() ERC20("GToken", "GT") public {
                  ^-------------------^

Error HH600: Compilation failed

原因分析

你使用的Solidity 0.5.0搭配的是旧版本OpenZeppelin ERC20合约,该版本的ERC20基类构造函数不需要传入代币名称和符号参数。早期OpenZeppelin的ERC20实现中,代币名称和符号是通过重写name()和symbol()视图函数来定义的,而非构造函数传参。

修复后的代码

pragma solidity 0.5.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/ownership/Ownable.sol";
 
contract GToken is ERC20, Ownable {
    constructor() public {
        // 向合约部署者铸造代币
        _mint(msg.sender, 50 * (10**18));
    }

    // 重写代币名称函数
    function name() public view returns (string memory) {
        return "GToken";
    }

    // 重写代币符号函数
    function symbol() public view returns (string memory) {
        return "GT";
    }
}

补充说明

若想通过构造函数传参定义代币名称和符号,需要将Solidity版本升级至0.8.x及以上,并搭配对应版本的OpenZeppelin合约(如OpenZeppelin v4.x+),这类新版本的ERC20基类才支持构造函数传入名称和符号参数。

内容的提问来源于stack exchange,提问作者Syed Ghufran Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:55:44