github.com/cwntr/go-defi@v0.0.0-20210629134751-07f9ec2f7e66/contracts/lib/libCache.sol (about)

     1  pragma solidity ^0.5.0;
     2  
     3  library LibCache {
     4      function setAddress(bytes32[] storage _cache, address _input) internal {
     5          _cache.push(bytes32(uint256(uint160(_input))));
     6      }
     7  
     8      function set(bytes32[] storage _cache, bytes32 _input) internal {
     9          _cache.push(_input);
    10      }
    11  
    12      function setHandlerType(bytes32[] storage _cache, uint256 _input) internal {
    13          require(_input < uint96(-1), "Invalid Handler Type");
    14          _cache.push(bytes12(uint96(_input)));
    15      }
    16  
    17      function setSender(bytes32[] storage _cache, address _input) internal {
    18          require(_cache.length == 0, "cache not empty");
    19          setAddress(_cache, _input);
    20      }
    21  
    22      function getAddress(bytes32[] storage _cache)
    23          internal
    24          returns (address ret)
    25      {
    26          ret = address(uint160(uint256(peek(_cache))));
    27          _cache.pop();
    28      }
    29  
    30      function getSig(bytes32[] storage _cache) internal returns (bytes4 ret) {
    31          ret = bytes4(peek(_cache));
    32          _cache.pop();
    33      }
    34  
    35      function get(bytes32[] storage _cache) internal returns (bytes32 ret) {
    36          ret = peek(_cache);
    37          _cache.pop();
    38      }
    39  
    40      function peek(bytes32[] storage _cache)
    41          internal
    42          view
    43          returns (bytes32 ret)
    44      {
    45          require(_cache.length > 0, "cache empty");
    46          ret = _cache[_cache.length - 1];
    47      }
    48  
    49      function getSender(bytes32[] storage _cache)
    50          internal
    51          view
    52          returns (address ret)
    53      {
    54          require(_cache.length > 0, "cache empty");
    55          ret = address(uint160(uint256(_cache[0])));
    56      }
    57  }