Skip to content

Commit c7f2d5d

Browse files
committed
fix: epoch nav computations for snapshots
1 parent f6c612c commit c7f2d5d

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/mappings/handlers/poolsHandlers.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
186186
const pool = await PoolService.getById(poolId.toString())
187187
if (!pool) throw missingPool
188188

189+
assertPropInitialized(pool, 'currencyId', 'string')
190+
const currency = await CurrencyService.get(pool.currencyId!)
191+
if (!currency) throw new Error(`Currency ${pool.currencyId!} not found for pool ${pool.id}`)
192+
189193
const epoch = await EpochService.getById(poolId.toString(), epochId.toNumber())
190194
if (!epoch) throw new Error(`Epoch ${epochId.toString(10)} not found for pool ${poolId.toString(10)}`)
191195

@@ -195,7 +199,6 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
195199
await pool.executeEpoch(epochId.toNumber())
196200
await pool.increaseInvestments(epoch.sumInvestedAmount!)
197201
await pool.increaseRedemptions(epoch.sumRedeemedAmount!)
198-
await pool.save()
199202

200203
// Compute and save aggregated order fulfillment
201204
const tranches = await TrancheService.getByPoolId(poolId.toString())
@@ -298,6 +301,12 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
298301
}
299302
await nextEpoch.saveWithStates()
300303

304+
// Update pool nav
305+
const partialNavs = tranches.map((tranche) => tranche.computePartialNav())
306+
await pool.setNav(partialNavs.reduce((nav, partialNav) => nav + partialNav, BigInt(0)))
307+
await pool.updateNormalizedNAV(currency.decimals)
308+
await pool.save()
309+
301310
// Track investments and redemptions for onchain cash
302311
if (!event.extrinsic) throw new Error('Event has no extrinsic')
303312
const onChainCashAsset = await AssetService.getById(pool.id, ONCHAIN_CASH_ASSET_ID)

src/mappings/services/poolService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ export class PoolService extends Pool {
286286
} else {
287287
this.normalizedNAV = BigInt(this.netAssetValue!.toString(10).substring(0, WAD_DIGITS))
288288
}
289+
logger.info(`Normalized NAV for pool ${this.id} set to ${this.normalizedNAV.toString(10)}`)
290+
return this
291+
}
292+
293+
public setNav(nav: bigint) {
294+
logger.info(`Setting nav for pool ${this.id} to ${nav.toString(10)}`)
295+
this.netAssetValue = nav
289296
return this
290297
}
291298

src/mappings/services/trancheService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ export class TrancheService extends Tranche {
287287
}
288288
this.snapshot = snapshots.pop()
289289
}
290+
291+
public computePartialNav(): bigint {
292+
const supply = bnToBn(this.tokenSupply)
293+
const price = bnToBn(this.tokenPrice)
294+
return nToBigInt(supply.mul(price).div(WAD))
295+
}
290296
}
291297

292298
type BigIntFields<T> = { [K in keyof Required<T>]: Required<T>[K] extends bigint ? K : never }[keyof Required<T>]

0 commit comments

Comments
 (0)