From 1ff84a95bf81056d5d0c457ce88d0704101183ab Mon Sep 17 00:00:00 2001 From: Jeroen Offerijns Date: Fri, 13 Sep 2024 22:15:37 +0200 Subject: [PATCH 1/5] feat: track tinlake totals --- src/mappings/handlers/ethHandlers.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/mappings/handlers/ethHandlers.ts b/src/mappings/handlers/ethHandlers.ts index fa57bdb..2e1ea86 100644 --- a/src/mappings/handlers/ethHandlers.ts +++ b/src/mappings/handlers/ethHandlers.ts @@ -169,6 +169,8 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: const newLoans = await getNewLoans(existingLoanIds as number[], shelf) logger.info(`Found ${newLoans.length} new loans for pool ${poolId}`) + const pool = await PoolService.getById(poolId) + const nftIdCalls: PoolMulticall[] = [] for (const loanIndex of newLoans) { nftIdCalls.push({ @@ -318,6 +320,9 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: } } + let totalDebt = BigInt(0) + let totalBorrowed = BigInt(0) + let totalRepaid = BigInt(0) for (let i = 0; i < existingLoans.length; i++) { const loan = existingLoans[i] const loanIndex = loan.id.split('-')[1] @@ -357,7 +362,16 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: } logger.info(`Updating loan ${loan.id} for pool ${poolId}`) await loan.save() + + totalDebt += loan.outstandingDebt + totalBorrowed += loan.totalBorrowed + totalRepaid += loan.totalRepaid } + + pool.sumDebt = totalDebt + pool.sumBorrowedAmount = totalBorrowed + pool.sumRepaidAmount = totalRepaid + await pool.save() } } From 99c3c63d1eb842162796e61652c9a553d533ca9b Mon Sep 17 00:00:00 2001 From: Jeroen Offerijns Date: Fri, 13 Sep 2024 22:17:35 +0200 Subject: [PATCH 2/5] feat: track wa interest rate --- schema.graphql | 4 ++++ src/mappings/handlers/ethHandlers.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/schema.graphql b/schema.graphql index 4e1eaa5..e30564e 100644 --- a/schema.graphql +++ b/schema.graphql @@ -55,6 +55,8 @@ type Pool @entity { availableReserve: BigInt maxReserve: BigInt + + weightedAverageInterestRatePerSec: BigInt # Aggregated transaction data over the last period sumBorrowedAmountByPeriod: BigInt @@ -121,6 +123,8 @@ type PoolSnapshot @entity { availableReserve: BigInt maxReserve: BigInt + weightedAverageInterestRatePerSec: BigInt + # Aggregated transaction data over the last period sumBorrowedAmountByPeriod: BigInt sumRepaidAmountByPeriod: BigInt diff --git a/src/mappings/handlers/ethHandlers.ts b/src/mappings/handlers/ethHandlers.ts index 2e1ea86..f7b64fd 100644 --- a/src/mappings/handlers/ethHandlers.ts +++ b/src/mappings/handlers/ethHandlers.ts @@ -323,6 +323,7 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: let totalDebt = BigInt(0) let totalBorrowed = BigInt(0) let totalRepaid = BigInt(0) + let totalInterestRatePerSec = BigInt(0) for (let i = 0; i < existingLoans.length; i++) { const loan = existingLoans[i] const loanIndex = loan.id.split('-')[1] @@ -366,11 +367,13 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: totalDebt += loan.outstandingDebt totalBorrowed += loan.totalBorrowed totalRepaid += loan.totalRepaid + totalInterestRatePerSec += loan.interestRatePerSec * loan.outstandingDebt } pool.sumDebt = totalDebt pool.sumBorrowedAmount = totalBorrowed pool.sumRepaidAmount = totalRepaid + pool.weightedAverageInterestRatePerSec = totalInterestRatePerSec / totalDebt await pool.save() } } From 7e52f73dccb9c21b76021888132664f9c82a8e8e Mon Sep 17 00:00:00 2001 From: Jeroen Offerijns Date: Fri, 13 Sep 2024 22:26:47 +0200 Subject: [PATCH 3/5] feat: track counts --- schema.graphql | 9 ++++++++ src/mappings/handlers/ethHandlers.ts | 32 +++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/schema.graphql b/schema.graphql index e30564e..c32b195 100644 --- a/schema.graphql +++ b/schema.graphql @@ -98,6 +98,9 @@ type Pool @entity { sumUnscheduledRepaidAmount: BigInt sumNumberOfAssets: BigInt + sumBorrowsCount: BigInt + sumRepaysCount: BigInt + tranches: [Tranche] @derivedFrom(field: "pool") assets: [Asset] @derivedFrom(field: "pool") } @@ -164,6 +167,9 @@ type PoolSnapshot @entity { sumInterestRepaidAmount: BigInt sumUnscheduledRepaidAmount: BigInt sumNumberOfAssets: BigInt + + sumBorrowsCount: BigInt + sumRepaysCount: BigInt } type Tranche @entity { @@ -493,6 +499,9 @@ type Asset @entity { totalRepaidInterest: BigInt totalRepaidUnscheduled: BigInt + borrowsCount: BigInt + repaysCount: BigInt + borrowedAmountByPeriod: BigInt repaidAmountByPeriod: BigInt diff --git a/src/mappings/handlers/ethHandlers.ts b/src/mappings/handlers/ethHandlers.ts index f7b64fd..45a584b 100644 --- a/src/mappings/handlers/ethHandlers.ts +++ b/src/mappings/handlers/ethHandlers.ts @@ -320,10 +320,12 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: } } - let totalDebt = BigInt(0) - let totalBorrowed = BigInt(0) - let totalRepaid = BigInt(0) - let totalInterestRatePerSec = BigInt(0) + let sumDebt = BigInt(0) + let sumBorrowed = BigInt(0) + let sumRepaid = BigInt(0) + let sumInterestRatePerSec = BigInt(0) + let sumBorrowsCount = BigInt(0) + let sumRepaysCount = BigInt(0) for (let i = 0; i < existingLoans.length; i++) { const loan = existingLoans[i] const loanIndex = loan.id.split('-')[1] @@ -351,6 +353,7 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: loan.totalRepaid ? (loan.totalRepaid += loan.repaidAmountByPeriod) : (loan.totalRepaid = loan.repaidAmountByPeriod) + loan.repaysCount += 1 } if ( prevDebt * (loan.interestRatePerSec / BigInt(10) ** BigInt(27)) * BigInt(86400) < @@ -360,20 +363,25 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: loan.totalBorrowed ? (loan.totalBorrowed += loan.borrowedAmountByPeriod) : (loan.totalBorrowed = loan.borrowedAmountByPeriod) + loan.borrowsCount += 1 } logger.info(`Updating loan ${loan.id} for pool ${poolId}`) await loan.save() - totalDebt += loan.outstandingDebt - totalBorrowed += loan.totalBorrowed - totalRepaid += loan.totalRepaid - totalInterestRatePerSec += loan.interestRatePerSec * loan.outstandingDebt + sumDebt += loan.outstandingDebt + sumBorrowed += loan.totalBorrowed + sumRepaid += loan.totalRepaid + sumInterestRatePerSec += loan.interestRatePerSec * loan.outstandingDebt + sumBorrowsCount += loan.borrowsCount + sumRepaysCount += loan.repaysCount } - pool.sumDebt = totalDebt - pool.sumBorrowedAmount = totalBorrowed - pool.sumRepaidAmount = totalRepaid - pool.weightedAverageInterestRatePerSec = totalInterestRatePerSec / totalDebt + pool.sumDebt = sumDebt + pool.sumBorrowedAmount = sumBorrowed + pool.sumRepaidAmount = sumRepaid + pool.weightedAverageInterestRatePerSec = sumInterestRatePerSec / sumDebt + pool.sumBorrowsCount = sumBorrowsCount + pool.sumRepaysCount = sumRepaysCount await pool.save() } } From a2524aaa1fa5ec50dbf3e1044d6fa629b088ea8d Mon Sep 17 00:00:00 2001 From: Jeroen Offerijns Date: Fri, 13 Sep 2024 22:29:25 +0200 Subject: [PATCH 4/5] fix: one type --- src/mappings/handlers/ethHandlers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mappings/handlers/ethHandlers.ts b/src/mappings/handlers/ethHandlers.ts index 45a584b..ce85314 100644 --- a/src/mappings/handlers/ethHandlers.ts +++ b/src/mappings/handlers/ethHandlers.ts @@ -353,7 +353,7 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: loan.totalRepaid ? (loan.totalRepaid += loan.repaidAmountByPeriod) : (loan.totalRepaid = loan.repaidAmountByPeriod) - loan.repaysCount += 1 + loan.repaysCount += BigInt(1) } if ( prevDebt * (loan.interestRatePerSec / BigInt(10) ** BigInt(27)) * BigInt(86400) < @@ -363,7 +363,7 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: loan.totalBorrowed ? (loan.totalBorrowed += loan.borrowedAmountByPeriod) : (loan.totalBorrowed = loan.borrowedAmountByPeriod) - loan.borrowsCount += 1 + loan.borrowsCount += BigInt(1) } logger.info(`Updating loan ${loan.id} for pool ${poolId}`) await loan.save() From f5321eb392b94d2de2d3644f76b12a57a4c5d064 Mon Sep 17 00:00:00 2001 From: Adam Stox Date: Sat, 21 Sep 2024 16:47:53 -0400 Subject: [PATCH 5/5] fix: default values and divide by 0 --- chains-evm/eth/centrifuge.yaml | 11 ++++++---- src/mappings/handlers/ethHandlers.ts | 30 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/chains-evm/eth/centrifuge.yaml b/chains-evm/eth/centrifuge.yaml index a73fdd8..3fde81a 100644 --- a/chains-evm/eth/centrifuge.yaml +++ b/chains-evm/eth/centrifuge.yaml @@ -4,15 +4,18 @@ network: #dictionary: 'https://gx.api.subquery.network/sq/subquery/eth-dictionary' dataSources: - kind: ethereum/Runtime #PoolManager V1 - startBlock: 18721030 + startBlock: 20778999 + # startBlock: 18721030 options: address: '0x78E9e622A57f70F1E0Ec652A4931E4e278e58142' - kind: ethereum/Runtime #PoolManager V2 - startBlock: 20432390 + startBlock: 20778999 + # startBlock: 20432390 options: address: '0x91808B5E2F6d7483D41A681034D7c9DbB64B9E29' - kind: ethereum/Runtime #Tinlake - startBlock: 11063000 + startBlock: 20778999 + # startBlock: 11063000 options: abi: navFeed assets: @@ -32,4 +35,4 @@ dataSources: - handler: handleEthBlock kind: ethereum/BlockHandler filter: - modulo: 300 \ No newline at end of file + modulo: 300 diff --git a/src/mappings/handlers/ethHandlers.ts b/src/mappings/handlers/ethHandlers.ts index ce85314..05ea65b 100644 --- a/src/mappings/handlers/ethHandlers.ts +++ b/src/mappings/handlers/ethHandlers.ts @@ -350,36 +350,36 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile: if (prevDebt > currentDebt) { loan.repaidAmountByPeriod = prevDebt - currentDebt - loan.totalRepaid - ? (loan.totalRepaid += loan.repaidAmountByPeriod) - : (loan.totalRepaid = loan.repaidAmountByPeriod) - loan.repaysCount += BigInt(1) + loan.totalRepaid = (loan.totalRepaid || BigInt(0)) + loan.repaidAmountByPeriod + loan.repaysCount = (loan.repaysCount || BigInt(0)) + BigInt(1) } if ( prevDebt * (loan.interestRatePerSec / BigInt(10) ** BigInt(27)) * BigInt(86400) < (loan.outstandingDebt || BigInt(0)) ) { loan.borrowedAmountByPeriod = (loan.outstandingDebt || BigInt(0)) - prevDebt - loan.totalBorrowed - ? (loan.totalBorrowed += loan.borrowedAmountByPeriod) - : (loan.totalBorrowed = loan.borrowedAmountByPeriod) - loan.borrowsCount += BigInt(1) + loan.totalBorrowed = (loan.totalBorrowed || BigInt(0)) + loan.borrowedAmountByPeriod + loan.borrowsCount = (loan.borrowsCount || BigInt(0)) + BigInt(1) } logger.info(`Updating loan ${loan.id} for pool ${poolId}`) await loan.save() - sumDebt += loan.outstandingDebt - sumBorrowed += loan.totalBorrowed - sumRepaid += loan.totalRepaid - sumInterestRatePerSec += loan.interestRatePerSec * loan.outstandingDebt - sumBorrowsCount += loan.borrowsCount - sumRepaysCount += loan.repaysCount + sumDebt += loan.outstandingDebt || BigInt(0) + sumBorrowed += loan.totalBorrowed || BigInt(0) + sumRepaid += loan.totalRepaid || BigInt(0) + sumInterestRatePerSec += (loan.interestRatePerSec || BigInt(0)) * (loan.outstandingDebt || BigInt(0)) + sumBorrowsCount += loan.borrowsCount || BigInt(0) + sumRepaysCount += loan.repaysCount || BigInt(0) } pool.sumDebt = sumDebt pool.sumBorrowedAmount = sumBorrowed pool.sumRepaidAmount = sumRepaid - pool.weightedAverageInterestRatePerSec = sumInterestRatePerSec / sumDebt + if (sumDebt > BigInt(0)) { + pool.weightedAverageInterestRatePerSec = sumInterestRatePerSec / sumDebt + } else { + pool.weightedAverageInterestRatePerSec = BigInt(0) + } pool.sumBorrowsCount = sumBorrowsCount pool.sumRepaysCount = sumRepaysCount await pool.save()