all files / contracts/ Stakable.sol

100% Statements 6/6
50% Branches 2/4
83.33% Functions 5/6
100% Lines 7/7
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                                  26× 26×           5×                       9×             22×             4× 4×      
// SPDX-License-Identifier: GPL-3.0-or-later
 
pragma solidity ^0.8.6;
 
import "openzeppelin-solidity/contracts/utils/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/access/Ownable.sol";
 
/// @title Library to implement items that can accumulate stake
/// @author Roberto García (http://rhizomik.net/~roberto/)
contract Stakable is Ownable {
  using SafeMath for uint256;
 
  address authorisedToken;
  mapping(string => uint256) public staked;
 
  /// @dev Modifier controlling that only the registered token can add stake
  modifier onlyAllowedToken() {
    Erequire(authorisedToken == msg.sender, "Only registered token can add stake");
    _;
  }
 
  /// @notice Sets the `token` address authorised to add and remove stake.
  /// @param token The address of the authorised token contract
  function setToken(address token) public onlyOwner {
    authorisedToken = token;
  }
 
  /// @notice Checks if the item with `hash` can have stake, depending on implementation.
  /// For instance, the item is already registered.
  /// @param hash Hash identifying the item, for instance IPFS Base58 Hash
  function isStakable(string memory hash) virtual public {}
 
  /// @notice Check if the stakable `hash` has some stake.
  /// @dev Used to check if the corresponding item has a current stake bigger than 0.
  /// @param hash Hash identifying the item to get stake, for instance IPFS Base58 Hash
  function isStaked(string memory hash) public view returns(bool) {
    return (staked[hash] > 0);
  }
 
  /// @notice Adds `amount` stake on the registered token to the item with `hash`.
  /// @param amount Amount of stake on the registered token to add
  /// @param hash Hash identifying the item to get stake, for instance IPFS Base58 Hash
  function addStake(uint256 amount, string memory hash) public onlyAllowedToken {
    staked[hash] += amount;
  }
 
  /// @notice Removes `amount` of stake on the registered token from the item with `hash`.
  /// @param amount Amount of stake on the registered token to remove
  /// @param hash Hash identifying the item to loose stake, for instance IPFS Base58 Hash
  function removeStake(uint256 amount, string memory hash) public onlyAllowedToken {
    Erequire(amount <= staked[hash], 'No more than the amount currently staked can be removed');
    staked[hash] -= amount;
  }
}