The contract address of the locally calculated asset pair in Pancake

// 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
            ))));
    }

The code is as above, where CREATE2 is an opcode of solidity. This opcode allows us to obtain the address of the contract without any interaction with the chain.

The first parameter is 0xFF, fixed

The second parameter is the eth address where the contract you want to calculate is deployed.

The third one is salt, which is the sorted hash of the two tokens.

The fourth is Init code hash

The first three are easy to understand. The following mainly talks about how the fourth parameter comes from.

const { bytecode } = require('@uniswap/v2-core/build/UniswapV2Pair.json');
const { keccak256 } = require('@ethersproject/solidity');

const COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])
console.log(COMPUTED_INIT_CODE_HASH);

Pancake directly obtains the Pair address based on the addresses of the two assets.

Guess you like

Origin blog.csdn.net/bbandxq521/article/details/128908883