Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tinlake additions #254

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions chains-evm/eth/centrifuge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -32,4 +35,4 @@ dataSources:
- handler: handleEthBlock
kind: ethereum/BlockHandler
filter:
modulo: 300
modulo: 300
13 changes: 13 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type Pool @entity {

availableReserve: BigInt
maxReserve: BigInt

weightedAverageInterestRatePerSec: BigInt

# Aggregated transaction data over the last period
sumBorrowedAmountByPeriod: BigInt
Expand Down Expand Up @@ -96,6 +98,9 @@ type Pool @entity {
sumUnscheduledRepaidAmount: BigInt
sumNumberOfAssets: BigInt

sumBorrowsCount: BigInt
sumRepaysCount: BigInt

tranches: [Tranche] @derivedFrom(field: "pool")
assets: [Asset] @derivedFrom(field: "pool")
}
Expand All @@ -121,6 +126,8 @@ type PoolSnapshot @entity {
availableReserve: BigInt
maxReserve: BigInt

weightedAverageInterestRatePerSec: BigInt

# Aggregated transaction data over the last period
sumBorrowedAmountByPeriod: BigInt
sumRepaidAmountByPeriod: BigInt
Expand Down Expand Up @@ -160,6 +167,9 @@ type PoolSnapshot @entity {
sumInterestRepaidAmount: BigInt
sumUnscheduledRepaidAmount: BigInt
sumNumberOfAssets: BigInt

sumBorrowsCount: BigInt
sumRepaysCount: BigInt
}

type Tranche @entity {
Expand Down Expand Up @@ -489,6 +499,9 @@ type Asset @entity {
totalRepaidInterest: BigInt
totalRepaidUnscheduled: BigInt

borrowsCount: BigInt
repaysCount: BigInt

borrowedAmountByPeriod: BigInt
repaidAmountByPeriod: BigInt

Expand Down
37 changes: 31 additions & 6 deletions src/mappings/handlers/ethHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -318,6 +320,12 @@ async function updateLoans(poolId: string, blockDate: Date, shelf: string, pile:
}
}

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]
Expand All @@ -342,22 +350,39 @@ 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.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.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 || 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
if (sumDebt > BigInt(0)) {
pool.weightedAverageInterestRatePerSec = sumInterestRatePerSec / sumDebt
} else {
pool.weightedAverageInterestRatePerSec = BigInt(0)
}
pool.sumBorrowsCount = sumBorrowsCount
pool.sumRepaysCount = sumRepaysCount
await pool.save()
}
}

Expand Down
Loading