Skip to content

Commit

Permalink
♻️ contracts: extract approved unspent depositing pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jan 7, 2025
1 parent 0f43037 commit 1902cef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 8 additions & 8 deletions contracts/.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ExaAccountFactoryTest:testFuzz_createAccount_EOAOwners(uint256,address[63]) (runs: 256, μ: 3533123, ~: 3419871)
ExaAccountFactoryTest:test_deploy_deploysToSameAddress() (gas: 25825799)
ExaPluginTest:testFork_crossRepay_repays() (gas: 15027926)
ExaPluginTest:testFork_debitCollateral_collects() (gas: 15117109)
ExaPluginTest:testFork_swap_swaps() (gas: 12295400)
ExaAccountFactoryTest:testFuzz_createAccount_EOAOwners(uint256,address[63]) (runs: 256, μ: 3675457, ~: 3551585)
ExaAccountFactoryTest:test_deploy_deploysToSameAddress() (gas: 25749329)
ExaPluginTest:testFork_crossRepay_repays() (gas: 14988816)
ExaPluginTest:testFork_debitCollateral_collects() (gas: 15078038)
ExaPluginTest:testFork_swap_swaps() (gas: 12256285)
ExaPluginTest:test_borrowAtMaturity_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408782)
ExaPluginTest:test_borrow_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408338)
ExaPluginTest:test_collectCredit_collects() (gas: 918651)
Expand Down Expand Up @@ -34,11 +34,11 @@ ExaPluginTest:test_collectInstallments_reverts_whenExpired() (gas: 360670)
ExaPluginTest:test_collectInstallments_reverts_whenReplay() (gas: 1068220)
ExaPluginTest:test_collectInstallments_reverts_whenTimelocked() (gas: 356959)
ExaPluginTest:test_collectInstallments_toleratesTimeDrift() (gas: 1192944)
ExaPluginTest:test_crossRepay_repays() (gas: 1463685)
ExaPluginTest:test_crossRepay_repays_whenKeeper() (gas: 1460464)
ExaPluginTest:test_crossRepay_repays() (gas: 1463691)
ExaPluginTest:test_crossRepay_repays_whenKeeper() (gas: 1460470)
ExaPluginTest:test_crossRepay_reverts_whenDisagreement() (gas: 1572899)
ExaPluginTest:test_crossRepay_reverts_whenNotKeeper() (gas: 958548)
ExaPluginTest:test_debitCollateral_collects() (gas: 776452)
ExaPluginTest:test_debitCollateral_collects() (gas: 776479)
ExaPluginTest:test_exitMarket_reverts() (gas: 388956)
ExaPluginTest:test_marketWithdraw_transfersAsset_asOwner() (gas: 818702)
ExaPluginTest:test_onUninstall_uninstalls() (gas: 284160)
Expand Down
11 changes: 6 additions & 5 deletions contracts/src/ExaPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
IERC20(EXA_USDC.asset()).safeTransfer(collector, amount);

_depositUnspent(collateral, maxAmountIn - amountIn, msg.sender);
uint256 usdcLeft = amountOut - amount;
if (usdcLeft != 0) EXA_USDC.deposit(usdcLeft, msg.sender);
_depositApprovedUnspent(EXA_USDC, amountOut - amount, msg.sender);

_checkLiquidity(msg.sender);
}
Expand Down Expand Up @@ -317,9 +316,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
_swap(IERC20(c.marketIn.asset()), IERC20(EXA_USDC.asset()), c.maxAmountIn, c.maxRepay, c.route);
IERC20(EXA_USDC.asset()).safeTransfer(address(BALANCER_VAULT), c.maxRepay);

uint256 usdcLeft = amountOut - actualRepay;
if (usdcLeft != 0) EXA_USDC.deposit(usdcLeft, c.borrower);

_depositApprovedUnspent(EXA_USDC, amountOut - actualRepay, c.borrower);
_depositUnspent(c.marketIn, c.maxAmountIn - amountIn, c.borrower);
_checkLiquidity(c.borrower);
}
Expand Down Expand Up @@ -635,6 +632,10 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
BALANCER_VAULT.flashLoan(address(this), tokens, amounts, data);
}

function _depositApprovedUnspent(IMarket market, uint256 unspent, address receiver) internal {
if (unspent != 0) market.deposit(unspent, receiver);
}

function _depositUnspent(IMarket market, uint256 unspent, address receiver) internal {
if (unspent != 0) {
IERC20(market.asset()).approve(address(market), unspent);
Expand Down

0 comments on commit 1902cef

Please sign in to comment.