diff --git a/contracts/token/MiniMeToken.sol b/contracts/token/MiniMeToken.sol index 3855bf5..9d586ff 100644 --- a/contracts/token/MiniMeToken.sol +++ b/contracts/token/MiniMeToken.sol @@ -1,6 +1,7 @@ -pragma solidity ^0.4.11; +pragma solidity ^0.4.17; import "../common/Controlled.sol"; +import "./MiniMeTokenFactory.sol"; import "./TokenController.sol"; import "./ApproveAndCallFallBack.sol"; @@ -36,13 +37,13 @@ contract MiniMeToken is Controlled { string public name; //The Token's name: e.g. DigixDAO Tokens uint8 public decimals; //Number of decimals of the smallest unit string public symbol; //An identifier: e.g. REP - string public version = 'MMT_0.1'; //An arbitrary versioning scheme + string public version = "MMT_0.1"; //An arbitrary versioning scheme /// @dev `Checkpoint` is the structure that attaches a block number to a /// given value, the block number attached is the one that last changed the /// value - struct Checkpoint { + struct Checkpoint { // `fromBlock` is the block number that the value was generated from uint128 fromBlock; @@ -104,7 +105,9 @@ contract MiniMeToken is Controlled { uint8 _decimalUnits, string _tokenSymbol, bool _transfersEnabled - ) { + ) + public + { tokenFactory = MiniMeTokenFactory(_tokenFactory); name = _tokenName; // Set the name decimals = _decimalUnits; // Set the decimals @@ -112,7 +115,7 @@ contract MiniMeToken is Controlled { parentToken = MiniMeToken(_parentToken); parentSnapShotBlock = _parentSnapShotBlock; transfersEnabled = _transfersEnabled; - creationBlock = getBlockNumber(); + creationBlock = block.number; } @@ -124,8 +127,8 @@ contract MiniMeToken is Controlled { /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return Whether the transfer was successful or not - function transfer(address _to, uint256 _amount) returns (bool success) { - if (!transfersEnabled) throw; + function transfer(address _to, uint256 _amount) public returns (bool success) { + require(transfersEnabled); return doTransfer(msg.sender, _to, _amount); } @@ -135,18 +138,26 @@ contract MiniMeToken is Controlled { /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return True if the transfer was successful - function transferFrom(address _from, address _to, uint256 _amount - ) returns (bool success) { + function transferFrom( + address _from, + address _to, + uint256 _amount + ) + public + returns (bool success) + { // The controller of this contract can move tokens around at will, // this is important to recognize! Confirm that you trust the // controller of this contract, which in most situations should be // another open source smart contract or 0x0 if (msg.sender != controller) { - if (!transfersEnabled) throw; + require(transfersEnabled); // The standard ERC 20 transferFrom functionality - if (allowed[_from][msg.sender] < _amount) return false; + if (allowed[_from][msg.sender] < _amount) { + return false; + } allowed[_from][msg.sender] -= _amount; } return doTransfer(_from, _to, _amount); @@ -158,29 +169,34 @@ contract MiniMeToken is Controlled { /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return True if the transfer was successful - function doTransfer(address _from, address _to, uint _amount - ) internal returns(bool) { + function doTransfer( + address _from, + address _to, + uint _amount + ) + internal + returns(bool) + { if (_amount == 0) { return true; } - if (parentSnapShotBlock >= getBlockNumber()) throw; + require(parentSnapShotBlock < block.number); // Do not allow transfer to 0x0 or the token contract itself - if ((_to == 0) || (_to == address(this))) throw; + require((_to != 0) && (_to != address(this))); // If the amount being transfered is more than the balance of the // account the transfer returns false - var previousBalanceFrom = balanceOfAt(_from, getBlockNumber()); + var previousBalanceFrom = balanceOfAt(_from, block.number); if (previousBalanceFrom < _amount) { return false; } // Alerts the token controller of the transfer if (isContract(controller)) { - if (!TokenController(controller).onTransfer(_from, _to, _amount)) - throw; + require(TokenController(controller).onTransfer(_from, _to, _amount)); } // First update the balance array with the new value for the address @@ -189,8 +205,8 @@ contract MiniMeToken is Controlled { // Then update the balance array with the new value for the address // receiving the tokens - var previousBalanceTo = balanceOfAt(_to, getBlockNumber()); - if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow + var previousBalanceTo = balanceOfAt(_to, block.number); + require(previousBalanceTo + _amount >= previousBalanceTo); // Check for overflow updateValueAtNow(balances[_to], previousBalanceTo + _amount); // An event to make the transfer easy to find on the blockchain @@ -201,8 +217,8 @@ contract MiniMeToken is Controlled { /// @param _owner The address that's balance is being requested /// @return The balance of `_owner` at the current block - function balanceOf(address _owner) constant returns (uint256 balance) { - return balanceOfAt(_owner, getBlockNumber()); + function balanceOf(address _owner) public constant returns (uint256 balance) { + return balanceOfAt(_owner, block.number); } /// @notice `msg.sender` approves `_spender` to spend `_amount` tokens on @@ -211,19 +227,18 @@ contract MiniMeToken is Controlled { /// @param _spender The address of the account able to transfer the tokens /// @param _amount The amount of tokens to be approved for transfer /// @return True if the approval was successful - function approve(address _spender, uint256 _amount) returns (bool success) { - if (!transfersEnabled) throw; + function approve(address _spender, uint256 _amount) public returns (bool success) { + require(transfersEnabled); // To change the approve amount you first have to reduce the addresses` // allowance to zero by calling `approve(_spender,0)` if it is not // already 0 to mitigate the race condition described here: // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 - if ((_amount!=0) && (allowed[msg.sender][_spender] !=0)) throw; + require((_amount == 0) || (allowed[msg.sender][_spender] == 0)); // Alerts the token controller of the approve function call if (isContract(controller)) { - if (!TokenController(controller).onApprove(msg.sender, _spender, _amount)) - throw; + require(TokenController(controller).onApprove(msg.sender, _spender, _amount)); } allowed[msg.sender][_spender] = _amount; @@ -236,8 +251,14 @@ contract MiniMeToken is Controlled { /// @param _spender The address of the account able to transfer the tokens /// @return Amount of remaining tokens of _owner that _spender is allowed /// to spend - function allowance(address _owner, address _spender - ) constant returns (uint256 remaining) { + function allowance( + address _owner, + address _spender + ) + public + constant + returns (uint256 remaining) + { return allowed[_owner][_spender]; } @@ -248,9 +269,15 @@ contract MiniMeToken is Controlled { /// @param _spender The address of the contract able to transfer the tokens /// @param _amount The amount of tokens to be approved for transfer /// @return True if the function call was successful - function approveAndCall(address _spender, uint256 _amount, bytes _extraData - ) returns (bool success) { - if (!approve(_spender, _amount)) throw; + function approveAndCall( + address _spender, + uint256 _amount, + bytes _extraData + ) + public + returns (bool success) + { + require(approve(_spender, _amount)); ApproveAndCallFallBack(_spender).receiveApproval( msg.sender, @@ -264,8 +291,8 @@ contract MiniMeToken is Controlled { /// @dev This function makes it easy to get the total number of tokens /// @return The total number of tokens - function totalSupply() constant returns (uint) { - return totalSupplyAt(getBlockNumber()); + function totalSupply() public constant returns (uint) { + return totalSupplyAt(block.number); } @@ -277,8 +304,14 @@ contract MiniMeToken is Controlled { /// @param _owner The address from which the balance will be retrieved /// @param _blockNumber The block number when the balance is queried /// @return The balance at `_blockNumber` - function balanceOfAt(address _owner, uint _blockNumber) constant - returns (uint) { + function balanceOfAt( + address _owner, + uint _blockNumber + ) + public + constant + returns (uint) + { // These next few lines are used when the balance of the token is // requested before a check point was ever created for this token, it @@ -303,7 +336,7 @@ contract MiniMeToken is Controlled { /// @notice Total amount of tokens at a specific `_blockNumber`. /// @param _blockNumber The block number when the totalSupply is queried /// @return The total amount of tokens at `_blockNumber` - function totalSupplyAt(uint _blockNumber) constant returns(uint) { + function totalSupplyAt(uint _blockNumber) public constant returns(uint) { // These next few lines are used when the totalSupply of the token is // requested before a check point was ever created for this token, it @@ -344,8 +377,13 @@ contract MiniMeToken is Controlled { string _cloneTokenSymbol, uint _snapshotBlock, bool _transfersEnabled - ) returns(address) { - if (_snapshotBlock == 0) _snapshotBlock = getBlockNumber(); + ) + public + returns(address) + { + if (_snapshotBlock == 0) { + _snapshotBlock = block.number; + } MiniMeToken cloneToken = tokenFactory.createCloneToken( this, _snapshotBlock, @@ -370,13 +408,19 @@ contract MiniMeToken is Controlled { /// @param _owner The address that will be assigned the new tokens /// @param _amount The quantity of tokens generated /// @return True if the tokens are generated correctly - function generateTokens(address _owner, uint _amount - ) onlyController returns (bool) { - uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber()); - if (curTotalSupply + _amount < curTotalSupply) throw; // Check for overflow + function generateTokens( + address _owner, + uint _amount + ) + public + onlyController + returns (bool) + { + uint curTotalSupply = totalSupply(); + require(curTotalSupply + _amount >= curTotalSupply); // Check for overflow + uint previousBalanceTo = balanceOf(_owner); + require(previousBalanceTo + _amount >= previousBalanceTo); // Check for overflow updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount); - var previousBalanceTo = balanceOf(_owner); - if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow updateValueAtNow(balances[_owner], previousBalanceTo + _amount); Transfer(0, _owner, _amount); return true; @@ -387,13 +431,19 @@ contract MiniMeToken is Controlled { /// @param _owner The address that will lose the tokens /// @param _amount The quantity of tokens to burn /// @return True if the tokens are burned correctly - function destroyTokens(address _owner, uint _amount - ) onlyController returns (bool) { - uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber()); - if (curTotalSupply < _amount) throw; + function destroyTokens( + address _owner, + uint _amount + ) + public + onlyController + returns (bool) + { + uint curTotalSupply = totalSupply(); + require(curTotalSupply >= _amount); + uint previousBalanceFrom = balanceOf(_owner); + require(previousBalanceFrom >= _amount); updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount); - var previousBalanceFrom = balanceOf(_owner); - if (previousBalanceFrom < _amount) throw; updateValueAtNow(balances[_owner], previousBalanceFrom - _amount); Transfer(_owner, 0, _amount); return true; @@ -406,7 +456,7 @@ contract MiniMeToken is Controlled { /// @notice Enables token holders to transfer their tokens freely if true /// @param _transfersEnabled True if transfers are allowed in the clone - function enableTransfers(bool _transfersEnabled) onlyController { + function enableTransfers(bool _transfersEnabled) public onlyController { transfersEnabled = _transfersEnabled; } @@ -418,20 +468,31 @@ contract MiniMeToken is Controlled { /// @param checkpoints The history of values being queried /// @param _block The block number to retrieve the value at /// @return The number of tokens being queried - function getValueAt(Checkpoint[] storage checkpoints, uint _block - ) constant internal returns (uint) { - if (checkpoints.length == 0) return 0; + function getValueAt( + Checkpoint[] storage checkpoints, + uint _block + ) + constant + internal + returns (uint) + { + if (checkpoints.length == 0) { + return 0; + } // Shortcut for the actual value - if (_block >= checkpoints[checkpoints.length-1].fromBlock) + if (_block >= checkpoints[checkpoints.length-1].fromBlock) { return checkpoints[checkpoints.length-1].value; - if (_block < checkpoints[0].fromBlock) return 0; + } + if (_block < checkpoints[0].fromBlock) { + return 0; + } // Binary search of the value in the array uint min = 0; uint max = checkpoints.length-1; while (max > min) { - uint mid = (max + min + 1)/ 2; + uint mid = (max + min + 1) / 2; if (checkpoints[mid].fromBlock<=_block) { min = mid; } else { @@ -445,15 +506,14 @@ contract MiniMeToken is Controlled { /// `totalSupplyHistory` /// @param checkpoints The history of data being updated /// @param _value The new number of tokens - function updateValueAtNow(Checkpoint[] storage checkpoints, uint _value - ) internal { + function updateValueAtNow(Checkpoint[] storage checkpoints, uint _value) internal { if ((checkpoints.length == 0) - || (checkpoints[checkpoints.length -1].fromBlock < getBlockNumber())) { - Checkpoint newCheckPoint = checkpoints[ checkpoints.length++ ]; - newCheckPoint.fromBlock = uint128(getBlockNumber()); + || (checkpoints[checkpoints.length -1].fromBlock < block.number)) { + Checkpoint storage newCheckPoint = checkpoints[ checkpoints.length++ ]; + newCheckPoint.fromBlock = uint128(block.number); newCheckPoint.value = uint128(_value); } else { - Checkpoint oldCheckPoint = checkpoints[checkpoints.length-1]; + Checkpoint storage oldCheckPoint = checkpoints[checkpoints.length-1]; oldCheckPoint.value = uint128(_value); } } @@ -463,7 +523,9 @@ contract MiniMeToken is Controlled { /// @return True if `_addr` is a contract function isContract(address _addr) constant internal returns(bool) { uint size; - if (_addr == 0) return false; + if (_addr == 0){ + return false; + } assembly { size := extcodesize(_addr) } @@ -478,23 +540,9 @@ contract MiniMeToken is Controlled { /// @notice The fallback function: If the contract's controller has not been /// set to 0, then the `proxyPayment` method is called which relays the /// ether and creates tokens as described in the token controller contract - function () payable { - if (isContract(controller)) { - if (! TokenController(controller).proxyPayment.value(msg.value)(msg.sender)) - throw; - } else { - throw; - } - } - - -////////// -// Testing specific methods -////////// - - /// @notice This function is overridden by the test Mocks. - function getBlockNumber() internal constant returns (uint256) { - return block.number; + function () public payable { + require(isContract(controller)); + require(TokenController(controller).proxyPayment.value(msg.value)(msg.sender)); } ////////// @@ -505,13 +553,13 @@ contract MiniMeToken is Controlled { /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. - function claimTokens(address _token) onlyController { + function claimTokens(address _token) public onlyController { if (_token == 0x0) { controller.transfer(this.balance); return; } - ERC20Token token = ERC20Token(_token); + MiniMeToken token = MiniMeToken(_token); uint balance = token.balanceOf(this); token.transfer(controller, balance); ClaimedTokens(_token, controller, balance); @@ -520,7 +568,6 @@ contract MiniMeToken is Controlled { //////////////// // Events //////////////// - event ClaimedTokens(address indexed _token, address indexed _controller, uint _amount); event Transfer(address indexed _from, address indexed _to, uint256 _amount); event NewCloneToken(address indexed _cloneToken, uint _snapshotBlock);