1

So far I have tried this:

    function swapEthForToken(
        address router,
        uint256 minAmount,
        address _tokenIn,
        address _tokenOut
    ) public payable {
        assembly {
            let path := mload(0x40)
            let deadline := add(timestamp(), 300)

            mstore(0x7c, SWAP_ETH_FOR_TOKEN)
            mstore(0x80, minAmount)
            // mstore(0xA0, path)
            mstore(path, 2)
            mstore(add(path, 0x20), _tokenIn)
            mstore(add(path, 0x40), _tokenOut)
            mstore(0xE0, address())
            mstore(0x100, deadline)

            let s1 := call(gas(), router, callvalue(), 0x7c, 0x100, 0x00, 0x40)

            if iszero(s1) {
                revert(0x00, 0x40)
            }
        }
    }

function I am trying to call is this:

    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);

1 Answer 1

1

You have to encode it following the solidity ABI specification https://docs.soliditylang.org/en/v0.8.21/abi-spec.html.

path is array of variable length, you have to encode an offset within the calldata, and at that offset encode the array. The call has 4 parameters so you will have to encode something like this:

offset data
0x00   amountOutMin
0x20   path_offset (0x80)
0x40   to
0x60   deadline
0x80   path size <--- offset of path
0xA0   path[0]
...

Your Answer

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

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