Skip to content

Commit

Permalink
♻️ contracts: extract common ERC20 approve
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jan 7, 2025
1 parent 70bdd67 commit 9f33dbf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 70 deletions.
122 changes: 61 additions & 61 deletions contracts/.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
ExaAccountFactoryTest:testFuzz_createAccount_EOAOwners(uint256,address[63]) (runs: 256, μ: 3581018, ~: 3514588)
ExaAccountFactoryTest:test_deploy_deploysToSameAddress() (gas: 26050670)
ExaPluginTest:testFork_crossRepay_repays() (gas: 15139849)
ExaPluginTest:testFork_debitCollateral_collects() (gas: 15229013)
ExaPluginTest:testFork_swap_swaps() (gas: 12407190)
ExaPluginTest:test_borrowAtMaturity_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408738)
ExaPluginTest:test_borrow_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408294)
ExaPluginTest:test_collectCredit_collects() (gas: 918607)
ExaPluginTest:test_collectCredit_collects_whenHealthFactorHigherThanOne() (gas: 799236)
ExaPluginTest:test_collectCredit_collects_withEnoughSlippage() (gas: 797861)
ExaPluginTest:test_collectCredit_collects_withPrevIssuerSignature() (gas: 994445)
ExaPluginTest:test_collectCredit_passes_whenProposalLeavesEnoughLiquidity() (gas: 1007643)
ExaPluginTest:test_collectCredit_reverts_asNotKeeper() (gas: 362541)
ExaPluginTest:test_collectCredit_reverts_whenDisagreement() (gas: 555926)
ExaPluginTest:test_collectCredit_reverts_whenExpired() (gas: 358643)
ExaPluginTest:test_collectCredit_reverts_whenHealthFactorLowerThanOne() (gas: 997891)
ExaPluginTest:test_collectCredit_reverts_whenPrevSignatureNotValidAnymore() (gas: 461408)
ExaPluginTest:test_collectCredit_reverts_whenProposalCausesInsufficientLiquidity() (gas: 1007797)
ExaPluginTest:test_collectCredit_reverts_whenReplay() (gas: 840870)
ExaPluginTest:test_collectCredit_reverts_whenTimelocked() (gas: 354811)
ExaPluginTest:test_collectCredit_toleratesTimeDrift() (gas: 801250)
ExaPluginTest:test_collectDebit_collects() (gas: 651450)
ExaPluginTest:test_collectDebit_collects_whenProposalLeavesEnoughLiquidity() (gas: 854849)
ExaPluginTest:test_collectDebit_reverts_asNotKeeper() (gas: 362352)
ExaPluginTest:test_collectDebit_reverts_whenExpired() (gas: 358338)
ExaPluginTest:test_collectDebit_reverts_whenProposalCausesInsufficientLiquidity() (gas: 851737)
ExaPluginTest:test_collectDebit_reverts_whenReplay() (gas: 690868)
ExaPluginTest:test_collectDebit_reverts_whenTimelocked() (gas: 354619)
ExaPluginTest:test_collectDebit_toleratesTimeDrift() (gas: 651546)
ExaPluginTest:test_collectInstallments_collects() (gas: 1336223)
ExaPluginTest:test_collectInstallments_revertsWhenNoSlippage() (gas: 1202659)
ExaPluginTest:test_collectInstallments_reverts_asNotKeeper() (gas: 363393)
ExaPluginTest:test_collectInstallments_reverts_whenExpired() (gas: 360626)
ExaPluginTest:test_collectInstallments_reverts_whenReplay() (gas: 1068117)
ExaPluginTest:test_collectInstallments_reverts_whenTimelocked() (gas: 356915)
ExaPluginTest:test_collectInstallments_toleratesTimeDrift() (gas: 1192841)
ExaPluginTest:test_crossRepay_repays() (gas: 1463850)
ExaPluginTest:test_crossRepay_repays_whenKeeper() (gas: 1460629)
ExaPluginTest:test_crossRepay_reverts_whenDisagreement() (gas: 1572900)
ExaPluginTest:test_crossRepay_reverts_whenNotKeeper() (gas: 958504)
ExaPluginTest:test_debitCollateral_collects() (gas: 776593)
ExaPluginTest:test_exitMarket_reverts() (gas: 388912)
ExaPluginTest:test_marketWithdraw_transfersAsset_asOwner() (gas: 818658)
ExaAccountFactoryTest:testFuzz_createAccount_EOAOwners(uint256,address[63]) (runs: 256, μ: 3615461, ~: 3497138)
ExaAccountFactoryTest:test_deploy_deploysToSameAddress() (gas: 26025726)
ExaPluginTest:testFork_crossRepay_repays() (gas: 15126926)
ExaPluginTest:testFork_debitCollateral_collects() (gas: 15216090)
ExaPluginTest:testFork_swap_swaps() (gas: 12394445)
ExaPluginTest:test_borrowAtMaturity_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408744)
ExaPluginTest:test_borrow_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408300)
ExaPluginTest:test_collectCredit_collects() (gas: 918613)
ExaPluginTest:test_collectCredit_collects_whenHealthFactorHigherThanOne() (gas: 799242)
ExaPluginTest:test_collectCredit_collects_withEnoughSlippage() (gas: 797867)
ExaPluginTest:test_collectCredit_collects_withPrevIssuerSignature() (gas: 994451)
ExaPluginTest:test_collectCredit_passes_whenProposalLeavesEnoughLiquidity() (gas: 1007649)
ExaPluginTest:test_collectCredit_reverts_asNotKeeper() (gas: 362547)
ExaPluginTest:test_collectCredit_reverts_whenDisagreement() (gas: 555932)
ExaPluginTest:test_collectCredit_reverts_whenExpired() (gas: 358649)
ExaPluginTest:test_collectCredit_reverts_whenHealthFactorLowerThanOne() (gas: 997897)
ExaPluginTest:test_collectCredit_reverts_whenPrevSignatureNotValidAnymore() (gas: 461414)
ExaPluginTest:test_collectCredit_reverts_whenProposalCausesInsufficientLiquidity() (gas: 1007803)
ExaPluginTest:test_collectCredit_reverts_whenReplay() (gas: 840876)
ExaPluginTest:test_collectCredit_reverts_whenTimelocked() (gas: 354817)
ExaPluginTest:test_collectCredit_toleratesTimeDrift() (gas: 801256)
ExaPluginTest:test_collectDebit_collects() (gas: 651456)
ExaPluginTest:test_collectDebit_collects_whenProposalLeavesEnoughLiquidity() (gas: 854855)
ExaPluginTest:test_collectDebit_reverts_asNotKeeper() (gas: 362358)
ExaPluginTest:test_collectDebit_reverts_whenExpired() (gas: 358344)
ExaPluginTest:test_collectDebit_reverts_whenProposalCausesInsufficientLiquidity() (gas: 851743)
ExaPluginTest:test_collectDebit_reverts_whenReplay() (gas: 690874)
ExaPluginTest:test_collectDebit_reverts_whenTimelocked() (gas: 354625)
ExaPluginTest:test_collectDebit_toleratesTimeDrift() (gas: 651552)
ExaPluginTest:test_collectInstallments_collects() (gas: 1336235)
ExaPluginTest:test_collectInstallments_revertsWhenNoSlippage() (gas: 1202671)
ExaPluginTest:test_collectInstallments_reverts_asNotKeeper() (gas: 363399)
ExaPluginTest:test_collectInstallments_reverts_whenExpired() (gas: 360632)
ExaPluginTest:test_collectInstallments_reverts_whenReplay() (gas: 1068129)
ExaPluginTest:test_collectInstallments_reverts_whenTimelocked() (gas: 356921)
ExaPluginTest:test_collectInstallments_toleratesTimeDrift() (gas: 1192853)
ExaPluginTest:test_crossRepay_repays() (gas: 1463642)
ExaPluginTest:test_crossRepay_repays_whenKeeper() (gas: 1460421)
ExaPluginTest:test_crossRepay_reverts_whenDisagreement() (gas: 1572861)
ExaPluginTest:test_crossRepay_reverts_whenNotKeeper() (gas: 958510)
ExaPluginTest:test_debitCollateral_collects() (gas: 776382)
ExaPluginTest:test_exitMarket_reverts() (gas: 388918)
ExaPluginTest:test_marketWithdraw_transfersAsset_asOwner() (gas: 818664)
ExaPluginTest:test_onUninstall_uninstalls() (gas: 284160)
ExaPluginTest:test_poke() (gas: 315757)
ExaPluginTest:test_pokeETH_deposits() (gas: 378936)
ExaPluginTest:test_poke() (gas: 315763)
ExaPluginTest:test_pokeETH_deposits() (gas: 378942)
ExaPluginTest:test_propose_emitsProposed() (gas: 218639)
ExaPluginTest:test_refund_refunds() (gas: 361573)
ExaPluginTest:test_repay_partiallyRepays() (gas: 1245227)
ExaPluginTest:test_repay_partiallyRepays_whenKeeper() (gas: 1187414)
ExaPluginTest:test_repay_repays() (gas: 1163575)
ExaPluginTest:test_repay_repays_whenKeeper() (gas: 1106256)
ExaPluginTest:test_rollDebt_rolls() (gas: 1231412)
ExaPluginTest:test_rollDebt_rolls_asKeeper() (gas: 1231060)
ExaPluginTest:test_repay_partiallyRepays() (gas: 1245239)
ExaPluginTest:test_repay_partiallyRepays_whenKeeper() (gas: 1187426)
ExaPluginTest:test_repay_repays() (gas: 1163587)
ExaPluginTest:test_repay_repays_whenKeeper() (gas: 1106268)
ExaPluginTest:test_rollDebt_rolls() (gas: 1231424)
ExaPluginTest:test_rollDebt_rolls_asKeeper() (gas: 1231072)
ExaPluginTest:test_setCollector_emitsCollectorSet() (gas: 40516)
ExaPluginTest:test_setCollector_reverts_whenAddressZero() (gas: 32311)
ExaPluginTest:test_setCollector_reverts_whenNotAdmin() (gas: 33802)
ExaPluginTest:test_setCollector_reverts_whenNotAdmin() (gas: 33772)
ExaPluginTest:test_setCollector_sets_whenAdmin() (gas: 39906)
ExaPluginTest:test_swap_reverts_withDisagreement() (gas: 288778)
ExaPluginTest:test_swap_swaps() (gas: 268445)
ExaPluginTest:test_withdrawWETH_transfersETH() (gas: 851626)
ExaPluginTest:test_withdraw_reverts_whenNoProposal() (gas: 414806)
ExaPluginTest:test_withdraw_reverts_whenNoProposalKeeper() (gas: 357799)
ExaPluginTest:test_withdraw_reverts_whenNotKeeper() (gas: 354881)
ExaPluginTest:test_withdraw_reverts_whenReceiverIsContractAndMarketNotWETH() (gas: 621081)
ExaPluginTest:test_swap_reverts_withDisagreement() (gas: 288784)
ExaPluginTest:test_swap_swaps() (gas: 268457)
ExaPluginTest:test_withdrawWETH_transfersETH() (gas: 851632)
ExaPluginTest:test_withdraw_reverts_whenNoProposal() (gas: 414812)
ExaPluginTest:test_withdraw_reverts_whenNoProposalKeeper() (gas: 357805)
ExaPluginTest:test_withdraw_reverts_whenNotKeeper() (gas: 354887)
ExaPluginTest:test_withdraw_reverts_whenReceiverIsContractAndMarketNotWETH() (gas: 621087)
ExaPluginTest:test_withdraw_reverts_whenTimelocked() (gas: 307341)
ExaPluginTest:test_withdraw_reverts_whenTimelockedKeeper() (gas: 309187)
ExaPluginTest:test_withdraw_reverts_whenWrongAmount() (gas: 308738)
ExaPluginTest:test_withdraw_reverts_whenWrongMarket() (gas: 308931)
ExaPluginTest:test_withdraw_reverts_whenWrongReceiver() (gas: 308342)
ExaPluginTest:test_withdraw_transfersAsset_asKeeper() (gas: 820049)
ExaPluginTest:test_withdraw_withdrawsProposed() (gas: 810795)
ExaPluginTest:test_withdraw_transfersAsset_asKeeper() (gas: 820055)
ExaPluginTest:test_withdraw_withdrawsProposed() (gas: 810801)
InstallmentsPreviewerTest:test_preview_returns() (gas: 135598)
IssuerCheckerTest:test_setIssuer_emits_IssuerSet() (gas: 70861)
IssuerCheckerTest:test_setIssuer_reverts_whenNotAdmin() (gas: 37272)
Expand Down
20 changes: 11 additions & 9 deletions contracts/src/ExaPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
uint256 balanceIn = assetIn.balanceOf(msg.sender);
uint256 balanceOut = assetOut.balanceOf(msg.sender);

_executeFromSender(address(assetIn), 0, abi.encodeCall(IERC20.approve, (SWAPPER, maxAmountIn)));
_approveFromSender(address(assetIn), SWAPPER, maxAmountIn);
_executeFromSender(SWAPPER, 0, route);

amountOut = assetOut.balanceOf(msg.sender) - balanceOut;
if (amountOut < minAmountOut) revert Disagreement();

_executeFromSender(address(assetIn), 0, abi.encodeCall(IERC20.approve, (SWAPPER, 0)));
_approveFromSender(address(assetIn), SWAPPER, 0);
amountIn = balanceIn - assetIn.balanceOf(msg.sender);
}

Expand Down Expand Up @@ -154,7 +154,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
)
)
);
_executeFromSender(address(collateral), 0, abi.encodeCall(IERC20.approve, (address(this), maxAmountIn)));
_approveFromSender(address(collateral), address(this), maxAmountIn);
_flashLoan(maxRepay, data);
}

Expand All @@ -172,9 +172,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
)
)
);
_executeFromSender(
address(EXA_USDC), 0, abi.encodeCall(IERC20.approve, (address(this), EXA_USDC.previewWithdraw(maxRepay)))
);
_approveFromSender(address(EXA_USDC), address(this), EXA_USDC.previewWithdraw(maxRepay));
_flashLoan(maxRepay, data);
}

