all files / contracts/staking/ xsLocker.sol

100% Statements 83/83
100% Branches 22/22
100% Functions 22/22
100% Lines 81/81
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                                                                                                                              46× 45×                         5140×                                                           319× 319× 319× 315× 315×   319×                                                   900×   898×                                   29×   28×   27×                     964×   960× 960×                             15×   14×   13× 13×                     167× 166× 165×                     288× 288×   250×                       100× 99×   94×                     55× 55× 55× 94× 94× 92× 91× 91×     49×                             925× 925×   925×   923× 923× 820×                     1138× 1138×   1138× 1137× 1137× 969×                   478×   458× 408× 400×     50× 50× 50× 50× 50×   442×                           1384× 1384×   1384×       71× 67×                               2567× 2536× 1691×                           60× 60×                 156× 117×                      
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.6;
 
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableMap.sol";
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
import "./../utils/ERC721Enhanced.sol";
import "./../utils/Governable.sol";
import "./../interfaces/staking/IxsListener.sol";
import "./../interfaces/staking/IxsLocker.sol";
 
 
/**
 * @title xsLocker
 * @author solace.fi
 * @notice Stake your [**SOLACE**](./../SOLACE) to receive voting rights, [**SOLACE**](./../SOLACE) rewards, and more.
 *
 * Locks are ERC721s and can be viewed with [`locks()`](#locks). Each lock has an `amount` of [**SOLACE**](./../SOLACE) and an `end` timestamp and cannot be transferred or withdrawn from before it unlocks. Locks have a maximum duration of four years.
 *
 * Users can create locks via [`createLock()`](#createlock) or [`createLockSigned()`](#createlocksigned), deposit more [**SOLACE**](./../SOLACE) into a lock via [`increaseAmount()`](#increaseamount) or [`increaseAmountSigned()`](#increaseamountsigned), extend a lock via [`extendLock()`](#extendlock), and withdraw via [`withdraw()`](#withdraw), [`withdrawInPart()`](#withdrawinpart), or [`withdrawMany()`](#withdrawmany).
 *
 * Users and contracts (eg BondTellers) may deposit on behalf of another user or contract.
 *
 * Any time a lock is updated it will notify the listener contracts (eg StakingRewards).
 *
 * Note that transferring [**SOLACE**](./../SOLACE) to this contract will not give you any rewards. You should deposit your [**SOLACE**](./../SOLACE) via [`createLock()`](#createlock) or [`createLockSigned()`](#createlocksigned).
 */
