Cryptocurrency Ethereum Mastering Development

Uniswap Factory contract event – how to find addresses for new token pair

When creating a new pair on Uniswap, Uniswap emits a PairCreated event that details which tokens are invovled. However, it sorts the token like this in the factory contract:

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

I’m lost as to retrieving specific token(s) as the order changes, here’s what I did by comparing the bytes:

tokenA := vLog.Topics[1].Hex()
bA := []byte(tokenA)

tokenB := vLog.Topics[2].Hex()
bB := []byte(tokenB)

if bytes.Compare(bA, bB) < 0 {
        newToken = tokenA
        backedToken = tokenB
    } else {
        newToken = tokenB
        backedToken = tokenA
fmt.Println("newToken", newToken)
fmt.Println("backedToken", backedToken)

But it is still incorrect. What’s the right way to go about this, and why does Uniswap compare the token addresses like this?

Leave a Reply

Your email address will not be published. Required fields are marked *