Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
feat: track accrued interest per period
Browse files Browse the repository at this point in the history
Fixes #234
  • Loading branch information
filo87 committed Apr 19, 2024
1 parent d8ee503 commit 80c0d98
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Pool @entity {
sumDebtOverdue: BigInt
sumDebtWrittenOffByPeriod: BigInt
deltaPortfolioValuationByPeriod: BigInt
sumInterestAccruedByPeriod: BigInt

sumChargedAmountByPeriod: BigInt
sumAccruedAmountByPeriod: BigInt
Expand Down Expand Up @@ -102,6 +103,7 @@ type PoolSnapshot @entity {
sumDebtOverdue: BigInt
sumDebtWrittenOffByPeriod: BigInt
deltaPortfolioValuationByPeriod: BigInt
sumInterestAccruedByPeriod: BigInt

sumChargedAmountByPeriod: BigInt
sumAccruedAmountByPeriod: BigInt
Expand Down Expand Up @@ -390,6 +392,8 @@ type Asset @entity {
borrowedAmountByPeriod: BigInt
repaidAmountByPeriod: BigInt

interestAccruedByPeriod: BigInt

writeOffIndex: Int
writtenOffPercentageByPeriod: BigInt
writtenOffAmountByPeriod: BigInt
Expand Down Expand Up @@ -421,6 +425,8 @@ type AssetSnapshot @entity {
borrowedAmountByPeriod: BigInt
repaidAmountByPeriod: BigInt

interestAccruedByPeriod: BigInt

writtenOffPercentageByPeriod: BigInt
writtenOffAmountByPeriod: BigInt
penaltyInterestRatePerSec: BigInt
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
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)
}

Expand Down
7 changes: 6 additions & 1 deletion src/mappings/services/assetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions src/mappings/services/poolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Option<PoolDetails>>(this.id)
logger.info(`Fetching tranches for pool: ${this.id}`)
Expand Down

0 comments on commit 80c0d98

Please sign in to comment.