简体   繁体   English

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

[英]Solidity compile error. (Contract should be marked as abstract.)

I have two questions.我有两个问题。

1) I am getting the following error: 1)我收到以下错误:

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

According to my understanding, contract should be abstract when there is a unimplemented function.根据我的理解,当有一个未实现的 function 时,合约应该是抽象的。 Here I have the function foo.在这里,我有 function foo。 But still getting this error?但仍然收到此错误?

2) Also I want write a constructor which passes totalSupply_ to the contract. 2)我还想编写一个将 totalSupply_ 传递给合同的构造函数。 Is it possible to implement in the way I have done?是否可以按照我的方式实施?

// 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;
    }
}

You are inheriting from ERC20 but you are not calling its constructor您是从ERC20继承的,但您没有调用它的构造函数

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

In your case you have to call ERC20("name","SYM") because ERC20 is inheriting from an abstract Context class.在您的情况下,您必须调用ERC20("name","SYM")因为ERC20是从abstract Context class 继承的。

contract ERC20 is Context, IERC20, IERC20Metadata {

if you did not inherit from Context you would not have to call ERC20("name","SYM")如果您没有从Context继承,则不必调用ERC20("name","SYM")

contract ERC20 is IERC20, IERC20Metadata {

Since you are calling ERC20("name","SYM") you are actually setting name and symbol so you dont have to set them in MyToken constructor:由于您正在调用ERC20("name","SYM")您实际上是在设置名称和符号,因此您不必在MyToken构造函数中设置它们:

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

Try this:尝试这个:

// 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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM