Solidity
TypeError:覆蓋函式將狀態可變性從“view”更改為“nonpayable”
我有2個問題。
- 部署契約時沒有設置我的建構子值。所有需要
name
、symbol
、decimals
和的函式totalSupply
。2a. 函式
name()
、symbol()
、decimals()
和totalSupply()
都balanceOf()
標記為TypeError: Overriding function 將狀態可變性從“view”更改為“nonpayable”。2b。如果我添加
view
了我不應該添加的,我將balanceOf
被標記為view
在除此之外的所有內容上使用balanceOf
。typeError: Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; abstract contract ERC20{ function name() external virtual view returns (string memory); function symbol() external virtual view returns (string memory); function decimals() external virtual view returns (uint8); function totalSupply() external virtual view returns (uint256); function balanceOf(address _owner) external virtual view returns (uint256 balance); function transfer(address _to, uint256 _value) external virtual returns (bool success); function transferFrom(address _from, address _to, uint256 _value) external virtual returns (bool success); function approve(address _spender, uint256 _value) external virtual returns (bool success); function allowance(address _owner, address _spender) external virtual view returns (uint256 remaining); event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); } contract Ownership{ address public contractOwner; address public newContractOwner; constructor(){ contractOwner = msg.sender; } function changeOwnership(address _newContractOwner) external returns(bool success){ require(msg.sender == contractOwner, "Not contract owner"); _newContractOwner = newContractOwner; return success; } function approveOwnershipChange() external returns(bool success){ require(msg.sender == newContractOwner, "Not the new contract owner"); newContractOwner = contractOwner; return success; } } contract FullContact is ERC20, Ownership{ constructor(string memory name_, string memory symbol_, uint8 decimals_, uint256 totalSupply_ ){ } string public _name; string _symbol; uint8 _decimals; uint256 _totalSupply; mapping(address => uint256) _balance; mapping(address => mapping( address => uint256)) _allowance; mapping(address => uint256) minter; function name() external override returns (string memory){ _name = "Fog Of War"; return _name; } function symbol() external override returns (string memory){ _symbol = "FOW"; return _symbol; } function decimals() external override returns (uint8){ _decimals = 18; return _decimals; } function totalSupply() external override returns (uint256){ _totalSupply = 100000000; return _totalSupply; } function balanceOf(address _owner) external override view returns (uint256 balance){ return _balance[_owner]; } function transfer(address _to, uint256 _value) external override returns (bool success){ require(_balance[msg.sender] >= _value, "Insufficent Funds"); _balance[msg.sender] -= _value; _balance[_to] += _value; emit Transfer(msg.sender, _to, _value); return success; } function transferFrom(address _from, address _to, uint256 _value) external override returns (bool success){ uint256 approvedAmount = _allowance[_from][_to]; require(approvedAmount <= _value, "Insufficent allowance"); _balance[_from] -= _value; _balance[_to] += _value; emit Transfer(_from, _to, _value); return success; } function approve(address _spender, uint256 _value) external override returns (bool success){ require(_balance[msg.sender] >= _value, "Insufficent Funds"); _allowance[msg.sender][_spender] = _value; emit Approval(msg.sender, _spender, _value); return success; } function allowance(address _owner, address _spender) external override view returns (uint256 remaining){ return _allowance[_owner][_spender]; } }
我認為您不能在頂部定義的抽象契約中使用“視圖”。您使用的模板用於介面。介面提供覆蓋“視圖”功能,但不提供抽象契約。因為現在您正在做的是嘗試在視圖函式中為字元串設置一個值,這是不允許的。
我建議您將抽象合約更改為介面,或使用標準 ERC20 合約。