1

From documentation it says:

token0
function token0() external view returns (address);
Returns the address of the pair token with the lower sort order.

token1
function token1() external view returns (address);
Returns the address of the pair token with the higher sort order.

As far as I understand, in order to compare them in , I would need to cast them to BigInt, i.e.

let token0;
let token1;

const address0 = BigInt('0xDB17618B140EFCB8a07BC8e9b88920de1daE6C87');
const address1 = BigInt('0x4d4CC29b9C4E413CFe5f898E16280f11db57E186');

if (address0 < address1) {
  token0 = address0;
  token1 = address1;
} else {
  token0 = address1;
  token1 = address0;
}

Two questions:

  1. Is there ever a case where two distinct addresses will have the same sorting value?
  2. If yes, then should I be using <= or < here?
1

Is there ever a case where two distinct addresses will have the same sorting value?

Each address is necessarily unique. Each address is a 20-byte number. Therefore unique numbers.

(If you're worried about the checksum-ing - i.e. the capitalization in various places - that still doesn't affect the underlying value.)

The Uniswap Factory contract does the comparison in the same way:

function createPair(address tokenA, address tokenB) external returns (address pair) {
    require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
--> (address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
    require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
    ...

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.