Ethtereum Virtual Machine (EVM) - All smart contracts are executed in EVM. It is completely isolated and has no access to file system or processes. Each node runs the EVM so it can maintain consensus.
pragma solidity ^0.6.0;
// import
import "./filename.sol";
import * as symbolName from "./filename.sol"
import {symbol1 as alias, symbol2} from "./filename.sol"
// First contract
contract SimpleContract {
// state variable
uint stored Data;
// modifier is a conditional
modifier only Data () {
require(
storedData >= 0);
_;
}
// function
function set(uint x) public {
storedData = x;
}
// event
event Sent (address from, adress to, uint storedData);
}
pragma solidity ^0.6.0;
// string
string name = "AlphaBravo";
// integrers
uint storedata = 56;
// boolean
bool trueorfalsevalue = true;
// address
address walletAddress = 0x03d10be591680Bba0d82611fbD0039e11cA5083;
// array
string[] names; // similar to python array but we declare the type of data it has
// bytes
bytes32 code;
// Struct to define, Solidity allows user to create their own data type in the form of structure
struct User {
string firstName;
string lastName;
}
// enums - Enums are used to define predefined values e.g. human readable names
enum userType {buyer, seller}
// mappings - similar to dictionary in python, used to define key-value pair
mapping (address => uint) public balances;
// There are globally available Units and Variables in Solidity
pragma solidity ^0.6.0;
contract Simple {
/* "ether sazbo finney wei" - we canot use any of these names for our variables
Because these are units which we can use in the smart contracts*/
bool isEqual = (1 ether == 5000 finney);
// time units
bool isTime = (1 == 1 seconds)
// seconds, minutes, hours, days, weeks
bool isTime = (1 hours == 60 minutes);
}
// In solidity there are a few special functions and variables
pragma solidity ^0.6.0;
contract Simple {
// block
block.number;
block.difficulty;
block.coinbase();
// message
msg.sender;
msg.value;
msg.data;
// transaction
tx.origin
}
/* In solidity functions are similar to other languages but the only difference
is that a function can return multiple outputs */
pragma solidity ^0.6.0;
// Basic function syntax in solidity
function function-name(parameters) scope returns() {
// statements
}
contract Simple {
function calcs (uint _a, uint _b) public pure
returns (uint o_sum, uint o_product){
o_sum = _a + _b;
o_product = _a * _b;
}
}
// Operators allow users to perform different operations on operands
// <https://www.geeksforgeeks.org/solidity-operators/>
pragma solidity ^0.6.0;
contract Simple {
// Arthmatic operators + - * / % **
uint a = 2;
uint b = 3;
// Logical operators ! && || == !=
bool hasMoney = !false;
// Bitwise operators & ! ^ ~ >> <<
bytes1 contractValue = 0x02 | 0x01
}
pragma solidity ^0.6.0;
contract Simple {
// if statement
if (a == 2) {
// code if condition is met
} else {
}
// checking condition first
while (a >= 0) {
// code if condition is met
}
//do run this code until condition is no longer true
do {
// do this code first
} while (a >=0)
// for loop
for (uint i=0; i>=50; i++) {
// code here
}
}
pragma solidity ^0.6.0;
contract Inheritance {
address owner;
bool deceased;
uint money;
constructor() public payable {
// constructor is __init__ in python to initialize objects
owner = msg.sender;
money = msg.value;
deceased = false;
}
modifier oneOwner {
require (msg.sender == owner);
_;
}
modifier isDeceased {
require (deceased == true);
_;
}
address[] wallets;
mapping (address => uint) inheritance
function setup(address _wallet, uint _inheritance) public oneOwner {
wallets.push(_wallet);
inheritance[_wallet] = _inheritance;
}
function moneyPaid() private isDeceased {
for (uint i=0; i<wallets.length; i++) {
wallets[i].transfer(inheritance[wallets[i]]);
}
}
function died() public oneOwner {
deceased = true;
moneyPaid();
}
}