diff --git a/contracts/BondController.sol b/contracts/BondController.sol index 99dcd8c..d52d648 100644 --- a/contracts/BondController.sol +++ b/contracts/BondController.sol @@ -131,7 +131,8 @@ contract BondController is IBondController, OwnableUpgradeable { TrancheData[] memory _tranches = tranches; uint256 newDebt; - uint256[] memory trancheValues = new uint256[](trancheCount); + // saving feeBps in memory to minimize sloads + uint256 _feeBps = feeBps; for (uint256 i = 0; i < _tranches.length; i++) { // NOTE: solidity 0.8 checks for over/underflow natively so no need for SafeMath uint256 trancheValue = (amount * _tranches[i].ratio) / TRANCHE_RATIO_GRANULARITY; @@ -143,15 +144,7 @@ contract BondController is IBondController, OwnableUpgradeable { trancheValue = Math.mulDiv(trancheValue, totalDebt, _collateralBalance); } newDebt += trancheValue; - trancheValues[i] = trancheValue; - } - totalDebt += newDebt; - - TransferHelper.safeTransferFrom(collateralToken, _msgSender(), address(this), amount); - // saving feeBps in memory to minimize sloads - uint256 _feeBps = feeBps; - for (uint256 i = 0; i < trancheValues.length; i++) { - uint256 trancheValue = trancheValues[i]; + // fee tranche tokens are minted and held by the contract // upon maturity, they are redeemed and underlying collateral are sent to the owner uint256 fee = (trancheValue * _feeBps) / BPS; @@ -160,7 +153,12 @@ contract BondController is IBondController, OwnableUpgradeable { } _tranches[i].token.mint(_msgSender(), trancheValue - fee); + } + totalDebt += newDebt; + + TransferHelper.safeTransferFrom(collateralToken, _msgSender(), address(this), amount); + emit Deposit(_msgSender(), amount, _feeBps); _enforceTotalDebt(); diff --git a/test/BondController.ts b/test/BondController.ts index 2f805d7..46ddf17 100644 --- a/test/BondController.ts +++ b/test/BondController.ts @@ -649,7 +649,7 @@ describe("Bond Controller", () => { const tx = await bond.connect(user).deposit(amount); const receipt = await tx.wait(); const gasUsed = receipt.gasUsed; - expect(gasUsed.toString()).to.equal("296633"); + expect(gasUsed.toString()).to.equal("293579"); }); }); @@ -1464,7 +1464,7 @@ describe("Bond Controller", () => { const receipt = await tx.wait(); const gasUsed = receipt.gasUsed; - expect(gasUsed.toString()).to.equal("157707"); + expect(gasUsed.toString()).to.equal("157695"); }); }); diff --git a/test/UniV3LoanRouter.ts b/test/UniV3LoanRouter.ts index 8887d25..6b30e0f 100644 --- a/test/UniV3LoanRouter.ts +++ b/test/UniV3LoanRouter.ts @@ -395,7 +395,7 @@ describe("Uniswap V3 Loan Router", () => { const receipt = await tx.wait(); const gasUsed = receipt.gasUsed; - expect(gasUsed.toString()).to.equal("499271"); + expect(gasUsed.toString()).to.equal("498428"); }); }); }); @@ -882,7 +882,7 @@ describe("Uniswap V3 Loan Router with wrapper", () => { const receipt = await tx.wait(); const gasUsed = receipt.gasUsed; - expect(gasUsed.toString()).to.equal("584912"); + expect(gasUsed.toString()).to.equal("584068"); }); }); }); diff --git a/test/WamplLoanRouter.ts b/test/WamplLoanRouter.ts index accf085..c6854d3 100644 --- a/test/WamplLoanRouter.ts +++ b/test/WamplLoanRouter.ts @@ -673,7 +673,7 @@ describe("WAMPL Loan Router", () => { const receipt = await tx.wait(); const gasUsed = receipt.gasUsed; - expect(gasUsed.toString()).to.equal("771292"); + expect(gasUsed.toString()).to.equal("770448"); const costOfGas = gasUsed.mul(receipt.effectiveGasPrice); expect(await user.getBalance()).to.eq( startingBalance.sub(costOfGas), diff --git a/test/WethLoanRouter.ts b/test/WethLoanRouter.ts index b5e5572..aaf73d3 100644 --- a/test/WethLoanRouter.ts +++ b/test/WethLoanRouter.ts @@ -578,7 +578,7 @@ describe("WETH Loan Router", () => { const receipt = await tx.wait(); const gasUsed = receipt.gasUsed; - expect(gasUsed.toString()).to.equal("699963"); + expect(gasUsed.toString()).to.equal("699120"); const costOfGas = gasUsed.mul(receipt.effectiveGasPrice); expect(await user.getBalance()).to.eq( startingBalance.sub(amount).sub(costOfGas),