简体   繁体   English

如何使用 UniswapV2 将 ETH 与 WETH 交换?

[英]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.我正在尝试使用 UniswapRouterV2 和 ethers.js 将 ETH 交换为 WETH 中的等价物,但我不知道要在路径数组中作为 ETH 放入哪个地址,因为它没有,所以请让我知道我做错了。

    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.就这么简单,根本不需要 UniswapRouterV2 合约。 One has to execute the deposit function of WETH contract and the contract gives ETH back.必须执行 WETH 合约的存款 function 并且合约将 ETH 返还。 Something like this:像这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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