Guest User

Untitled

a guest
Nov 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. pragma solidity ^0.4.18;
  2.  
  3. contract SharedAccount {
  4. // Owner takes all the money. [DONE] [Owner leaves accounts active]
  5. // Owner can close account and share the balance with all accounts. [DONE] [Deactivate account and share its balance will all active accounts]
  6. // Owner can change owner. [DONE] [Ownership can be changed]
  7.  
  8. struct AccountBalance {
  9. address addr;
  10. uint balance;
  11. bool isActive;
  12. }
  13.  
  14. mapping(address => AccountBalance) accounts;
  15. mapping(uint => address) users;
  16. address owner;
  17. uint maxAccountCount;
  18. uint public numberOfAccounts;
  19.  
  20. modifier isAccountExists() {
  21. require(accounts[msg.sender].addr != address(0) && accounts[msg.sender].isActive);
  22. _;
  23. }
  24.  
  25. modifier isAccountNotExists() {
  26. require(!(accounts[msg.sender].addr != address(0) && accounts[msg.sender].isActive));
  27. _;
  28. }
  29.  
  30. modifier isBalanceEnough(uint amount) {
  31. require(accounts[msg.sender].balance >= amount);
  32. _;
  33. }
  34.  
  35. modifier onlyOwner() {
  36. require(owner == msg.sender);
  37. _;
  38. }
  39.  
  40. function SharedAccount(uint _maxAccountCount) public {
  41. owner = msg.sender;
  42. if(_maxAccountCount != 0) {
  43. maxAccountCount = _maxAccountCount;
  44. } else {
  45. maxAccountCount = 128;
  46. }
  47. }
  48.  
  49. function openAccount() isAccountNotExists() payable public {
  50. require(numberOfAccounts < maxAccountCount); // modifier ile yapilir mi?
  51.  
  52. accounts[msg.sender] = AccountBalance(msg.sender, msg.value, true);
  53. users[numberOfAccounts] = msg.sender;
  54. numberOfAccounts++;
  55. }
  56.  
  57. function withdrawMoney(uint amount) isAccountExists() isBalanceEnough(amount) public {
  58. msg.sender.transfer(amount);
  59. accounts[msg.sender].balance -= amount;
  60. }
  61.  
  62. function depositMoney() isAccountExists() payable public {
  63. accounts[msg.sender].balance += msg.value;
  64. }
  65.  
  66. function displayBalance() isAccountExists() public constant returns (uint) {
  67. return accounts[msg.sender].balance;
  68. }
  69.  
  70. // Owner functions
  71.  
  72. function changeOwner(address _newOwner) onlyOwner public {
  73. owner = _newOwner;
  74. }
  75.  
  76. function deleteAccount(address _accountOwner) onlyOwner public {
  77. accounts[_accountOwner].isActive = false;
  78. uint sharedBalance = accounts[_accountOwner].balance / (numberOfAccounts - 1);
  79. accounts[_accountOwner].balance = 0;
  80. for(uint i=0; i < numberOfAccounts; i++) {
  81. if(accounts[users[i]].isActive) {
  82. accounts[users[i]].balance += sharedBalance;
  83. }
  84. }
  85.  
  86. }
  87.  
  88. function takeAllMoney() onlyOwner public {
  89. uint totalMoney = 0;
  90. for(uint i=0; i < numberOfAccounts; i++) {
  91. if(accounts[users[i]].isActive) {
  92. totalMoney += accounts[users[i]].balance;
  93. //accounts[users[i]].isActive = false;
  94. accounts[users[i]].balance = 0;
  95. }
  96. }
  97. msg.sender.transfer(totalMoney);
  98. }
  99. }
Add Comment
Please, Sign In to add comment