Solidity

Solidity:合約應標記為抽象

  • April 3, 2022

我正在嘗試編寫一個智能合約來為現有的加密貨幣生成 Staking Rewards 但我一直收到這個錯誤

合約“Stakeable”應標記為抽象

我該如何解決?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
// import "@openzeppelin/contracts/access/Ownable.sol";
// import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Stakeable is ERC20, Ownable{
       using SafeMath for uint256;
   
       string private constant _name = "Schrodinger";
       string private constant _symbol = "Kitty Dinger";
       
       /**
       * @notice We usually require to know who are all the stakeholders.
       */
       address[] internal stakeholders;
   
       /**
        * @notice The stakes for each stakeholder.
        */
       mapping(address => uint256) internal stakes;
   
       /**
        * @notice The accumulated rewards for each stakeholder.
        */
       mapping(address => uint256) internal rewards;
   
       /**
       * @notice The constructor for the Staking Token.
       * @param _owner The address to receive all tokens on construction.
       * @param _supply The amount of tokens to mint on construction.
       */
   
       constructor(string memory name_, string memory symbol_, address _owner, uint256 _supply)
       {    
           name_ = _name;
           symbol_ = _symbol;
           _mint(_owner, _supply);
       }

不要直接設置nameandsymbol屬性,ERC20而是添加對建構子的呼叫。

更新建構子程式碼:

constructor(string memory name_, string memory symbol_, address _owner, uint256 _supply) ERC20(name_, symbol_)
   {    
       _mint(_owner, _supply);
   }

@ashhanai 有一個很好的觀點。

您正在從 ERC20 繼承您的契約,但您沒有呼叫 ERC20 建構子。如果您像@ashhanai 的建議那樣更新建構子,則問題可能會消失。

引用自:https://ethereum.stackexchange.com/questions/125309