I'm trying to use the UniswapV3 Router to execute a Multihop Swap which accepts the path as an encoded combination of token addresses. Documentation: https://docs.uniswap.org/protocol/guides/swaps/multihop-swaps

From the documentation, for a swap from DAI to WETH via USDC the encoded path is: abi.encodePacked(DAI, poolFee, USDC, poolFee, WETH9)

Now I want to generate this encoded path dynamically from a address[] _path array containing the token addresses and a poolFees array containing the pool fees. (which is one element shorter than path).

I would like to know how best to achieve this.

What I currently have tried is this:

function _encodePathV3(address[] memory _path, uint24[] memory _fees) internal view returns (bytes memory path) {
        path = abi.encodePacked(_path[0]);
        for(uint i = 1; i < _fees.length; i++){
            path = abi.encodePacked(path, _fees[i], _path[i+1]);

However this does not seem to work since it results in the following exception from the Uniswap Router, which I believe is connected to the malformed encoded path.

Error: VM Exception while processing transaction: reverted with reason string 'slice_outOfBounds'

I'm using solidity 0.6.10 with experimental ABIEncoderV2

1 Answer 1


The starting index of the loop was mistakenly set to 1. Changing it to for(uint i = 0; i < _fees.length; i++) fixed it.

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.