智能合约大巡礼--WETH

什么是WETH

WETH字面意思是"包装过的ETH"("wrapped ETH")。首先,Ether(ETH)是以太坊网络(Ethereum)上的原生币种,选择建立在以太坊上的dAPP通常会部署自己的代币,比如Augur的REP和Bancor的BNT。而ERC-20是在ETH发布之后制定的一个标准,这个标准定义了代币应该如何在以太坊网络上转移,以及如保持代币在以太坊网络上的转移能够有一个一致的记录。

ERC20标准:

interface IERC20 {
  function totalSupply() external view returns (uint256);
  function balanceOf(address who) external view returns (uint256);
  function allowance(address owner, address spender) external view returns (uint256);
  function transfer(address to, uint256 value) external returns (bool);
  function approve(address spender, uint256 value) external returns (bool);
  function transferFrom(address from, address to, uint256 value) external returns (bool);
  event Transfer(address indexed from, address indexed to, uint256 value);
  event Approval(address indexed owner, address indexed spender, uint256 value);
}

为什么需要wETH

ERC20是以太坊上最为普遍通用的代币标准,而ETH作为原生币种本身不符合ERC20标准,但一些dAPP对接的是ERC20标准代币,比如Radar Relay,0x Portal。所以我们需要利用智能合约实现一种兼具ERC20标准的token形式的ETH。

实现代码及前景

我们看看实现代码:

pragma solidity ^0.4.18;

contract WETH9 {
    string public name     = "Wrapped Ether";
    string public symbol   = "WETH";
    uint8  public decimals = 18;

    event  Approval(address indexed src, address indexed guy, uint wad);
    event  Transfer(address indexed src, address indexed dst, uint wad);
    event  Deposit(address indexed dst, uint wad);
    event  Withdrawal(address indexed src, uint wad);

    mapping (address => uint)                       public  balanceOf;
    mapping (address => mapping (address => uint))  public  allowance;

    function() public payable {
        deposit();
    }
    function deposit() public payable {
        balanceOf[msg.sender] += msg.value;
        Deposit(msg.sender, msg.value);
    }
    function withdraw(uint wad) public {
        require(balanceOf[msg.sender] >= wad);
        balanceOf[msg.sender] -= wad;
        msg.sender.transfer(wad);
        Withdrawal(msg.sender, wad);
    }

    function totalSupply() public view returns (uint) {
        return this.balance;
    }

    function approve(address guy, uint wad) public returns (bool) {
        allowance[msg.sender][guy] = wad;
        Approval(msg.sender, guy, wad);
        return true;
    }

    function transfer(address dst, uint wad) public returns (bool) {
        return transferFrom(msg.sender, dst, wad);
    }

    function transferFrom(address src, address dst, uint wad)
        public
        returns (bool)
    {
        require(balanceOf[src] >= wad);

        if (src != msg.sender && allowance[src][msg.sender] != uint(-1)) {
            require(allowance[src][msg.sender] >= wad);
            allowance[src][msg.sender] -= wad;
        }

        balanceOf[src] -= wad;
        balanceOf[dst] += wad;

        Transfer(src, dst, wad);

        return true;
    }
}

代码非常简单,实现了ERC20标准,并提供depositwithdraw函数完成原生ETH与WETH之间的1:1置换。

在etherscan上可以看到WETH自2017年12月12号部署以来,目前合约内抵押了230万个Ether,也就是有同样数量的WETH处于流通状态。

更新以太坊代码使ETH符合ERC-20标准的计划已经在实施了。wETH只是目前阶段为满足一些使用场景,在合约层面做的实现。如果你有需要,可以直接使用WETH。

你可能感兴趣的:(智能合约大巡礼--WETH)