模仿ERC20写的代币合约

  • arrom
  • 更新于 2022-11-16 16:12
  • 阅读 1885

模仿ERC20写的代币合约

标准接口

//总发行量
function totalSupply() public view returns (uint256) 

// 指定账户的余额
function balanceOf(address _owner) public view returns (uint256 balance)

//币的持有人直接调用,进行转账
function transfer(address _to, uint256 _value) public returns (bool success)

// 1. 我这个owner对合约进行approve,此时approve内部会修改allowance变量
// 2. 合约内部调用transferFrom来支配owner的token
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)

// owner: 币的持有人
// spender: 是指定帮助花费的代理人(被授权的人)
function allowance(address owner, address spender) external view returns (uint); // 授权的额度

// decimals view,这是一个public 的变量,自动提供了一个读取的方法 // 返回精度
// 持有人对spender进行授权,在approve内部,会调用msg.sender来知道owner是谁
function approve(address spender, uint amount) external returns (bool);

// 2  事件
event Transfer(address indexed _from, address indexed _to, uint256 _value)
event Approval(address indexed _owner, address indexed _spender, uint256 _value)

// 3. 
function name() public view returns (string)
function symbol() public view returns (string)
function decimals() public view returns (uint8)

定义相关接口:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

interface ERC20Interface {
    function totalSupply() external view returns (uint256);

    function balanceOf(address _owner) external view returns (uint256 balance);

    function transfer(address _to, uint256 _value)
        external
        returns (bool success);

    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    ) external returns (bool success);

    function approve(address _spender, uint256 _value)
        external
        returns (bool success);

    function allowance(address _owner, address _spender)
        external
        view
        returns (uint256 remaining);

    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    event Approval(
        address indexed _owner,
        address indexed _spender,
        uint256 _value
    );
}

实现接口的合约代码

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "./ERC20Interface.sol";

contract ERC20 is ERC20Interface {
    string private _name;
    string private _symbol;
    uint8 private _decimals;

    mapping(address => uint256) balances;

    mapping(address => mapping(address => uint256)) private allowed;

    constructor() {
        _name = "Arrom";
        _decimals = 18;
        _symbol = "AOM";
        balances[msg.sender] = totalSupply();
    }

    function name() public view returns (string memory) {
        return _name;
    }

    function symbol() public view returns (string memory) {
        return _symbol;
    }

    function decimals() public view returns (uint8) {
        return _decimals;
    }

    function totalSupply() public pure returns (uint256) {
        return 10_0000_0000;
    }

    function balanceOf(address _owner) external view returns (uint256 balance) {
        return balances[_owner];
    }

    function transfer(address _to, uint256 _value)
        external
        returns (bool success) 
    {
        require(_to != address(0));
        require(balances[msg.sender] >= _value);
        require(balances[_to] + _value >= balances[_to]);

        balances[msg.sender] -= _value;
        balances[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    ) external returns (bool success) {
        require(_to != address(0));
        require(balances[_from] >= _value);

        require(allowed[_from][msg.sender] >= _value);

        require(balances[_to] + _value >= balances[_to]);

        balances[_from] -= _value;
        balances[_to] += _value;
        emit Transfer(_from, _to, _value);
        return true;
    }

    function approve(address _spender, uint256 _value)
        external
        returns (bool success)
    {
        require(_spender != address(0));
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);

        return true;
    }

    function allowance(address _owner, address _spender)
        external
        view
        returns (uint256 remaining)
    {
        return allowed[_owner][_spender];
    }
}

部署成功的

image.png

转账的截图

image.png

点赞 2
收藏 2
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
arrom
arrom
江湖只有他的大名,没有他的介绍。