Skip to content

Instantly share code, notes, and snippets.

@jigar23
Created May 16, 2018 16:06
Show Gist options
  • Save jigar23/1ca13c2da417631b28d598d7615052ff to your computer and use it in GitHub Desktop.
Save jigar23/1ca13c2da417631b28d598d7615052ff 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.4.23+commit.124ca40d.js&optimize=false&gist=
pragma solidity ^0.4.0;
// Pure Virtual class
interface Regulator {
function checkValue(uint amount) returns (bool);
function loan() returns (bool);
}
// Inheriting Regulator
// Anyone can access this bank class, To restrict this
// to person who created the contract
contract Bank is Regulator {
uint private value;
address private owner;
// Modifier owner based function
modifier ownerFunc {
require(owner == msg.sender);
// Execute the function now
_;
}
// Throw is deprecated
// function testThrow() {
// throw;
// }
// Parameterized Constructor
// Bank constructor is called only once, so a Bank
// account is linked to the contract address
function Bank(uint amount) {
value = amount;
owner = msg.sender;
}
function deposit(uint amount) ownerFunc {
value += amount;
}
function withdraw(uint amount) ownerFunc {
if (checkValue(amount)) {
value -= amount;
}
// error handling later
}
function balance() returns (uint) {
return value;
}
function checkValue(uint amount) returns (bool) {
return value >= amount;
}
function loan() returns (bool) {
return value > 0;
}
}
// Inheriting Bank
contract MyFirstContract is Bank(10) {
string private name;
uint private age;
// Can use var newName but that by default will take 256 bits
function setName(string newName) public {
name = newName;
}
function getName() public view returns (string) {
return name;
}
function setAge(uint newAge) public {
age= newAge;
}
function getAge() public view returns (uint) {
return age;
}
}
// https://medium.com/blockchannel/the-use-of-revert-assert-and-require-in-solidity-and-the-new-revert-opcode-in-the-evm-1a3a7990e06e
contract TestThrows {
function testAssert() {
// all gas will be comsumed in message
// Use only if something terrible has happened
assert(1 == 2);
}
function testRequire() {
// Use this more often
// Calls revert internally and reverts the remaning gas
require(2 == 1);
}
function testRevert() {
// Same as require but will complex conditions
if (2 != 1) {
revert();
}
}
function testThrow() {
// Consumes all the gas
// DONT USE THIS
// Now internally calls revert
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment