How to encode variable number of arguments with `abi.encodePacked`

How to encode variable number of arguments with `abi.encodePacked`

Manage alerts

Loading saved threads...

Chris · External communityPost link
External question — Ethereum Stack Exchange Author: Chris Original post: https://ethereum.stackexchange.com/questions/122671 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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
Quote
Report
Chris · External communityPost link
External answer — Ethereum Stack Exchange Author: Chris Original post: https://ethereum.stackexchange.com/a/122723 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. The starting index of the loop was mistakenly set to 1 . Changing it to for(uint i = 0; i < _fees.length; i++) fixed it.
Quote
Report

Post Reply

Checking account access…