Solidity编译错误.(合同应标记为摘要)



我有两个问题

1(我收到以下错误:

TypeError: Contract "MyToken" should be marked as abstract.
--> contracts/MyToken.sol:8:1:

根据我的理解,当有一个未实现的功能时,合同应该是抽象的。这里有函数foo。但仍然会出现这个错误?

2(我还想写一个构造函数,将totalSupply_传递给合约。有可能以我所做的方式实施吗

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
//import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
contract MyToken is ERC20 {
uint256 private _totalSupply;
string private _name;
string private _symbol;
constructor(string memory name_,string memory symbol_, uint totalSupply_ ) {
_name = name_;
_symbol = symbol_;
_totalSupply = totalSupply_;
}
function foo() external  returns (uint) {
uint temp;
temp = 1+1;
return temp;
}
}

试试这个:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
//import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
contract MyToken is ERC20 {
uint256 private _totalSupply;
constructor(string memory name_,string memory symbol_, uint totalSupply_ )  ERC20(name_, symbol_) {
_totalSupply = totalSupply_;
}
function foo() external  returns (uint) {
uint temp;
temp = 1+1;
return temp;
}
}

您从ERC20继承,但没有调用其构造函数

constructor(string memory name_,string memory symbol_,uint totalSupply_)ERC20("name","SYM") {
_name = name_;
_symbol = symbol_;
_totalSupply = totalSupply_;
}

在您的情况下,您必须调用ERC20("name","SYM"),因为ERC20是从abstract Context类继承的。

contract ERC20 is Context, IERC20, IERC20Metadata {

如果您没有从Context继承,则不必调用ERC20("name","SYM")

contract ERC20 is IERC20, IERC20Metadata {

由于您正在调用ERC20("name","SYM"),所以实际上您正在设置名称和符号,因此不必在MyToken构造函数中设置它们

uint256 private _totalSupply;
constructor(uint totalSupply_ )ERC20("name","SYM") {
_totalSupply = totalSupply_;
}

相关内容

  • 没有找到相关文章

最新更新