Skip to content

Commit

Permalink
♻️ contracts: extract approve and execute pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jan 7, 2025
1 parent 2712e43 commit 3b819cb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 66 deletions.
116 changes: 58 additions & 58 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, μ: 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)
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)
ExaPluginTest:test_borrowAtMaturity_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408782)
ExaPluginTest:test_borrow_reverts_withUnauthorized_whenReceiverNotCollector() (gas: 408338)
ExaPluginTest:test_collectCredit_collects() (gas: 918651)
ExaPluginTest:test_collectCredit_collects_whenHealthFactorHigherThanOne() (gas: 799280)
ExaPluginTest:test_collectCredit_collects_withEnoughSlippage() (gas: 797905)
ExaPluginTest:test_collectCredit_collects_withPrevIssuerSignature() (gas: 994489)
ExaPluginTest:test_collectCredit_passes_whenProposalLeavesEnoughLiquidity() (gas: 1007687)
ExaPluginTest:test_collectCredit_reverts_asNotKeeper() (gas: 362585)
ExaPluginTest:test_collectCredit_reverts_whenDisagreement() (gas: 555970)
ExaPluginTest:test_collectCredit_reverts_whenExpired() (gas: 358687)
ExaPluginTest:test_collectCredit_reverts_whenHealthFactorLowerThanOne() (gas: 997935)
ExaPluginTest:test_collectCredit_reverts_whenPrevSignatureNotValidAnymore() (gas: 461452)
ExaPluginTest:test_collectCredit_reverts_whenProposalCausesInsufficientLiquidity() (gas: 1007841)
ExaPluginTest:test_collectCredit_reverts_whenReplay() (gas: 840914)
ExaPluginTest:test_collectCredit_reverts_whenTimelocked() (gas: 354855)
ExaPluginTest:test_collectCredit_toleratesTimeDrift() (gas: 801294)
ExaPluginTest:test_collectDebit_collects() (gas: 651494)
ExaPluginTest:test_collectDebit_collects_whenProposalLeavesEnoughLiquidity() (gas: 854893)
ExaPluginTest:test_collectDebit_reverts_asNotKeeper() (gas: 362396)
ExaPluginTest:test_collectDebit_reverts_whenExpired() (gas: 358382)
ExaPluginTest:test_collectDebit_reverts_whenProposalCausesInsufficientLiquidity() (gas: 851781)
ExaPluginTest:test_collectDebit_reverts_whenReplay() (gas: 690912)
ExaPluginTest:test_collectDebit_reverts_whenTimelocked() (gas: 354663)
ExaPluginTest:test_collectDebit_toleratesTimeDrift() (gas: 651590)
ExaPluginTest:test_collectInstallments_collects() (gas: 1336326)
ExaPluginTest:test_collectInstallments_revertsWhenNoSlippage() (gas: 1202745)
ExaPluginTest:test_collectInstallments_reverts_asNotKeeper() (gas: 363437)
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_reverts_whenDisagreement() (gas: 1572899)
ExaPluginTest:test_crossRepay_reverts_whenNotKeeper() (gas: 958548)
ExaPluginTest:test_debitCollateral_collects() (gas: 776420)
ExaPluginTest:test_exitMarket_reverts() (gas: 388956)
ExaPluginTest:test_marketWithdraw_transfersAsset_asOwner() (gas: 818702)
ExaPluginTest:test_onUninstall_uninstalls() (gas: 284160)
ExaPluginTest:test_poke() (gas: 315763)
ExaPluginTest:test_pokeETH_deposits() (gas: 378942)
ExaPluginTest:test_poke() (gas: 315801)
ExaPluginTest:test_pokeETH_deposits() (gas: 378980)
ExaPluginTest:test_propose_emitsProposed() (gas: 218639)
ExaPluginTest:test_refund_refunds() (gas: 361573)
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_repay_partiallyRepays() (gas: 1245277)
ExaPluginTest:test_repay_partiallyRepays_whenKeeper() (gas: 1187464)
ExaPluginTest:test_repay_repays() (gas: 1163625)
ExaPluginTest:test_repay_repays_whenKeeper() (gas: 1106306)
ExaPluginTest:test_rollDebt_rolls() (gas: 1231509)
ExaPluginTest:test_rollDebt_rolls_asKeeper() (gas: 1231157)
ExaPluginTest:test_setCollector_emitsCollectorSet() (gas: 40516)
ExaPluginTest:test_setCollector_reverts_whenAddressZero() (gas: 32311)
ExaPluginTest:test_setCollector_reverts_whenNotAdmin() (gas: 33772)
ExaPluginTest:test_setCollector_sets_whenAdmin() (gas: 39906)
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_withdrawWETH_transfersETH() (gas: 851670)
ExaPluginTest:test_withdraw_reverts_whenNoProposal() (gas: 414850)
ExaPluginTest:test_withdraw_reverts_whenNoProposalKeeper() (gas: 357843)
ExaPluginTest:test_withdraw_reverts_whenNotKeeper() (gas: 354925)
ExaPluginTest:test_withdraw_reverts_whenReceiverIsContractAndMarketNotWETH() (gas: 621125)
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: 820055)
ExaPluginTest:test_withdraw_withdrawsProposed() (gas: 810801)
ExaPluginTest:test_withdraw_transfersAsset_asKeeper() (gas: 820093)
ExaPluginTest:test_withdraw_withdrawsProposed() (gas: 810839)
InstallmentsPreviewerTest:test_preview_returns() (gas: 135598)
IssuerCheckerTest:test_setIssuer_emits_IssuerSet() (gas: 70861)
IssuerCheckerTest:test_setIssuer_reverts_whenNotAdmin() (gas: 37272)
Expand Down
24 changes: 16 additions & 8 deletions contracts/src/ExaPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
uint256 maxBorrowAssets,
uint256 percentage
) external {
_approveFromSender(address(EXA_USDC), address(DEBT_MANAGER), maxRepayAssets);
_executeFromSender(
_approveAndExecuteFromSender(
address(DEBT_MANAGER),
0,
address(EXA_USDC),
maxRepayAssets,
abi.encodeCall(
IDebtManager.rollFixed, (EXA_USDC, repayMaturity, borrowMaturity, maxRepayAssets, maxBorrowAssets, percentage)
)
Expand Down Expand Up @@ -276,10 +276,10 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
}
ISSUER_CHECKER.checkIssuer(msg.sender, totalAmount, timestamp, signature);

_approveFromSender(address(EXA_USDC), address(INSTALLMENTS_ROUTER), maxRepay);
_executeFromSender(
_approveAndExecuteFromSender(
address(INSTALLMENTS_ROUTER),
0,
address(EXA_USDC),
maxRepay,
abi.encodeCall(IInstallmentsRouter.borrow, (EXA_USDC, firstMaturity, amounts, maxRepay, collector))
);
_checkLiquidity(msg.sender);
Expand Down Expand Up @@ -585,6 +585,13 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
revert NotImplemented(msg.sig, functionId);
}

function _approveAndExecuteFromSender(address target, address assetIn, uint256 maxAmountIn, bytes memory data)
internal
{
_approveFromSender(assetIn, target, maxAmountIn);
_executeFromSender(target, 0, data);
}

function _approveFromSender(address assetIn, address spender, uint256 maxAmountIn) internal {
_executeFromSender(assetIn, 0, abi.encodeCall(IERC20.approve, (spender, maxAmountIn)));
}
Expand Down Expand Up @@ -650,8 +657,9 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
// slither-disable-next-line incorrect-equality -- unsigned zero check
if (balance == 0) revert NoBalance();

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

Expand Down

0 comments on commit 3b819cb

Please sign in to comment.