Guest User

Untitled

a guest
Feb 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. pragma solidity ^0.4.18;
  2.  
  3. //Contract is just like classes
  4. contract MessageBoard {
  5. //Defining User structure. address is a variable type from solidity
  6. struct User {
  7. address _address;
  8. string name;
  9. bool isValue;
  10. }
  11.  
  12. //Defining Message structure. You can see it have User type member
  13. struct Message {
  14. string text;
  15. User _user;
  16. }
  17.  
  18. //Declaring an event
  19.  
  20. event MessageCreated (Message message);
  21.  
  22. //Constructor
  23. function MessageBoard() public {
  24.  
  25. }
  26.  
  27. //mapping is a key value store. We are specified type of key and value
  28. mapping (address => User) users;
  29.  
  30. // public function, which is returning boolean
  31. function setUsername(string name) public returns (bool) {
  32. //require() is throwing error, if the input is not true
  33. //require(userExists(msg.sender) == false);
  34. //msg.sender is the address, from where the signUp was called.
  35. users[msg.sender] = User(msg.sender, name, true);
  36. return true;
  37. }
  38.  
  39. //8 bit unsigned integer (so no negative values).
  40. uint8 public messageCount;
  41.  
  42. //it is creating an event Message with text and user.
  43. function sendMessage (string text) public returns (bool) {
  44. //require() is throwing error, if the input is not true
  45. //require(userExists(msg.sender));
  46. messageCount++;
  47. MessageCreated(Message(text, users[msg.sender]));
  48. return true;
  49. }
  50.  
  51. //We are using view keyword, to specify that it is not modified the state of contract. It will consume less resources.
  52. function userExists(address userAddress) view public returns (bool) {
  53. return users[userAddress].isValue;
  54. }
  55. }
Add Comment
Please, Sign In to add comment