Expand All @@ -185,7 +183,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
uint256 maxBorrowAssets,
uint256 percentage
) external {
_executeFromSender(address(EXA_USDC), 0, abi.encodeCall(IERC20.approve, (address(DEBT_MANAGER), maxRepayAssets)));
_approveFromSender(address(EXA_USDC), address(DEBT_MANAGER), maxRepayAssets);
_executeFromSender(
address(DEBT_MANAGER),
0,
Expand Down Expand Up @@ -278,7 +276,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
}
ISSUER_CHECKER.checkIssuer(msg.sender, totalAmount, timestamp, signature);

_executeFromSender(address(EXA_USDC), 0, abi.encodeCall(IERC20.approve, (address(INSTALLMENTS_ROUTER), maxRepay)));
_approveFromSender(address(EXA_USDC), address(INSTALLMENTS_ROUTER), maxRepay);
_executeFromSender(
address(INSTALLMENTS_ROUTER),
0,
Expand Down Expand Up @@ -617,6 +615,10 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
if (sumDebtPlusEffects > sumCollateral) revert InsufficientLiquidity();
}

function _approveFromSender(address assetIn, address spender, uint256 maxAmountIn) internal {
_executeFromSender(assetIn, 0, abi.encodeCall(IERC20.approve, (spender, maxAmountIn)));
}

function _checkMarket(IMarket market) internal view {
if (!_isMarket(market)) revert NotMarket();
}
Expand Down Expand Up @@ -648,7 +650,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
// slither-disable-next-line incorrect-equality -- unsigned zero check
if (balance == 0) revert NoBalance();

_executeFromSender(market.asset(), 0, abi.encodeCall(IERC20.approve, (address(market), balance)));
_approveFromSender(market.asset(), address(market), balance);
_executeFromSender(address(market), 0, abi.encodeCall(IERC4626.deposit, (balance, msg.sender)));
_executeFromSender(address(AUDITOR), 0, abi.encodeCall(IAuditor.enterMarket, (market)));
}
Expand Down

0 comments on commit 9f33dbf

Please sign in to comment.