diff --git a/contracts/.gas-snapshot b/contracts/.gas-snapshot index 6adc7fce..ed702013 100644 --- a/contracts/.gas-snapshot +++ b/contracts/.gas-snapshot @@ -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) @@ -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) diff --git a/contracts/src/ExaPlugin.sol b/contracts/src/ExaPlugin.sol index ac5f76a6..36e6f235 100644 --- a/contracts/src/ExaPlugin.sol +++ b/contracts/src/ExaPlugin.sol @@ -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); @@ -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); } @@ -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;