2. PLAN FOR TODAY
Understand basis Etherium concepts
Selenium Language
Install Etherium Test Network
Install truffle code development environment
Create your first contract
Compile
Test
5. CONTRACTS
Contracts are accounts that contain code
Store the state
• Example:
keeps account
balance
Source of notifications
(events)
• Example: messaging
service that forwards
only messages when
certain conditions are
met
Mapping - relationship
between users
• Example: mapping
real-world financial
contract to Etherium
contract
Act like a software
library
• Example:
return calculated
value based on
message arguments
6. CALLS AND MESSAGES
Contract A Contract B
Message Call
• Source
• Target
• Data Payload
• Ether
• Gas
• Return Data
Externally
owned
account
Message CallSend Transaction
Call
7. WHAT CONTRACT CAN DO?
Read internal state
• contract
ReadStateDemo {
• uint public state;
• function read() {
• console.log(“State:
"+state);
• }
• }
Modify internal State
• contract
ModStateDemo {
• uint public state;
• function update() {
• state=state+1;
• }
• }
Consume message
• contract
ReadStateDemo {
• uint public state;
• function hi() {
• console.log(“Hello
from: “ "+msg.sender
+ “ to “ + msg.
receiver);
• }
• }
Trigger execution of
another contract
• contract CallDemo {
• function send(address
receiver, uint amount)
public {
• Sent(msg.sender,
receiver, amount);
• }
• }
9. REGISTRARS (ETHEREUM NAME
SERVICE, ENS)
1. Send ether to an address in Metamask
(soon: MEW, Mist, Bitfinex)
2. Use it inside your own contracts to
resolve names of other contracts and
addresses
3. Store contract ABIs for easy lookup
using the ethereum-ens library
4. Address of a Swarm site
Use Cases
Registry
Regista
r
Resolve
r
11. GAS
step 1 Default amount of gas to pay for an execution
cycle
stop 0 Nothing paid for the STOP operation
suicide 0 Nothing paid for the SUICIDE operation
sha3 20 Paid for a SHA3 operation
sload 30 Paid for a SLOAD operation
sstore 100 Paid for a normal SSTORE operation
(doubled or waived sometimes)
balance 20 Paid for a BALANCE operation
create 100 Paid for a CREATE operation
call 20 Paid for a CALL operation
memory 1 Paid for every additional word when
expanding memory
txdata 5 Paid for every byte of data or code for a
transaction
transaction 500 Paid for every transaction
Example:
300 CPU cycles = 300 gas units = 0.000006 ETH =
$0.00553
(as of 2/20/2018)
Cost of Gas as in 2018
13. ELEMENTARY DATA TYPES
Data Type Example
uint<M> , M=1,2,4, …, 256
int<M>, uint, int
Unsigned/signed integer uint256 counter = 1;
address 160-bit value that does not
allow any arithmetic
operations. It is suitable for
storing addresses of
contracts or keypairs
belonging to external
persons
address owner =
msg.sender;
bool Same as uint8 but values are
0 or 1
Bool b = 0;
fixed<M>x<N>
ufixed<M>x<N>
Signed fixed-point decimal
number of M bits
fixed128x19 f = 1.1;
bytes<M>, M=1..32 Binary of M bytes bytes32 n=123;
function Same as bytes24 Function f;
14. ARRAYS
Data Type Example
<type>[M] fixed-length array of fixed-
length type
byte[100]
<type>[] variable-length array of
fixed-length type
byte[]
bytes dynamic sized byte sequence bytes ab;
string dynamic sized Unicode
string
String s = “String”;
fixed<M>x<N>
ufixed<M>x<N>
Signed fixed-point decimal
number of M bits
fixed128x19 f = 1.1;
15. FUNCTIONS
Functions
Constant
Transactional
Function Visibility
External Can be called via
transactions
Public Can be called via
messages or locally
Internal Only locally called or
from derived contracts
Private Locally called
pragma solidity^0.4.12;
contract Test {
function test(uint[20] a) public returns (uint){
return a[10]*2;
}
function test2(uint[20] a) external returns (uint){
return a[10]*2; }
}
}
22. HELLO WORLD CONTRACT
(SOLIDITY CODE)
pragma solidity ^0.4.4;
contract Hello {
function Hello() public {
// constructor
}
function sayHello() public pure returns
(string) {
//console.log("sayHello() function
called...");
return 'Hello World!';
}
}
26. STAY IN TOUCH
Gene Leybzon https://www.linkedin.com/in/leybzon/
https://www.meetup.com/members/90744
20/
https://www.leybzon.com
Editor's Notes
Every account has a persistent key-value store mapping 256-bit words to 256-bit words called storage
Accounts can be externally owned or contracts
Message: This is contract-to-contract. These are not delayed by mining because they are part of the transaction execution.
Transaction: Like a message, but originates with an externally owned account. It's always a transaction that gets things started, but multiple messages may be fired off to complete the action.
Ethereum also has two modes of execution. These modes are indifferent to the syntax or vernacular in the client that summoned the contract. So, the important things are to know which one is appropriate, and how to select it in your client of choice.
sendTransaction: Transaction is sent to the network and verified by miners. Sender gets a transaction hash initially since no results are available until after the transaction is mined. These are potentially state-changing.
call: Transaction is executed locally on the user's local machine which alone evaluates the result. These are read-only and fast. They can't change the blockchain in any way because they are never sent to the network. Most languages/platforms don't have this idea of running the code in either "read-only/dry run/practice" mode and "read-write/real world/sticky" mode.
(https://ethereum.stackexchange.com/questions/12065/what-is-the-difference-between-a-call-message-call-and-a-message)
https://github.com/trufflesuite/ganache-cli
Options:
-a or --accounts: Specify the number of accounts to generate at startup.
-e or --defaultBalanceEther: Amount of ether to assign each test account. Default is 100.
-b or --blocktime: Specify blocktime in seconds for automatic mining. Default is 0 and no auto-mining.
-d or --deterministic: Generate deterministic addresses based on a pre-defined mnemonic.
-n or --secure: Lock available accounts by default (good for third party transaction signing)
-m or --mnemonic: Use a specific HD wallet mnemonic to generate initial addresses.
-p or --port: Port number to listen on. Defaults to 8545.
-h or --hostname: Hostname to listen on. Defaults to Node's server.listen() default.
-s or --seed: Use arbitrary data to generate the HD wallet mnemonic to be used.
-g or --gasPrice: Use a custom Gas Price (defaults to 20000000000)
-l or --gasLimit: Use a custom Gas Limit (defaults to 90000)
-f or --fork: Fork from another currently running Ethereum client at a given block. Input should be the HTTP location and port of the other client, e.g. http://localhost:8545. You can optionally specify the block to fork from using an @sign: http://localhost:8545@1599200.
-i or --networkId: Specify the network id the ganache-cli will use to identify itself (defaults to the current time or the network id of the forked blockchain if configured)
--db: Specify a path to a directory to save the chain database. If a database already exists, ganache-cli will initialize that chain instead of creating a new one.
--debug: Output VM opcodes for debugging
--mem: Output ganache-cli memory usage statistics. This replaces normal output.
--noVMErrorsOnRPCResponse: Do not transmit transaction failures as RPC errors. Enable this flag for error reporting behaviour which is compatible with other clients such as geth and Parity.