Skip to content

Commit

Permalink
code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Feb 27, 2023
1 parent 1613767 commit 9788855
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions spot-contracts/contracts/_utils/BondHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.18;

import { SafeCastUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol";
import { MathUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol";

import { IERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import { IBondController } from "../_interfaces/buttonwood/IBondController.sol";
Expand Down Expand Up @@ -191,8 +192,8 @@ library BondHelpers {
return (td, collateralBalances, trancheSupplies);
}

/// @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.
/// @notice For a given bond and user address, computes the maximum number of each of the bond's tranches
/// the user is able to redeem before the bond's maturity. These tranche amounts necessarily match the bond's tranche ratios.
/// @param b The address of the bond contract.
/// @param u The address to check balance for.
/// @return The tranche data and an array of tranche token balances.
Expand All @@ -204,18 +205,28 @@ library BondHelpers {
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;
// Calculate how many underlying assets could be redeemed from each tranche balance,
// assuming other tranches are not an issue, and record the smallest amount.
uint256 minUnderlyingOut = 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;
for (i = 0; i < td.trancheCount; i++) {
uint256 d = MathUpgradeable.mulDiv(
td.tranches[i].balanceOf(u),
TRANCHE_RATIO_GRANULARITY,
td.trancheRatios[i]
);
if (d < minUnderlyingOut) {
minUnderlyingOut = d;
}

// if one of the balances is zero, we return
if (minUnderlyingOut == 0) {
return (td, redeemableAmts);
}
}

for (i = 0; i < td.trancheCount; i++) {
redeemableAmts[i] = (td.trancheRatios[i] * min) / TRANCHE_RATIO_GRANULARITY;
redeemableAmts[i] = MathUpgradeable.mulDiv(td.trancheRatios[i], minUnderlyingOut, TRANCHE_RATIO_GRANULARITY);
}

return (td, redeemableAmts);
Expand Down

0 comments on commit 9788855

Please sign in to comment.