如何向此ERC20智能合约添加平衡检查功能



我需要知道这个代币在Remix Ethereum Firefox中部署后的余额。我想知道在哪里添加支票余额功能。请帮助伙计们。这是我的第一个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);
}
}

您的代币目前不是 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合约的实现,看看这是否满足您的需求。有关详细信息,请参阅文档:https://docs.openzeppelin.com/contracts/2.x/tokens#ERC20

您可以使用从OpenZeppelin合约实现继承的Remix部署的示例ERC20令牌如下,使用您指定的名称,符号,小数和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的社区经理

下面,我提到了checkBalance函数。在ERC20标准中,CheckBalance函数被声明为平衡功能。 在功能上,视图意味着一个人只能读而不能写

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的完整代码。让我知道。

相关内容

  • 没有找到相关文章

最新更新