github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/contracts/ens/contract/MatrixDeposit.sol (about)

     1  pragma solidity ^0.4.0;
     2  
     3  contract Owned {
     4      address public owner;
     5      modifier onlyOwner {
     6          require(msg.sender == owner);
     7          _;
     8      }
     9  
    10      function Owned() public {
    11          owner = msg.sender;
    12      }
    13  
    14      function changeOwner(address newOwner) onlyOwner public {
    15          owner = newOwner;
    16      }
    17  }
    18  library SafeMath {
    19  
    20      function mul(uint256 a, uint256 b) internal constant returns (uint256) {
    21          uint256 c = a * b;
    22          assert(a == 0 || c / a == b);
    23          return c;
    24      }
    25  
    26      function div(uint256 a, uint256 b) internal constant returns (uint256) {
    27          // assert(b > 0); // Solidity automatically throws when dividing by 0
    28          uint256 c = a / b;
    29          // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    30          return c;
    31      }
    32  
    33      function sub(uint256 a, uint256 b) internal constant returns (uint256) {
    34          assert(b <= a);
    35          return a - b;
    36      }
    37  
    38      function add(uint256 a, uint256 b) internal constant returns (uint256) {
    39          uint256 c = a + b;
    40          assert(c >= a);
    41          return c;
    42      }
    43  
    44  }
    45  contract MatrixDeposit is Owned {
    46      using SafeMath for uint256;
    47      enum StatePhases { deposit,elect, withdraw ,refund}
    48      struct depInfo {
    49          uint deposit;
    50          bytes NodeID;
    51          uint withDrawHeight;
    52          StatePhases state;
    53      }
    54      uint totalDeposit;
    55      /** the number of stake holders **/
    56      uint public numDepositors;
    57      /** List of all Depositors **/
    58      address[] public Depositors;
    59      /** Deposit per user address **/
    60      mapping(address => depInfo) public mapDeposites;
    61  
    62      function deposit(bytes nodeID) payable public{
    63          require(msg.value > 0);     // Check that something has been sent
    64          totalDeposit += msg.value;
    65          addDepositor(msg.sender,nodeID,msg.value);
    66      }
    67      /**
    68  * Adds a new depositor to the list.
    69  * @param _from the address of the depositor
    70  *        nodeID  the current depositor' nodeID
    71  **/
    72      function addDepositor(address _from,bytes nodeID, uint value) internal{
    73          depInfo storage dep = mapDeposites[_from];
    74          require(dep.state == StatePhases.deposit || dep.state == StatePhases.elect);
    75          if(dep.deposit == 0){
    76              if(numDepositors < Depositors.length){
    77                  Depositors[numDepositors] = _from;
    78              }
    79              else{
    80                  Depositors.push(_from);
    81              }
    82              numDepositors++;
    83          }
    84          dep.deposit += value;
    85          dep.NodeID = nodeID;
    86          dep.state = StatePhases.deposit;
    87          mapDeposites[_from] = dep;
    88          if(numDepositors < Depositors.length)
    89              Depositors[numDepositors] = _from;
    90          else
    91              Depositors.push(_from);
    92      }
    93  
    94      function elect() public {
    95  
    96      }
    97      function withDraw() public{
    98          depInfo storage dep = mapDeposites[msg.sender];
    99          require(dep.deposit > 0 );
   100          require(dep.state == StatePhases.deposit);
   101          dep.state = StatePhases.withdraw;
   102          dep.withDrawHeight = block.number;
   103      }
   104      function refund() public{
   105          depInfo storage dep = mapDeposites[msg.sender];
   106          require(dep.deposit > 0 );
   107          require(dep.state == StatePhases.withdraw);
   108          require(dep.withDrawHeight+300 < block.number);
   109          msg.sender.transfer(dep.deposit);
   110          totalDeposit -= dep.deposit;
   111          dep.deposit = 0;
   112          dep.state = StatePhases.refund;
   113          dep.withDrawHeight = 0;
   114          mapDeposites[msg.sender] = dep;
   115          for (uint index = 0; index<numDepositors; index++) {
   116              if (Depositors[index] == msg.sender){
   117                  removeHolder(msg.sender,index);
   118                  break;
   119              }
   120          }
   121  
   122      }
   123      /**
   124      * Removes a depositor from the list.
   125      * @param _from the address of the depositor
   126      *        index  the index of the depositor
   127      **/
   128      function removeHolder(address _from, uint index) internal{
   129          require(Depositors[index] == _from);
   130          numDepositors -= 1;
   131          Depositors[index] = Depositors[numDepositors];
   132      }
   133  
   134  }