Skip to content

Commit 8560eb7

Browse files
authored
Audit nice to have changes (#156)
* renamed bondtranches variable from td to bt * added getReserveTokenBalance helper to perp * ran linter
1 parent 0c79751 commit 8560eb7

8 files changed

Lines changed: 107 additions & 93 deletions

File tree

spot-contracts/contracts/PerpetualTranche.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,16 @@ contract PerpetualTranche is
678678
}
679679

680680
/// @inheritdoc IPerpetualTranche
681+
function getReserveTokenBalance(IERC20Upgradeable token) external override afterStateUpdate returns (uint256) {
682+
if (!_inReserve(token)) {
683+
return 0;
684+
}
685+
return _reserveBalance(token);
686+
}
687+
688+
/// @inheritdoc IPerpetualTranche
689+
/// @dev In the case of the collateral token, it returns the "virtual" tranche balance.
690+
// In all other cases, it just returns the token balance.
681691
function getReserveTrancheBalance(IERC20Upgradeable tranche) external override afterStateUpdate returns (uint256) {
682692
if (!_inReserve(tranche)) {
683693
return 0;

spot-contracts/contracts/RouterV1.sol

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ contract RouterV1 {
5151
{
5252
IBondController bond = perp.getDepositBond();
5353

54-
BondTranches memory td;
54+
BondTranches memory bt;
5555
uint256[] memory trancheAmts;
56-
(td, trancheAmts, ) = bond.previewDeposit(collateralAmount);
56+
(bt, trancheAmts, ) = bond.previewDeposit(collateralAmount);
5757

58-
return (bond, td.tranches, trancheAmts);
58+
return (bond, bt.tranches, trancheAmts);
5959
}
6060

6161
/// @notice Calculates the amount of perp tokens minted and fees for the operation.
@@ -100,7 +100,7 @@ contract RouterV1 {
100100
uint256 collateralAmount,
101101
uint256 feePaid
102102
) external afterPerpStateUpdate(perp) {
103-
BondTranches memory td = bond.getTranches();
103+
BondTranches memory bt = bond.getTranches();
104104
IERC20Upgradeable collateralToken = IERC20Upgradeable(bond.collateralToken());
105105
IERC20Upgradeable feeToken = perp.feeToken();
106106

@@ -119,18 +119,18 @@ contract RouterV1 {
119119
// approves fee to be spent to mint perp tokens
120120
_checkAndApproveMax(feeToken, address(perp), feePaid);
121121

122-
for (uint8 i = 0; i < td.tranches.length; i++) {
123-
uint256 trancheAmt = td.tranches[i].balanceOf(address(this));
124-
uint256 mintAmt = perp.computeMintAmt(td.tranches[i], trancheAmt);
122+
for (uint8 i = 0; i < bt.tranches.length; i++) {
123+
uint256 trancheAmt = bt.tranches[i].balanceOf(address(this));
124+
uint256 mintAmt = perp.computeMintAmt(bt.tranches[i], trancheAmt);
125125
if (mintAmt > 0) {
126126
// approves tranches to be spent
127-
_checkAndApproveMax(td.tranches[i], address(perp), trancheAmt);
127+
_checkAndApproveMax(bt.tranches[i], address(perp), trancheAmt);
128128

129129
// mints perp tokens using tranches
130-
perp.deposit(td.tranches[i], trancheAmt);
130+
perp.deposit(bt.tranches[i], trancheAmt);
131131
} else {
132132
// transfers unused tranches back
133-
td.tranches[i].safeTransfer(msg.sender, trancheAmt);
133+
bt.tranches[i].safeTransfer(msg.sender, trancheAmt);
134134
}
135135
}
136136

@@ -238,7 +238,7 @@ contract RouterV1 {
238238
RolloverBatch[] calldata rollovers,
239239
uint256 feePaid
240240
) external afterPerpStateUpdate(perp) {
241-
BondTranches memory td = bond.getTranches();
241+
BondTranches memory bt = bond.getTranches();
242242
IERC20Upgradeable collateralToken = IERC20Upgradeable(bond.collateralToken());
243243
IERC20Upgradeable feeToken = perp.feeToken();
244244

@@ -276,10 +276,10 @@ contract RouterV1 {
276276
}
277277

278278
// transfers unused tranches back
279-
for (uint8 i = 0; i < td.tranches.length; i++) {
280-
uint256 trancheBalance = td.tranches[i].balanceOf(address(this));
279+
for (uint8 i = 0; i < bt.tranches.length; i++) {
280+
uint256 trancheBalance = bt.tranches[i].balanceOf(address(this));
281281
if (trancheBalance > 0) {
282-
td.tranches[i].safeTransfer(msg.sender, trancheBalance);
282+
bt.tranches[i].safeTransfer(msg.sender, trancheBalance);
283283
}
284284
}
285285

spot-contracts/contracts/_interfaces/IPerpetualTranche.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ interface IPerpetualTranche is IERC20Upgradeable {
110110
/// @return If the token is part of the reserve.
111111
function inReserve(IERC20Upgradeable token) external returns (bool);
112112

113+
/// @notice Fetches the reserve's token balance.
114+
/// @param token The address of the tranche token held by the reserve.
115+
/// @return The ERC-20 balance of the reserve token.
116+
function getReserveTokenBalance(IERC20Upgradeable token) external returns (uint256);
117+
113118
/// @notice Fetches the reserve's tranche token balance.
114119
/// @param tranche The address of the tranche token held by the reserve.
115120
/// @return The ERC-20 balance of the reserve tranche token.

spot-contracts/contracts/_utils/BondHelpers.sol

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ struct BondTranches {
2525
*/
2626
library BondTranchesHelpers {
2727
/// @notice Iterates through the tranche data to find the seniority index of the given tranche.
28-
/// @param td The tranche data object.
28+
/// @param bt The tranche data object.
2929
/// @param t The address of the tranche to check.
3030
/// @return the index of the tranche in the tranches array.
31-
function indexOf(BondTranches memory td, ITranche t) internal pure returns (uint8) {
32-
for (uint8 i = 0; i < td.tranches.length; i++) {
33-
if (td.tranches[i] == t) {
31+
function indexOf(BondTranches memory bt, ITranche t) internal pure returns (uint8) {
32+
for (uint8 i = 0; i < bt.tranches.length; i++) {
33+
if (bt.tranches[i] == t) {
3434
return i;
3535
}
3636
}
@@ -50,11 +50,11 @@ library TrancheHelpers {
5050
/// @return The collateral balance and the tranche token supply.
5151
function getTrancheCollateralization(ITranche t) internal view returns (uint256, uint256) {
5252
IBondController bond = IBondController(t.bond());
53-
BondTranches memory td;
53+
BondTranches memory bt;
5454
uint256[] memory collateralBalances;
5555
uint256[] memory trancheSupplies;
56-
(td, collateralBalances, trancheSupplies) = BondHelpers.getTrancheCollateralizations(bond);
57-
uint256 trancheIndex = BondTranchesHelpers.indexOf(td, t);
56+
(bt, collateralBalances, trancheSupplies) = BondHelpers.getTrancheCollateralizations(bond);
57+
uint256 trancheIndex = BondTranchesHelpers.indexOf(bt, t);
5858
return (collateralBalances[trancheIndex], trancheSupplies[trancheIndex]);
5959
}
6060
}
@@ -86,17 +86,17 @@ library BondHelpers {
8686
/// @param b The address of the bond contract.
8787
/// @return The tranche data.
8888
function getTranches(IBondController b) internal view returns (BondTranches memory) {
89-
BondTranches memory td;
89+
BondTranches memory bt;
9090
uint8 trancheCount = b.trancheCount().toUint8();
91-
td.tranches = new ITranche[](trancheCount);
92-
td.trancheRatios = new uint256[](trancheCount);
91+
bt.tranches = new ITranche[](trancheCount);
92+
bt.trancheRatios = new uint256[](trancheCount);
9393
// Max tranches per bond < 2**8 - 1
9494
for (uint8 i = 0; i < trancheCount; i++) {
9595
(ITranche t, uint256 ratio) = b.tranches(i);
96-
td.tranches[i] = t;
97-
td.trancheRatios[i] = ratio;
96+
bt.tranches[i] = t;
97+
bt.trancheRatios[i] = ratio;
9898
}
99-
return td;
99+
return bt;
100100
}
101101

102102
/// @notice Given a bond, returns the tranche at the specified index.
@@ -122,29 +122,29 @@ library BondHelpers {
122122
uint256[] memory
123123
)
124124
{
125-
BondTranches memory td = getTranches(b);
126-
uint256[] memory trancheAmts = new uint256[](td.tranches.length);
127-
uint256[] memory fees = new uint256[](td.tranches.length);
125+
BondTranches memory bt = getTranches(b);
126+
uint256[] memory trancheAmts = new uint256[](bt.tranches.length);
127+
uint256[] memory fees = new uint256[](bt.tranches.length);
128128

129129
uint256 totalDebt = b.totalDebt();
130130
uint256 collateralBalance = IERC20Upgradeable(b.collateralToken()).balanceOf(address(b));
131131
uint256 feeBps = b.feeBps();
132132

133-
for (uint8 i = 0; i < td.tranches.length; i++) {
134-
trancheAmts[i] = collateralAmount.mulDiv(td.trancheRatios[i], TRANCHE_RATIO_GRANULARITY);
133+
for (uint8 i = 0; i < bt.tranches.length; i++) {
134+
trancheAmts[i] = collateralAmount.mulDiv(bt.trancheRatios[i], TRANCHE_RATIO_GRANULARITY);
135135
if (collateralBalance > 0) {
136136
trancheAmts[i] = trancheAmts[i].mulDiv(totalDebt, collateralBalance);
137137
}
138138
}
139139

140140
if (feeBps > 0) {
141-
for (uint8 i = 0; i < td.tranches.length; i++) {
141+
for (uint8 i = 0; i < bt.tranches.length; i++) {
142142
fees[i] = trancheAmts[i].mulDiv(feeBps, BPS);
143143
trancheAmts[i] -= fees[i];
144144
}
145145
}
146146

147-
return (td, trancheAmts, fees);
147+
return (bt, trancheAmts, fees);
148148
}
149149

150150
/// @notice Given a bond, for each tranche token retrieves the total collateral redeemable
@@ -162,24 +162,24 @@ library BondHelpers {
162162
uint256[] memory
163163
)
164164
{
165-
BondTranches memory td = getTranches(b);
166-
uint256[] memory collateralBalances = new uint256[](td.tranches.length);
167-
uint256[] memory trancheSupplies = new uint256[](td.tranches.length);
165+
BondTranches memory bt = getTranches(b);
166+
uint256[] memory collateralBalances = new uint256[](bt.tranches.length);
167+
uint256[] memory trancheSupplies = new uint256[](bt.tranches.length);
168168

169169
// When the bond is mature, the collateral is transferred over to the individual tranche token contracts
170170
if (b.isMature()) {
171-
for (uint8 i = 0; i < td.tranches.length; i++) {
172-
trancheSupplies[i] = td.tranches[i].totalSupply();
173-
collateralBalances[i] = IERC20Upgradeable(b.collateralToken()).balanceOf(address(td.tranches[i]));
171+
for (uint8 i = 0; i < bt.tranches.length; i++) {
172+
trancheSupplies[i] = bt.tranches[i].totalSupply();
173+
collateralBalances[i] = IERC20Upgradeable(b.collateralToken()).balanceOf(address(bt.tranches[i]));
174174
}
175-
return (td, collateralBalances, trancheSupplies);
175+
return (bt, collateralBalances, trancheSupplies);
176176
}
177177

178178
// Before the bond is mature, all the collateral is held by the bond contract
179179
uint256 bondCollateralBalance = IERC20Upgradeable(b.collateralToken()).balanceOf(address(b));
180-
uint256 zTrancheIndex = td.tranches.length - 1;
181-
for (uint8 i = 0; i < td.tranches.length; i++) {
182-
trancheSupplies[i] = td.tranches[i].totalSupply();
180+
uint256 zTrancheIndex = bt.tranches.length - 1;
181+
for (uint8 i = 0; i < bt.tranches.length; i++) {
182+
trancheSupplies[i] = bt.tranches[i].totalSupply();
183183

184184
// a to y tranches
185185
if (i != zTrancheIndex) {
@@ -194,7 +194,7 @@ library BondHelpers {
194194
}
195195
}
196196

197-
return (td, collateralBalances, trancheSupplies);
197+
return (bt, collateralBalances, trancheSupplies);
198198
}
199199

200200
/// @notice For a given bond and user address, computes the maximum number of each of the bond's tranches
@@ -207,29 +207,29 @@ library BondHelpers {
207207
view
208208
returns (BondTranches memory, uint256[] memory)
209209
{
210-
BondTranches memory td = getTranches(b);
211-
uint256[] memory redeemableAmts = new uint256[](td.tranches.length);
210+
BondTranches memory bt = getTranches(b);
211+
uint256[] memory redeemableAmts = new uint256[](bt.tranches.length);
212212

213213
// Calculate how many underlying assets could be redeemed from each tranche balance,
214214
// assuming other tranches are not an issue, and record the smallest amount.
215215
uint256 minUnderlyingOut = type(uint256).max;
216216
uint8 i;
217-
for (i = 0; i < td.tranches.length; i++) {
218-
uint256 d = td.tranches[i].balanceOf(u).mulDiv(TRANCHE_RATIO_GRANULARITY, td.trancheRatios[i]);
217+
for (i = 0; i < bt.tranches.length; i++) {
218+
uint256 d = bt.tranches[i].balanceOf(u).mulDiv(TRANCHE_RATIO_GRANULARITY, bt.trancheRatios[i]);
219219
if (d < minUnderlyingOut) {
220220
minUnderlyingOut = d;
221221
}
222222

223223
// if one of the balances is zero, we return
224224
if (minUnderlyingOut == 0) {
225-
return (td, redeemableAmts);
225+
return (bt, redeemableAmts);
226226
}
227227
}
228228

229-
for (i = 0; i < td.tranches.length; i++) {
230-
redeemableAmts[i] = td.trancheRatios[i].mulDiv(minUnderlyingOut, TRANCHE_RATIO_GRANULARITY);
229+
for (i = 0; i < bt.tranches.length; i++) {
230+
redeemableAmts[i] = bt.trancheRatios[i].mulDiv(minUnderlyingOut, TRANCHE_RATIO_GRANULARITY);
231231
}
232232

233-
return (td, redeemableAmts);
233+
return (bt, redeemableAmts);
234234
}
235235
}

spot-contracts/contracts/strategies/TrancheClassDiscountStrategy.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ contract TrancheClassDiscountStrategy is IDiscountStrategy, OwnableUpgradeable {
7373
/// @return The class hash.
7474
function trancheClass(ITranche tranche) public view returns (bytes32) {
7575
IBondController bond = IBondController(tranche.bond());
76-
BondTranches memory td = bond.getTranches();
77-
return keccak256(abi.encode(bond.collateralToken(), td.trancheRatios, td.indexOf(tranche)));
76+
BondTranches memory bt = bond.getTranches();
77+
return keccak256(abi.encode(bond.collateralToken(), bt.trancheRatios, bt.indexOf(tranche)));
7878
}
7979
}

spot-contracts/contracts/test/BondHelpersTester.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ contract BondHelpersTester {
1313
return b.secondsToMaturity();
1414
}
1515

16-
function getTranches(IBondController b) public view returns (BondTranches memory td) {
16+
function getTranches(IBondController b) public view returns (BondTranches memory bt) {
1717
return b.getTranches();
1818
}
1919

@@ -33,7 +33,7 @@ contract BondHelpersTester {
3333
public
3434
view
3535
returns (
36-
BondTranches memory td,
36+
BondTranches memory bt,
3737
uint256[] memory,
3838
uint256[] memory
3939
)
@@ -42,14 +42,14 @@ contract BondHelpersTester {
4242
}
4343

4444
function indexOf(IBondController b, ITranche t) public view returns (uint8) {
45-
BondTranches memory td = b.getTranches();
46-
return td.indexOf(t);
45+
BondTranches memory bt = b.getTranches();
46+
return bt.indexOf(t);
4747
}
4848

4949
function computeRedeemableTrancheAmounts(IBondController b, address u)
5050
public
5151
view
52-
returns (BondTranches memory td, uint256[] memory)
52+
returns (BondTranches memory bt, uint256[] memory)
5353
{
5454
return b.computeRedeemableTrancheAmounts(u);
5555
}

0 commit comments

Comments
 (0)