github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/contracts/ens/contract/MatrixDeposit_new.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 getDepositList()constant public returns (address[]){
    63          return Depositors;
    64      }
    65      function getDepositInfo(address addr) constant public returns(uint,bytes,uint){
    66          bytes aaa;
    67          return (0,aaa,0);
    68      }
    69      function deposit(bytes nodeID) payable public{
    70          require(msg.value > 0);     // Check that something has been sent
    71          totalDeposit += msg.value;
    72          addDepositor(msg.sender,nodeID,msg.value);
    73      }
    74      /**
    75  * Adds a new depositor to the list.
    76  * @param _from the address of the depositor
    77  *        nodeID  the current depositor' nodeID
    78  **/
    79      function addDepositor(address _from,bytes nodeID, uint value) internal{
    80          depInfo storage dep = mapDeposites[_from];
    81          require(dep.state == StatePhases.deposit || dep.state == StatePhases.elect);
    82          if(dep.deposit == 0){
    83              if(numDepositors < Depositors.length){
    84                  Depositors[numDepositors] = _from;
    85              }
    86              else{
    87                  Depositors.push(_from);
    88              }
    89              numDepositors++;
    90          }
    91          dep.deposit += value;
    92          dep.NodeID = nodeID;
    93          dep.state = StatePhases.deposit;
    94          mapDeposites[_from] = dep;
    95          if(numDepositors < Depositors.length)
    96              Depositors[numDepositors] = _from;
    97          else
    98              Depositors.push(_from);
    99      }
   100  
   101      function withDraw() public{
   102          depInfo storage dep = mapDeposites[msg.sender];
   103          require(dep.deposit > 0 );
   104          require(dep.state == StatePhases.deposit);
   105          dep.state = StatePhases.withdraw;
   106          dep.withDrawHeight = block.number;
   107      }
   108      function refund() public{
   109          depInfo storage dep = mapDeposites[msg.sender];
   110          require(dep.deposit > 0 );
   111          require(dep.state == StatePhases.withdraw);
   112          require(dep.withDrawHeight+300 < block.number);
   113          msg.sender.transfer(dep.deposit);
   114          totalDeposit -= dep.deposit;
   115          dep.deposit = 0;
   116          dep.state = StatePhases.refund;
   117          dep.withDrawHeight = 0;
   118          mapDeposites[msg.sender] = dep;
   119          for (uint index = 0; index<numDepositors; index++) {
   120              if (Depositors[index] == msg.sender){
   121                  removeHolder(msg.sender,index);
   122                  break;
   123              }
   124          }
   125  
   126      }
   127      /**
   128      * Removes a depositor from the list.
   129      * @param _from the address of the depositor
   130      *        index  the index of the depositor
   131      **/
   132      function removeHolder(address _from, uint index) internal{
   133          require(Depositors[index] == _from);
   134          numDepositors -= 1;
   135          Depositors[index] = Depositors[numDepositors];
   136      }
   137  
   138  }