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 all 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 @@ -63,6 +63,9 @@ type Market @entity {
positions: [Position!]! @derivedFrom(field: "marketRef")
"open orders"
openOrders: [OpenOrder!]! @derivedFrom(field: "marketRef")

"trader open interest"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should change this comment as well

takerOpenInterest: BigDecimal!
}

type Token @entity {
Expand Down
22 changes: 17 additions & 5 deletions src/mappings/clearingHouse.ts
Original file line number Diff line number Diff line change
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,12 @@ 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

const diff = abs(traderMarket.takerPositionSize).minus(abs(beforePositionSize))
market.takerOpenInterest = market.takerOpenInterest.plus(diff)

// NOTE: position size does not consider maker position
position.positionSize = position.positionSize.plus(positionChanged.exchangedPositionSize)
position.openNotional = positionChanged.openNotional
Expand Down Expand Up @@ -288,6 +296,11 @@ export function handleLiquidityChanged(event: LiquidityChangedEvent): void {
maker.timestamp = event.block.timestamp
maker.collectedFee = maker.collectedFee.plus(liquidityChanged.quoteFee)

// upsert market
const market = getOrCreateMarket(event.params.baseToken)
market.baseAmount = market.baseAmount.plus(liquidityChanged.base)
market.quoteAmount = market.quoteAmount.plus(liquidityChanged.quote)

// upsert Trader
const trader = getOrCreateTrader(event.params.maker)
trader.blockNumber = event.block.number
Expand All @@ -296,6 +309,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 All @@ -314,6 +328,9 @@ export function handleLiquidityChanged(event: LiquidityChangedEvent): void {
const position = getOrCreatePosition(event.params.maker, event.params.baseToken)
position.positionSize = fixedDataMap.get("takerPositionSize")
position.openNotional = fixedDataMap.get("openNotional")

const diff = abs(traderMarket.takerPositionSize).minus(abs(beforePositionSize))
market.takerOpenInterest = market.takerOpenInterest.plus(diff)
}
}

Expand All @@ -334,11 +351,6 @@ export function handleLiquidityChanged(event: LiquidityChangedEvent): void {
}
openOrder.collectedFee = openOrder.collectedFee.plus(liquidityChanged.quoteFee)

// upsert market
const market = getOrCreateMarket(event.params.baseToken)
market.baseAmount = market.baseAmount.plus(liquidityChanged.base)
market.quoteAmount = market.quoteAmount.plus(liquidityChanged.quote)

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