|
| 1 | +pragma solidity 0.4.24; |
| 2 | + |
| 3 | +import "openzeppelin-solidity/contracts/math/SafeMath.sol"; |
| 4 | +import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; |
| 5 | +import "openzeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @title Micro Fragments ERC20 token |
| 9 | + * @notice https://www.fragments.org/protocol/ |
| 10 | + * |
| 11 | + * @dev Fragment balances are internally represented with a hidden denomination, 'gons'. We support |
| 12 | + * splitting the currency in expansion by changing the exchange rate between the hidden 'gon' |
| 13 | + * currency and the public 'fragments' currency. This exchange rate is determined by the |
| 14 | + * internal properties 'maxTotalGons' and 'totalSupply_'. |
| 15 | + * |
| 16 | + * Anytime there is division, there is a risk of numerical instability from rounding errors. In |
| 17 | + * order to minimize this risk, we adhere to the following guidelines: |
| 18 | + * - The conversion rate adopted is the number of gons that equals 1 fragment. The inverse |
| 19 | + * rate must not be used--maxTotalGons is always the numerator and totalSupply_ is |
| 20 | + * always the denominator. (i.e. If you want to convert gons to fragments instead of |
| 21 | + * multiplying by the inverse rate, you should divide by the normal rate) |
| 22 | + * - Gon balances converted into fragments are always rounded down (truncated). |
| 23 | + * - Fragment values converted to gon values (such as in transfers) are chosen such at the |
| 24 | + * below guarantees are upheld. |
| 25 | + * |
| 26 | + * We make the following guarantees: |
| 27 | + * - If address 'A' transfers x fragments to address 'B'. A's resulting external balance will |
| 28 | + * be decreased by precisely x fragments, and B's external balance will be precisely |
| 29 | + * increased by x fragments. |
| 30 | + * |
| 31 | + * We do not guarantee that the sum of all balances equals the result of calling totalSupply(). |
| 32 | + * This is because, for any conversion function 'f()' that has non-zero rounding error, |
| 33 | + * f(x0) + f(x1) + ... + f(xn) is not always equal to f(x0 + x1 + ... xn). |
| 34 | + * |
| 35 | + * 'The Introduction of the Euro and the Rounding of Currency Amounts (1999)' is a good starting |
| 36 | + * reference for practices related to currency conversions. |
| 37 | + * http://ec.europa.eu/economy_finance/publications/pages/publication1224_en.pdf |
| 38 | + */ |
| 39 | +contract MicroFragments is DetailedERC20("Fragments", "mFRGD", 2), Ownable { |
| 40 | + using SafeMath for uint256; |
| 41 | + |
| 42 | + event Rebase(uint256 indexed epoch, int256 supplyDelta); |
| 43 | + |
| 44 | + uint256 private totalSupply_ = 1000; |
| 45 | + |
| 46 | + mapping(address => uint256) private gonBalances; |
| 47 | + |
| 48 | + // These two numbers determine the gons-fragments exchange rate. (numerator and denominator, |
| 49 | + //respectively). The effective exchange rate ONLY changes on expansion. |
| 50 | + uint256 private maxTotalGons = 1 << 170; |
| 51 | + |
| 52 | + // This is denominated in Fragments, because the gons-fragments conversion might change before |
| 53 | + // it's fully paid. |
| 54 | + mapping (address => mapping (address => uint256)) internal allowedFragments; |
| 55 | + |
| 56 | + uint256 public epoch = 0; |
| 57 | + |
| 58 | + constructor() public { |
| 59 | + gonBalances[msg.sender] = maxTotalGons; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dev Notifies Fragments contract about a new rebase cycle. |
| 64 | + * @param supplyDelta The number of new fragment tokens to add into circulation/remove from circulation. |
| 65 | + */ |
| 66 | + function rebase(int256 supplyDelta) public onlyOwner { |
| 67 | + if (supplyDelta < 0) { |
| 68 | + totalSupply_ = totalSupply_.sub(uint256(supplyDelta)); |
| 69 | + require(totalSupply_ > 0); |
| 70 | + } else { |
| 71 | + totalSupply_ = totalSupply_.add(uint256(supplyDelta)); |
| 72 | + } |
| 73 | + |
| 74 | + epoch++; |
| 75 | + emit Rebase(epoch, supplyDelta); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return The total number of fragments. |
| 80 | + */ |
| 81 | + function totalSupply() public view returns (uint256) { |
| 82 | + return totalSupply_; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param who The address to query. |
| 87 | + * @return The balance of the specified address. |
| 88 | + */ |
| 89 | + function balanceOf(address who) public view returns (uint256) { |
| 90 | + return gonBalances[who].div(maxTotalGons.div(totalSupply_)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @dev Transfer tokens to a specified address. |
| 95 | + * @param to The address to transfer to. |
| 96 | + * @param value The amount to be transferred. |
| 97 | + * @return True on success, false otherwise. |
| 98 | + */ |
| 99 | + function transfer(address to, uint256 value) public returns (bool) { |
| 100 | + transferHelper(msg.sender, to, value); |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @dev Function to check the amount of tokens that an owner has allowed to a spender. |
| 106 | + * @param owner The address which owns the funds. |
| 107 | + * @param spender The address which will spend the funds. |
| 108 | + * @return The number of tokens still available for the spender. |
| 109 | + */ |
| 110 | + function allowance(address owner, address spender) public view returns (uint256) { |
| 111 | + return allowedFragments[owner][spender]; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @dev Transfer tokens from one address to another. |
| 116 | + * @param from The address you want to send tokens from. |
| 117 | + * @param to The address you want to transfer to. |
| 118 | + * @param value The amount of tokens to be transferred. |
| 119 | + */ |
| 120 | + function transferFrom(address from, address to, uint256 value) public returns (bool) { |
| 121 | + require(value <= allowedFragments[from][msg.sender]); |
| 122 | + allowedFragments[from][msg.sender] = allowedFragments[from][msg.sender].sub(value); |
| 123 | + transferHelper(from, to, value); |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender. |
| 129 | + * |
| 130 | + * Beware that changing an allowance with this method brings the risk that someone may use both the old |
| 131 | + * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this |
| 132 | + * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: |
| 133 | + * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 |
| 134 | + * @param spender The address which will spend the funds. |
| 135 | + * @param value The amount of tokens to be spent. |
| 136 | + */ |
| 137 | + function approve(address spender, uint256 value) public returns (bool) { |
| 138 | + allowedFragments[msg.sender][spender] = value; |
| 139 | + emit Approval(msg.sender, spender, value); |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @dev Increase the amount of tokens that an owner has allowed to a spender. |
| 145 | + * |
| 146 | + * approve should be called when allowed[_spender] == 0. To increment |
| 147 | + * allowed value is better to use this function to avoid 2 calls (and wait until |
| 148 | + * the first transaction is mined) |
| 149 | + * From MonolithDAO Token.sol |
| 150 | + * @param spender The address which will spend the funds. |
| 151 | + * @param addedValue The amount of tokens to increase the allowance by. |
| 152 | + */ |
| 153 | + function increaseApproval(address spender, uint addedValue) public returns (bool) { |
| 154 | + allowedFragments[msg.sender][spender] = allowedFragments[msg.sender][spender].add(addedValue); |
| 155 | + emit Approval(msg.sender, spender, allowedFragments[msg.sender][spender]); |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @dev Decrease the amount of tokens that an owner has allowed to a spender. |
| 161 | + * |
| 162 | + * approve should be called when allowed[_spender] == 0. To decrement |
| 163 | + * allowed value is better to use this function to avoid 2 calls (and wait until |
| 164 | + * the first transaction is mined) |
| 165 | + * From MonolithDAO Token.sol |
| 166 | + * @param spender The address which will spend the funds. |
| 167 | + * @param subtractedValue The amount of tokens to decrease the allowance by. |
| 168 | + */ |
| 169 | + function decreaseApproval(address spender, uint subtractedValue) public returns (bool) { |
| 170 | + uint oldValue = allowedFragments[msg.sender][spender]; |
| 171 | + if (subtractedValue > oldValue) { |
| 172 | + allowedFragments[msg.sender][spender] = 0; |
| 173 | + } else { |
| 174 | + allowedFragments[msg.sender][spender] = oldValue.sub(subtractedValue); |
| 175 | + } |
| 176 | + emit Approval(msg.sender, spender, allowedFragments[msg.sender][spender]); |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @dev Transfers a number of gons between from and to, such that the resulting balances match |
| 182 | + * the expectations when denominated in fragments. |
| 183 | + */ |
| 184 | + function transferHelper(address from, address to, uint256 value) private { |
| 185 | + require(to != address(0)); |
| 186 | + |
| 187 | + uint256 gonsPerFragment = maxTotalGons.div(totalSupply_); |
| 188 | + uint256 senderMod = gonBalances[from] % gonsPerFragment; |
| 189 | + uint256 receiverMod = gonBalances[to] % gonsPerFragment; |
| 190 | + uint256 baseAmt = value.mul(gonsPerFragment); |
| 191 | + |
| 192 | + uint256 senderGonMinAmt = baseAmt.sub(gonsPerFragment.sub(senderMod).sub(1)); |
| 193 | + uint256 senderGonMaxAmt = baseAmt.add(senderMod); |
| 194 | + uint256 receiverGonMinAmt = baseAmt.sub(receiverMod); |
| 195 | + uint256 receiverGonMaxAmt = baseAmt.add(gonsPerFragment.sub(receiverMod).sub(1)); |
| 196 | + |
| 197 | + // Choose the max of the minimum viable transfer amounts on each side. |
| 198 | + uint256 gonValue = (senderGonMinAmt >= receiverGonMinAmt) ? senderGonMinAmt : receiverGonMinAmt; |
| 199 | + |
| 200 | + assert(gonValue <= senderGonMaxAmt); |
| 201 | + assert(gonValue <= receiverGonMaxAmt); |
| 202 | + |
| 203 | + require(gonValue <= gonBalances[from]); |
| 204 | + gonBalances[from] = gonBalances[from].sub(gonValue); |
| 205 | + gonBalances[to] = gonBalances[to].add(gonValue); |
| 206 | + emit Transfer(from, to, value); |
| 207 | + } |
| 208 | +} |
0 commit comments