Skip to content

Commit

Permalink
Merge pull request #15 from overlay-market/arthurka/bac-89-add-fundin…
Browse files Browse the repository at this point in the history
…g-payment-for-unwinds-and-liquidates-entities

feat: add `fundingPayment` to `Unwind` and `Liquidate` entities
  • Loading branch information
arthurka-o authored Jun 14, 2024
2 parents 4039b4d + 080f878 commit 6549c16
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
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

0 comments on commit 6549c16

Please sign in to comment.