Building a Pool Key
This function toPoolKey deterministically generates a unique identifier (a "pool key") for a pair of books and a salt value. It ensures consistent ordering by always placing the smaller book ID first, regardless of input order. The three components — bookIdA, bookIdB, and salt — are packed together and hashed using keccak256, producing a fixed 32-byte pool key. This key can be used to uniquely identify a liquidity pool or a trading pair on-chain.
import { encodePacked, keccak256 } from 'viem'
export const toPoolKey = (
    bookIdA: bigint,
    bookIdB: bigint,
    salt: `0x${string}`,
) => {
    if (bookIdA > bookIdB) {
        ;[bookIdA, bookIdB] = [bookIdB, bookIdA]
    }
    return keccak256(
        encodePacked(
            ['uint192', 'uint192', 'bytes32'],
            [bookIdA, bookIdB, toBytes32(salt)],
        ),
    )
}