Skip to content

Commit fcd9fcf

Browse files
committed
Avoid deposit() method when only repaying, to support borrow-only vaults
spearbit #8
1 parent e44a49a commit fcd9fcf

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/EulerSwap.sol

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,34 @@ contract EulerSwap is IEulerSwap, EVCUtil {
221221
/// @dev After successful deposit, if the user has any outstanding controller-enabled debt, it attempts to repay it.
222222
/// @dev If all debt is repaid, the controller is automatically disabled to reduce gas costs in future operations.
223223
function depositAssets(address vault, uint256 amount) internal returns (uint256) {
224-
try IEVault(vault).deposit(amount, eulerAccount) {}
225-
catch (bytes memory reason) {
226-
require(bytes4(reason) == EVKErrors.E_ZeroShares.selector, DepositFailure(reason));
227-
return 0;
228-
}
224+
uint256 debt = myDebt(vault);
225+
uint256 deposited;
229226

230-
if (IEVC(evc).isControllerEnabled(eulerAccount, vault)) {
231-
IEVC(evc).call(
232-
vault, eulerAccount, 0, abi.encodeCall(IBorrowing.repayWithShares, (type(uint256).max, eulerAccount))
233-
);
227+
if (debt > 0) {
228+
uint256 repayAmount = amount > debt ? debt : amount;
229+
230+
IEVault(vault).repay(repayAmount, eulerAccount);
234231

235-
if (myDebt(vault) == 0) {
232+
amount -= repayAmount;
233+
debt -= repayAmount;
234+
deposited += repayAmount;
235+
236+
if (debt == 0) {
236237
IEVC(evc).call(vault, eulerAccount, 0, abi.encodeCall(IRiskManager.disableController, ()));
237238
}
238239
}
239240

240-
return amount;
241+
if (amount > 0) {
242+
try IEVault(vault).deposit(amount, eulerAccount) {}
243+
catch (bytes memory reason) {
244+
require(bytes4(reason) == EVKErrors.E_ZeroShares.selector, DepositFailure(reason));
245+
return deposited;
246+
}
247+
248+
deposited += amount;
249+
}
250+
251+
return deposited;
241252
}
242253

243254
/// @notice Approves tokens for a given vault, supporting both standard approvals and permit2

0 commit comments

Comments
 (0)