Tracking Volume

Each market has its own LBPair contract with its own address. These are created by the LBFactory contract.

The general approach is to track all LBPairs created by the LBFactory and then track all Swap events emitted by the LBPair.

LBPairCreated

The LBPairCreated event is emitted by the LBFactory when new LBPair is created. Pools are uniquely identified within factories by pid.

    // ILBFactory.sol
    event LBPairCreated(
        IERC20 indexed tokenX, 
        IERC20 indexed tokenY, 
        uint256 indexed binStep, 
        ILBPair LBPair, 
        uint256 pid
    );

Below is code example to extract the LBPair, tokenX, tokenY addresses, pid, and binstep.

function handleLBPairCreated(event: LBPairCreated): void {

  const lbPair = new LBPair(
    event.params.pid,
    event.params.LBPair.toHexString(), 
    event.params.tokenX.toHexString(), 
    event.params.tokenY.toHexString(), 
    event.params.binStep
  )

  lbPair.save()
}

Swap

The Swap event is emitted by LBPairs when tokens are swapped.

  • Volume traded is tracked by amountsIn, amountsOut, which are tuple [amountX, amountY] encoded into byte32.

  • Fees paid by Traders to Liquidity Providers are tracked in totalFees, while fees paid by Liquidity Providers to Protocol are tracked in protocolFees. They are tuple [feeX, feeY] encoded into byte32.

  • See Byte32 Decoding for more details.

  • The active bin when the swap has finished is tracked by id, which is the Bin ID in integer.

Below are code example to get the price, and amounts swapped.

Last updated