团结,将Eth从合同中包裹起来



我正试图从智能合约中包装Eth,因为我想稍后在uniswap中交换weth,但我不知道如何从goerli-scanWETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;导入weth代码。根据互联网上的例子,我完成了包装Eth,但所有有影响力的人都只导入了一个ERC20库来创建一个新库。我不明白他们为什么使用这个,因为他们没有与正确的湿合同互动。

这是我使用的代码,但只是在创建一个新的令牌。有人能给我一些建议吗?

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;
pragma abicoder v2;
import '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';
import '@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol';
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

contract SwapExamples is ERC20 {
// For the scope of these swap examples,
// we will detail the design considerations when using `exactInput`, `exactInputSingle`, `exactOutput`, and  `exactOutputSingle`.
// It should be noted that for the sake of these examples we pass in the swap router as a constructor argument instead of inheriting it.
// More advanced example contracts will detail how to inherit the swap router safely.
// This example swaps DAI/WETH9 for single path swaps and DAI/USDC/WETH9 for multi path swaps.
ISwapRouter public immutable swapRouter;
address payable [] private s_Wallets;
uint256 public walletA = address(this).balance;
// Router = 0xE592427A0AEce92De3Edee1F18E0157C05861564
address public constant WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6; //0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

// For this example, we will set the pool fee to 0.3%.
uint24 public constant poolFee = 3000;
uint256 public UsdOut;
constructor(ISwapRouter _swapRouter) ERC20("Wrapped Ether", "WETH") {//ERC20("Wrapped Ether", "WETH") 
swapRouter = _swapRouter;
}
function Deposit() public payable {
s_Wallets.push(payable(msg.sender));

} 
function Mint() external payable {
_mint(address(this), address(this).balance);
}
}

您不需要创建新的ERC20令牌。

呼叫:WETH.deposit.value(msg.value)();帮助你包装你的ETH,你不需要从任何地方导入WETH代码。

包装后,您可以像您所说的那样,将WETH换成Uniswap上的任何其他代币。

最新更新