Skip to content

Commit

Permalink
intermediate
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Dec 29, 2022
1 parent 515a658 commit 4a1c8de
Show file tree
Hide file tree
Showing 5 changed files with 561 additions and 168 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);
}
}
16 changes: 8 additions & 8 deletions spot-contracts/contracts/test/BondHelpersTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ contract BondHelpersTester {
return b.previewDeposit(collateralAmount);
}

function getTrancheCollateralBalances(IBondController b, address u)
public
view
returns (TrancheData memory, uint256[] memory)
{
return b.getTrancheCollateralBalances(u);
}

function getTrancheCollateralizations(IBondController b)
public
view
Expand All @@ -57,4 +49,12 @@ contract BondHelpersTester {
TrancheData memory td = b.getTrancheData();
return td.getTrancheIndex(t);
}

function computeRedeemableTrancheAmounts(IBondController b, address u)
public
view
returns (TrancheData memory td, uint256[] memory)
{
return b.computeRedeemableTrancheAmounts(u);
}
}
5 changes: 5 additions & 0 deletions spot-contracts/contracts/test/mocks/MockPerpetualTranche.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ contract MockPerpetualTranche is MockERC20 {
uint256 private _reserveTrancheBalance;
uint256 public matureTrancheBalance;
address public collateral;
address public feeToken;

function protocolFeeCollector() public view returns (address) {
return address(this);
Expand All @@ -29,4 +30,8 @@ contract MockPerpetualTranche is MockERC20 {
function setCollateral(address c) external {
collateral = c;
}

function setFeeToken(address c) external {
feeToken = c;
}
}
Loading

0 comments on commit 4a1c8de

Please sign in to comment.