Skip to content

Instantly share code, notes, and snippets.

@ElementalBrian
Created September 15, 2020 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElementalBrian/4c070c6f7cf08b0ebe9b330d915a08ef to your computer and use it in GitHub Desktop.
Save ElementalBrian/4c070c6f7cf08b0ebe9b330d915a08ef 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=
pragma solidity ^0.5.3;
contract ActQualifier{
int var1 = 5;
int public var2 = 4;
function f1() public view returns(int _total){
_total = var1 + var2;
return _total;
}
function f2() public pure returns(int _total){
_total = 3+5;
return _total;
}
function() external payable{
}
}
pragma solidity ^0.5.3;
contract addressDataType{
address public contract_owner;
address payable public recipient_address;
uint public transfer_amount;
uint public recipient_balance;
constructor() public {
contract_owner = msg.sender;
}
function transferEther(address payable _recipient_address) public payable returns(uint) {
recipient_address = _recipient_address;
transfer_amount = msg.value;
recipient_address.transfer(transfer_amount);
recipient_balance = recipient_address.balance;
return recipient_balance;
}
}
pragma solidity ^0.5.3;
contract byteExample{
bytes1 public var1 = 'a';
bytes2 public var2 = 'ab';
bytes3 public var3 = 'abc';
bytes4 public var4 = 0x12345678;
function getByte(uint i) public view returns(bytes1){
return var3[i];
}
function getLength() public view returns(uint) {
return var4.length;
}
}
pragma solidity ^0.5.3;
contract controls{
//if - else
event LogLoop(int);
function ifElse(int x, int y) public pure returns(int result) {
if (x > y) {
result = x -y;
return result;
} else if (x < y) {
result = y - x;
return result;
}else{
return 0;
}
}
// while loop
function whileLoop(int counter) public {
int i = 0;
while (i < counter) {
i = i + 1;
}
}
function doLoop(int counter) public {
int i = 0;
do{
i = i + 1;
}while(i < counter);
}
function forLoop(int counter) public {
for (int i = 0; i < counter; i++){
emit LogLoop(i);
}
}
function breakStatement(int counter) public {
for (int i = 0; i < counter; i++){
if (i==4)
break;
emit LogLoop(i);
}
}
}
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function destroy() public onlyOwner {
address payable receiver = msg.sender;
selfdestruct(receiver);
}
}
pragma solidity ^0.5.3;
contract DynamicArrays{
byte[] public var1;
int[] public num1;
function setBytes(byte _var1) public {
var1.push(_var1);
}
function getBytesLength() public view returns(uint){
return var1.length;
}
}
pragma solidity ^0.5.3;
contract enumsExample{
enum ClaimStatus{Approved, Rejected}
ClaimStatus status;
function setStatus(uint _claim_amount) public {
if (_claim_amount > 100) {
status = ClaimStatus.Rejected;
}
else if (_claim_amount < 100) {
status = ClaimStatus.Approved;
}
}
function getStatus() public view returns(ClaimStatus){
return status;
}
}
pragma solidity ^0.5.3;
contract ErrorHandling{
address public owner;
uint public balance;
constructor() public{
owner = msg.sender;
}
function setDestructContract(address payable _address) public {
require (msg.sender == owner, "only owner can run");
assert (address(this).balance != 0);
selfdestruct(_address);
}
function RecieveEther() public payable returns(uint){
balance = address(this).balance;
return balance;
}
}
pragma solidity ^0.5.3;
contract Events{
address public fromAccount;
uint public toAcctBalance;
event Transferred(address, address, uint);
function transfer(address payable _toAcct) public payable{
fromAccount = msg.sender;
_toAcct.transfer(msg.value);
toAcctBalance = _toAcct.balance;
emit Transferred(fromAccount, _toAcct, msg.value);
}
}
pragma solidity 0.5.12;
//Interface
contract HelloWorld{
function createPerson(address creator, string memory name, uint age, uint height) public payable;
}
contract ExternalContract{
HelloWorld instance = HelloWorld(0x1Ed9cd9AB6DdF1e9Acad3F796B0Fc899f8FA0B84);
function externalCreatePerson(string memory name, uint age, uint height) public payable{
//CALL createPerson in HelloWorld contract
//Forward any ether to HelloWorld
instance.createPerson.value(msg.value)(msg.sender, name, age, height);
}
}
pragma solidity ^0.5.3;
contract FixedArrays{
bytes1 public var1 = 'a';
bytes2 public var2 = 'ab';
bytes3 public var3 = 'abc';
bytes4 public var4 = 0x12345678;
int[2] public num1 = [1,2];
int8[3] public num2;
function getBytes(uint i) public view returns(bytes1){
return var3[i];
}
function getBytesLength() public view returns(uint){
return var3.length;
}
function setInt(uint i, int8 _num2) public {
num2[i] = _num2;
}
function getIntLength() public view returns(uint){
return num2.length;
}
}
pragma solidity ^0.5.3;
contract Functions{
int public var1;
int public var2;
//Setter Functions
function setValues(int _var1, int _var2) public {
var1 = _var1;
var2 = _var2;
}
//getter function
function getValues() public view returns(int _total) {
_total = var1 + var2;
}
}
pragma solidity ^0.5.3;
contract Global{
//1 - blockhash
function getBlkHash(uint _blockNumber) public view returns(bytes32){
return blockhash(_blockNumber);
}
}
pragma solidity ^0.5.3;
contract IntBoolExample{
int8 public intVar8 = -128;
int16 public intVar16 = 10000;
uint8 public uintVar8 = 255;
bool public flagVar = true;
function setIntBool(int8 _intVar8, uint8 _uintVar8, bool _flagVar) public {
intVar8 = _intVar8;
uintVar8 = _uintVar8;
flagVar = _flagVar;
}
}
pragma solidity ^0.5.12;
contract Lottery{
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
constructor() public{
owner = msg.sender;
}
address public owner;
address payable[] public players;
function registration() public payable returns (uint _random){
players.push(msg.sender);
_random = randomNumber();
return _random;
}
function randomNumber() public view returns (uint _random) {
uint random = uint(keccak256(abi.encodePacked(block.timestamp,msg.sender)));
_random = random;
return _random;
}
function winner() public onlyOwner() returns (bool complete) {
uint number = randomNumber();
uint index = number % players.length;
address payable to = players[index];
to.transfer(address(this).balance);
return true;
}
}
pragma solidity ^0.5.12;
contract MappingExample{
mapping(address => uint) public balances;
function setBalance(uint _balance) public{
balances[msg.sender] = _balance;
}
function getBalances (address _address) public view returns(uint) {
return balances[_address];
}
}
pragma solidity ^0.5.3;
contract ErrorHandling{
address public owner;
uint public balance;
constructor() public{
owner = msg.sender;
}
modifier OwnerOnly{
if (msg.sender == owner) {
_;
}
}
function setDestructContract(address payable _address) OwnerOnly public {
selfdestruct(_address);
}
function RecieveEther() public payable returns(uint){
balance = address(this).balance;
return balance;
}
}
pragma solidity ^0.5.3;
contract controls{
function ifElse(int x, int y) public pure returns(int result) {
if (x > y) {
result = x -y;
return result;
} else if (x < y) {
result = y - x;
return result;
}else{
return 0;
}
}
}
pragma solidity 0.5.12;
contract Ownable{
address public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_; //Continue execution
}
constructor() public{
owner = msg.sender;
}
}
pragma solidity 0.5.12;
import './Destroyable.sol';
import './Ownable.sol';
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);
uint public balance;
modifier costs(uint cost){
require(msg.value >= cost);
_;
}
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);
return toTransfer;
}
}
pragma solidity ^0.5.12;
contract random{
function randomNumber() public view returns (uint _random) {
uint random = uint(keccak256(abi.encodePacked(msg.sender)));
_random = random;
return _random;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.5.12;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
pragma solidity ^0.5.3;
contract SpecialArrays{
bytes public var1 = new bytes(0);
string public var2 = "solidity";
bytes public var3 = bytes(var2);
function setBytes(byte _var1) public {
var1.push(_var1);
}
function getLength() public view returns(uint){
return var1.length;
}
function popBytes() public {
var1.pop();
}
}
pragma solidity ^0.5.12;
contract structTrading{
enum TitleStatus{Clean, Restored}
struct Car {
string make;
string model;
uint year;
TitleStatus status;
}
Car[] cars;
function buyCar(string memory _make, string memory _model, uint _year, TitleStatus _status) public {
cars.push(Car(_make, _model, _year, _status));
}
function viewCar(uint index) public view returns (string memory _make, string memory _model, uint _year, TitleStatus _status){
return (cars[index].make, cars[index].model, cars[index].year, cars[index].status);
}
function sellCar(uint index) public returns (bool sold) {
delete cars[index];
return true;
}
}
import "./Ownable.sol";
import "./Safemath.sol";
pragma solidity 0.5.12;
contract ERC20 is Ownable{
using SafeMath for uint256;
mapping (address => uint256) private _balances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
uint8 private _decimals;
constructor (string memory name, string memory symbol) public {
_name = name;
_symbol = symbol;
_decimals = 18;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view returns (uint256) {
return _balances[account];
}
function mint(address account, uint256 amount) public onlyOwner{
require(account != address(0), "mint to zero address");
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
}
function transfer(address recipient, uint256 amount) public returns (bool) {
require(recipient != address(0), "transfer to the 0 address");
require(_balances[msg.sender] >= amount, "insufficient funds");
uint toTransfer = _balances[msg.sender];
_balances[msg.sender] = toTransfer.sub(amount);
_balances[recipient] = _balances[recipient].add(amount);
return true;
}
}
pragma solidity ^0.5.3;
contract VarTypes{
int public stateVar1;
string public stateVar2;
function f1() public {
int _var1 = 2;
string memory _var2 = "Brian";
stateVar1 = _var1;
stateVar2 = _var2;
}
function f2() public {
int _var1 = 5;
stateVar1 = _var1;
}
}
pragma solidity ^0.5.3;
contract A{
int public var1=7;
function getVal1() public view returns(int){
return var1;
}
function getVal2() external view returns(int){
int x = getVal1();
return var1;
}
function getVal3() private view returns(int){
int x = getVal1();
return var1;
}
function getVal4() internal view returns(int){
return var1;
}
}
contract B{
A public a = new A();
int public x1 = a.getVal1();
int public x2 = a.getVal2();
//int public x3 = a.getVal3();
}
contract C is A{
int public x1 = getVal4();
//int public x2 = getVal3();
}
// this line is added to create a gist. Empty file is not allowed.
import "./People.sol";
pragma solidity 0.5.12;
contract Workers is HelloWorld{
mapping(address => uint) salary;
function createWorker(string memory name, uint age, uint height) public {
require(age < 75);
createPerson(name, age, height);
salary[msg.sender] = 25000;
}
function fireWorker() public {
deletePerson(msg.sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment