我写了一个erc20代币合同,并部署在rinkeby tetstnet中。我给toatl供应量= 1000000,但是我的令牌余额在元掩码中显示为0。我如何获得令牌并告诉我如何获得令牌。以下是我的合同
pragma solidity ^0.5.0;
contract COCOTOKEN {
string public constant symbol = "COCO";
string public constant name = "COCOTOKEN";
uint8 public constant decimals = 18;
uint256 totalSupply = 1000000;
address public owner;
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
constructor() public{
owner = msg.sender;
balances[owner] = totalSupply;
}
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
function transfer(address _to, uint256 _amount) public returns (bool success) {
if (balances[msg.sender] >= _amount
&& _amount > 0
&& balances[_to] + _amount > balances[_to]) {
balances[msg.sender] -= _amount;
balances[_to] += _amount;
emit Transfer(msg.sender, _to, _amount);
return true;
} else {
return false;
}
}
function transferFrom(
address _from,
address _to,
uint256 _amount
) public returns (bool success) {
if (balances[_from] >= _amount
&& allowed[_from][msg.sender] >= _amount
&& _amount > 0
&& balances[_to] + _amount > balances[_to]) {
balances[_from] -= _amount;
allowed[_from][msg.sender] -= _amount;
balances[_to] += _amount;
emit Transfer(_from, _to, _amount);
return true;
} else {
return false;
}
}
function approve(address _spender, uint256 _amount) public returns (bool success) {
allowed[msg.sender][_spender] = _amount;
emit Approval(msg.sender, _spender, _amount);
return true;
}
function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
return allowed[_owner][_spender];
}
}
,并且已部署的合同地址为“ 0xc3384a37d041b99d437734a80e88b39e0efa630d”。为什么令牌余额显示为0。在rinkeby etehrscan中,它显示liks 链上令牌属性检查结果: 总供应量= 0 名称= COCOTOKEN 符号= COCO 小数= 18 ERC-165接口= {不可用} 实现ERC-721 = {不可用}。 有人可以告诉我如何添加令牌吗?
答案 0 :(得分:0)
因为那是您醚的很小一部分。只需从水龙头转移乙醚。 或仅以wei单位1000000000000000000进行转移,那么您的屏幕上就会看到1个以太币。
答案 1 :(得分:0)
您需要将totalSupply
的小数位数增加18位。有关decimal
和totalSupply
的更多说明,请选中此Answer。
答案 2 :(得分:0)
您在totalSupply处设置的值太低,可以检查erc20令牌标准here。在构造函数中,他们像下面的代码一样设置totalSupply:
constructor() public {
symbol = "FIXED";
name = "Example Fixed Supply Token";
decimals = 18;
_totalSupply = 1000000 * 10**uint(decimals);
balances[owner] = _totalSupply;
emit Transfer(address(0), owner, _totalSupply);
}
答案 3 :(得分:0)
您提供的总供应量就像totalsupply = 1000000 * 10 ^ -18等于0.0000000000001,因此当您尝试执行任何交易时,它显然将显示为零。
答案 4 :(得分:0)
这是使用OpenZeppelin的完整实现,它将总供应量设置为1000000,并在合同初始化时将代币分配给您。
pragma solidity 0.5.2;
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
contract TokenMock is ERC20
{
constructor () public {
_mint(msg.sender, 1000000);
}
}