// solhint-disable-next-line contract-name-camelcase
contract xsLocker is IxsLocker, ERC721Enhanced, ReentrancyGuard, Governable {
    using EnumerableSet for EnumerableSet.AddressSet;
 
    /***************************************
    GLOBAL VARIABLES
    ***************************************/
 
    /// @notice [**SOLACE**](./../SOLACE) token.
    address public override solace;
 
    /// @notice The maximum time into the future that a lock can expire.
    uint256 public constant override MAX_LOCK_DURATION = 4 * (365 days);
 
    /// @notice The total number of locks that have been created.
    uint256 public override totalNumLocks;
 
    // Info on locks
    mapping(uint256 => Lock) private _locks;
 
    // Contracts that listen for lock changes
    EnumerableSet.AddressSet private _xsLockListeners;
 
    /**
     * @notice Construct the xsLocker contract.
     * @param governance_ The address of the [governor](/docs/protocol/governance).
     * @param solace_ Address of [**SOLACE**](./../SOLACE).
     */
    constructor(address governance_, address solace_)
        ERC721Enhanced("xsolace lock", "xsLOCK")
        Governable(governance_)
    {
        require(solace_ != address(0x0), "zero address solace");
        solace = solace_;
    }
 
    /***************************************
    VIEW FUNCTIONS
    ***************************************/
 
    /**
     * @notice Information about a lock.
     * @param xsLockID The ID of the lock to query.
     * @return lock_ Information about the lock.
     */
    function locks(uint256 xsLockID) external view override tokenMustExist(xsLockID) returns (Lock memory lock_) {
        return _locks[xsLockID];
    }
 
    /**
     * @notice Determines if the lock is locked.
     * @param xsLockID The ID of the lock to query.
     * @return locked True if the lock is locked, false if unlocked.
     */
    function isLocked(uint256 xsLockID) external view override tokenMustExist(xsLockID) returns (bool locked) {
        // solhint-disable-next-line not-rely-on-time
        return _locks[xsLockID].end > block.timestamp;
    }
 
    /**
     * @notice Determines the time left until the lock unlocks.
     * @param xsLockID The ID of the lock to query.
     * @return time The time left in seconds, 0 if unlocked.
     */
    function timeLeft(uint256 xsLockID) external view override tokenMustExist(xsLockID) returns (uint256 time) {
        // solhint-disable-next-line not-rely-on-time
        return (_locks[xsLockID].end > block.timestamp)
            // solhint-disable-next-line not-rely-on-time
            ? _locks[xsLockID].end - block.timestamp // locked
            : 0; // unlocked
    }
 
    /**
     * @notice Returns the amount of [**SOLACE**](./../SOLACE) the user has staked.
     * @param account The account to query.
     * @return balance The user's balance.
     */
    function stakedBalance(address account) external view override returns (uint256 balance) {
        uint256 numOfLocks = balanceOf(account);
        balance = 0;
        for (uint256 i = 0; i < numOfLocks; i++) {
            uint256 xsLockID = tokenOfOwnerByIndex(account, i);
            balance += _locks[xsLockID].amount;
        }
        return balance;
    }
 
    /**
     * @notice The list of contracts that are listening to lock updates.
     * @return listeners_ The list as an array.
     */
    function getXsLockListeners() external view override returns (address[] memory listeners_) {
        uint256 len = _xsLockListeners.length();
        listeners_ = new address[](len);
        for(uint256 index = 0; index < len; index++) {
            listeners_[index] = _xsLockListeners.at(index);
        }
        return listeners_;
    }
 
    /***************************************
    MUTATOR FUNCTIONS
    ***************************************/
 
    /**
     * @notice Deposit [**SOLACE**](./../SOLACE) to create a new lock.
     * @dev [**SOLACE**](./../SOLACE) is transferred from msg.sender, assumes its already approved.
     * @dev use end=0 to initialize as unlocked.
     * @param recipient The account that will receive the lock.
     * @param amount The amount of [**SOLACE**](./../SOLACE) to deposit.
     * @param end The timestamp the lock will unlock.
     * @return xsLockID The ID of the newly created lock.
     */
    function createLock(address recipient, uint256 amount, uint256 end) external override nonReentrant returns (uint256 xsLockID) {
        // pull solace
        SafeERC20.safeTransferFrom(IERC20(solace), msg.sender, address(this), amount);
        // accounting
        return _createLock(recipient, amount, end);
    }
 
    /**
     * @notice Deposit [**SOLACE**](./../SOLACE) to create a new lock.
     * @dev [**SOLACE**](./../SOLACE) is transferred from msg.sender using ERC20Permit.
     * @dev use end=0 to initialize as unlocked.
     * @dev recipient = msg.sender
     * @param amount The amount of [**SOLACE**](./../SOLACE) to deposit.
     * @param end The timestamp the lock will unlock.
     * @param deadline Time the transaction must go through before.
     * @param v secp256k1 signature
     * @param r secp256k1 signature
     * @param s secp256k1 signature
     * @return xsLockID The ID of the newly created lock.
     */
    function createLockSigned(uint256 amount, uint256 end, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external override nonReentrant returns (uint256 xsLockID) {
        // permit
        IERC20Permit(solace).permit(msg.sender, address(this), amount, deadline, v, r, s);
        // pull solace
        SafeERC20.safeTransferFrom(IERC20(solace), msg.sender, address(this), amount);
        // accounting
        return _createLock(msg.sender, amount, end);
    }
 
    /**
     * @notice Deposit [**SOLACE**](./../SOLACE) to increase the value of an existing lock.
     * @dev [**SOLACE**](./../SOLACE) is transferred from msg.sender, assumes its already approved.
     * @param xsLockID The ID of the lock to update.
     * @param amount The amount of [**SOLACE**](./../SOLACE) to deposit.
     */
    function increaseAmount(uint256 xsLockID, uint256 amount) external override nonReentrant tokenMustExist(xsLockID) {
        // pull solace
        SafeERC20.safeTransferFrom(IERC20(solace), msg.sender, address(this), amount);
        // accounting
        uint256 newAmount = _locks[xsLockID].amount + amount;
        _updateLock(xsLockID, newAmount, _locks[xsLockID].end);
    }
 
    /**
     * @notice Deposit [**SOLACE**](./../SOLACE) to increase the value of an existing lock.
     * @dev [**SOLACE**](./../SOLACE) is transferred from msg.sender using ERC20Permit.
     * @param xsLockID The ID of the lock to update.
     * @param amount The amount of [**SOLACE**](./../SOLACE) to deposit.
     * @param deadline Time the transaction must go through before.
     * @param v secp256k1 signature
     * @param r secp256k1 signature
     * @param s secp256k1 signature
     */
    function increaseAmountSigned(uint256 xsLockID, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external override nonReentrant tokenMustExist(xsLockID) {
        // permit
        IERC20Permit(solace).permit(msg.sender, address(this), amount, deadline, v, r, s);
        // pull solace
        SafeERC20.safeTransferFrom(IERC20(solace), msg.sender, address(this), amount);
        // accounting
        uint256 newAmount = _locks[xsLockID].amount + amount;
        _updateLock(xsLockID, newAmount, _locks[xsLockID].end);
    }
 
    /**
     * @notice Extend a lock's duration.
     * @dev Can only be called by the lock owner or approved.
     * @param xsLockID The ID of the lock to update.
     * @param end The new time for the lock to unlock.
     */
    function extendLock(uint256 xsLockID, uint256 end) external override nonReentrant onlyOwnerOrApproved(xsLockID) {
        // solhint-disable-next-line not-rely-on-time
        require(end <= block.timestamp + MAX_LOCK_DURATION, "Max lock is 4 years");
        require(_locks[xsLockID].end <= end, "not extended");
        _updateLock(xsLockID, _locks[xsLockID].amount, end);
    }
 
    /**
     * @notice Withdraw from a lock in full.
     * @dev Can only be called by the lock owner or approved.
     * @dev Can only be called if unlocked.
     * @param xsLockID The ID of the lock to withdraw from.
     * @param recipient The user to receive the lock's [**SOLACE**](./../SOLACE).
     */
    function withdraw(uint256 xsLockID, address recipient) external override nonReentrant onlyOwnerOrApproved(xsLockID) {
        uint256 amount = _locks[xsLockID].amount;
        _withdraw(xsLockID, amount);
        // transfer solace
        SafeERC20.safeTransfer(IERC20(solace), recipient, amount);
    }
 
    /**
     * @notice Withdraw from a lock in part.
     * @dev Can only be called by the lock owner or approved.
     * @dev Can only be called if unlocked.
     * @param xsLockID The ID of the lock to withdraw from.
     * @param recipient The user to receive the lock's [**SOLACE**](./../SOLACE).
     * @param amount The amount of [**SOLACE**](./../SOLACE) to withdraw.
     */
    function withdrawInPart(uint256 xsLockID, address recipient, uint256 amount) external override nonReentrant onlyOwnerOrApproved(xsLockID) {
        require(amount <= _locks[xsLockID].amount, "excess withdraw");
        _withdraw(xsLockID, amount);
        // transfer solace
        SafeERC20.safeTransfer(IERC20(solace), recipient, amount);
    }
 
    /**
     * @notice Withdraw from multiple locks in full.
     * @dev Can only be called by the lock owner or approved.
     * @dev Can only be called if unlocked.
     * @param xsLockIDs The ID of the locks to withdraw from.
     * @param recipient The user to receive the lock's [**SOLACE**](./../SOLACE).
     */
    function withdrawMany(uint256[] calldata xsLockIDs, address recipient) external override nonReentrant {
        uint256 len = xsLockIDs.length;
        uint256 amount = 0;
        for(uint256 i = 0; i < len; i++) {
            uint256 xsLockID = xsLockIDs[i];
            require(_isApprovedOrOwner(msg.sender, xsLockID), "only owner or approved");
            uint256 amount_ = _locks[xsLockID].amount;
            amount += amount_;
            _withdraw(xsLockID, amount_);
        }
        // transfer solace
        SafeERC20.safeTransfer(IERC20(solace), recipient, amount);
    }
 
    /***************************************
    HELPER FUNCTIONS
    ***************************************/
 
    /**
     * @notice Creates a new lock.
     * @param recipient The user that the lock will be minted to.
     * @param amount The amount of [**SOLACE**](./../SOLACE) in the lock.
     * @param end The end of the lock.
     * @param xsLockID The ID of the new lock.
     */
    function _createLock(address recipient, uint256 amount, uint256 end) internal returns (uint256 xsLockID) {
        xsLockID = ++totalNumLocks;
        Lock memory newLock = Lock(amount, end);
        // solhint-disable-next-line not-rely-on-time
        require(newLock.end <= block.timestamp + MAX_LOCK_DURATION, "Max lock is 4 years");
        // accounting
        _locks[xsLockID] = newLock;
        _safeMint(recipient, xsLockID);
        emit LockCreated(xsLockID);
    }
 
    /**
     * @notice Updates an existing lock.
     * @param xsLockID The ID of the lock to update.
     * @param amount The amount of [**SOLACE**](./../SOLACE) now in the lock.
     * @param end The end of the lock.
     */
    function _updateLock(uint256 xsLockID, uint256 amount, uint256 end) internal {
        // checks
        Lock memory prevLock = _locks[xsLockID];
        Lock memory newLock = Lock(amount, end); // end was sanitized before passed in
        // accounting
        _locks[xsLockID] = newLock;
        address owner = ownerOf(xsLockID);
        _notify(xsLockID, owner, owner, prevLock, newLock);
        emit LockUpdated(xsLockID, amount, newLock.end);
    }
 
    /**
     * @notice Withdraws from a lock.
     * @param xsLockID The ID of the lock to withdraw from.
     * @param amount The amount of [**SOLACE**](./../SOLACE) to withdraw.
     */
    function _withdraw(uint256 xsLockID, uint256 amount) internal {
        // solhint-disable-next-line not-rely-on-time
        require(_locks[xsLockID].end <= block.timestamp, "locked"); // cannot withdraw while locked
        // accounting
        if(amount == _locks[xsLockID].amount) {
            _burn(xsLockID);
            delete _locks[xsLockID];
        }
        else {
            Lock memory oldLock = _locks[xsLockID];
            Lock memory newLock = Lock(oldLock.amount-amount, oldLock.end);
            _locks[xsLockID].amount -= amount;
            address owner = ownerOf(xsLockID);
            _notify(xsLockID, owner, owner, oldLock, newLock);
        }
        emit Withdrawl(xsLockID, amount);
    }
 
    /**
     * @notice Hook that is called after any token transfer. This includes minting and burning.
     * @param from The user that sends the token, or zero if minting.
     * @param to The zero that receives the token, or zero if burning.
     * @param xsLockID The ID of the token being transferred.
     */
    function _afterTokenTransfer(
        address from,
        address to,
        uint256 xsLockID
    ) internal override {
        super._afterTokenTransfer(from, to, xsLockID);
        Lock memory lock = _locks[xsLockID];
        // notify listeners
        if(from == address(0x0)) _notify(xsLockID, from, to, Lock(0, 0), lock); // mint
        else if(to == address(0x0)) _notify(xsLockID, from, to, lock, Lock(0, 0)); // burn
        else { // transfer
            // solhint-disable-next-line not-rely-on-time
            require(lock.end <= block.timestamp, "locked"); // cannot transfer while locked
            _notify(xsLockID, from, to, lock, lock);
        }
    }
 
    /**
     * @notice Notify the listeners of any updates.
     * @dev Called on transfer, mint, burn, and update.
     * Either the owner will change or the lock will change, not both.
     * @param xsLockID The ID of the lock that was altered.
     * @param oldOwner The old owner of the lock.
     * @param newOwner The new owner of the lock.
     * @param oldLock The old lock data.
     * @param newLock The new lock data.
     */
    function _notify(uint256 xsLockID, address oldOwner, address newOwner, Lock memory oldLock, Lock memory newLock) internal {
        // register action with listener
        uint256 len = _xsLockListeners.length();
        for(uint256 i = 0; i < len; i++) {
            IxsListener(_xsLockListeners.at(i)).registerLockEvent(xsLockID, oldOwner, newOwner, oldLock, newLock);
        }
    }
 
    /***************************************
    GOVERNANCE FUNCTIONS
    ***************************************/
 
    /**
     * @notice Adds a listener.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param listener The listener to add.
     */
    function addXsLockListener(address listener) external override onlyGovernance {
        _xsLockListeners.add(listener);
        emit xsLockListenerAdded(listener);
    }
 
    /**
     * @notice Removes a listener.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param listener The listener to remove.
     */
    function removeXsLockListener(address listener) external override onlyGovernance {
        _xsLockListeners.remove(listener);
        emit xsLockListenerRemoved(listener);
    }
 
    /**
     * @notice Sets the base URI for computing `tokenURI`.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param baseURI_ The new base URI.
     */
    function setBaseURI(string memory baseURI_) external override onlyGovernance {
        _setBaseURI(baseURI_);
    }
}