Skip to content
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
9 changes: 9 additions & 0 deletions generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
6 changes: 5 additions & 1 deletion src/mappings/bondTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions src/utils/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/utils/tranche.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down