Skip to content

Commit

Permalink
feat: track realizedProfit in pool entity
Browse files Browse the repository at this point in the history
  • Loading branch information
filo87 committed Jun 7, 2024
1 parent 2362c09 commit 4174983
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Pool @entity {
sumPoolFeesPaidAmount: BigInt #Applies to All
sumPoolFeesPendingAmount: BigInt #Applies to All

sumRealizedProfitFifoByPeriod: BigInt

# Cumulated transaction data since pool creation
sumBorrowedAmount: BigInt
sumRepaidAmount: BigInt
Expand Down Expand Up @@ -123,6 +125,8 @@ type PoolSnapshot @entity {
sumPoolFeesPaidAmount: BigInt #Applies to All
sumPoolFeesPendingAmount: BigInt #Applies to All

sumRealizedProfitFifoByPeriod: BigInt

# Cumulated transaction data since pool creation
sumBorrowedAmount: BigInt
sumRepaidAmount: BigInt
Expand Down
9 changes: 6 additions & 3 deletions src/mappings/handlers/loansHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ async function _handleLoanRepaid(event: SubstrateEvent<LoanRepaidEvent>) {
quantity.toBigInt(),
settlementPrice.toBigInt()
)
await pool.increaseRealizedProfitFifo(realizedProfitFifo)
}

const at = await AssetTransactionService.repaid({ ...assetTransactionBaseData, realizedProfitFifo })
Expand Down Expand Up @@ -337,6 +338,7 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
quantity.toBigInt(),
settlementPrice.toBigInt()
)
await pool.increaseRealizedProfitFifo(realizedProfitFifo)
}
await fromAsset.updateIpfsAssetName()
await fromAsset.save()
Expand Down Expand Up @@ -443,15 +445,16 @@ async function _handleLoanDebtTransferred1024(event: SubstrateEvent<LoanDebtTran
await fromAsset.repay(amount)
await fromAsset.save()

const quantity = nToBigInt(bnToBn(amount).mul(WAD).div(bnToBn(fromAsset.currentPrice)))
const realizedProfitFifo = await AssetPositionService.sellFifo(toAsset.id, quantity, toAsset.currentPrice)

await pool.increaseRealizedProfitFifo(realizedProfitFifo)
await pool.increaseRepayments1024(amount)
await pool.save()

await epoch.increaseRepayments(amount)
await epoch.save()

const quantity = nToBigInt(bnToBn(amount).mul(WAD).div(bnToBn(fromAsset.currentPrice)))
const realizedProfitFifo = await AssetPositionService.sellFifo(toAsset.id, quantity, toAsset.currentPrice)

// principal repayment transaction
const principalRepayment = await AssetTransactionService.repaid({
...txData,
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 @@ -72,6 +72,7 @@ export class PoolService extends Pool {
this.sumPoolFeesPaidAmountByPeriod = BigInt(0)
this.deltaPortfolioValuationByPeriod = BigInt(0)
this.sumInterestAccruedByPeriod = BigInt(0)
this.sumRealizedProfitFifoByPeriod = BigInt(0)

this.sumBorrowedAmount = BigInt(0)
this.sumRepaidAmount = BigInt(0)
Expand Down Expand Up @@ -403,6 +404,11 @@ export class PoolService extends Pool {
logger.info(`Updating sumPoolFeesPendingAmount for pool ${this.id} to ${pendingAmount.toString(10)}`)
this.sumPoolFeesPendingAmount = pendingAmount
}

public increaseRealizedProfitFifo(amount: bigint) {
logger.info(`Increasing umRealizedProfitFifoByPeriod for pool ${this.id} by ${amount.toString(10)}`)
this.sumRealizedProfitFifoByPeriod += amount
}
}

export interface ActiveLoanData {
Expand Down

0 comments on commit 4174983

Please sign in to comment.