Last active
September 4, 2020 15:44
-
-
Save BHaunT/577cea0e17bd01f4c453fcc786576652 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
pragma solidity 0.5.12; | |
// pragma experimental ABIEncoderV2; | |
import "./Ownable.sol"; | |
contract Destroyable is Ownable{ | |
function close() public onlyOwner { //onlyOwner is custom modifier | |
address payable receiver = msg.sender; | |
selfdestruct(receiver); | |
} | |
} |
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; | |
// pragma experimental ABIEncoderV2; | |
import "./Ownable.sol"; | |
import "./Destroyable.sol"; | |
contract hidapp is Ownable, Destroyable { | |
struct Person { | |
uint id; | |
string name; | |
uint number; | |
bool senior; | |
} | |
event saidHello(string name); | |
event personCreated(string name, uint number, bool senior); | |
event personDeleted(string name, bool senior, address deletedBy); | |
uint public balance; | |
modifier costs(uint cost){ | |
require(msg.value >= cost); | |
_; | |
} | |
function sayHello() public pure returns( string memory hello) { | |
return "Hi dApp!"; | |
} | |
mapping (address => Person) public people; | |
address[] private creators; | |
function createPerson(string memory name, uint number) public payable costs(0.001 ether) { | |
require(number < 999, "Numbe needs to be below 999"); | |
balance += msg.value; | |
//This creates a person | |
Person memory newPerson; | |
newPerson.name = name; | |
newPerson.number = number; | |
if(number > 99){ | |
newPerson.senior = true; | |
} | |
else{ | |
newPerson.senior = false; | |
} | |
insertPerson(newPerson); | |
creators.push(msg.sender); | |
assert( | |
keccak256( | |
abi.encodePacked( | |
people[msg.sender].name, | |
people[msg.sender].number, | |
people[msg.sender].senior | |
) | |
) | |
== | |
keccak256( | |
abi.encodePacked( | |
newPerson.name, | |
newPerson.number, | |
newPerson.senior | |
) | |
) | |
); | |
emit personCreated(newPerson.name, newPerson.number, newPerson.senior); | |
} | |
function insertPerson(Person memory newPerson) private { | |
address creator = msg.sender; | |
people[creator] = newPerson; | |
} | |
function getPerson() public view returns(string memory name, uint number, bool senior){ | |
address creator = msg.sender; | |
return (people[creator].name, people[creator].number, 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].number == 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); | |
return 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; | |
// pragma experimental ABIEncoderV2; | |
contract Ownable{ | |
address public owner; | |
modifier onlyOwner(){ | |
require(msg.sender == owner, "Caller needs to be owner"); | |
_; //how we close modifiers.. 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