Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. pragma lity ^1.2.3;
  2.  
  3. contract MyContract {
  4.  
  5. //EVENTS
  6. // Create an event called HelloWorld which will allow us to write to the blockchain's event logs (immutable data)'
  7. event HelloWorld(address myAddress, string message, safeuint counter);
  8. // Create an event called ContractCreated which will allow us to record the creation of the contract with a message
  9. event ContractCreated(address myAddress, string creationMessage);
  10. // Create an event called ContractTerminated which will allow us to record the termination of the contract with a message
  11. event ContractTerminated(address myAddress, string terminationMessage);
  12.  
  13. // CONTRACT VARIABLES
  14. // Create a contract variable called counter
  15. safeuint public counter;
  16. // Create a contract variable which records who the original owner is
  17. address private owner;
  18.  
  19. // CONSTRUCTOR
  20. // This is the constructor which will execute internally when the contract is deployed
  21. constructor(string memory _creationMessage) public {
  22. if (isValidString(_creationMessage) == true){
  23. // Set the counter to zero
  24. counter = 0;
  25. // Set the owner of this contract to the person who deployed it
  26. owner = msg.sender;
  27. emit ContractCreated(msg.sender, _creationMessage);
  28. }else{
  29. revert("Invalid message string, please try again");
  30. }
  31. }
  32.  
  33. // MODIFIERS
  34. // This is a modifier which enforces that the message sender must be the owner of this contract
  35. modifier onlyOwner() {
  36. require(msg.sender == owner);
  37. _;
  38. }
  39.  
  40. // PRIVATE FUNCTIONS
  41. // This is an internal/private function which increments the counter
  42. function incrementCounter() private {
  43. counter += 1;
  44. }
  45.  
  46. // This is a function which checks the length of a string using temporary memory
  47. function isValidString(string memory _theString) private pure returns(bool) {
  48. bytes memory theBytes = bytes(_theString);
  49. if (theBytes.length > 0) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55.  
  56. // PUBLIC FUNCTIONS
  57. // This is a public function which firstly calls our very own incrementCounter() and then emits the HelloWorld event.
  58. function sayHello() public {
  59. incrementCounter();
  60. emit HelloWorld(msg.sender, "Hello World!", counter);
  61. }
  62.  
  63. // This is a public function that allows us to delete the contract from the blockchain
  64. // Whilst the counter will no longer be accessible, the log events which we emitted, will forever be accessible
  65. // Note how we are using the onlyOwner modifier. This is so that only the owner is able to terminate this contract
  66. function terminateTheContract() public onlyOwner {
  67. emit ContractTerminated(msg.sender, "Contract has been terminated");
  68. selfdestruct(msg.sender);
  69. }
  70.  
  71. // PUBLIC DESTRUCTOR
  72. // This is a fallback function which makes it impossible for people to send funds to this contract by accident
  73. function() public payable {
  74. revert("Please do not send funds to this contract");
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement