3

I am using ethers-rs to write a defi app. I need to calculate the CREATE2 address in rust. I couldn't find the equivalent of abi.encodePacked(token0, token1) in rust.

The code used in Uniswap's library (https://vomtom.at/how-to-use-uniswap-v2-as-a-developer):

    // calculates the CREATE2 address for a pair without making any external calls
    function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
        (address token0, address token1) = sortTokens(tokenA, tokenB);
        pair = address(uint(keccak256(abi.encodePacked(
                hex'ff',
                factory,
                keccak256(abi.encodePacked(token0, token1)),
                hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
            ))));
    }

Unfortunately I didn't get much help from the internet.

What's the equivalent for abi.encodePacked in rust.

Regards.

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Ganesh Rathinavel
  • 1,243
  • 4
  • 17
  • 38

1 Answers1

2

abi.encodePacked simply concatenates the bytes of the serialised parameters - in this case two addresses.

Usually EVM pads data items to uint256 word boundaries, but encodePacked is special.

Address is 160 bits (20 bytes) so the result of encodePacked should be 40 bytes, bytes of two addresses concatenated.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435