简体   繁体   English

uniswapv2router如何返回WETH地址

[英]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.

相关问题 如何使用 UniswapV2 将 ETH 与 WETH 交换? - How to swap ETH with WETH using UniswapV2? UniswapV3 position 铸币算法无法根据我传入的参数获得正确的矿池地址 - UniswapV3 position minting algorithm can't get correct pool address based on params I've passed in 无法使用 UniswapV2Router02 将 USDC 换成 USDT - Cant swap USDC for USDT with UniswapV2Router02 “amountIn”在 UniswapV2Router02 合约中有什么单位? - What unit does "amountIn" have in UniswapV2Router02 contracts? 如何使用 UniswapV2Router02 合约用 swapExactTokensForETH() 将 Token 换成 ETH - how to swap Token for ETH with swapExactTokensForETH() using UniswapV2Router02 Contract 合约部署在Polygon(Mumbai/Mai.net)上如何在WETH接受支付? - How To Accept Payment In WETH When The Contract Is Deployed on Polygon (Mumbai/Mainnet)? UniswapV2Pair,铸币函数,流动性是如何按对流动性增加的比例创造的 - UniswapV2Pair, mint function, how is liquidity is created proportion to pair liquidity added 我如何存储合约地址 - How can i store a contract address 如何通过 RSS 订阅以太坊地址? - How can I RSS subscribe to Ethereum address? 我如何查看哪个地址批准了 WBNB 为我的地址花费? - How can i see which address Approved a WBNB spend for my address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM