Tracking Liquidity

This guide explains how to monitor liquidity in OraclePool, including the difference between reserves and balances, and how to track changes via events.

Reserves vs Balances

OraclePool has two important concepts:

Reserves (getReserves)

function getReserves() external view returns (uint256 reserveX, uint256 reserveY);

Reserves are the tokens allocated for trading. These directly affect:

  • Price calculations

  • Available liquidity for swaps

  • Execution price (via the CLCP formula)

Reserves change when:

  • Swaps occur (tokens flow in/out)

  • Allocation is adjusted via allocate()

Balances (getBalances)

function getBalances() external view returns (uint256 amountX, uint256 amountY);

Balances are the total tokens held by the pool (including reserves + any unallocated tokens).

Balances can be higher than reserves if:

  • Tokens were deposited but not yet allocated

  • There are extra tokens from fees or other sources

Reading Current State

Events

chevron-rightSwap Eventhashtag

Emitted on every swap. Reserves change as tokens flow in/out.

Parameters:

  • sender - Address that initiated the swap

  • recipient - Address that received the output tokens

  • swapXtoY - Direction: true = tokenX → tokenY, false = tokenY → tokenX

  • amountIn - Actual amount of input token consumed (includes fee)

  • amountOut - Amount of output token sent to recipient

  • feeIn - Fee deducted from input

Reserve changes from Swap:

  • If swapXtoY = true: reserveX increases by (amountIn - feeIn), reserveY decreases by amountOut

  • If swapXtoY = false: reserveY increases by (amountIn - feeIn), reserveX decreases by amountOut

chevron-rightAllocated Eventhashtag

Emitted when reserves are reallocated by the allocator role.

Parameters:

  • allocator - Address that called allocate()

  • reserveX - New reserve amount for tokenX

  • reserveY - New reserve amount for tokenY

This event gives you the absolute new reserve values, not deltas.

TypeScript Example: Tracking Liquidity

Python Example

Understanding Reserve Changes

From Swaps

When a swap occurs with swapXtoY = true (selling tokenX for tokenY):

When swapXtoY = false (selling tokenY for tokenX):

Note: The fee is not added to reserves. It's sent to the fee rewarder, if it's set.

From Allocation

The allocate() function sets reserves as a percentage of balances:

Where allocateXBps and allocateYBps are in basis points (10000 = 100%).

For example, allocate(8000, 8000) allocates 80% of each token balance to reserves.

Key Points

circle-info
  • Reserves affect price — Only allocated reserves participate in the CLCP pricing formula

  • Balances include everything — Reserves + unallocated tokens

  • Fees don't go to reserves — They're sent to the fee rewarder

  • Allocated event is absolute — It gives new reserve values, not deltas

  • Swap event gives deltas — Calculate new reserves from amountIn, amountOut, and feeIn

Last updated