Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions contracts/BondController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/BondController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

Expand Down Expand Up @@ -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");
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/UniV3LoanRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
});
Expand Down Expand Up @@ -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");
});
});
});
2 changes: 1 addition & 1 deletion test/WamplLoanRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/WethLoanRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down