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 fundingPayment to Unwind and Liquidate entities #15

Merged
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
26 changes: 26 additions & 0 deletions generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,19 @@ export class Unwind extends Entity {
this.set("feeAmount", Value.fromBigInt(value));
}

get fundingPayment(): BigInt {
let value = this.get("fundingPayment");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toBigInt();
}
}

set fundingPayment(value: BigInt) {
this.set("fundingPayment", Value.fromBigInt(value));
}

get size(): BigInt {
let value = this.get("size");
if (!value || value.kind == ValueKind.NULL) {
Expand Down Expand Up @@ -1941,6 +1954,19 @@ export class Liquidate extends Entity {
this.set("fractionOfPosition", Value.fromBigInt(value));
}

get fundingPayment(): BigInt {
let value = this.get("fundingPayment");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toBigInt();
}
}

set fundingPayment(value: BigInt) {
this.set("fundingPayment", Value.fromBigInt(value));
}

get size(): BigInt {
let value = this.get("size");
if (!value || value.kind == ValueKind.NULL) {
Expand Down
4 changes: 4 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ type Unwind @entity {
pnl: BigInt!
# Fee amount of this unwind
feeAmount: BigInt!
# funding payment of this unwind
fundingPayment: BigInt!
# fraction of initial collateral of this unwind
size: BigInt!
# volume = transferAmount + (initialDebt * fractionOfPosition)
Expand Down Expand Up @@ -271,6 +273,8 @@ type Liquidate @entity {
position: Position!
# fraction of the original position
fractionOfPosition: BigInt!
# funding payment of this liquidation
fundingPayment: BigInt!
# liquidate size = initialCollateral * fractionOfPosition
size: BigInt!
# current position oi
Expand Down
24 changes: 24 additions & 0 deletions src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,14 @@ export function handleUnwind(event: UnwindEvent): void {
// position.currentOi = stateContract.oi(marketAddress, senderAddress, positionId)
position.currentDebt = position.currentDebt.times(ONE_18DEC_BI.minus(unwind.fraction)).div(ONE_18DEC_BI)

let oiUnwound: BigInt; // used later for fundingPayment calculations

if (position.isLong) {
oiUnwound = market.oiLong.minus(event.params.oiAfterUnwind)
market.oiLong = event.params.oiAfterUnwind
market.oiLongShares = event.params.oiSharesAfterUnwind
} else {
oiUnwound = market.oiShort.minus(event.params.oiAfterUnwind)
market.oiShort = event.params.oiAfterUnwind
market.oiShortShares = event.params.oiSharesAfterUnwind
}
Expand Down Expand Up @@ -444,6 +448,16 @@ export function handleUnwind(event: UnwindEvent): void {
.div(ONE_18DEC_BI)
)

// funding = exitPrice * (oiUnwound - oiInitial * fractionUnwound)
// oiUnwound = oiBeforeUnwind - oiAfterUnwind

const fundingPayment = event.params.price.times(
oiUnwound.minus(
position.initialOi.times(fractionOfPosition)
)
)
unwind.fundingPayment = fundingPayment

sender.numberOfUnwinds = sender.numberOfUnwinds.plus(ONE_BI)
sender.realizedPnl = sender.realizedPnl.plus(pnl)
if (event.params.fraction == ONE_18DEC_BI) {
Expand Down Expand Up @@ -649,10 +663,14 @@ export function handleLiquidate(event: LiquidateEvent): void {
position.isLiquidated = true
position.fractionUnwound = ONE_18DEC_BI

let oiUnwound: BigInt; // used later for fundingPayment calculations

if (position.isLong) {
oiUnwound = market.oiLong.minus(event.params.oiAfterLiquidate)
market.oiLong = event.params.oiAfterLiquidate
market.oiLongShares = event.params.oiSharesAfterLiquidate
} else {
oiUnwound = market.oiShort.minus(event.params.oiAfterLiquidate)
market.oiShort = event.params.oiAfterLiquidate
market.oiShortShares = event.params.oiSharesAfterLiquidate
}
Expand All @@ -663,6 +681,12 @@ export function handleLiquidate(event: LiquidateEvent): void {
let transaction = loadTransaction(event)
let liquidate = new Liquidate(position.id) as Liquidate

// funding = exitPrice * (oiUnwound - oiInitial * fractionUnwound)
// oiUnwound = oiBeforeUnwind - oiAfterUnwind
const fundingPayment = event.params.price.times(
oiUnwound.minus(fractionOfPosition)
)
liquidate.fundingPayment = fundingPayment
liquidate.position = position.id
liquidate.owner = owner.id
liquidate.sender = sender.id
Expand Down
Loading