0

Some functions require multiple addresses to be sorted.

For example, the uniswap router requires for some functions: "token0 is guaranteed to be strictly less than token1 by sort order."

I do know how to sort them and why it is necessary for some functions to work. I just wonder how they are actually compared?

Like do they compare the byte code of each address, or do they just compare each number from the beginning of an address so 0x0... would be less than an address starting with 0x1... or what are the exact values of each address to decide which one is less than the other?

2 Answers 2

2

The comparison is tokenA < tokenB (line 25 of https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Factory.sol)

Considering that addreses are hex representations of uint160 numbers, the compiler just compares the corresponding numbers.

2

I just did some testing and it's actually just by the order of the first values, so actually easy to spot by just looking at the two addresses.

So in case of WETH which starts with 0xC... everything from 0x0... to 0x9... as well as 0xA... to 0xB... would be considered as the lower address. Therefore the created pair would be YourToken / WETH.

Every address from 0xE... and above would be considered as the higher address, which would create the pair as WETH / YourToken.

1
  • That's right, that way there can only be one address for each pair of tokens. As for the sorting, yes, you can think of it like beign a lexicographical order, it also works, although internally it's just two big numbers.
    – ceseshi
    Jun 8, 2023 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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