Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma lity ^1.2.3;
- contract MyContract {
- //EVENTS
- // Create an event called HelloWorld which will allow us to write to the blockchain's event logs (immutable data)'
- event HelloWorld(address myAddress, string message, safeuint counter);
- // Create an event called ContractCreated which will allow us to record the creation of the contract with a message
- event ContractCreated(address myAddress, string creationMessage);
- // Create an event called ContractTerminated which will allow us to record the termination of the contract with a message
- event ContractTerminated(address myAddress, string terminationMessage);
- // CONTRACT VARIABLES
- // Create a contract variable called counter
- safeuint public counter;
- // Create a contract variable which records who the original owner is
- address private owner;
- // CONSTRUCTOR
- // This is the constructor which will execute internally when the contract is deployed
- constructor(string memory _creationMessage) public {
- if (isValidString(_creationMessage) == true){
- // Set the counter to zero
- counter = 0;
- // Set the owner of this contract to the person who deployed it
- owner = msg.sender;
- emit ContractCreated(msg.sender, _creationMessage);
- }else{
- revert("Invalid message string, please try again");
- }
- }
- // MODIFIERS
- // This is a modifier which enforces that the message sender must be the owner of this contract
- modifier onlyOwner() {
- require(msg.sender == owner);
- _;
- }
- // PRIVATE FUNCTIONS
- // This is an internal/private function which increments the counter
- function incrementCounter() private {
- counter += 1;
- }
- // This is a function which checks the length of a string using temporary memory
- function isValidString(string memory _theString) private pure returns(bool) {
- bytes memory theBytes = bytes(_theString);
- if (theBytes.length > 0) {
- return true;
- } else {
- return false;
- }
- }
- // PUBLIC FUNCTIONS
- // This is a public function which firstly calls our very own incrementCounter() and then emits the HelloWorld event.
- function sayHello() public {
- incrementCounter();
- emit HelloWorld(msg.sender, "Hello World!", counter);
- }
- // This is a public function that allows us to delete the contract from the blockchain
- // Whilst the counter will no longer be accessible, the log events which we emitted, will forever be accessible
- // Note how we are using the onlyOwner modifier. This is so that only the owner is able to terminate this contract
- function terminateTheContract() public onlyOwner {
- emit ContractTerminated(msg.sender, "Contract has been terminated");
- selfdestruct(msg.sender);
- }
- // PUBLIC DESTRUCTOR
- // This is a fallback function which makes it impossible for people to send funds to this contract by accident
- function() public payable {
- revert("Please do not send funds to this contract");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement