Created
August 19, 2020 16:55
-
-
Save ElementalBrian/bd98e8b2002d50dd38630fcf120189ae to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.12+commit.7709ece9.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "./Ownable.sol"; | |
pragma solidity 0.5.12; | |
contract Destroyable is Ownable{ | |
function close() public onlyOwner { //onlyOwner is custom modifier | |
address payable ownerpayable = address(uint160(owner)); | |
selfdestruct(ownerpayable); // `owner` is the owners address | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "./Ownable.sol"; | |
import "./Destroyable.sol"; | |
pragma solidity 0.5.12; | |
contract HelloWorld is Ownable, Destroyable{ | |
struct Person { | |
uint id; | |
string name; | |
uint age; | |
uint height; | |
bool senior; | |
} | |
event personCreated(string name, bool senior); | |
event personDeleted(string name, bool senior, address deletedBy); | |
//Create an event called PersonUpdated that is emitted when a persons information is updated. The event should contain both the old and the updated information of the person. | |
event PersonUpdated(string nameold, string name, uint ageold, uint age, uint heightold, uint height) ; | |
uint public balance; | |
modifier costs(uint cost) { | |
require(msg.value >= cost); | |
_; //Continue execution | |
} | |
mapping (address => Person) private people; | |
address[] private creators; | |
function createPerson(string memory name, uint age, uint height) public payable costs(1 ether) { | |
require(age < 150, "Age needs to be below 150"); | |
balance += msg.value; | |
//This creates a person | |
Person memory newPerson; | |
newPerson.name = name; | |
newPerson.age = age; | |
newPerson.height = height; | |
if(age >= 65){ | |
newPerson.senior = true; | |
} | |
else{ | |
newPerson.senior = false; | |
} | |
insertPerson(newPerson); | |
creators.push(msg.sender); | |
assert( | |
keccak256( | |
abi.encodePacked( | |
people[msg.sender].name, | |
people[msg.sender].age, | |
people[msg.sender].height, | |
people[msg.sender].senior | |
) | |
) | |
== | |
keccak256( | |
abi.encodePacked( | |
newPerson.name, | |
newPerson.age, | |
newPerson.height, | |
newPerson.senior | |
) | |
) | |
); | |
emit personCreated(newPerson.name, newPerson.senior); | |
} | |
function insertPerson(Person memory newPerson) private { | |
address creator = msg.sender; | |
people[creator] = newPerson; | |
} | |
function getPerson() public view returns(string memory name, uint age, uint height, bool senior){ | |
address creator = msg.sender; | |
return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior); | |
} | |
function deletePerson(address creator) public onlyOwner { | |
string memory name = people[creator].name; | |
bool senior = people[creator].senior; | |
delete people[creator]; | |
assert(people[creator].age == 0); | |
emit personDeleted(name, senior, owner); | |
} | |
function getCreator(uint index) public view onlyOwner returns(address){ | |
return creators[index]; | |
} | |
function withdrawAll() public onlyOwner returns(uint) { | |
uint toTransfer = balance; | |
balance = 0; | |
msg.sender.transfer(toTransfer); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.5.12; | |
contract Ownable{ | |
address public owner; | |
modifier onlyOwner(){ | |
require(msg.sender == owner); | |
_; //Continue execution | |
} | |
constructor() public{ | |
owner = msg.sender; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment