github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/periphery/drippie/dripchecks/CheckGelatoLow.t.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity 0.8.15;
     3  
     4  import { Test } from "forge-std/Test.sol";
     5  import { CheckGelatoLow, IGelatoTreasury } from "src/periphery/drippie/dripchecks/CheckGelatoLow.sol";
     6  
     7  /// @title  MockGelatoTreasury
     8  /// @notice Mocks the Gelato treasury for testing purposes. Allows arbitrary
     9  ///        setting of user balances.
    10  contract MockGelatoTreasury is IGelatoTreasury {
    11      mapping(address => mapping(address => uint256)) private tokenBalances;
    12  
    13      function setTokenBalance(address _user, address _token, uint256 _balance) external {
    14          tokenBalances[_token][_user] = _balance;
    15      }
    16  
    17      function userTokenBalance(address _user, address _token) external view returns (uint256) {
    18          return tokenBalances[_token][_user];
    19      }
    20  }
    21  
    22  /// @title  CheckGelatoLowTest
    23  /// @notice Tests the CheckGelatoLow contract via fuzzing both the success case
    24  ///         and the failure case.
    25  contract CheckGelatoLowTest is Test {
    26      /// @notice An instance of the CheckGelatoLow contract.
    27      CheckGelatoLow c;
    28  
    29      /// @notice An instance of the MockGelatoTreasury contract.
    30      MockGelatoTreasury gelato;
    31  
    32      /// @notice The account Gelato uses to represent ether
    33      address internal constant eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
    34  
    35      /// @notice Deploy the `CheckGelatoLow` and `MockGelatoTreasury` contracts.
    36      function setUp() external {
    37          c = new CheckGelatoLow();
    38          gelato = new MockGelatoTreasury();
    39      }
    40  
    41      /// @notice Fuzz the `check` function and assert that it always returns true
    42      ///         when the user's balance in the treasury is less than the threshold.
    43      function testFuzz_check_succeeds(uint256 _threshold, address _recipient) external {
    44          CheckGelatoLow.Params memory p =
    45              CheckGelatoLow.Params({ treasury: address(gelato), threshold: _threshold, recipient: _recipient });
    46  
    47          vm.assume(gelato.userTokenBalance(_recipient, eth) < _threshold);
    48  
    49          assertEq(c.check(abi.encode(p)), true);
    50      }
    51  
    52      /// @notice Fuzz the `check` function and assert that it always returns false
    53      ///         when the user's balance in the treasury is greater than or equal
    54      ///         to the threshold.
    55      function testFuzz_check_highBalance_fails(uint256 _threshold, address _recipient) external {
    56          CheckGelatoLow.Params memory p =
    57              CheckGelatoLow.Params({ treasury: address(gelato), threshold: _threshold, recipient: _recipient });
    58  
    59          gelato.setTokenBalance(_recipient, eth, _threshold);
    60  
    61          assertEq(c.check(abi.encode(p)), false);
    62      }
    63  }