all files / contracts/ CLYToken.sol

95.83% Statements 46/48
90.91% Branches 20/22
100% Functions 16/16
96.08% Lines 49/51
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                                                                                                        8× 8× 8×       7×         26×   24× 24× 19×   24×         30× 28×   26×   24×   22×   22× 22× 19×     22× 22×   22× 22×   22×   22×         6×   4×           6×   4×   4×   4× 4× 4× 4×   4×   4×       1×         1×         3×       3× 1×       1×       2×       2×       11× 9×       46× 46×       12× 10× 6×      
// SPDX-License-Identifier: GPL-3.0-or-later
 
pragma solidity ^0.8.6;
 
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-solidity/contracts/utils/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/access/Ownable.sol";
import "openzeppelin-solidity/contracts/security/Pausable.sol";
import "./bondingcurve/BancorFormula.sol";
import "./Stakable.sol";
 
/**
 * @title CopyrightLY Token (CLY)
 * @dev Token with bonding curve contract based on Bancor formula
 * inspired by Commons Stack:
 * https://github.com/commons-stack/genesis-contracts/tree/master/contracts/bondingcurve
 */
contract CLYToken is ERC20, BancorFormula, Pausable, Ownable {
  using SafeMath for uint256;
 
  // Helper to represent fractions => 100% = 1000000, 1% = 10000
  uint256 constant DENOMINATOR_PPM = 1000000;
  uint256 public poolBalance;
 
  /*
    reserve ratio, represented in ppm, 1-1000000
    1/3 corresponds to y= multiple * x^2
    1/2 corresponds to y= multiple * x
    2/3 corresponds to y= multiple * x^1/2
    multiple will depends on contract initialization,
    specifically totalAmount and poolBalance parameters
    we might want to add an 'initialize' function that will allow
    the owner to send ether to the contract and mint a given amount of tokens
  */
  uint32 public reserveRatio;
  uint32 public initialSlope;
 
  /*
    Front-running attacks are currently mitigated by defining a maximum
    gas price limit which tries to prevent users from having control over the order of execution
  */
  uint256 public gasPrice; // maximum gas price for Bancor transactions
 
  mapping(string => mapping(address => uint256)) public individualStakes;
 
  event Minted(address buyer, uint256 amount, uint256 payed, address stakable, string item);
  event Burned(address seller, uint256 amount, uint256 earned, address stakable, string item);
  event CurvePurchase(uint256 supply, uint256 balance, uint256 amount, uint256 price);
  event CurveSale(uint256 supply, uint256 balance, uint256 amount, uint256 price);
 
  constructor(uint32 _reserveRatio, uint32 _initialSlope, uint256 _gasPrice) mustBeInPPM(_reserveRatio)
  ERC20("CopyrightLY Token", "CLY") payable {
    reserveRatio = _reserveRatio;
    initialSlope = _initialSlope;
    gasPrice = _gasPrice;
  }
 
  function decimals() public view virtual override returns (uint8) {
    return 16;
  }
 
  function getMintPrice(uint256 amount) public view
  returns (uint256) {
    require(amount > 0, "Must specify a non-zero amount of CLY");
 
    uint256 price = amount.mul(initialSlope); // Use default curve slope when poolBalance = 0
    if (poolBalance > 0) {
      price = purchaseTargetAmount(totalSupply(), poolBalance, reserveRatio, amount);
    }
    return price;
  }
 
  function mint(uint256 amount, uint256 maxPrice, address stakable, string memory item) public payable
  validGasPrice() whenNotPaused() returns (uint256) {
    require(maxPrice != 0 && msg.value == maxPrice, "Incorrect Ether value sent");
    Stakable(stakable).isStakable(item);
 
    uint256 price = getMintPrice(amount);
 
    require(price <= maxPrice, "Current price exceeds maximum provided");
 
    emit CurvePurchase(totalSupply(), poolBalance, amount, price);
 
    uint256 refund = maxPrice.sub(price);
    if (refund > 0) {
      payable(msg.sender).transfer(refund);
    }
 
    poolBalance = poolBalance.add(price);
    _mint(msg.sender, amount);
 
    individualStakes[item][msg.sender] += amount;
    Stakable(stakable).addStake(amount, item);
 
    emit Minted(msg.sender, amount, price, stakable, item);
 
    return price;
  }
 
  function getBurnPrice(uint256 amount) public view
  returns (uint256) {
    require(amount > 0, "Must specify a non-zero amount of CLY");
 
    return saleTargetAmount(totalSupply(), poolBalance, reserveRatio, amount);
  }
 
  function burn(uint256 amount, address stakable, string memory item) public
  validGasPrice() whenNotPaused() validBurn(stakable, item, amount) returns (uint256) {
 
    uint256 price = getBurnPrice(amount);
 
    emit CurveSale(totalSupply(), poolBalance, amount, price);
 
    payable(msg.sender).transfer(price);
 
    poolBalance = poolBalance.sub(price);
    _burn(msg.sender, amount);
    individualStakes[item][msg.sender] -= amount;
    Stakable(stakable).removeStake(amount, item);
 
    emit Burned(msg.sender, amount, price, stakable, item);
 
    return price;
  }
 
  function transfer(address, uint256) public pure override returns (bool) {
    Irequire(false, "CLY Token is not transferable");
    return false;
  }
 
  function transferFrom(address, address, uint256) public pure override returns (bool) {
    Irequire(false, "CLY Token is not transferable");
    return false;
  }
 
  function getIndividualStake(string memory item, address staker) public view returns (uint256) {
    return individualStakes[item][staker];
  }
 
  function setGasPrice(uint256 _gasPrice) public onlyOwner {
    require(_gasPrice > 0, "Maximum gas price should be bigger than 0");
    gasPrice = _gasPrice;
  }
 
  function setReserveRatio(uint32 _reserveRatio) public onlyOwner mustBeInPPM(_reserveRatio) {
    reserveRatio = _reserveRatio;
  }
 
  function pause() public onlyOwner {
    super._pause();
  }
 
  function unpause() public onlyOwner {
    super._unpause();
  }
 
  modifier mustBeInPPM(uint256 _val) {
    require(_val <= DENOMINATOR_PPM, "Value must be in PPM");
    _;
  }
 
  modifier validGasPrice() {
    assert(tx.gasprice <= gasPrice);
    _;
  }
 
  modifier validBurn(address stakable, string memory item, uint256 amount) {
    Stakable(stakable).isStakable(item);
    require(individualStakes[item][msg.sender] >= amount, "Holder hasn't enough CLY to unstake");
    _;
  }
}