Skip to content

Commit

Permalink
rollover vault
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Nov 18, 2022
1 parent 30bfe84 commit 7875d90
Show file tree
Hide file tree
Showing 2 changed files with 529 additions and 13 deletions.
32 changes: 19 additions & 13 deletions spot-contracts/contracts/_utils/BondHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -191,27 +191,33 @@ library BondHelpers {
return (td, collateralBalances, trancheSupplies);
}

/// @notice Given a bond, retrieves the collateral redeemable for
/// each tranche held by the given address.
/// @notice Given a bond and a user address computes the tranche amounts proportional to the tranche ratios,
// that can be redeemed for the collateral before maturity.
/// @param b The address of the bond contract.
/// @param u The address to check balance for.
/// @return The tranche data and an array of collateral balances.
function getTrancheCollateralBalances(IBondController b, address u)
/// @return The tranche data and an array of tranche token balances.
function computeRedeemableTrancheAmounts(IBondController b, address u)
internal
view
returns (TrancheData memory, uint256[] memory)
{
TrancheData memory td;
uint256[] memory collateralBalances;
uint256[] memory trancheSupplies;

(td, collateralBalances, trancheSupplies) = getTrancheCollateralizations(b);
TrancheData memory td = getTrancheData(b);
uint256[] memory redeemableAmts = new uint256[](td.trancheCount);

// We calculate the minimum value of {trancheBal/trancheRatio} across tranches
uint256 min = type(uint256).max;
uint8 i;
for (i = 0; i < td.trancheCount && min != 0; i++) {
uint256 d = (td.tranches[i].balanceOf(u) * TRANCHE_RATIO_GRANULARITY) / td.trancheRatios[i];
if (d < min) {
min = d;
}
}

uint256[] memory balances = new uint256[](td.trancheCount);
for (uint8 i = 0; i < td.trancheCount; i++) {
balances[i] = (td.tranches[i].balanceOf(u) * collateralBalances[i]) / trancheSupplies[i];
for (i = 0; i < td.trancheCount; i++) {
redeemableAmts[i] = (td.trancheRatios[i] * min) / TRANCHE_RATIO_GRANULARITY;
}

return (td, balances);
return (td, redeemableAmts);
}
}
Loading

0 comments on commit 7875d90

Please sign in to comment.