|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | +pragma solidity ^0.8.26; |
| 3 | + |
| 4 | +import {IERC4626} from "@openzeppelin-contracts/interfaces/IERC4626.sol"; |
| 5 | +import {IERC20} from "@openzeppelin-contracts/token/ERC20/IERC20.sol"; |
| 6 | + |
| 7 | +import {InstructionLib} from "../libraries/Instruction.sol"; |
| 8 | + |
| 9 | +import {OtimFee} from "./fee-models/OtimFee.sol"; |
| 10 | + |
| 11 | +import {IAction} from "./interfaces/IAction.sol"; |
| 12 | +import { |
| 13 | + ISweepRequestDepositERC7540Action, |
| 14 | + INSTRUCTION_TYPEHASH, |
| 15 | + ARGUMENTS_TYPEHASH |
| 16 | +} from "./interfaces/ISweepRequestDepositERC7540Action.sol"; |
| 17 | +import {IERC7540Deposit} from "./external/IERC7540.sol"; |
| 18 | + |
| 19 | +import {InvalidArguments, BalanceUnderThreshold, TotalSharesTooLow, MaxDepositTooLow} from "./errors/Errors.sol"; |
| 20 | + |
| 21 | +/// @title SweepRequestDepositERC7540Action |
| 22 | +/// @author Otim Labs, Inc. |
| 23 | +/// @notice an Action that submits an async deposit request to an ERC7540 vault when the underlying balance is greater than or equal to a threshold |
| 24 | +contract SweepRequestDepositERC7540Action is IAction, ISweepRequestDepositERC7540Action, OtimFee { |
| 25 | + constructor(address feeTokenRegistryAddress, address treasuryAddress, uint256 gasConstant_) |
| 26 | + OtimFee(feeTokenRegistryAddress, treasuryAddress, gasConstant_) |
| 27 | + {} |
| 28 | + |
| 29 | + /// @inheritdoc IAction |
| 30 | + function argumentsHash(bytes calldata arguments) public pure returns (bytes32, bytes32) { |
| 31 | + return (INSTRUCTION_TYPEHASH, hash(abi.decode(arguments, (SweepRequestDepositERC7540)))); |
| 32 | + } |
| 33 | + |
| 34 | + /// @inheritdoc ISweepRequestDepositERC7540Action |
| 35 | + function hash(SweepRequestDepositERC7540 memory arguments) public pure returns (bytes32) { |
| 36 | + return keccak256( |
| 37 | + abi.encode( |
| 38 | + ARGUMENTS_TYPEHASH, |
| 39 | + arguments.vault, |
| 40 | + arguments.recipient, |
| 41 | + arguments.controller, |
| 42 | + arguments.threshold, |
| 43 | + arguments.endBalance, |
| 44 | + arguments.minDeposit, |
| 45 | + arguments.minTotalShares, |
| 46 | + hash(arguments.fee) |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /// @inheritdoc IAction |
| 52 | + function execute( |
| 53 | + InstructionLib.Instruction calldata instruction, |
| 54 | + InstructionLib.Signature calldata, |
| 55 | + InstructionLib.ExecutionState calldata executionState |
| 56 | + ) external override returns (bool) { |
| 57 | + // initial gas measurement for fee calculation |
| 58 | + uint256 startGas = gasleft(); |
| 59 | + |
| 60 | + // decode the arguments from the instruction |
| 61 | + SweepRequestDepositERC7540 memory arguments = abi.decode(instruction.arguments, (SweepRequestDepositERC7540)); |
| 62 | + |
| 63 | + // if first execution, validate the input |
| 64 | + if (executionState.executionCount == 0) { |
| 65 | + if ( |
| 66 | + arguments.vault == address(0) || arguments.recipient == address(0) || arguments.controller == address(0) |
| 67 | + || arguments.endBalance > arguments.threshold || arguments.minTotalShares == 0 |
| 68 | + ) { |
| 69 | + revert InvalidArguments(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // get the underlying token |
| 74 | + address underlyingToken = IERC4626(arguments.vault).asset(); |
| 75 | + |
| 76 | + // get the account's token balance |
| 77 | + uint256 balance = IERC20(underlyingToken).balanceOf(address(this)); |
| 78 | + |
| 79 | + // if the balance is under the threshold or equal to the endBalance, revert. |
| 80 | + // the endBalance check is to prevent the instruction from executing |
| 81 | + // when threshold == endBalance == balance because in this case we would have requestAmount == 0 |
| 82 | + // slither-disable-next-line incorrect-equality |
| 83 | + if (balance < arguments.threshold || balance == arguments.endBalance) { |
| 84 | + revert BalanceUnderThreshold(); |
| 85 | + } |
| 86 | + |
| 87 | + // check if vault total shares is too low |
| 88 | + if (IERC4626(arguments.vault).totalSupply() < arguments.minTotalShares) { |
| 89 | + revert TotalSharesTooLow(); |
| 90 | + } |
| 91 | + |
| 92 | + // initialize request amount |
| 93 | + uint256 requestAmount = balance - arguments.endBalance; |
| 94 | + |
| 95 | + // approve the vault to pull assets from the executing account (owner) |
| 96 | + // slither-disable-next-line unused-return |
| 97 | + IERC20(underlyingToken).approve(arguments.vault, requestAmount); |
| 98 | + |
| 99 | + // submit async deposit request: owner = address(this), controller from arguments |
| 100 | + uint256 requestId = |
| 101 | + IERC7540Deposit(arguments.vault).requestDeposit(requestAmount, arguments.controller, address(this)); |
| 102 | + |
| 103 | + // if the request amount is less than the minimum deposit amount, revert |
| 104 | + if ( |
| 105 | + IERC7540Deposit(arguments.vault).pendingDepositRequest(requestId, arguments.controller) |
| 106 | + < arguments.minDeposit |
| 107 | + ) { |
| 108 | + revert MaxDepositTooLow(); |
| 109 | + } |
| 110 | + |
| 111 | + // charge the fee |
| 112 | + chargeFee(startGas - gasleft(), arguments.fee); |
| 113 | + |
| 114 | + // this action has no auto-deactivation cases |
| 115 | + return false; |
| 116 | + } |
| 117 | +} |
0 commit comments