美文网首页
2023-11-30 erc20转账逻辑

2023-11-30 erc20转账逻辑

作者: gdlooker | 来源:发表于2023-11-29 11:53 被阅读0次

    下面是自己发行一个代币示例代码如下:

    contract  GdLooker is IERC20{
    
        mapping(address => uint256) public override balanceOf;
    
        mapping(address => mapping(address => uint256)) public override allowance;
    
        uint256 public override totalSupply;   // 代币总供给
    
        string public name="GdLooker";   // 在钱包添加合约地址的时候该名称会立即在钱包显示
        string public symbol;  // 代号
    
        uint8 public decimals = 18; // 小数位数
    
        constructor(string memory name_, string memory symbol_){
            name = name_;
            symbol = symbol_;
        }
    
        function transfer(address to, uint amount) external override returns (bool) {
            balanceOf[msg.sender] -= amount;
            balanceOf[to] += amount;
            emit Transfer(msg.sender, to, amount);
            return true;
        }
    
        function approve(address spender, uint amount) external override returns (bool) {
            allowance[msg.sender][spender] = amount;
            emit Approval(msg.sender, spender, amount);
            return true;
        }
    
        function transferFrom(
            address sender,
            address recipient,
            uint amount
        ) external override returns (bool) {
            allowance[sender][msg.sender] -= amount;
            balanceOf[sender] -= amount;
            balanceOf[recipient] += amount;
            emit Transfer(sender, recipient, amount);
            return true;
        }
    }
    

    上面示例代码解读 transfer跟transferFrom的区别
    首先 用了2个map key是作为用户的地址 value专门用来存储用户有多少币数
    allowance //这个是已经授权的map 基于key(也就是钱包地址)来缓存用户在拉起钱包授权输入数量作为value值
    balanceOf 是用来存储所有用户的余额

    相关文章

      网友评论

          本文标题:2023-11-30 erc20转账逻辑

          本文链接:https://www.haomeiwen.com/subject/ntcfgdtx.html