Skip to content

Commit

Permalink
♻️ contracts: extract approved unspent depositing pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jan 7, 2025
1 parent 0f43037 commit 05f6fa9
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions contracts/src/ExaPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
IERC20(EXA_USDC.asset()).safeTransfer(collector, amount);

_depositUnspent(collateral, maxAmountIn - amountIn, msg.sender);
uint256 usdcLeft = amountOut - amount;
if (usdcLeft != 0) EXA_USDC.deposit(usdcLeft, msg.sender);
_depositApprovedUnspent(EXA_USDC, amountOut - amount, msg.sender);

_checkLiquidity(msg.sender);
}
Expand Down Expand Up @@ -317,9 +316,7 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
_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);

uint256 usdcLeft = amountOut - actualRepay;
if (usdcLeft != 0) EXA_USDC.deposit(usdcLeft, c.borrower);

_depositApprovedUnspent(EXA_USDC, amountOut - actualRepay, c.borrower);
_depositUnspent(c.marketIn, c.maxAmountIn - amountIn, c.borrower);
_checkLiquidity(c.borrower);
}
Expand Down Expand Up @@ -635,6 +632,10 @@ contract ExaPlugin is AccessControl, BasePlugin, IExaAccount {
BALANCER_VAULT.flashLoan(address(this), tokens, amounts, data);
}

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

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

0 comments on commit 05f6fa9

Please sign in to comment.