all files / contracts/products/ SolaceCoverProductV3.sol

95.29% Statements 81/85
92.5% Branches 37/40
92.59% Functions 25/27
96.43% Lines 81/84
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424                                                                                                                                                                            121× 117×       21×       19×                                                                   96×               14× 13×                       99×               105×               96×                 18×               104×                                                   14× 14×                             27×                   30× 30×                                                 15× 12×                         11×                                                     94×     94× 94×                       96×   94× 94× 90×     88× 39× 39× 39× 39×   48× 36×       75× 75× 69×               13× 13× 13×   13× 12× 12×                 88×                     40× 40×                 31× 29×     29× 29× 27×     27× 27× 25× 21×      
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.6;
 
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
 
import "../utils/Governable.sol";
import "../interfaces/utils/IRegistry.sol";
import "../interfaces/risk/IRiskManager.sol";
import "../interfaces/payment/ISCP.sol";
import "../interfaces/products/ISolaceCoverProductV3.sol";
 
/**
 * @title SolaceCoverProductV3
 * @author solace.fi
 * @notice A Solace insurance product that allows users to insure all of their DeFi positions against smart contract risk through a single policy.
 *
 * Policies can be **purchased** via [`activatePolicy()`](#activatepolicy). Policies are represented as ERC721s, which once minted, cannot then be transferred or burned. Users can change the cover limit of their policy through [`updateCoverLimit()`](#updatecoverlimit).
 *
 * The policy will remain active until i.) the user cancels their policy or ii.) the user's account runs out of funds. The policy will be billed like a subscription, every epoch a fee will be charged from the user's account.
 *
 * Users can **deposit funds** into their account via [`deposit()`](#deposit). Currently the contract only accepts deposits in **FRAX**. Note that both [`activatePolicy()`](#activatepolicy) and [`deposit()`](#deposit) enables a user to perform these actions (activate a policy, make a deposit) on behalf of another user.
 *
 * Users can **cancel** their policy via [`deactivatePolicy()`](#deactivatepolicy). This will start a cooldown timer. Users can **withdraw funds** from their account via [`withdraw()`](#withdraw).
 *
 * Before the cooldown timer starts or passes, the user cannot withdraw their entire account balance. A minimum required account balance (to cover one epoch's fee) will be left in the user's account. After the cooldown has passed, a user will be able to withdraw their entire account balance.
 *
 * Users can enter a **referral code** with [`activatePolicy()`](#activatePolicy) or [`updateCoverLimit()`](#updatecoverlimit). A valid referral code will earn reward points to both the referrer and the referee. When the user's account is charged, reward points will be deducted before solace cover dollars.
 * Each account can only enter a valid referral code once, however there are no restrictions on how many times a referral code can be used for new accounts.
 */
