0

I want to convert ETH to USDC using my solidity contract I am using the uniswapV2 contract for converting ETH to token I am using this function swapExactETHForTokens I am not able to get the path that will be used for swapping ETH to USDC.

This address of the WETH and UniswapV2 router is for goerli.

My Code:-


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);        
    }
}

If I am not wrong there is some small thing I am missing using which I can get the conversion path.

1 Answer 1

0

To get the conversion path for swapping ETH to USDC using the Uniswap V2 Router contract, you can use the getPath function. Here is an example of how you can use the getPath function to get the conversion path and then use it in the swapExactETHForTokens function:

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.