Skip to content

Instantly share code, notes, and snippets.

@granawkins
Last active May 13, 2020 03:54
Show Gist options
  • Save granawkins/204e4a5f4ef4249de9ea5fd024ca472f to your computer and use it in GitHub Desktop.
Save granawkins/204e4a5f4ef4249de9ea5fd024ca472f to your computer and use it in GitHub Desktop.
Practice smart contract for IvanOnTech Ethereum Programming 101 Course. Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime.
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable{
function close() public onlyOwner{ //onlyOwner is custom modifier
selfdestruct(msg.sender); // `owner` is the owners address
}
}
pragma solidity 0.5.12;
contract eventManager {
struct Person {
string name;
uint age;
uint height;
bool senior;
}
}
pragma solidity 0.5.12;
//Interface
contract People{
function createPerson(string memory name, uint age, uint height) public payable;
}
contract ExternalContract{
People instance = People(0x164c6595277Cb5b1F1526b718D279Ae10196F9a9);
function externalCreatePerson(string memory name, uint age, uint height) public payable {
//CALL createPerson in People contract
//Forward any ether to People
instance.createPerson.value(msg.value)(name, age, height);
}
}
pragma solidity 0.5.1;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
// Costs:
// 1. Storage: Most expensive, permanent storage on Ethereum blockchain. Also exp to modify
// 2. Memory: Second most expensive
// 3. Stack: Very cheap
}
pragma solidity 0.5.12;
contract Ownable{
address public owner;
constructor() public{
owner = msg.sender;
}
// Add modifiers to function definition call line
modifier onlyOwner(){
require(msg.sender == owner);
_; // Continue Execution
}
// Study how we can destroy contracts (from links)
// Implement a new file where I can inherit
// "Destroyable" from my contract.
}
import "./Ownable.sol"; // "./" means look in the same folder
import "./Destroyable.sol";
pragma solidity 0.5.12; // EVERY FIRST LINE
contract peopleManager is Ownable, Destroyable{
// 3 Places where things can be stored:
// - 1. Storage: saved for life of contract, most expensive
// - 2. Memory: during execution, second most expensive
// function arguments default to memory
// - 3. Stack: local variables, very cheap
struct Person {
string name;
uint age;
uint height;
bool senior;
}
// Create an event, trigger with 'emit' later in function
event personCreated(string name, bool senior);
event personDeleted(string name, bool senior, address personDeletedBy);
event returnBalance(uint balance);
uint public contractBalance;
modifier feePayable(uint fee) {
require(msg.value >= fee, "Can't afford fees!");
_;
emit returnBalance(contractBalance);
}
mapping(address => Person) private people;
address[] private creators;
// Include 'payable' in the header to charge
function createPerson(string memory name, uint age, uint height) public payable feePayable(1000 wei) {
require(age < 150, "Age needs to be below 150");
contractBalance += msg.value;
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);
//Double-check (invariate)
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
)
)
);
}
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, msg.sender);
}
function getCreator(uint index) public view onlyOwner returns(address) {
return creators[index];
}
function withdrawAll() public onlyOwner returns(uint) {
require(contractBalance > 0, "Contract is empty!");
uint toTransfer = contractBalance;
contractBalance = 0;
msg.sender.transfer(toTransfer); //REVERT
return toTransfer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment