Skip to content

Commit

Permalink
♻️ contracts: extract unspent depositing pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jan 7, 2025
1 parent 3b819cb commit 0f43037
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 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, μ: 3571361, ~: 3437932)
ExaAccountFactoryTest:test_deploy_deploysToSameAddress() (gas: 25964325)
ExaPluginTest:testFork_crossRepay_repays() (gas: 15098896)
ExaPluginTest:testFork_debitCollateral_collects() (gas: 15188098)
ExaPluginTest:testFork_swap_swaps() (gas: 12366415)
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)
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: 1463680)
ExaPluginTest:test_crossRepay_repays_whenKeeper() (gas: 1460459)
ExaPluginTest:test_crossRepay_repays() (gas: 1463685)
ExaPluginTest:test_crossRepay_repays_whenKeeper() (gas: 1460464)
ExaPluginTest:test_crossRepay_reverts_whenDisagreement() (gas: 1572899)
ExaPluginTest:test_crossRepay_reverts_whenNotKeeper() (gas: 958548)
ExaPluginTest:test_debitCollateral_collects() (gas: 776420)
ExaPluginTest:test_debitCollateral_collects() (gas: 776452)
ExaPluginTest:test_exitMarket_reverts() (gas: 388956)
ExaPluginTest:test_marketWithdraw_transfersAsset_asOwner() (gas: 818702)
ExaPluginTest:test_onUninstall_uninstalls() (gas: 284160)
Expand Down
19 changes: 9 additions & 10 deletions contracts/src/ExaPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
(amountIn, amountOut) = _swap(IERC20(collateral.asset()), IERC20(EXA_USDC.asset()), maxAmountIn, amount, route);
IERC20(EXA_USDC.asset()).safeTransfer(collector, amount);

uint256 unused = maxAmountIn - amountIn;
if (unused != 0) {
IERC20(collateral.asset()).approve(address(collateral), unused);
collateral.deposit(unused, msg.sender);
}
_depositUnspent(collateral, maxAmountIn - amountIn, msg.sender);
uint256 usdcLeft = amountOut - amount;
if (usdcLeft != 0) EXA_USDC.deposit(usdcLeft, msg.sender);

Expand Down Expand Up @@ -324,11 +320,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
uint256 usdcLeft = amountOut - actualRepay;
if (usdcLeft != 0) EXA_USDC.deposit(usdcLeft, c.borrower);

uint256 unused = c.maxAmountIn - amountIn;
if (unused != 0) {
IERC20(c.marketIn.asset()).approve(address(c.marketIn), unused);
c.marketIn.deposit(unused, c.borrower);
}
_depositUnspent(c.marketIn, c.maxAmountIn - amountIn, c.borrower);
_checkLiquidity(c.borrower);
}

Expand Down Expand Up @@ -643,6 +635,13 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
BALANCER_VAULT.flashLoan(address(this), tokens, amounts, data);
}

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

function _hash(bytes memory data) internal returns (bytes memory) {
callHash = keccak256(data);
return data;
Expand Down

0 comments on commit 0f43037

Please sign in to comment.