In implementation about uniswapv2router, uniswapv2router.WETH()
returns the canonical WETH address.
I want to know how it returns the WETH address. One thing I know is that in the uniwapv2router.sol code, the constructor sets the WETH
value. However, in the below example, I can't see any value given in initialization of 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.
If you wanted to deploy the specified contract, you'd need to use the new
keyword:
// can't deploy an interface, needs to be a contract
UniswapV2Router02 newlyDeployedRouter = new UniswapV2Router02(
// constructor params
factoryAddress,
wethAddress
);
Source code of UniswapV2Router02.sol
In the UniswapExample
contract example that you passed, in the constructor, the contract is getting reference to the already deployed UniswapV2Router02
contract.
constructor() {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
}
Now uniswapRouter
is a reference to the UniswapV2Router02 contract. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.