Skip to content

Commit 8c6c48e

Browse files
authored
feat: add yieldMTD, yieldQTD, yieldYTD (#204)
Fixes #176
1 parent eeb9e3f commit 8c6c48e

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

schema.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ type Tranche @entity {
174174
yield90DaysAnnualized: BigInt
175175
yieldSinceInception: BigInt
176176
yieldSinceLastPeriod: BigInt
177+
yieldMTD: BigInt
178+
yieldQTD: BigInt
179+
yieldYTD: BigInt
180+
177181
}
178182

179183
type TrancheSnapshot @entity {

src/mappings/handlers/blockHandlers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
3737
const lastPeriodStart = new Date(blockPeriodStart.valueOf() - SNAPSHOT_INTERVAL_SECONDS * 1000)
3838
const daysAgo30 = new Date(blockPeriodStart.valueOf() - 30 * 24 * 3600 * 1000)
3939
const daysAgo90 = new Date(blockPeriodStart.valueOf() - 90 * 24 * 3600 * 1000)
40+
const beginningOfMonth = new Date(lastPeriodStart.getFullYear(), lastPeriodStart.getMonth(), 1)
41+
const quarter = Math.floor(lastPeriodStart.getMonth() / 3)
42+
const beginningOfQuarter = new Date(lastPeriodStart.getFullYear(), quarter * 3, 1)
43+
const beginningOfYear = new Date(lastPeriodStart.getFullYear(), 0, 1)
4044

4145
// Update Pool States
4246
const pools = await PoolService.getCfgActivePools()
@@ -57,6 +61,9 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
5761
await tranche.updateDebt(trancheData[tranche.trancheId].data.debt.toBigInt())
5862
await tranche.computeYield('yieldSinceLastPeriod', lastPeriodStart)
5963
await tranche.computeYield('yieldSinceInception')
64+
await tranche.computeYield('yieldYTD', beginningOfYear)
65+
await tranche.computeYield('yieldQTD', beginningOfQuarter)
66+
await tranche.computeYield('yieldMTD', beginningOfMonth)
6067
await tranche.computeYieldAnnualized('yield30DaysAnnualized', blockPeriodStart, daysAgo30)
6168
await tranche.computeYieldAnnualized('yield90DaysAnnualized', blockPeriodStart, daysAgo90)
6269
await tranche.save()

src/mappings/services/trancheService.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ export class TrancheService extends Tranche {
104104
return this
105105
}
106106
const accruedFees = bnToBn(navResponse.unwrap().navFees.toBigInt())
107-
if (!this.tokenSupply || this.tokenSupply === BigInt(0) ) {
107+
if (!this.tokenSupply || this.tokenSupply === BigInt(0)) {
108108
logger.warn(`Token supply equal 0! Cannot perform division. Token price: ${price}`)
109109
this.tokenPrice = price
110110
return this
111111
}
112-
this.tokenPrice = nToBigInt(bnToBn(price).sub(accruedFees.mul(WAD).div(bnToBn(this.tokenSupply))))
112+
this.tokenPrice = nToBigInt(bnToBn(price).sub(accruedFees.mul(WAD).div(bnToBn(this.tokenSupply))))
113113
logger.info(`Updating price for tranche ${this.id} to: ${this.tokenPrice} (ACCOUNTING FOR ACCRUED FEES)`)
114114
return this
115115
}
@@ -134,9 +134,9 @@ export class TrancheService extends Tranche {
134134
return this
135135
}
136136

137-
public async computeYield(yieldField: string, referencePeriodStart?: Date) {
137+
public async computeYield(yieldField: BigIntFields<TrancheService>, referencePeriodStart?: Date) {
138138
logger.info(
139-
`Computing yield for tranche ${this.trancheId} of ` +
139+
`Computing yield ${yieldField} for tranche ${this.trancheId} of ` +
140140
`pool ${this.poolId} with reference date ${referencePeriodStart}`
141141
)
142142

@@ -167,9 +167,13 @@ export class TrancheService extends Tranche {
167167
return this
168168
}
169169

170-
public async computeYieldAnnualized(yieldField: string, currentPeriodStart: Date, referencePeriodStart: Date) {
170+
public async computeYieldAnnualized(
171+
yieldField: BigIntFields<TrancheService>,
172+
currentPeriodStart: Date,
173+
referencePeriodStart: Date
174+
) {
171175
logger.info(
172-
`Computing annualized yield for tranche ${this.trancheId} of ` +
176+
`Computing annualized yield ${yieldField} for tranche ${this.trancheId} of ` +
173177
`pool ${this.poolId} with reference date ${referencePeriodStart}`
174178
)
175179
const trancheSnapshots = await TrancheSnapshot.getByPeriodStart(referencePeriodStart)
@@ -246,3 +250,5 @@ export class TrancheService extends Tranche {
246250
this.isActive = true
247251
}
248252
}
253+
254+
type BigIntFields<T> = { [K in keyof T]: T[K] extends bigint ? K : never }[keyof T]

0 commit comments

Comments
 (0)