diff --git a/schema.graphql b/schema.graphql index bf632d8e..b945502e 100644 --- a/schema.graphql +++ b/schema.graphql @@ -130,6 +130,7 @@ type PoolSnapshot @entity { sumRealizedProfitFifoByPeriod: BigInt sumUnrealizedProfitAtMarketPrice: BigInt sumUnrealizedProfitAtNotional: BigInt + unrealizedProfitByPeriod: BigInt # Cumulated transaction data since pool creation sumBorrowedAmount: BigInt diff --git a/src/mappings/handlers/blockHandlers.ts b/src/mappings/handlers/blockHandlers.ts index 566b62cf..4c1a3735 100644 --- a/src/mappings/handlers/blockHandlers.ts +++ b/src/mappings/handlers/blockHandlers.ts @@ -75,7 +75,11 @@ async function _handleBlock(block: SubstrateBlock): Promise { 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) } diff --git a/src/mappings/services/assetService.ts b/src/mappings/services/assetService.ts index 810a18ec..59b7f15f 100644 --- a/src/mappings/services/assetService.ts +++ b/src/mappings/services/assetService.ts @@ -59,6 +59,7 @@ export class AssetService extends Asset { asset.unrealizedProfitAtMarketPrice = BigInt(0) asset.unrealizedProfitAtNotional = BigInt(0) + asset.unrealizedProfitByPeriod = BigInt(0) return asset } diff --git a/src/mappings/services/poolService.ts b/src/mappings/services/poolService.ts index fb3a70ea..741ac727 100644 --- a/src/mappings/services/poolService.ts +++ b/src/mappings/services/poolService.ts @@ -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 } }