From 80c0d98041d7fa2254ad0c2d9b50725fe4de2e8e Mon Sep 17 00:00:00 2001 From: Filippo Fontana Date: Fri, 19 Apr 2024 12:51:16 +0200 Subject: [PATCH] feat: track accrued interest per period Fixes #234 --- schema.graphql | 6 ++++++ src/mappings/handlers/blockHandlers.ts | 2 +- src/mappings/services/assetService.ts | 7 ++++++- src/mappings/services/poolService.ts | 6 ++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/schema.graphql b/schema.graphql index fd15f26f..42321352 100644 --- a/schema.graphql +++ b/schema.graphql @@ -54,6 +54,7 @@ type Pool @entity { sumDebtOverdue: BigInt sumDebtWrittenOffByPeriod: BigInt deltaPortfolioValuationByPeriod: BigInt + sumInterestAccruedByPeriod: BigInt sumChargedAmountByPeriod: BigInt sumAccruedAmountByPeriod: BigInt @@ -102,6 +103,7 @@ type PoolSnapshot @entity { sumDebtOverdue: BigInt sumDebtWrittenOffByPeriod: BigInt deltaPortfolioValuationByPeriod: BigInt + sumInterestAccruedByPeriod: BigInt sumChargedAmountByPeriod: BigInt sumAccruedAmountByPeriod: BigInt @@ -390,6 +392,8 @@ type Asset @entity { borrowedAmountByPeriod: BigInt repaidAmountByPeriod: BigInt + interestAccruedByPeriod: BigInt + writeOffIndex: Int writtenOffPercentageByPeriod: BigInt writtenOffAmountByPeriod: BigInt @@ -421,6 +425,8 @@ type AssetSnapshot @entity { borrowedAmountByPeriod: BigInt repaidAmountByPeriod: BigInt + interestAccruedByPeriod: BigInt + writtenOffPercentageByPeriod: BigInt writtenOffAmountByPeriod: BigInt penaltyInterestRatePerSec: BigInt diff --git a/src/mappings/handlers/blockHandlers.ts b/src/mappings/handlers/blockHandlers.ts index 908d505d..aa401510 100644 --- a/src/mappings/handlers/blockHandlers.ts +++ b/src/mappings/handlers/blockHandlers.ts @@ -65,8 +65,8 @@ async function _handleBlock(block: SubstrateBlock): Promise { for (const loanId in activeLoanData) { const loan = await AssetService.getById(pool.id, loanId) await loan.updateActiveAssetData(activeLoanData[loanId]) + await pool.increaseInterestAccrued(loan.interestAccruedByPeriod) await loan.save() - if (loan.actualMaturityDate < block.timestamp) pool.increaseDebtOverdue(loan.outstandingDebt) } diff --git a/src/mappings/services/assetService.ts b/src/mappings/services/assetService.ts index 51e7f441..d9973a5d 100644 --- a/src/mappings/services/assetService.ts +++ b/src/mappings/services/assetService.ts @@ -48,6 +48,7 @@ export class AssetService extends Asset { asset.repaidAmountByPeriod = BigInt(0) asset.writtenOffPercentageByPeriod = BigInt(0) asset.writtenOffAmountByPeriod = BigInt(0) + asset.interestAccruedByPeriod = BigInt(0) return asset } @@ -98,8 +99,12 @@ export class AssetService extends Asset { } public async updateActiveAssetData(activeAssetData: ActiveLoanData[keyof ActiveLoanData]) { + const oldOutstaidingInterest = this.outstandingInterest + const oldTotalRepaidInterest = this.totalRepaidInterest Object.assign(this, activeAssetData) - logger.info(`Updating outstanding debt for asset: ${this.id} to ${this.outstandingDebt.toString()}`) + const deltaRepaidInterestAmount = this.totalRepaid - oldTotalRepaidInterest + this.interestAccruedByPeriod = this.outstandingInterest - oldOutstaidingInterest + deltaRepaidInterestAmount + logger.info(`Updated outstanding debt for asset: ${this.id} to ${this.outstandingDebt.toString()}`) } public async updateItemMetadata() { diff --git a/src/mappings/services/poolService.ts b/src/mappings/services/poolService.ts index da002251..a7538004 100644 --- a/src/mappings/services/poolService.ts +++ b/src/mappings/services/poolService.ts @@ -69,6 +69,7 @@ export class PoolService extends Pool { this.sumAccruedAmountByPeriod = BigInt(0) this.sumPaidAmountByPeriod = BigInt(0) this.deltaPortfolioValuationByPeriod = BigInt(0) + this.sumInterestAccruedByPeriod = BigInt(0) this.sumBorrowedAmount = BigInt(0) this.sumRepaidAmount = BigInt(0) @@ -261,6 +262,11 @@ export class PoolService extends Pool { this.sumDebtWrittenOffByPeriod += amount } + public increaseInterestAccrued(amount: bigint) { + logger.info(`Increasing interestAccrued by ${amount}`) + this.sumInterestAccruedByPeriod += amount + } + public async getTranches() { const poolResponse = await api.query.poolSystem.pool>(this.id) logger.info(`Fetching tranches for pool: ${this.id}`)