它一直带来这个错误ParserError:这行代码中需要函数、变量、结构或修饰符声明public address owner;
这里是完整的代码
pragma solidity ^0.4.24;
contract BasicToken is owned {
uint public totalSupply;
string public name;
string public symbol;
uint public decimals = 18;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed _from, address indexed _to, uint tokens);
event Approval(address indexed _tokenOwner, address indexed _spender, uint tokens);
event Burn(address indexed _from, uint256 _value);
constructor (string tokenName, string tokenSymbol, uint initialSupply) public {
totalSupply = initialSupply*10**uint256(decimals);
balanceOf[msg.sender]= totalSupply;
name = tokenName;
symbol = tokenSymbol;
}
function _transfer(address _from,address _to,uint256 _value ) internal {
require(_to != 0x0);
require(balanceOf[_from] >= _value);
require(balanceOf[_to] + _value >= balanceOf[_to]);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
}
function transfer(address _to, uint256 _value) public returns (bool success){
_transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success){
require(_value <= allowance[_from][msg.sender]);
allowance [_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns(bool success){
allowance[msg.sender][_spender] = _value;
emit Approval (msg.sender, _spender, _value);
return true;
}
function mintToken (address _target, uint256 _mintedAmount) onlyOwner {
balanceOf[_target] += _mintedAmount;
totalSupply += _mintedAmount;
emit Transfer(0, owner, _mintedAmount);
emit Transfer(owner, _target, _mintedAmount);
}
function burn(uint256 _value) onlyOwner returns(bool success) {
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
totalSupply -= _value;
emit Burn(msg.sender, _value);
return true;
}
}
contract owned {
public address owner;
constructor {
owner = msg.sender;
}
modifier onlyOwner{
require(msg.sender == owner);
_;
}
function transferOwnership (address newOwner) onlyOwner{
owner = newOwner;
}
}
存在一些错误:
own
合同
1-代替public address owner
address public owner;
2-而不是constructor
constructor()
3-增加transferOwnership
的可见性。我添加了public
function transferOwnership(address newOwner) public onlyOwner{
4-因为你是owner
合同的继承人,所以把它放在首位。否则你会得到";基础的定义必须先于派生合同的定义;错误
BasicToken
合同
1-为的最后几个功能添加类似public
的可见性
2-在constructor
签名中删除public
(因为我在0.8.7版本编译,但在您的版本中我想这是必要的(,并为字符串参数添加memory
constructor (string memory tokenName, string memory tokenSymbol, uint initialSupply) {
3-
function _transfer(address _from,address _to,uint256 _value ) internal {
// not 0x0 because _to is address type
require(_to != 0x0000000000000000000000000000000000000000);
// or require(_to != address(0);
4-在mintToken
中
function mintToken (address _target, uint256 _mintedAmount) public onlyOwner {
balanceOf[_target] += _mintedAmount;
totalSupply += _mintedAmount;
// not 0, address(0)
emit Transfer(address(0), owner, _mintedAmount);
emit Transfer(owner, _target, _mintedAmount);
}
我在pragma solidity ^0.8.7;
版本编译了它