// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . pragma solidity ^0.8.0; import '@mimic-fi/v2-helpers/contracts/math/FixedPoint.sol'; import './BaseAction.sol'; /** * @title TokenThresholdAction * @dev Action that offers a token threshold limit. It can be used for minimum swap amounts, or minimum withdrawal * amounts, etc. This type of action does not require any specific permission on the Smart Vault. */ abstract contract TokenThresholdAction is BaseAction { using FixedPoint for uint256; address public thresholdToken; uint256 public thresholdAmount; event ThresholdSet(address indexed token, uint256 amount); /** * @dev Sets a new threshold configuration. Sender must be authorized. * @param token New token threshold to be set * @param amount New amount threshold to be set */ function setThreshold(address token, uint256 amount) external auth { thresholdToken = token; thresholdAmount = amount; emit ThresholdSet(token, amount); } /** * @dev Internal function to check the set threshold * @param token Token address of the given amount to evaluate the threshold * @param amount Amount of tokens to validate the threshold */ function _passesThreshold(address token, uint256 amount) internal view returns (bool) { uint256 price = _getPrice(_wrappedIfNative(token), thresholdToken); // Result balance is rounded down to make sure we always match at least the threshold return amount.mulDown(price) >= thresholdAmount; } /** * @dev Internal function to validate the set threshold * @param token Token address of the given amount to evaluate the threshold * @param amount Amount of tokens to validate the threshold */ function _validateThreshold(address token, uint256 amount) internal view { require(_passesThreshold(token, amount), 'MIN_THRESHOLD_NOT_MET'); } }