2

How the best path is calculated ? Is it a fonction in the smart contract or on the website ?

Because I checked the function swapExactETHForTokens and the path need to be set by the function caller.

function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        virtual
        override
        payable
        ensure(deadline)
        returns (uint[] memory amounts)
    {
        require(path[0] == WETH, 'UniswapV2Router: INVALID_PATH');
        amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
        require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
        IWETH(WETH).deposit{value: amounts[0]}();
        assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
        _swap(amounts, path, to);
    }

If I want to swap programatically, how I'm supposed to know the best path ?

1

The best path is not calculated within the contracts. You must provide the exact path when calling the contract, so you must know it in advance.

We can infer from this that it is calculated off-chain by the website. I do not know if the code for the website is public, but you could check the uniswap github repo and see if they published it there.

1
  • Yeah that's what I was thinking, thank you ! – Kioup May 11 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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