1

When creating a new pair contract via the UniswapV2Factory contract, the order in which tokens get assigned in storage (that is, the token0 and token1 vars) cannot be arbitrarily set by the caller:

(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);

What does tokenA < tokenB do in Solidity? The vars are address types.

3

It's easy to see what that comparison does with a hands-on approach on Remix:

pragma solidity =0.5.16;

contract AddressComparison {
    function cmp(address token0, address token1) external pure returns (bool) {
        return token0 < token1;
    }
}

Say you're passing the following two addresses as inputs:

  • token0 = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599
  • token1 = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48

The function returns true:

Remix Screenshot #1

It looks like Solidity compares the numerical value of the addresses (that is, their hexadecimal values). Since token1's first digit is greater in value than token0's digit (0xA versus 0x2), the comparison yields true.

Let's see another example to drive the point home:

  • token0 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
  • token1 = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48

This time, the function returns false:

Remix Screenshot #2

You can probably guess why: 0xC is bigger than 0xA.

Most likely Uniswap did this to reduce the costs of integration. If everybody knows that tokens are always ordered by their hexadecimal value in a Uniswap v2 pair, clients don't have to query the blockchain to find out the order of the tokens in the pool provided they know the token addresses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.