简体   繁体   中英

How to swap ETH with WETH using UniswapV2?

I'm trying to swap ETH for its equivalent in WETH using UniswapRouterV2 and ethers.js but I don't know what address to put in as ETH in the path array since it doesn't have one, so please let me know what I'm doing wrong.

    await this.uniswapRouter.swapETHForExactTokens(
        ethers.provider.getBalance(attacker.addres), // amountOut
        [0, this.weth.address], // path: what should it be instead of 0?
        attacker.address, // to
        (await ethers.provider.getBlock('latest').timestamp) * 2, // deadline
        {value: ethers.provider.getBalance(attacker.addres)} // msg.value
    )

Just got it. It was so simple and the UniswapRouterV2 contract was not needed at all. One has to execute the deposit function of WETH contract and the contract gives ETH back. Something like this:

 await this.weth.connect(attacker).deposit({value: ethToWETHAmount})

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM