all files / contracts/ GEMS.sol

92.31% Statements 12/13
83.33% Branches 5/6
100% Functions 2/2
92.31% Lines 12/13
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                                                        14× 14× 14×         220× 206× 206×   206×   18×     18×     18×     18×   206×        
// SPDX-License-Identifier: BUSL-1.1
 
pragma solidity 0.8.3;
 
import "./abstract/AccessControlledAndUpgradeable.sol";
 
/** Contract giving user GEMS*/
 
// Inspired by https://github.com/andrecronje/rarity/blob/main/rarity.sol
 
/** @title GEMS */
contract GEMS is AccessControlledAndUpgradeable {
  bytes32 public constant GEM_ROLE = keccak256("GEM_ROLE");
 
  uint256 constant gems_per_day = 250e18;
  uint256 constant DAY = 1 days;
 
  mapping(address => uint256) public gems;
  mapping(address => uint256) public streak;
  mapping(address => uint256) public lastAction;
 
  event GemsCollected(address user, uint256 gems, uint256 streak);
 
  function initialize(
    address _admin,
    address _longShort,
    address _staker
  ) external initializer {
    _AccessControlledAndUpgradeable_init(_admin);
    _setupRole(GEM_ROLE, _longShort);
    _setupRole(GEM_ROLE, _staker);
  }
 
  // Say gm and get gems by performing an action in LongShort or Staker
  function gm(address user) external {
    if (hasRole(GEM_ROLE, msg.sender)) {
      uint256 usersLastAction = lastAction[user];
      uint256 blocktimestamp = block.timestamp;
 
      if (blocktimestamp - usersLastAction >= DAY) {
        // Award gems
        gems[user] += gems_per_day;
 
        // Increment streak
        Iif (blocktimestamp - usersLastAction < 2 * DAY) {
          streak[user] += 1;
        } else {
          streak[user] = 1; // reset streak to 1
        }
 
        lastAction[user] = blocktimestamp;
      }
      emit GemsCollected(user, gems[user], streak[user]);
    }
  }
}