contract SolaceCoverProductV3 is
    ISolaceCoverProductV3,
    ERC721,
    ReentrancyGuard,
    Governable
{
    /***************************************
    STATE VARIABLES
    ***************************************/
 
    /// @notice Registry contract.
    address public registry;
 
    /// @notice RiskManager contract.
    address public riskManager;
 
    /// @notice SCP(Solace Cover Points) contract.
    address public scp;
 
    /// @notice Cannot buy new policies while paused. (Default is False)
    bool public paused;
 
    /// @notice The base token uri url for policies.
    string public baseURI;
 
    /// @notice The total policy count.
    uint256 public policyCount;
 
    /// @notice The maximum rate charged per second per 1e-18 (wei) of coverLimit.
    /// @dev Default to charge 10% of cover limit annually = 1/315360000.
    uint256 public maxRateNum;
 
    /// @notice The maximum rate denomination value.
    /// @dev  Max premium rate of 10% of cover limit per annum.
    uint256 public maxRateDenom;
 
    /// @notice Maximum epoch duration over which premiums are charged (Default is one week).
    uint256 public chargeCycle; 
 
    /// @notice The latest premium charged timestamp.
    uint256 public latestChargedTime;
 
    /// @notice policyholder => policyID.
    mapping(address => uint256) public policyOf;
 
    /// @notice policyholder => policy debt.
    mapping(address => uint256) public debtOf;
 
    /// @notice policyID => coverLimit.
    mapping(uint256 => uint256) public coverLimitOf;
 
    /***************************************
    MODIFIERS
    ***************************************/
 
    modifier whileUnpaused() {
        require(!paused, "contract paused");
        _;
    }
 
    modifier onlyCollector() {
        require(
            msg.sender == IRegistry(registry).get("premiumCollector") ||
            msg.sender == governance(), "not premium collector"
        );
        _;
    }
 
    /**
     * @notice Constructs `Solace Cover Product`.
     * @param _governance The address of the governor.
     * @param _registry The [`Registry`](./Registry) contract address.
    */
    constructor(address _governance, address _registry) ERC721("Solace Wallet Coverage", "SWC") Governable(_governance) {
        // set registry
        _setRegistry(_registry);
 
        // set defaults
        maxRateNum = 1;
        maxRateDenom = 315360000;
        chargeCycle = 604800;
        baseURI = string(abi.encodePacked("https://stats.solace.fi/policy/?chainID=", Strings.toString(block.chainid), "&policyID="));
    }
 
    /***************************************
    POLICY FUNCTIONS
    ***************************************/
 
    /**
     * @notice Activates policy for `msg.sender`.
     * @param _user The account to purchase policy. 
     * @param _coverLimit The maximum value to cover in **USD**.
     * @return policyID The ID of the newly minted policy.
     */
    function purchaseFor(address _user, uint256 _coverLimit) external override nonReentrant whileUnpaused returns (uint256 policyID) {
        return _purchase(_user, _coverLimit);
    }
 
    /**
     * @notice Activates policy for `msg.sender`.
     * @param _coverLimit The maximum value to cover in **USD**.
     * @return policyID The ID of the newly minted policy.
     */
    function purchase(uint256 _coverLimit) external override nonReentrant whileUnpaused returns (uint256 policyID) {
       return _purchase(msg.sender, _coverLimit);
    }
 
    /**
     * @notice Cancels the policy.
     * The function cancels the policy of the policyholder.
     */
    function cancel() external override {
        require(policyStatus(policyOf[msg.sender]), "invalid policy");
        _cancel(msg.sender);
    }
 
    /***************************************
    VIEW FUNCTIONS
    ***************************************/
 
    /**
     * @notice The maximum amount of cover that can be sold in **USD** to 18 decimals places.
     * @return cover The max amount of cover.
     */
    function maxCover() public view override returns (uint256 cover) {
        return IRiskManager(riskManager).maxCoverPerStrategy(address(this));
    }
 
    /**
     * @notice Returns the active cover limit in **USD** to 18 decimal places. In other words, the total cover that has been sold at the current time.
     * @return amount The active cover limit.
     */
    function activeCoverLimit() public view override returns (uint256 amount) {
        return IRiskManager(riskManager).activeCoverLimitPerStrategy(address(this));
    }
 
    /**
     * @notice Determine the available remaining capacity for new cover.
     * @return capacity The amount of available remaining capacity for new cover.
     */
    function availableCoverCapacity() public view override returns (uint256 capacity) {
        capacity = maxCover() - activeCoverLimit();
    }
 
    /**
     * @notice Returns true if the policy is active, false if inactive
     * @param _policyID The policy ID.
     * @return status True if policy is active. False otherwise.
     */
    function policyStatus(uint256 _policyID) public view override returns (bool status) {
        return coverLimitOf[_policyID] > 0 ? true : false;
    }
 
    /**
     * @notice Calculate minimum required account balance for a given cover limit. Equals the maximum chargeable fee for one epoch.
     * @param _coverLimit The maximum value to cover in **USD**.
     */
    function minRequiredAccountBalance(uint256 _coverLimit) public view override returns (uint256 mrab) {
        mrab = (maxRateNum * chargeCycle * _coverLimit) / maxRateDenom;
    }
 
    /**
     * @notice Calculates the minimum amount of Solace Credit Points required by this contract for the account to hold.
     * @param _policyholder The account to query.
     * @return amount The amount of SCP the account must hold.
     */
    function minScpRequired(address _policyholder) external view override returns (uint256 amount) {
          return minRequiredAccountBalance(coverLimitOf[policyOf[_policyholder]]) + debtOf[_policyholder];
    }
 
    /**
     * @notice Returns the Uniform Resource Identifier (URI) for `policyID`.
     * @param policyID The policy ID.
     */
    function tokenURI(uint256 policyID) public view virtual override returns (string memory uri) {
        require(_exists(policyID), "invalid policy");
        return string(abi.encodePacked(baseURI, Strings.toString(policyID)));
    }
 
    /**
     * @notice Calculates the policy cancellation fee.
     * @param policyID The policy id.
     * @return fee The cancellation fee.
    */
    function calculateCancelFee(uint256 policyID) public view override returns (uint256 fee) {
        // calculate cancellation fee
        Eif (latestChargedTime > 0) {
            return (minRequiredAccountBalance(coverLimitOf[policyID]) * ((block.timestamp - latestChargedTime) / 86400)) / chargeCycle;
        }
        return 0;
    }
 
    /***************************************
    GOVERNANCE FUNCTIONS
    ***************************************/
 
    /**
     * @notice Sets the [`Registry`](./Registry) contract address.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param _registry The address of `Registry` contract.
     */
    function setRegistry(address _registry) external override onlyGovernance {
        _setRegistry(_registry);
    }
 
    /**
     * @notice Pauses or unpauses policies.
     * Deactivating policies are unaffected by pause.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param _paused True to pause, false to unpause.
     */
    function setPaused(bool _paused) external override onlyGovernance {
        paused = _paused;
        emit PauseSet(_paused);
    }
 
    /**
     * @notice set _maxRate.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param _maxRateNum Desired maxRateNum.
     * @param _maxRateDenom Desired maxRateDenom.
     */
    function setMaxRate(uint256 _maxRateNum, uint256 _maxRateDenom) external override onlyGovernance {
        maxRateNum = _maxRateNum;
        maxRateDenom = _maxRateDenom;
        emit MaxRateSet(_maxRateNum, _maxRateDenom);
    }
 
    /**
     * @notice Sets maximum epoch duration over which premiums are charged.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param _chargeCycle The charge period to set.
     */
    function setChargeCycle(uint256 _chargeCycle) external override onlyGovernance {
        chargeCycle = _chargeCycle;
        emit ChargeCycleSet(_chargeCycle);
    }
 
    /**
     * @notice Sets the base URI for computing `tokenURI`.
     * @param _baseURI The new base URI.
     */
    function setBaseURI(string memory _baseURI) external override onlyGovernance {
        baseURI = _baseURI;
        emit BaseURISet(_baseURI);
    }
 
    /***************************************
    PREMIUM COLLECTOR FUNCTIONS
    ***************************************/
 
    /**
     * @notice Sets the latest premium charged time.
     * @param _timestamp The timestamp value when the premiums are charged.
    */
    function setChargedTime(uint256 _timestamp) external override whileUnpaused onlyCollector {
        // solhint-disable-next-line not-rely-on-time
        require(_timestamp > 0 && _timestamp <= block.timestamp, "invalid charged timestamp");
        latestChargedTime = _timestamp;
        emit LatestChargedTimeSet(_timestamp);
    }
 
    /**
     * @notice Add debts for each policy holder. Can only be called by the **Premium Collector** role.
     * @param _policyholders The array of addresses of the policyholders to add debt.
     * @param _debts The array of debt amounts (in **USD** to 18 decimal places) for each policyholder.
     */
    function setDebts(address[] calldata _policyholders, uint256[] calldata _debts) external override whileUnpaused onlyCollector {
        uint256 count = _policyholders.length;
        require(count == _debts.length, "length mismatch");
        require(count <= policyCount, "policy count exceeded");
 
        address policyholder;
        uint256 debt;
 
        for (uint256 i = 0; i < count; i++) {
            policyholder = _policyholders[i];
            Eif (policyOf[policyholder] > 0) {
                debt = _debts[i];
                debtOf[policyholder] += debt;
                emit DebtSet(policyholder, debt);
            }
        }
    }
 
    /***************************************
    INTERNAL FUNCTIONS
    ***************************************/
 
    /**
     * @notice Returns true if there is sufficient capacity to update a policy's cover limit, false if not.
     * @param _currentCoverLimit The current cover limit, 0 if policy has not previously been activated.
     * @param _newCoverLimit  The new cover limit requested.
     * @return acceptable True there is sufficient capacity for the requested new cover limit, false otherwise.
     */
    function _checkCapacity(uint256 _currentCoverLimit, uint256 _newCoverLimit) internal view returns (bool acceptable) {
        // return true if user is lowering cover limit
        Iif (_newCoverLimit <= _currentCoverLimit) return true;
 
        // check capacity
        uint256 diff = _newCoverLimit - _currentCoverLimit;
        if (diff < availableCoverCapacity()) return true;
        
        // no available capacity
        return false;
    }
 
    /**
     * @notice Activates policy for `msg.sender`.
     * @param _user The account to purchase policy. 
     * @param _coverLimit The maximum value to cover in **USD**.
     * @return policyID The ID of the newly minted policy.
     */
     function _purchase(address _user, uint256 _coverLimit) internal returns (uint256 policyID) {
        require(_coverLimit > 0, "zero cover value");
 
        policyID = policyOf[_user];
        require(_checkCapacity(0, _coverLimit), "insufficient capacity");
        require(ISCP(scp).balanceOf(_user) > minRequiredAccountBalance(_coverLimit), "insufficient scp balance");
 
        // mint policy if doesn't currently exist
        if (policyID == 0) {
            policyID = ++policyCount;
            policyOf[_user] = policyID;
            _mint(_user, policyID);
            emit PolicyCreated(policyID);
        } else {
            if (_coverLimit == coverLimitOf[policyID]) return policyID;
            emit PolicyUpdated(policyID);
        }
 
        // update cover amount
        _updateActiveCoverLimit(coverLimitOf[policyID], _coverLimit);
        coverLimitOf[policyID] = _coverLimit;
        return policyID;
    }
 
    /**
     * @notice Cancels the policy.
     * @param _policyholder The policyholder address.
     */
    function _cancel(address _policyholder) internal {
        uint256 policyID = policyOf[_policyholder];
        uint256 coverLimit = coverLimitOf[policyID];
        _updateActiveCoverLimit(coverLimit, 0);
 
        debtOf[_policyholder] += calculateCancelFee(policyID);
        coverLimitOf[policyID] = 0;
        emit PolicyCanceled(policyID);
    }
 
    /**
     * @notice Updates the Risk Manager on the current total cover limit purchased by policyholders.
     * @param _currentCoverLimit The current policyholder cover limit (0 if activating policy).
     * @param _newCoverLimit The new policyholder cover limit.
     */
    function _updateActiveCoverLimit(uint256 _currentCoverLimit, uint256 _newCoverLimit) internal {
        IRiskManager(riskManager).updateActiveCoverLimitForStrategy(address(this), _currentCoverLimit, _newCoverLimit);
    }
 
    /**
     * @notice Override _beforeTokenTransfer hook from ERC721 standard to ensure policies are non-transferable, and only one can be minted per user.
     * @dev This hook is called on mint, transfer and burn.
     * @param from sending address.
     * @param to receiving address.
     * @param tokenId tokenId.
     */
    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual override {
        super._beforeTokenTransfer(from, to, tokenId);
        require(from == address(0), "only minting permitted");
    }
 
    /**
     * @notice Sets registry and related contract addresses.
     * @param _registry The registry address to set.
    */
    function _setRegistry(address _registry) internal {
        // set registry
        require(_registry != address(0x0), "zero address registry");
        registry = _registry;
 
        // set risk manager
        (, address riskManagerAddr) = IRegistry(_registry).tryGet("riskManager");
        require(riskManagerAddr != address(0x0), "zero address riskmanager");
        riskManager = riskManagerAddr;
 
        // set scp
        (, address scpAddr) = IRegistry(_registry).tryGet("scp");
        require(scpAddr != address(0x0), "zero address scp");
        scp = scpAddr;
        emit RegistrySet(_registry);
    }
}