diff --git a/generated/schema.ts b/generated/schema.ts index 9ea6658..3398c6f 100644 --- a/generated/schema.ts +++ b/generated/schema.ts @@ -196,6 +196,15 @@ export class Tranche extends Entity { this.set("totalCollateral", Value.fromBigInt(value)); } + get totalCollateralSimulated(): BigInt { + let value = this.get("totalCollateralSimulated"); + return value!.toBigInt(); + } + + set totalCollateralSimulated(value: BigInt) { + this.set("totalCollateralSimulated", Value.fromBigInt(value)); + } + get totalCollateralAtMaturity(): BigInt | null { let value = this.get("totalCollateralAtMaturity"); if (!value || value.kind == ValueKind.NULL) { diff --git a/schema.graphql b/schema.graphql index 0fd02f1..73c3b08 100644 --- a/schema.graphql +++ b/schema.graphql @@ -29,8 +29,10 @@ type Tranche @entity { ratio: BigInt! " index of this tranche, i.e. A-tranche is 0, B is 1 and so on " index: BigInt! - " The amount of collateral in this tranche for redemption " + " The amount of collateral held by the contract. Is 0 until bond is matured, and after that point will update with rebases and user redemptions " totalCollateral: BigInt! + " The amount of collateral the tranche would receive if the bond were to be matured at this point in time. Post maturity this is equal to totalCollateral " + totalCollateralSimulated: BigInt! " The amount of collateral in this tranche for redemption at time bond matured " totalCollateralAtMaturity: BigInt " token total supply at time bond matured " diff --git a/src/mappings/bondTemplate.ts b/src/mappings/bondTemplate.ts index c4ecedb..fbb616e 100644 --- a/src/mappings/bondTemplate.ts +++ b/src/mappings/bondTemplate.ts @@ -6,27 +6,31 @@ import { Redeem, RedeemMature, } from '../../generated/templates/BondTemplate/BondController'; -import { fetchBond, matureBond, updateBond } from '../utils/bond'; +import { fetchBond, matureBond, updateBond, updateSimulatedTrancheCollateral } from '../utils/bond'; export function handleDeposit(event: Deposit): void { const bond = fetchBond(event.address.toHexString()); updateBond(bond); + updateSimulatedTrancheCollateral(bond); } export function handleRedeem(event: Redeem): void { const bond = fetchBond(event.address.toHexString()); updateBond(bond); + updateSimulatedTrancheCollateral(bond); } export function handleRedeemMature(event: RedeemMature): void { const bond = fetchBond(event.address.toHexString()); updateBond(bond); + updateSimulatedTrancheCollateral(bond); } export function handleMature(event: Mature): void { const bond = fetchBond(event.address.toHexString()); updateBond(bond); matureBond(bond, event); + updateSimulatedTrancheCollateral(bond); } export function handleFeeUpdate(event: FeeUpdate): void { diff --git a/src/utils/bond.ts b/src/utils/bond.ts index 3d3a0df..431cbc6 100644 --- a/src/utils/bond.ts +++ b/src/utils/bond.ts @@ -165,6 +165,35 @@ export function updateBondAfterRebase(bondId: string): void { tranche.save(); } bond.save(); + updateSimulatedTrancheCollateral(bond); +} + +export function updateSimulatedTrancheCollateral(bond: Bond): void { + const tranches = bond.tranches; + let collateralRemaining = bond.totalCollateral; + for (let trancheIndex = 0; trancheIndex < tranches.length; trancheIndex++) { + const trancheId = tranches[trancheIndex]; + const tranche = fetchTranche(trancheId); + if (bond.isMature) { + tranche.totalCollateralSimulated = tranche.totalCollateral; + } else { + const trancheSupply = fetchToken(tranche.token).totalSupply; + if (collateralRemaining.lt(trancheSupply)) { + tranche.totalCollateralSimulated = collateralRemaining; + collateralRemaining = ZERO_BI; + } else { + if (trancheIndex < tranches.length - 1) { + // Senior tranches get paid up to 1:1 + tranche.totalCollateralSimulated = trancheSupply; + collateralRemaining = collateralRemaining.minus(trancheSupply); + } else { + // Junior tranche gets all excess + tranche.totalCollateralSimulated = collateralRemaining; + } + } + } + tranche.save(); + } } export function updateBond(bond: Bond): void { diff --git a/src/utils/tranche.ts b/src/utils/tranche.ts index 299be9c..87aa670 100644 --- a/src/utils/tranche.ts +++ b/src/utils/tranche.ts @@ -16,6 +16,7 @@ export function createTranche(bondId: string, collateralId: string, trancheIndex tranche.token = trancheId; tranche.ratio = trancheResult.value.getRatio(); tranche.totalCollateral = ERC20.bind(castToAddress(collateralId)).balanceOf(trancheAddress); + tranche.totalCollateralSimulated = tranche.totalCollateral; tranche.save(); return tranche; } else {