目录
WETH是什么与ETH的关系
为什么需要WETH
WETH合约
合约重点关注
wETH总结
参考
WETH是什么与ETH的关系
Put plainly, wETH is "wrapped ETH" but let's start by introducing the players.
FIRST, THERE'S ETHER TOKEN
Ether or ETH is the native currency built on the Ethereum blockchain.
SECOND, THERE ARE ALT TOKENS
When a dApp (decentralized app) is built off of the Ethereum Blockchain it usually implements its own form of Token. Think Augur’s REP Token, or Bancor's BNT Token.
FINALLY THE ERC-20 STANDARD
ERC-20 is a standard developed after the release of ETH that defines how tokens are transferred and how to keep a consistent record of those transfers among tokens in the Ethereum Network.
即WETH是对ETH的包装。
首先,Ether or ETH是Ethereum区块链上的本地币种。
其次,建立在Ethereum区块链上的dApp (decentralized app),经常需要实现自己的Token。
最后,ERC-20标准是ETH之后开发的Token标准,用于定义tokens定义
为什么需要WETH
ETH DOESN’T CONFORM TO ITS OWN ERC-20 STANDARD.
As mentioned above, ETH was the proto-token of the Ethereum Alt tokens, which means it was built before the ERC-20 standard existed.
WRAPPING ETH ALLOWS YOU TO TRADE DIRECTLY WITH ALT TOKENS.
The reason you need wETH is to be able to trade ETH for other ERC-20 tokens on decentralized platforms like Radar Relay. Because decentralized platforms running on Ethereum use smart contracts to facilitate trades directly between users, every user needs to have the same standardized format for every token they trade. This ensures tokens don’t get lost in translation.
1. ETH并不兼容现在的ERC-20标准。
2.对于一些传输应用,为了在不同代币之间进行传输交换,需要采用相同的标准,来防止代币丢失。
WETH合约
合约地址:0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
/**
*Submitted for verification at Etherscan.io on 2017-12-12
*/
// Copyright (C) 2015, 2016, 2017 Dapphub
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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;
}
}
代码中也可以看出,这是一个标准的ERC-20的Token,Token的名称是“Wrapped Ether”,标识是“WETH”,有效位是18位。
1、第16行代码,合约的fallback函数调用了deposit方法(存款),这也意味着使用imtoken这样的钱包,直接给该合约地址转账以太币就能够完成兑换WETH。
2、第19行代码,合约的deposit方法,用来接收以太币,逻辑很简单,就是给调用者地址的WETH余额增加了接收到的以太币数量。
3、第23行代码,合约的withdraw方法,用来提取以太币,将WETH转换回以太币,wad参数是提取的数量。该方法首先判断了账户余额是否足够,其次对账户的WETH余额减去提取数量,再向合约调用地址发送对应的以太币。
其它approve、transfer、transferFrom都是ERC-20的标准方法
合约重点关注
1. 该合约声明了4个event
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);
1. 在进行ETH->WETH进行包装时,调用的是deposit()函数,该函数触发的是Deposit事件。
2.在进行WETH->ETH进行提现时,调用的是withdraw()函数,该函数触发的是Withdrawal事件。
也就是说这两种转换都不会吃触发标准ERC20的Transfer时候的Transfer事件,这样我我们在通过解析Tx的Logs的时候,就需要特别注意处理下Transfer以外Deposit和Withdrawal事件,具体在Etherscan展示如下:
可以看到上图是在进行withdraw操作,目标是将0.185的WETH提现为ETH。具体操作Log也只有Withdrawal事件,如下图所示:
wETH总结
WETH是一种符合ERC-20标准以太坊代币,与以太坊网络的原生代币以太币(ETH)可以进行互换,可以将1ETH兑换为1WETH,同时也可以将1WETH兑换回1ETH,兑换是基于智能合约完成的。
ERC-20是建立在以太坊上的一种标准Token接口,只要你开发的应用程序支持ERC-20标准,那你的程序就可以处理所有依据ERC-20标准创建出来的Token。而以太币(ETH)是以太坊网络的原生代币,并不符合ERC-20标准,这就会造成应用程序需要对ETH相关的逻辑进行单独处理。
很多dApp的开发人员发现,如果是在去中心化交易的场景下,两种符合ERC-20标准的Token可以很容易实现交易,而以太币和某一ERC-20的Token进行交易,则复杂的多。如果将以太币抽象成为符合ERC-20标准的Token,就可以达到简化智能合约的目的,因此WETH应运而生。
参考
wETH | ERC20 tradable version of ETH
Wrapped Ether | Address 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 | Etherscan
https://etherscan.io/token/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
一种符合ERC-20标准的ETH-WETH - 简书文章来源:https://www.toymoban.com/news/detail-595769.html
https://etherscan.io/tx/0x8ec6ca56559e1479f31e7a5cd388816756b7b1946638fbaf0d3321c8003f2909文章来源地址https://www.toymoban.com/news/detail-595769.html
到了这里,关于从WETH标准合约分析WETH前世今生的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!