我希望使用我的实体契约将ETH转换为USDC,我使用uniswapV2契约将ETH转换成令牌,我使用这个函数swapExactETHForTokens,我无法获得将ETH交换到USDC的路径。
WETH和UniswapV2路由器的这个地址是给goerli的。
我的守则:-
interface IUniswapV2Router {
function swapExactETHForTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable returns (uint[] memory amounts);
}
contract Test{
address UNISWAP_V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;
function ConvertEthToUSDC(address _tokenOut) public payable returns(bool) {
address[] memory path;
path = new address[](2);
path[1] = WETH;
path[2] = _tokenOut;
//Deadlimit 10 mins
//minAmount is 0
IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactETHForTokens{value: msg.value}(0, path, msg.sender, block.timestamp + 600);
}
}如果我没有错,有一些小的东西,我错过了使用,我可以得到转换路径。
发布于 2023-01-06 13:05:02
要获得使用Uniswap V2路由器契约将ETH转换为USDC的转换路径,可以使用getPath函数。下面是一个示例,说明如何使用getPath函数获取转换路径,然后在swapExactETHForTokens函数中使用它:
function ConvertEthToUSDC(address _tokenOut, uint _amountOut) public payable returns(bool) {
// Get the conversion path
address[] memory path = IUniswapV2Router(UNISWAP_V2_ROUTER).getPath(_tokenOut, _amountOut);
// Swap ETH for the desired tokens
IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactETHForTokens{value: msg.value}(0, path, msg.sender, block.timestamp + 600);
}https://ethereum.stackexchange.com/questions/142427
复制相似问题