Unpack signed token deltas from a Uniswap v4 BalanceDelta

A BalanceDelta packs two signed 128-bit values into one 256-bit word. Decoding both halves as unsigned numbers can turn a one-unit debt into an enormous apparent credit.

Sign-extend each half independently

Extract the upper 128 bits for amount0 and the lower 128 for amount1. Interpret each as two’s-complement int128. The example’s high half consists entirely of one bits and must therefore decode to minus one.

amount0 = -1
amount1 = 5
packed = (uint128_bits(-1) << 128) | 5

If only the high half is sign-extended, a negative amount1 still appears as a large positive integer. Include negative-low, negative-high and zero test fixtures. Compare the decoded pair to the library’s amount0 and amount1 accessors.

Keep currency order and perspective

Attach amounts to currency0 and currency1 from the same pool key and document whose delta the specific method returns. A packed delta is an accounting result; it need not correspond to two direct token transfers if settlement nets multiple actions. github.com BalanceDelta.sol