Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add open interest in market query #41

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ type Maker @entity {

"open orders"
openOrders: [OpenOrder!]! @derivedFrom(field: "makerRef")

"trader open interest"
openInterest: BigDecimal!
}

type OpenOrder @entity {
Expand Down
22 changes: 21 additions & 1 deletion src/mappings/clearingHouse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, Bytes } from "@graphprotocol/graph-ts"
import { BigDecimal, BigInt, Bytes } from "@graphprotocol/graph-ts"
import {
FundingPaymentSettled as FundingPaymentSettledEvent,
LiquidityChanged as LiquidityChangedEvent,
Expand Down Expand Up @@ -167,6 +167,8 @@ export function handlePositionChanged(event: PositionChangedEvent): void {
const traderMarket = getOrCreateTraderMarket(event.params.trader, event.params.baseToken)
traderMarket.blockNumber = event.block.number
traderMarket.timestamp = event.block.timestamp

const beforePositionSize = traderMarket.takerPositionSize
traderMarket.takerPositionSize = traderMarket.takerPositionSize.plus(positionChanged.exchangedPositionSize)
traderMarket.openNotional = positionChanged.openNotional
// NOTE: according to contract, a position size < 10 wei cannot be closed or liquidated so we set it to 0
Expand All @@ -185,6 +187,18 @@ export function handlePositionChanged(event: PositionChangedEvent): void {
const position = getOrCreatePosition(event.params.trader, event.params.baseToken)
position.blockNumber = event.block.number
position.timestamp = event.block.timestamp

// update open interest based on position change

// Here we only calculate taker's position size change (fee = 0 means it's maker)
// And add position change*2, that means we calculate maker's impermanent position in advance
// Which means when a maker removes liquidity, the corresponding taker position (fee = 0) is already included. So we should ignore this case.
if (!event.params.fee.isZero()) {
CheshireCatNick marked this conversation as resolved.
Show resolved Hide resolved
// taker
const diff = abs(traderMarket.takerPositionSize).minus(abs(beforePositionSize))
CheshireCatNick marked this conversation as resolved.
Show resolved Hide resolved
market.openInterest = market.openInterest.plus(diff.times(BigDecimal.fromString("2")))
}

// NOTE: position size does not consider maker position
position.positionSize = position.positionSize.plus(positionChanged.exchangedPositionSize)
position.openNotional = positionChanged.openNotional
Expand Down Expand Up @@ -296,6 +310,7 @@ export function handleLiquidityChanged(event: LiquidityChangedEvent): void {

// upsert TraderMarket
const traderMarket = getOrCreateTraderMarket(event.params.maker, event.params.baseToken)
const beforePositionSize = traderMarket.takerPositionSize
traderMarket.blockNumber = event.block.number
traderMarket.timestamp = event.block.timestamp
traderMarket.makerFee = traderMarket.makerFee.plus(liquidityChanged.quoteFee)
Expand Down Expand Up @@ -339,6 +354,11 @@ export function handleLiquidityChanged(event: LiquidityChangedEvent): void {
market.baseAmount = market.baseAmount.plus(liquidityChanged.base)
market.quoteAmount = market.quoteAmount.plus(liquidityChanged.quote)

const diff = abs(traderMarket.takerPositionSize).minus(abs(beforePositionSize))
CheshireCatNick marked this conversation as resolved.
Show resolved Hide resolved
if (diff.gt(BigDecimal.zero())) {
market.openInterest = market.openInterest.plus(diff.times(BigDecimal.fromString("2")))
}

// commit changes
liquidityChanged.save()
maker.save()
Expand Down