简体   繁体   中英

Equivalent for abi.encodePacked

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM