在Remix Ethereum Firefox中部署该令牌后,我需要知道该令牌的余额。我想知道在哪里添加checkBalance函数。请帮助伙计们。这是我的第一个ERC20智能合约。
pragma solidity ^0.5.0;
contract TusharCoin {
uint256 public totalSupply;
string public name;
string public symbol;
uint32 public decimals;
address public owner;
mapping(address => uint256 ) balances;
event Transfer(address to, uint256 amount);
constructor () public {
symbol = "TUSHAR";
name = "TusharCoin";
decimals = 5;
totalSupply = 100000000000;
owner = msg.sender;
balances[msg.sender] = totalSupply;
emit Transfer(msg.sender, totalSupply);
}
}
答案 0 :(得分:0)
您的令牌当前不是ERC20令牌,因为它尚未完全实现ERC20标准。
要将balanceOf
函数添加到现有合同中,可以添加以下内容:
function balanceOf(address account) public view returns (uint256) {
return balances[account];
}
如果要创建自己的ERC20实现,则应考虑使用SafeMath,有关详细信息,请参阅文档:https://docs.openzeppelin.com/contracts/2.x/utilities#math
如果您正在创建ERC20令牌,则可能需要查看OpenZeppelin Contracts实施以查看它是否满足您的需求。有关详细信息,请参见文档:https://docs.openzeppelin.com/contracts/2.x/tokens#ERC20
下面是您可以使用Remix部署的示例ERC20令牌的示例,该令牌是从OpenZeppelin Contracts实现继承而来的,使用您指定的名称,符号,小数和totalSupply:
pragma solidity ^0.5.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20Detailed.sol";
contract Token is ERC20, ERC20Detailed {
constructor () public ERC20Detailed("Tushar Token", "TUSHAR", 5) {
_mint(msg.sender, 1000000 * (10 ** uint256(decimals())));
}
}
如果您对使用OpenZeppelin有疑问,可以在社区论坛中提问:https://forum.openzeppelin.com/
披露:我是OpenZeppelin的社区经理
答案 1 :(得分:0)
下面,我提到checkBalance函数。在ERC20标准中,将checkBalance函数声明为balanceOf函数。 在功能上,视图意味着只能读不写
function balanceOf(address accountAddress) public view returns (uint256) {
return balances[accountAddress];
}
完整的源代码。
pragma solidity ^0.5.0;
contract TusharCoin {
uint256 public totalSupply;
string public name;
string public symbol;
uint32 public decimals;
address public owner;
mapping(address => uint256 ) balances;
event Transfer(address to, uint256 amount);
constructor () public {
symbol = "TUSHAR";
name = "TusharCoin";
decimals = 5;
totalSupply = 100000000000;
owner = msg.sender;
balances[msg.sender] = totalSupply;
emit Transfer(msg.sender, totalSupply);
}
function balanceOf(address accountAddress) public view returns (uint256) {
return balances[accountAddress];
}
}
如果您需要ERC20的完整代码。让我知道。