all files / contracts/risk/ CoverageDataProvider.sol

97.37% Statements 37/38
93.75% Branches 15/16
100% Functions 10/10
100% Lines 43/43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                                                          155× 152×                                       16×   14× 14× 42× 42× 42× 42× 42×       14× 14× 41×                   105×   103× 102× 78× 78× 77× 76×   100×               31× 31×   25× 25×   25× 12× 12× 12×     25× 25× 24× 22× 19×                         3410× 3410× 3411×                   3431×                 13×                                          
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.6;
 
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
 
import "../interfaces/risk/ICoverageDataProvider.sol";
import "../interfaces/utils/IRegistry.sol";
import "../interfaces/ISOLACE.sol";
import "../utils/Governable.sol";
 
/**
 * @title  CoverageDataProvider
 * @author solace.fi
 * @notice Holds underwriting pool amounts in `USD`. Provides information to the [**Risk Manager**](./RiskManager.sol) that is the maximum amount of cover that `Solace` protocol can sell as a coverage.
*/
contract CoverageDataProvider is ICoverageDataProvider, Governable {
 
    /***************************************
     STATE VARIABLES
    ***************************************/
 
    /// @notice The balance of underwriting pool in usd.
    mapping(string => uint256) private _uwpBalanceOf;
 
    /// @notice The index to underwriting pool.
    mapping(uint256 => string) private _indexToUwp;
 
    /// @notice The underwriting pool to index.
    mapping(string => uint256) private _uwpToIndex;
 
    /// @notice The underwriting pool count
    uint256 public numOfPools;
 
    /// @notice The pool updater.
    address private _uwpUpdater;
 
    modifier canUpdate() {
      require(msg.sender == super.governance() || msg.sender == _uwpUpdater, "!governance");
      _;
    }
 
    /**
     * @notice Constructs the `CoverageDataProvider` contract.
     * @param governance The address of the [governor](/docs/protocol/governance).
    */
    // solhint-disable-next-line no-empty-blocks
    constructor(address governance) Governable(governance) {}
 
    /***************************************
     MUTUATOR FUNCTIONS
    ***************************************/
   
    /**
      * @notice Resets the underwriting pool balances.
      * @param uwpNames The underwriting pool values to set.
      * @param amounts The underwriting pool balances in `USD`.
    */
    function reset(string[] calldata uwpNames, uint256[] calldata amounts) external override canUpdate {
      require(uwpNames.length == amounts.length, "length mismatch");
      // delete current underwriting pools
      uint256 poolCount = numOfPools;
      for (uint256 i = poolCount; i > 0; i--) {
        string memory uwpName = _indexToUwp[i];
        delete _uwpToIndex[uwpName];
        delete _indexToUwp[i];
        delete _uwpBalanceOf[uwpName];
        emit UnderwritingPoolRemoved(uwpName);
      }
 
      // set new underwriting pools
      numOfPools = 0;
      for (uint256 i = 0; i < uwpNames.length; i++) {
        set(uwpNames[i], amounts[i]);
      }
    }
 
    /**
     * @notice Sets the balance of the given underwriting pool.
     * @param uwpName The underwriting pool name to set balance.
     * @param amount The balance of the underwriting pool in `USD`.
    */
    function set(string calldata uwpName, uint256 amount) public override canUpdate {
      require(bytes(uwpName).length > 0, "empty underwriting pool name");
     
      _uwpBalanceOf[uwpName] = amount;
      if (_uwpToIndex[uwpName] == 0) {
        uint256 index = numOfPools;
        _uwpToIndex[uwpName] = ++index;
        _indexToUwp[index] = uwpName;
        numOfPools = index;
      }
      emit UnderwritingPoolSet(uwpName, amount);
    }
 
    /**
     * @notice Removes the given underwriting pool.
     * @param uwpName The underwriting pool name to remove.
    */
    function remove(string calldata uwpName) external override canUpdate {
      uint256 index = _uwpToIndex[uwpName];
      if (index == 0) return;
 
      uint256 poolCount = numOfPools;
      Iif (poolCount == 0) return;
 
      if (index != poolCount) {
        string memory lastPool = _indexToUwp[poolCount];
        _uwpToIndex[lastPool] = index;
        _indexToUwp[index] = lastPool;
      }
 
      delete _uwpToIndex[uwpName];
      delete _indexToUwp[poolCount];
      delete _uwpBalanceOf[uwpName];
      numOfPools -= 1;
      emit UnderwritingPoolRemoved(uwpName);
    }
 
    /***************************************
     VIEW FUNCTIONS
    ***************************************/
 
    /**
     * @notice Returns the maximum amount of cover in `USD` that Solace as a whole can sell.
     * @return cover The max amount of cover in `USD`.
    */
    function maxCover() external view override returns (uint256 cover) {
      // get pool balance
      uint256 pools = numOfPools;
      for (uint256 i = pools; i > 0; i--) {
        cover += balanceOf(_indexToUwp[i]);
      }
    }
   
    /**
     * @notice Returns the balance of the underwriting pool in `USD`.
     * @param uwpName The underwriting pool name to get balance.
     * @return amount The balance of the underwriting pool in `USD`.
    */
    function balanceOf(string memory uwpName) public view override returns (uint256 amount) {
      return _uwpBalanceOf[uwpName];
    }
 
    /**
     * @notice Returns underwriting pool name for given index.
     * @param index The underwriting pool index to get.
     * @return uwpName The underwriting pool name.
    */
    function poolOf(uint256 index) external view override returns (string memory uwpName) {
      return _indexToUwp[index];
    }
 
    /**
     * @notice Returns the underwriting pool bot updater address.
     * @return uwpUpdater The bot address.
    */
    function getUwpUpdater() external view override returns (address uwpUpdater) {
      return _uwpUpdater;
    }
 
    /***************************************
     GOVERNANCE FUNCTIONS
    ***************************************/
    
    /**
     * @notice Sets the underwriting pool bot updater.
     * @param uwpUpdater The bot address to set.
    */
    function setUwpUpdater(address uwpUpdater) external override onlyGovernance {
      require(uwpUpdater != address(0x0), "zero address uwp updater");
      _uwpUpdater = uwpUpdater;
      emit UwpUpdaterSet(uwpUpdater);
    }
}