Skip to content

Commit

Permalink
⚡️ contracts: optimize repay bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jan 7, 2025
1 parent 05f6fa9 commit c2f8969
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
26 changes: 13 additions & 13 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, μ: 3581832, ~: 3439966)
ExaAccountFactoryTest:test_deploy_deploysToSameAddress() (gas: 25698785)
ExaPluginTest:testFork_crossRepay_repays() (gas: 14965414)
ExaPluginTest:testFork_debitCollateral_collects() (gas: 15052899)
ExaPluginTest:testFork_swap_swaps() (gas: 12231146)
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,22 +34,22 @@ 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_reverts_whenDisagreement() (gas: 1572899)
ExaPluginTest:test_crossRepay_repays() (gas: 1465030)
ExaPluginTest:test_crossRepay_repays_whenKeeper() (gas: 1461809)
ExaPluginTest:test_crossRepay_reverts_whenDisagreement() (gas: 1574262)
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)
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: 1245277)
ExaPluginTest:test_repay_partiallyRepays_whenKeeper() (gas: 1187464)
ExaPluginTest:test_repay_repays() (gas: 1163625)
ExaPluginTest:test_repay_repays_whenKeeper() (gas: 1106306)
ExaPluginTest:test_repay_partiallyRepays() (gas: 1245357)
ExaPluginTest:test_repay_partiallyRepays_whenKeeper() (gas: 1187544)
ExaPluginTest:test_repay_repays() (gas: 1163705)
ExaPluginTest:test_repay_repays_whenKeeper() (gas: 1106386)
ExaPluginTest:test_rollDebt_rolls() (gas: 1231509)
ExaPluginTest:test_rollDebt_rolls_asKeeper() (gas: 1231157)
ExaPluginTest:test_setCollector_emitsCollectorSet() (gas: 40516)
Expand Down
41 changes: 24 additions & 17 deletions contracts/src/ExaPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -295,30 +295,37 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
assert(msg.sender == address(BALANCER_VAULT) && callHash == keccak256(data));
delete callHash;

uint256 maturity;
uint256 positionAssets;
uint256 maxRepay;
address borrower;
uint256 actualRepay;

if (data[0] == 0x01) {
RepayCallbackData memory r = abi.decode(data[1:], (RepayCallbackData));
actualRepay = EXA_USDC.repayAtMaturity(r.maturity, r.positionAssets, r.maxRepay, r.borrower);

if (actualRepay < r.maxRepay) EXA_USDC.deposit(r.maxRepay - actualRepay, r.borrower);

EXA_USDC.withdraw(r.maxRepay, address(BALANCER_VAULT), r.borrower);

_checkLiquidity(r.borrower);
return;
(maturity, positionAssets, maxRepay, borrower) = (r.maturity, r.positionAssets, r.maxRepay, r.borrower);
} else {
CrossRepayCallbackData memory c = abi.decode(data[1:], (CrossRepayCallbackData));
(maturity, positionAssets, maxRepay, borrower) = (c.maturity, c.positionAssets, c.maxRepay, c.borrower);
}

CrossRepayCallbackData memory c = abi.decode(data[1:], (CrossRepayCallbackData));
actualRepay = EXA_USDC.repayAtMaturity(c.maturity, c.positionAssets, c.maxRepay, c.borrower);
actualRepay = EXA_USDC.repayAtMaturity(maturity, positionAssets, maxRepay, borrower);

c.marketIn.withdraw(c.maxAmountIn, address(this), c.borrower);
(uint256 amountIn, uint256 amountOut) =
_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);
if (data[0] == 0x01) {
if (actualRepay < maxRepay) EXA_USDC.deposit(maxRepay - actualRepay, borrower);
EXA_USDC.withdraw(maxRepay, address(BALANCER_VAULT), borrower);
} else {
CrossRepayCallbackData memory c = abi.decode(data[1:], (CrossRepayCallbackData));
c.marketIn.withdraw(c.maxAmountIn, address(this), borrower);
(uint256 amountIn, uint256 amountOut) =
_swap(IERC20(c.marketIn.asset()), IERC20(EXA_USDC.asset()), c.maxAmountIn, maxRepay, c.route);
IERC20(EXA_USDC.asset()).safeTransfer(address(BALANCER_VAULT), maxRepay);

_depositApprovedUnspent(EXA_USDC, amountOut - actualRepay, borrower);
_depositUnspent(c.marketIn, c.maxAmountIn - amountIn, borrower);
}

_depositApprovedUnspent(EXA_USDC, amountOut - actualRepay, c.borrower);
_depositUnspent(c.marketIn, c.maxAmountIn - amountIn, c.borrower);
_checkLiquidity(c.borrower);
_checkLiquidity(borrower);
}

receive() external payable { } // solhint-disable-line no-empty-blocks
Expand Down

0 comments on commit c2f8969

Please sign in to comment.