◆ Purpose

to calculate from the address of factory, tokenA and tokenB

◆ Input & Return value

(address factory, address tokenA, address tokenB) ⇒ (address pair)

The difference between this function and getPair is whether the pair address is existing or not.

getPair ⇒ pair address is existing
pairFor ⇒ pair address is not existing but, it can calculate.

If you want to understand more depth, check this page

Create2

◆ Example

factory(ETH factory) = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f tokenA(USDC) = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48

tokenB(WETH) = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2

pair(USDC/WETH) = 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc

◆ Code and Explain

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

◆ Resources

https://github.com/Uniswap/v2-periphery/blob/master/contracts/libraries/UniswapV2Library.sol#L18