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.
Below are code example to get the price, and amounts swapped.
export function handleSwap(event: Swap): void {
// We can get the tokenX and tokenY from the LBPair
const lbPair = LBPair.load(event.address.toHexString())
const tokenX = lbPair.tokenX
const tokenY = lbPair.tokenY
// We can infer the price of 1 tokenY = __ tokenX
// from the binstep and the active Bin Id
const price = getPriceOfBin(
BigInt.fromString(event.params.id.toString()),
lbPair.binStep,
tokenX,
tokenY
)
// NOTE: reverse bytes to convert to big endianness
event.params.amountsIn.reverse()
event.params.amountsOut.reverse()
// amountsIn is [amountX, amountY] packed into byte32
const amountInX = decodeX(event.params.amountsIn)
const amountInY = decodeY(event.params.amountsIn)
const amountOutX = decodeX(event.params.amountsOut)
const amountOutY = decodeY(event.params.amountsOut)
}
// Assemblyscript API
// https://thegraph.com/docs/en/developing/assemblyscript-api/
function decodeX(packedAmounts: Bytes): BigInt {
// Read the right 128 bits of the 256 bits
return BigInt.fromUnsignedBytes(packedAmounts).bitAnd(BigInt.fromI32(2).pow(128).minus(BigInt.fromI32(1)))
}
function decodeY(packedAmounts: Bytes): BigInt {
// Read the left 128 bits of the 256 bits
return BigInt.fromUnsignedBytes(packedAmounts).rightShift(128)
}