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

Commit

Permalink
234-Track-accrued-interest-per-period (#242)
Browse files Browse the repository at this point in the history
* feat: track accrued interest per period
Fixes #234

* chore: use default dictionaries

* chore: fix sepolia for demo

* ci: update deployment to demo-multichain
  • Loading branch information
filo87 authored Apr 24, 2024
1 parent 1deeff2 commit f284553
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
name: Deploy to SubQL (EMBRIO)
strategy:
matrix:
chainId: [development, demo]
chainId: [demo]
uses: ./.github/workflows/subql_deploy_workflow.yaml
with:
chainId: ${{ matrix.chainId }}
Expand All @@ -34,7 +34,7 @@ jobs:
name: Deploy to SubQL Multichain (EMBRIO)
strategy:
matrix:
chainId: [centrifuge]
chainId: [demo, centrifuge]
uses: ./.github/workflows/subql_multi_deploy_workflow.yaml
with:
chainId: ${{ matrix.chainId }}
Expand Down
2 changes: 1 addition & 1 deletion chains-evm/eth/centrifuge.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
network:
chainId: '1' # Ethereum Mainnet
endpoint: 'https://eth.api.onfinality.io/ws?apikey=${ONFINALITY_API_KEY}'
dictionary: 'https://gx.api.subquery.network/sq/subquery/eth-dictionary'
#dictionary: 'https://gx.api.subquery.network/sq/subquery/eth-dictionary'
dataSources:
- kind: ethereum/Runtime
startBlock: 18721030
Expand Down
9 changes: 4 additions & 5 deletions chains-evm/eth/demo.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
network:
chainId: '5' #Ethereum Goerli
endpoint: "https://eth-goerli.api.onfinality.io/ws?apikey=${ONFINALITY_API_KEY}"
dictionary: "https://dict-tyk.subquery.network/query/eth-goerli"
chainId: '11155111' #Ethereum Sepolia
endpoint: "https://eth-sepolia.api.onfinality.io/ws?apikey=${ONFINALITY_API_KEY}"
dataSources:
- kind: ethereum/Runtime
startBlock: 10055835
startBlock: 5232630
options:
address: '0x53c155d44C03CC28f892f90aA0C442850716D75F'
address: '0x5c8657b827a138D52a4e3f03683A28B1FaD86893'
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 f284553

Please sign in to comment.