From fdd53ab1c22ce0eb7a804fdf30ec5ae047037df8 Mon Sep 17 00:00:00 2001 From: Jeroen Offerijns Date: Tue, 11 Jun 2024 15:07:42 +0200 Subject: [PATCH] chore: refactor --- src/mappings/handlers/blockHandlers.ts | 18 ++---------------- src/mappings/services/assetService.ts | 10 +++++++--- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/mappings/handlers/blockHandlers.ts b/src/mappings/handlers/blockHandlers.ts index 225e1a21..5d3d12c0 100644 --- a/src/mappings/handlers/blockHandlers.ts +++ b/src/mappings/handlers/blockHandlers.ts @@ -19,7 +19,6 @@ import { TrancheSnapshot, } from '../../types/models' import { AssetPositionService } from '../services/assetPositionService' -import { bnToBn } from '@polkadot/util' const timekeeper = TimekeeperService.init() @@ -66,24 +65,11 @@ async function _handleBlock(block: SubstrateBlock): Promise { pool.resetUnrealizedProfit() for (const loanId in activeLoanData) { const asset = await AssetService.getById(pool.id, loanId) - const previousUnrealizedProfitByPeriod = bnToBn(asset.unrealizedProfitByPeriod) await asset.updateActiveAssetData(activeLoanData[loanId]) - const unrealizedProfitAtMarketPrice = await AssetPositionService.computeUnrealizedProfitAtPrice( - asset.id, - asset.currentPrice - ) - const unrealizedProfitAtNotional = await AssetPositionService.computeUnrealizedProfitAtPrice( - asset.id, - asset.notional - ) - logger.info(`byPeriod for ${asset.id} = ${unrealizedProfitAtMarketPrice} - ${previousUnrealizedProfitByPeriod}`) - const unrealizedProfitByPeriod = unrealizedProfitAtMarketPrice - previousUnrealizedProfitByPeriod - logger.info(`byPeriod for ${asset.id} = ${unrealizedProfitByPeriod}`) await asset.updateUnrealizedProfit( - unrealizedProfitAtMarketPrice, - unrealizedProfitAtNotional, - unrealizedProfitByPeriod + await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.currentPrice), + await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.notional) ) await asset.save() await pool.increaseInterestAccrued(asset.interestAccruedByPeriod) diff --git a/src/mappings/services/assetService.ts b/src/mappings/services/assetService.ts index 03e4880c..62085d46 100644 --- a/src/mappings/services/assetService.ts +++ b/src/mappings/services/assetService.ts @@ -225,11 +225,15 @@ export class AssetService extends Asset { ) } - public updateUnrealizedProfit(atMarketPrice: bigint, atNotional: bigint, byPeriod: bigint) { - logger.info(`Updating unrealizedProfit for asset ${this.id}: ${atMarketPrice}, ${atNotional}, ${byPeriod}`) + public updateUnrealizedProfit(atMarketPrice: bigint, atNotional: bigint) { + logger.info(`Updating unrealizedProfit for asset ${this.id}: ${atMarketPrice}, ${atNotional}`) this.unrealizedProfitAtMarketPrice = atMarketPrice this.unrealizedProfitAtNotional = atNotional - this.unrealizedProfitByPeriod = byPeriod + + logger.info( + `byPeriod: ${atMarketPrice} - ${this.unrealizedProfitByPeriod} = ${atMarketPrice - this.unrealizedProfitByPeriod}` + ) + this.unrealizedProfitByPeriod = atMarketPrice - this.unrealizedProfitByPeriod } }