Skip to content

Instantly share code, notes, and snippets.

@skplunkerin
Last active November 23, 2021 23:52
Show Gist options
  • Save skplunkerin/bbc00d1299e6c35d59a7fafb26f0727a to your computer and use it in GitHub Desktop.
Save skplunkerin/bbc00d1299e6c35d59a7fafb26f0727a 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.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
pragma solidity 0.8.7;
contract HelloWorld {
// called state variables:
// string message;
// CONSTRUCTORS
// https://academy.moralis.io/lessons/constructors-2
// runs only 1x during compile.
// constructor(string memory _msg){
// message = _msg;
// }
// VIEW vs PURE
// https://academy.moralis.io/lessons/view-pure
// view function, reads data outside of func.
// function HelloExternalScope() public view returns(string memory) {
// return message;
// }
// pure function, does NOT read/write data outside of func.
// function HelloLocalScope() public pure returns(string memory) {
// // called local variables:
// string memory localMessage = "Hello Function";
// // NOTE ABOUT STRINGS:
// // + and . don't work to concat strings, nothing
// // is built-in to solidity for string concats.
// // return localMessage + message;
// // return localMessage . message;
// return localMessage;
// }
// CONTROL FLOW (if statements)
// https://academy.moralis.io/lessons/control-flow
// NOTE: parameters naming scheme is prepended with "_" to mark is as an "input variable",
// but you can name the variable anything you want
// function HelloIfStatement(int _number) public pure returns(string memory) {
// // NOTE: these variables are ALWAYS available in every function:
// // msg.sender // this address executing the code
// // msg.value // the callers amount of ETH (or currency)
// // standard if statement
// if (_number <= 10) {
// return "good number";
// } else {
// return "bad number";
// }
// }
// LOOPS
// https://academy.moralis.io/lessons/loops
// NOTE: returns(int) doesn't need memory *unlike returns(string memory)
// function HelloWhile(int _number) public pure returns(int) {
// int i = 0;
// while(i < 10){
// // NOTE: no way to console.log();
// _number++;
// i++;
// }
// return _number;
// }
// function HelloFor(int _number) public pure returns(int) {
// for(int i = 0; i < 10; i++){
// // NOTE: no way to console.log();
// _number++;
// }
// return _number;
// }
// GETTERS & SETTERS
// https://academy.moralis.io/lessons/setter-functions
// int num;
// // Get
// function GetNum() public view returns(int){
// return num;
// }
// // Set
// function SetNum(int _n) public returns(int){
// num = _n;
// return num;
// }
// ARRAYS
// https://academy.moralis.io/lessons/arrays
// int[] numbersDynamic; // dynamic length array
// int[3] numbersStatic; // static length array of 3
// // int[3] numbersStaticVals = [1,2,3]; // static length with init vals
// function AppendDynamicNumbers(int _n) public returns(int[] memory){
// // only dynamic arrays have the ".push()" method.
// numbersDynamic.push(_n);
// return numbersDynamic;
// }
// function GetDynamicNumbersID(uint _id) public view returns(int){
// // NOTE: if _id is passed that doesn't exist, an error is triggered:
// // call to HelloWorld.GetDynamicNumbersID errored: VM error: revert.
// // revert
// // The transaction has been reverted to the initial state.
// // Note: The called function should be payable if you send value and the value you send should be less than your current balance.
// // Debug the transaction to get more information
// return numbersDynamic[_id];
// }
// // NOTE: _id needs to be uint type, since no position is negative.
// function AppendStaticNumbers(uint _id, int _n) public returns(int[3] memory){
// // static length arrays don't have ".push()",
// // you need to specify the position:
// numbersStatic[_id] = _n;
// return numbersStatic;
// }
// STRUCTS
// https://academy.moralis.io/lessons/structs-3
// struct Person{
// uint age;
// string name;
// }
// Person[] people;
// function AddPerson(uint _age, string memory _name) public {
// Person memory p = Person(_age, _name);
// people.push(p);
// // This also works
// // people.push(Person(_age, _name));
// }
// // NOTE: Filip says you can't return "Person", instead you should do:
// // function getPerson(uint _index) public view returns (uint, string memory){...}
// // but returning Person seems to work?
// function GetPerson(uint _index) public view returns (Person memory){
// return people[_index];
// }
// // BUT, if we do return multiple values like recommended by Filip, this is how:
// function GetPersonValues(uint _index) public view returns (uint, string memory){
// Person memory personToReturn = people[_index];
// return (personToReturn.age, personToReturn.name);
// }
// MAPPINGS
// https://academy.moralis.io/lessons/mappings-introduction
// https://academy.moralis.io/lessons/mappings-2
mapping(address => uint) balance;
// see below for re-declaration of AddBalance() being reserved for
// the owner address.
// function AddBalance(uint _amount) public returns(uint){
// balance[msg.sender] += _amount;
// return balance[msg.sender];
// }
function GetBalance() public view returns(uint){
return balance[msg.sender];
}
// IMPLEMENTING VISIBILITY
// https://academy.moralis.io/lessons/introduction-to-visibility-2
// https://academy.moralis.io/lessons/implementing-visibility
// AND ERROR HANDLING
// https://academy.moralis.io/lessons/require-theory
// https://academy.moralis.io/lessons/assert-invariants-theory
// https://academy.moralis.io/lessons/require
// https://academy.moralis.io/lessons/assert
// MODIFIERS
// https://academy.moralis.io/lessons/modifiers-2
address owner;
// don't need to pass the address in, msg.sender works here
// constructor(address _owner){
constructor(){
owner = msg.sender;
}
// modifiers used to restrict access to functions
modifier onlyOwner {
require(msg.sender == owner, "owner address missing");
_; // means to: run the function
// but what actually happens is, the function logic calling this
// modifer get's placed right here.
}
modifier costs(uint _price){
require(msg.value >= _price);
_;
}
// used like, where value needs to be a static value:
// function doSomething() public costs(100) {...}
function AddBalance(uint _amount) public onlyOwner returns(uint){
balance[msg.sender] += _amount;
return balance[msg.sender];
}
function Transfer(address _recipient, uint _amount) public {
// check if sender has balance first
require(balance[msg.sender] >= _amount, "insufficient balance");
require(msg.sender != _recipient, "must send to a separate address"); // should never allow this, doesn't make sense and might cause glitches/bugs
uint previousBalance = balance[msg.sender];
// break out re-usable code into private functions
// balance[msg.sender] -= _amount;
// balance[_recipient] += _amount;
_transfer(msg.sender, _recipient, _amount + 10);
// NOTE:
// assert(false);
// will consume all remaining gas in the function call
assert(balance[msg.sender] == previousBalance - _amount); // unsure how to set custom err: "new balance doesn't match expected value"
// event logs & further checks
// TODO
}
// NOTE: private functions naming scheme is also using "_" prepended
// to the function name, but you can name it anything.
function _transfer(address _from, address _to, uint _amount) private {
balance[_from] -= _amount;
balance[_to] += _amount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment