[英]How can uniswapv2router return WETH address
In implementation about uniswapv2router, uniswapv2router.WETH()
returns the canonical WETH address.在uniswapv2router的实现中, uniswapv2router.WETH()
返回规范的WETH地址。
I want to know how it returns the WETH address.我想知道它是如何返回 WETH 地址的。 One thing I know is that in the uniwapv2router.sol code, the constructor sets the WETH
value.我知道的一件事是,在 uniwapv2router.sol 代码中,构造函数设置了WETH
值。 However, in the below example, I can't see any value given in initialization of uniswapRouter
.但是,在下面的示例中,我看不到在uniswapRouter
的初始化中给出的任何值。 Sorry for the newbie question!抱歉新手问题!
pragma solidity 0.7.1;
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
contract UniswapExample {
address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;
IUniswapV2Router02 public uniswapRouter;
address private multiDaiKovan = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
constructor() {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
}
function convertEthToDai(uint daiAmount) public payable {
uint deadline = block.timestamp + 15; // using 'now' for convenience, for mainnet pass deadline from frontend!
uniswapRouter.swapETHForExactTokens{ value: msg.value }(daiAmount, getPathForETHtoDAI(), address(this), deadline);
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
function getEstimatedETHforDAI(uint daiAmount) public view returns (uint[] memory) {
return uniswapRouter.getAmountsIn(daiAmount, getPathForETHtoDAI());
}
function getPathForETHtoDAI() private view returns (address[] memory) {
address[] memory path = new address[](2);
path[0] = uniswapRouter.WETH();
path[1] = multiDaiKovan;
return path;
}
// important to receive ETH
receive() payable external {}
}
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
This snippet initializes a pointer to a contract deployed on the specified address ( UNISWAP_ROUTER_ADDRESS
), assuming it implements the IUniswapV2Router02
interface.该片段初始化一个指向部署在指定地址 ( UNISWAP_ROUTER_ADDRESS
) 上的合约的指针,假设它实现了IUniswapV2Router02
接口。
If you wanted to deploy the specified contract, you'd need to use the new
keyword:如果你想部署指定的合约,你需要使用new
关键字:
// can't deploy an interface, needs to be a contract
UniswapV2Router02 newlyDeployedRouter = new UniswapV2Router02(
// constructor params
factoryAddress,
wethAddress
);
Source code of UniswapV2Router02.sol UniswapV2Router02.sol源码
In the UniswapExample
contract example that you passed, in the constructor, the contract is getting reference to the already deployed UniswapV2Router02
contract.在您传递的UniswapExample
合约示例中,在构造函数中,合约正在引用已部署的UniswapV2Router02
合约。
constructor() {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
}
Now uniswapRouter
is a reference to the UniswapV2Router02 contract.现在uniswapRouter
是对 UniswapV2Router02 合约的引用。 If you check the source code, there is this:如果你检查源代码,有这样的:
address public immutable override WETH;
which is set in its constructor:这是在其构造函数中设置的:
constructor(address _factory, address _WETH) public {
factory = _factory;
WETH = _WETH;
}
Since WETH
is a public variable, solidity assigns it a getter function. Calling uniswapv2router.WETH()
will return that WETH
address由于WETH
是一个公共变量,solidity 为其分配了一个 getter function。调用uniswapv2router.WETH()
将返回该WETH
地址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.