Skip to content

Commit

Permalink
feat: add pool tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
hieronx committed Jun 10, 2024
1 parent 4c1319e commit 28c9356
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type PoolSnapshot @entity {
sumRealizedProfitFifoByPeriod: BigInt
sumUnrealizedProfitAtMarketPrice: BigInt
sumUnrealizedProfitAtNotional: BigInt
unrealizedProfitByPeriod: BigInt

# Cumulated transaction data since pool creation
sumBorrowedAmount: BigInt
Expand Down
6 changes: 5 additions & 1 deletion src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
await asset.save()
await pool.increaseInterestAccrued(asset.interestAccruedByPeriod)
if (asset.isNonCash())
pool.increaseUnrealizedProfit(asset.unrealizedProfitAtMarketPrice, asset.unrealizedProfitAtNotional)
pool.increaseUnrealizedProfit(
asset.unrealizedProfitAtMarketPrice,
asset.unrealizedProfitAtNotional,
asset.unrealizedProfitByPeriod
)
if (asset.actualMaturityDate < block.timestamp) pool.increaseDebtOverdue(asset.outstandingDebt)
if (asset.isOffchainCash()) pool.increaseOffchainCashValue(asset.presentValue)
}
Expand Down
1 change: 1 addition & 0 deletions src/mappings/services/assetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class AssetService extends Asset {

asset.unrealizedProfitAtMarketPrice = BigInt(0)
asset.unrealizedProfitAtNotional = BigInt(0)
asset.unrealizedProfitByPeriod = BigInt(0)

return asset
}
Expand Down
4 changes: 3 additions & 1 deletion src/mappings/services/poolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,14 @@ export class PoolService extends Pool {
logger.info(`Resetting unrealizedProfit for pool ${this.id}`)
this.sumUnrealizedProfitAtMarketPrice = BigInt(0)
this.sumUnrealizedProfitAtNotional = BigInt(0)
this.sumUnrealizedProfitByPeriod = BigInt(0)
}

public increaseUnrealizedProfit(atMarket: bigint, atNotional: bigint) {
public increaseUnrealizedProfit(atMarket: bigint, atNotional: bigint, byPeriod) {
logger.info(`Increasing unrealizedProfit for pool ${this.id} atMarket: ${atMarket} notional: ${atNotional}`)
this.sumUnrealizedProfitAtMarketPrice += atMarket
this.sumUnrealizedProfitAtNotional += atNotional
this.sumUnrealizedProfitByPeriod += byPeriod
}
}

Expand Down

0 comments on commit 28c9356

Please sign in to comment.