将以太坊封装为 ERC20

Posted baizx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将以太坊封装为 ERC20相关的知识,希望对你有一定的参考价值。

将以太坊封装为 ERC20 TOKEN

很多 DAPP 都是在处理 ERC20接口的 token, 其实很容易将以太坊封装为 ERC20,这样就可以统一处理,
至少我目前在做的雷电网络就是这么处理的.

主要内容复制在网络
https://programtheblockchain.com/posts/2018/05/26/wrapping-ether-in-an-erc20-token/

直接上代码,核心部分是

pragma solidity ^0.4.24;

import "baseerc20token.sol";

contract EtherToken is BaseERC20Token {
    constructor(string _name, string _symbol)
        BaseERC20Token(0, 18, _name, _symbol) public
    {
    }

    function buy() public payable {
        balanceOf[msg.sender] += msg.value;
        totalSupply += msg.value;

        emit Transfer(address(0), msg.sender, msg.value);
    }

    function sell(uint256 amount) public {
        require(balanceOf[msg.sender] >= amount, "Insufficient balance.");

        balanceOf[msg.sender] -= amount;
        totalSupply -= amount;
        msg.sender.transfer(amount);

        emit Transfer(msg.sender, address(0), amount);
    }
}

它提供了以太坊和 ERC20互换的接口,换成 ERC20以后就很简单了.
实际上baseerc20token.sol只是一个非常标准的 ERC20实现

pragma solidity ^0.4.23;

contract BaseERC20Token {
    mapping (address => uint256) public balanceOf;

    string public name;
    string public symbol;
    uint8 public decimals;
    uint256 public totalSupply;

    event Transfer(address indexed from, address indexed to, uint256 value);

    constructor (
        uint256 _totalSupply,
        uint8 _decimals,
        string _name,
        string _symbol
    )
        public
    {
        name = _name;
        symbol = _symbol;
        decimals = _decimals;

        totalSupply = _totalSupply;
        balanceOf[msg.sender] = _totalSupply;
        emit Transfer(address(0), msg.sender, _totalSupply);
    }

    function transfer(address to, uint256 value) public returns (bool success) {
        require(balanceOf[msg.sender] >= value);

        balanceOf[msg.sender] -= value;
        balanceOf[to] += value;
        emit Transfer(msg.sender, to, value);
        return true;
    }

    event Approval(address indexed owner, address indexed spender, uint256 value);

    mapping(address => mapping(address => uint256)) public allowance;

    function approve(address spender, uint256 value)
        public
        returns (bool success)
    {
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;
    }

    function transferFrom(address from, address to, uint256 value)
        public
        returns (bool success)
    {
        require(value <= balanceOf[from]);
        require(value <= allowance[from][msg.sender]);

        balanceOf[from] -= value;
        balanceOf[to] += value;
        allowance[from][msg.sender] -= value;
        emit Transfer(from, to, value);
        return true;
    }
}

这样简单部署,就可以在雷电网络中将以太坊当做普通的 ERC20 TOKEN进行处理了.

以上是关于将以太坊封装为 ERC20的主要内容,如果未能解决你的问题,请参考以下文章

什么是以太坊ERC20和ERC721

以太坊上发行ERC20代币

如何检测以太坊地址是不是为 ERC20 代币合约?

以太坊开发入门-ERC20合约

第三方可以向以太坊区块链发送 ERC20 代币交易吗?

ERC20 代币余额如何存储在以太坊区块链上