Guest User

Untitled

a guest
May 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. pragma solidity ^0.4.21;
  2.  
  3. contract Users {
  4. // data structure that stores a user
  5. struct User {
  6. string name;
  7. bytes32 status;
  8. address walletAddress;
  9. uint createdAt;
  10. uint updatedAt;
  11. }
  12.  
  13. // it maps the user's wallet address with the user ID
  14. mapping (address => uint) public usersIds;
  15.  
  16. // Array of User that holds the list of users and their details
  17. User[] public users;
  18.  
  19. // event fired when an user is registered
  20. event newUserRegistered(uint id);
  21.  
  22. // event fired when the user updates his status or name
  23. event userUpdateEvent(uint id);
  24.  
  25. // Modifier: check if the caller of the smart contract is registered
  26. modifier checkSenderIsRegistered {
  27. require(isRegistered());
  28. _;
  29. }
  30.  
  31. /**
  32. * Constructor function
  33. */
  34. function Users() public {
  35. // NOTE: the first user MUST be emtpy
  36. addUser(0x0, "", "");
  37.  
  38. // Some dummy data
  39. addUser(0x333333333333, "Leo Brown", "Available");
  40. addUser(0x111111111111, "John Doe", "Very happy");
  41. addUser(0x222222222222, "Mary Smith", "Not in the mood today");
  42. }
  43.  
  44. /**
  45. * Function to register a new user.
  46. *
  47. * @param userName The displaying name
  48. * @param status The status of the user
  49. */
  50. function registerUser(string userName, bytes32 status) public
  51. returns(uint)
  52. {
  53. return addUser(msg.sender, userName, status);
  54. }
  55.  
  56. /**
  57. * Add a new user. This function must be private because an user
  58. * cannot insert another user on behalf of someone else.
  59. *
  60. * @param wAddr Address wallet of the user
  61. * @param userName Displaying name of the user
  62. * @param status Status of the user
  63. */
  64. function addUser(address wAddr, string userName, bytes32 status) private
  65. returns(uint)
  66. {
  67. // checking if the user is already registered
  68. uint userId = usersIds[wAddr];
  69. require (userId == 0);
  70.  
  71. // associating the user wallet address with the new ID
  72. usersIds[wAddr] = users.length;
  73. uint newUserId = users.length++;
  74.  
  75. // storing the new user details
  76. users[newUserId] = User({
  77. name: userName,
  78. status: status,
  79. walletAddress: wAddr,
  80. createdAt: now,
  81. updatedAt: now
  82. });
  83.  
  84. // emitting the event that a new user has been registered
  85. emit newUserRegistered(newUserId);
  86.  
  87. return newUserId;
  88. }
  89.  
  90. /**
  91. * Update the user profile of the caller of this method.
  92. * Note: the user can modify only his own profile.
  93. *
  94. * @param newUserName The new user's displaying name
  95. * @param newStatus The new user's status
  96. */
  97. function updateUser(string newUserName, bytes32 newStatus) checkSenderIsRegistered public
  98. returns(uint)
  99. {
  100. // An user can modify only his own profile.
  101. uint userId = usersIds[msg.sender];
  102.  
  103. User storage user = users[userId];
  104.  
  105. user.name = newUserName;
  106. user.status = newStatus;
  107. user.updatedAt = now;
  108.  
  109. emit userUpdateEvent(userId);
  110.  
  111. return userId;
  112. }
  113.  
  114. /**
  115. * Get the user's profile information.
  116. *
  117. * @param id The ID of the user stored on the blockchain.
  118. */
  119. function getUserById(uint id) public view
  120. returns(
  121. uint,
  122. string,
  123. bytes32,
  124. address,
  125. uint,
  126. uint
  127. ) {
  128. // checking if the ID is valid
  129. require( (id > 0) || (id <= users.length) );
  130.  
  131. User memory i = users[id];
  132.  
  133. return (
  134. id,
  135. i.name,
  136. i.status,
  137. i.walletAddress,
  138. i.createdAt,
  139. i.updatedAt
  140. );
  141. }
  142.  
  143. /**
  144. * Return the profile information of the caller.
  145. */
  146. function getOwnProfile() checkSenderIsRegistered public view
  147. returns(
  148. uint,
  149. string,
  150. bytes32,
  151. address,
  152. uint,
  153. uint
  154. ) {
  155. uint id = usersIds[msg.sender];
  156.  
  157. return getUserById(id);
  158. }
  159.  
  160. /**
  161. * Check if the user that is calling the smart contract is registered.
  162. */
  163. function isRegistered() public view returns (bool)
  164. {
  165. return (usersIds[msg.sender] != 0);
  166. }
  167.  
  168. /**
  169. * Return the number of total registered users.
  170. */
  171. function totalUsers() public view returns (uint)
  172. {
  173. return users.length;
  174. }
  175.  
  176. }
Add Comment
Please, Sign In to add comment