Skip to content

Instantly share code, notes, and snippets.

@msusur
Created March 24, 2018 10:52
Show Gist options
  • Save msusur/a581e75a743bb2374f3a581ec6a8ed64 to your computer and use it in GitHub Desktop.
Save msusur/a581e75a743bb2374f3a581ec6a8ed64 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.21;
contract Ownable {
address public owner;
event OwnerChanged(address oldOwner, address newOwner);
function Ownable() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function changeOwner(address _newOwner) external onlyOwner {
address oldOwner = owner;
owner = _newOwner;
OwnerChanged(oldOwner, _newOwner);
}
}
contract Greeting is Ownable {
struct GreetingItem {
string greeting;
address greeter;
}
modifier hasNotGreetYet() {
require(greetingsMap[msg.sender].greeter == address(0));
_;
}
mapping(address => GreetingItem) greetingsMap;
uint count;
address lastGreeterAddress;
address owner;
event NewGreetingCreated(address newGreeter, string newGreeting);
function Greeting() public {
}
function greet(string _greeting) hasNotGreetYet onlyOwner public {
count += 1;
GreetingItem memory greetingItem = GreetingItem(_greeting, msg.sender);
greetingsMap[msg.sender] = greetingItem;
lastGreeterAddress = msg.sender;
NewGreetingCreated(msg.sender, _greeting);
}
function getGreeting() public constant returns (string lastGreeting, address lastGreeter, uint lastCount) {
GreetingItem storage greetingItem = greetingsMap[lastGreeterAddress];
return (greetingItem.greeting, greetingItem.greeter, count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment