Skip to content

Commit

Permalink
Merge pull request #105 from embrio-tech/main
Browse files Browse the repository at this point in the history
20240205 Sprint 27
  • Loading branch information
hieronx authored Feb 5, 2024
2 parents e16160e + ae9ff16 commit 9a96119
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export COMPOSE_PROFILES=subql
export COMPOSE_PROFILES=subql-cfg
export DB_USER=postgres
export DB_PASS=postgres
export DB_DATABASE=postgres
Expand Down
8 changes: 2 additions & 6 deletions chains-cfg/_root.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ dataSources:
- handler: handleBlock
kind: substrate/BlockHandler
filter:
timestamp: '1 0,12 * * *'
modulo: 300
#timestamp: '1 0,12 * * *'
# - handler: logEvents
# kind: substrate/EventHandler
- handler: handlePoolCreated
Expand Down Expand Up @@ -109,11 +110,6 @@ dataSources:
filter:
module: ormlTokens
method: Transfer
- handler: handleTokenEndowed
kind: substrate/EventHandler
filter:
module: ormlTokens
method: Endowed
- handler: handleTokenDeposited
kind: substrate/EventHandler
filter:
Expand Down
2 changes: 2 additions & 0 deletions chains-cfg/centrifuge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ network:
chainId: '0xb3db41421702df9a7fcac62b53ffeac85f7853cc4e689e0b93aeb3db18c09d82'
chaintypes:
file: ./dist/chaintypes.js
bypassBlocks:
- "3858150-4216110"
dataSources:
- kind: substrate/Runtime
startBlock: 3858140 # block first pool was created at
46 changes: 43 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type Pool @entity {
type: String! @index
isActive: Boolean! @index


createdAt: Date
createdAtBlockNumber: Int

Expand All @@ -37,6 +36,10 @@ type Pool @entity {
# Aggregated transaction data over the last period
sumBorrowedAmountByPeriod: BigInt
sumRepaidAmountByPeriod: BigInt
sumPrincipalRepaidAmountByPeriod: BigInt
sumInterestRepaidAmountByPeriod: BigInt
sumUnscheduledRepaidAmountByPeriod: BigInt

sumInvestedAmountByPeriod: BigInt
sumRedeemedAmountByPeriod: BigInt
sumNumberOfLoansByPeriod: BigInt
Expand All @@ -48,6 +51,9 @@ type Pool @entity {
# Cumulated transaction data since pool creation
sumBorrowedAmount: BigInt
sumRepaidAmount: BigInt
sumPrincipalRepaidAmount: BigInt
sumInterestRepaidAmount: BigInt
sumUnscheduledRepaidAmount: BigInt
sumNumberOfLoans: BigInt

tranches: [Tranche] @derivedFrom(field: "pool")
Expand All @@ -73,15 +79,24 @@ type PoolSnapshot @entity {
# Aggregated transaction data over the last period
sumBorrowedAmountByPeriod: BigInt
sumRepaidAmountByPeriod: BigInt
sumPrincipalRepaidAmountByPeriod: BigInt
sumInterestRepaidAmountByPeriod: BigInt
sumUnscheduledRepaidAmountByPeriod: BigInt

sumInvestedAmountByPeriod: BigInt
sumRedeemedAmountByPeriod: BigInt
sumNumberOfLoansByPeriod: BigInt

sumNumberOfActiveLoans: BigInt
sumDebtOverdue: BigInt
sumDebtWrittenOffByPeriod: BigInt

# Cumulated transaction data since pool creation
sumBorrowedAmount: BigInt
sumRepaidAmount: BigInt
sumPrincipalRepaidAmount: BigInt
sumInterestRepaidAmount: BigInt
sumUnscheduledRepaidAmount: BigInt
sumNumberOfLoans: BigInt
}

Expand Down Expand Up @@ -309,7 +324,6 @@ type Loan @entity {
probabilityOfDefault: BigInt
lossGivenDefault: BigInt
discountRate: BigInt
maturityDate: Date

interestRatePerSec: BigInt

Expand All @@ -320,7 +334,19 @@ type Loan @entity {
isActive: Boolean! @index
status: LoanStatus!

outstandingPrincipal: BigInt
outstandingInterest: BigInt
outstandingDebt: BigInt
presentValue: BigInt
actualMaturityDate: Date
timeToMaturity: Int
actualOriginationDate: Date
writeOffPercentage: BigInt
totalBorrowed: BigInt
totalRepaid: BigInt
totalRepaidPrincipal: BigInt
totalRepaidInterest: BigInt
totalRepaidUnscheduled: BigInt

borrowedAmountByPeriod: BigInt
repaidAmountByPeriod: BigInt
Expand All @@ -339,7 +365,19 @@ type LoanSnapshot @entity {
blockNumber: Int!
periodStart: Date! @index

outstandingPrincipal: BigInt
outstandingInterest: BigInt
outstandingDebt: BigInt
presentValue: BigInt
actualMaturityDate: Date
timeToMaturity: Int
actualOriginationDate: Date
writeOffPercentage: BigInt
totalBorrowed: BigInt
totalRepaid: BigInt
totalRepaidPrincipal: BigInt
totalRepaidInterest: BigInt
totalRepaidUnscheduled: BigInt

borrowedAmountByPeriod: BigInt
repaidAmountByPeriod: BigInt
Expand Down Expand Up @@ -371,6 +409,8 @@ type Currency @entity {
id: ID! # chainId - currencySpec - [currencySpec]
chain: Blockchain!
decimals: Int!
name: String
symbol: String

tokenAddress: String
escrowAddress: String
Expand All @@ -390,4 +430,4 @@ type CurrencyBalance @entity {

type Blockchain @entity {
id: ID! #EVM chainId
}
}
46 changes: 46 additions & 0 deletions src/chaintypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@ import type { OverrideBundleDefinition } from '@polkadot/types/types'
/* eslint-disable sort-keys */

const definitions: OverrideBundleDefinition = {
types: [
{
minmax: [undefined, undefined],
types: {
ActiveLoanInfo: {
activeLoan: 'PalletLoansEntitiesLoansActiveLoan',
presentValue: 'Balance',
outstandingPrincipal: 'Balance',
outstandingInterest: 'Balance',
},
},
},
],
runtime: {
LoansApi: [
{
methods: {
portfolio: {
description: 'Get active pool loan',
params: [
{
name: 'pool_id',
type: 'u64',
},
],
type: 'Vec<(u64, ActiveLoanInfo)>',
},
portfolio_loan: {
description: 'Get active pool loan',
params: [
{
name: 'pool_id',
type: 'u64',
},
{
name: 'loan_id',
type: 'u64',
},
],
type: 'Option<PalletLoansEntitiesLoansActiveLoan>',
},
},
version: 1,
},
],
},
rpc: {
pools: {
trancheTokenPrices: {
Expand Down
19 changes: 16 additions & 3 deletions src/helpers/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//find out types: const a = createType(api.registry, '[u8;32]', 18)
import { AugmentedRpc, PromiseRpcResult } from '@polkadot/api/types'
import { AugmentedCall, AugmentedRpc, PromiseRpcResult } from '@polkadot/api/types'
import { Enum, Null, Struct, u128, u32, u64, U8aFixed, Option, Vec, Bytes } from '@polkadot/types'
import { AccountId32, Perquintill } from '@polkadot/types/interfaces'
import { AccountId32, Perquintill, Balance } from '@polkadot/types/interfaces'
import { ITuple, Observable } from '@polkadot/types/types'

export interface PoolDetails extends Struct {
Expand Down Expand Up @@ -162,6 +162,13 @@ export interface LoanInfoCreated extends Struct {
}
}

export interface LoanInfoActivePortfolio extends Struct {
activeLoan: LoanInfoActive,
presentValue: Balance,
outstandingPrincipal: Balance,
outstandingInterest: Balance,
}

export interface LoanInfoActive extends Struct {
schedule: LoanRepaymentSchedule
collateral: ITuple<[u64, u128]>
Expand Down Expand Up @@ -339,7 +346,7 @@ export type InvestOrdersCollectedEvent = ITuple<
who: AccountId32,
processedOrders: Vec<u64>,
collection: InvestCollection,
outcome: Enum
outcome: Enum,
]
>
export type RedeemOrdersCollectedEvent = ITuple<
Expand All @@ -364,3 +371,9 @@ export type ExtendedRpc = typeof api.rpc & {
trancheTokenPrices: PromiseRpcResult<AugmentedRpc<(poolId: number | string) => Observable<Vec<u128>>>>
}
}

export type ExtendedCall = typeof api.call & {
loansApi: {
portfolio: AugmentedCall<'promise', (poolId: string) => Observable<Vec<ITuple<[u64, LoanInfoActivePortfolio]>>>>
}
}
17 changes: 8 additions & 9 deletions src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { stateSnapshotter } from '../../helpers/stateSnapshot'
import { SNAPSHOT_INTERVAL_SECONDS } from '../../config'
import { PoolService } from '../services/poolService'
import { TrancheService } from '../services/trancheService'
import { LoanService } from '../services/loanService'

const timekeeper = TimekeeperService.init()

Expand Down Expand Up @@ -45,16 +46,14 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
await tranche.save()
}

const activeLoanData = await pool.getActiveLoanData()
// TODO: Reinclude outstanding debt calculation
// for (const loanId in activeLoanData) {
// const loan = await LoanService.getById(pool.id, loanId)
// const { normalizedAcc, interestRate } = activeLoanData[loanId]
// await loan.updateOutstandingDebt(normalizedAcc, interestRate)
// await loan.save()
const activeLoanData = await pool.getPortfolio()
for (const loanId in activeLoanData) {
const loan = await LoanService.getById(pool.id, loanId)
await loan.updateActiveLoanData(activeLoanData[loanId])
await loan.save()

// if (loan.maturityDate < block.timestamp) await pool.increaseDebtOverdue(loan.outstandingDebt)
// }
if (loan.actualMaturityDate < block.timestamp) await pool.increaseDebtOverdue(loan.outstandingDebt)
}

await pool.updateNumberOfActiveLoans(BigInt(Object.keys(activeLoanData).length))
await pool.save()
Expand Down
Loading

0 comments on commit 9a96119

Please sign in to comment.