From a803820e53e0db3768723ba19f368f72a8cf1086 Mon Sep 17 00:00:00 2001 From: craig-iam-smith Date: Sun, 19 Mar 2023 16:57:22 -0600 Subject: [PATCH 1/4] perf(bond-controller): consolidate loops, remove array alloc, gas optimization --- contracts/BondController.sol | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) 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(); From 5f7db1527b87e85b143ba16ec9a8c2e3d6736cd6 Mon Sep 17 00:00:00 2001 From: craig-iam-smith Date: Sun, 19 Mar 2023 16:58:44 -0600 Subject: [PATCH 2/4] perf(bond-controller): update tests to match gas optimization --- test/BondController.ts | 4 ++-- test/UniV3LoanRouter.ts | 4 ++-- test/WamplLoanRouter.ts | 2 +- test/WethLoanRouter.ts | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/BondController.ts b/test/BondController.ts index 2f805d7..d101004 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("293967"); }); }); @@ -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..9ebe712 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("498738"); }); }); }); @@ -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("584379"); }); }); }); diff --git a/test/WamplLoanRouter.ts b/test/WamplLoanRouter.ts index accf085..a333f7d 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("770759"); 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..b436f19 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("699430"); const costOfGas = gasUsed.mul(receipt.effectiveGasPrice); expect(await user.getBalance()).to.eq( startingBalance.sub(amount).sub(costOfGas), From 7de03a60dd5c824c2ac2cd219895e3660d7200ed Mon Sep 17 00:00:00 2001 From: craig-iam-smith Date: Mon, 20 Mar 2023 07:12:58 -0600 Subject: [PATCH 3/4] perf(bond-controller): update tests to match gas optimization --- test/BondController.ts | 2 +- test/UniV3LoanRouter.ts | 4 ++-- test/WamplLoanRouter.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/BondController.ts b/test/BondController.ts index d101004..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("293967"); + expect(gasUsed.toString()).to.equal("293579"); }); }); diff --git a/test/UniV3LoanRouter.ts b/test/UniV3LoanRouter.ts index 9ebe712..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("498738"); + 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("584379"); + expect(gasUsed.toString()).to.equal("584068"); }); }); }); diff --git a/test/WamplLoanRouter.ts b/test/WamplLoanRouter.ts index a333f7d..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("770759"); + expect(gasUsed.toString()).to.equal("770448"); const costOfGas = gasUsed.mul(receipt.effectiveGasPrice); expect(await user.getBalance()).to.eq( startingBalance.sub(costOfGas), From 60b5392441ed2f32a7eb930b14890cd7daea4199 Mon Sep 17 00:00:00 2001 From: craig-iam-smith Date: Mon, 20 Mar 2023 07:13:26 -0600 Subject: [PATCH 4/4] perf(bond-controller): update tests to match gas optimization --- test/WethLoanRouter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/WethLoanRouter.ts b/test/WethLoanRouter.ts index b436f19..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("699430"); + expect(gasUsed.toString()).to.equal("699120"); const costOfGas = gasUsed.mul(receipt.effectiveGasPrice); expect(await user.getBalance()).to.eq( startingBalance.sub(amount).sub(costOfGas),