4. WHY BLOCKCHAIN IS A BIG DEAL
“Bitcoin is a technological tour de force.”
– Bill Gates
”I think the fact that within the bitcoin universe an algorithm replaces
the function of the government …[that] is actually pretty cool.”
- Al Gore
“[Bitcoin] is a remarkable cryptographic achievement… The ability to
create something which is not duplicable in the digital world has
enormous value…Lot’s of people will build businesses on top of that.”
- Eric Schmidt
5. BLOCKCHAIN IS
Ledger of facts
Ledger of facts replicated across large number of
computers
Ledger of facts replicated across large number of
computers connected as peer-to-peer network
Ledger of facts replicated across large number of
computers connected as peer-to-peer network that
implements consensus algorithm that can securely
identify sender and receiver of facts
FACT
S
Monetary
transactio
n
Content
hash
Signature
Asset
informatio
n
Ownership
Agreemen
t
Contract
14. Smart Contract Definition
Smart contract are
applications that run
exactly as programmed
without any possibility of
downtime, censorship,
fraud or third party
interference.
19. 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
20. CALLS AND MESSAGES
Contract A Contract B
Message Call
• Source
• Target
• Data Payload
• Ether
• Gas
• Return Data
Externally
owned
account
Message CallSend Transaction
Call
21. 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);
}
}
23. REGISTRARS (ETHEREUM NAME SERVICE, ENS)
1. Send ether to an address in
Metamask (soon: MEW, Mist, Bitfinex)
1. Use it inside your own contracts to
resolve names of other contracts and
addresses
2. Store contract ABIs for easy lookup
using the ethereum-ens library
3. Address of a Swarm site
Use Cases
Registry
Registar
Resolver
25. 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
Example:
300 CPU cycles = 300 gas units = 0.000006 ETH =
$0.00553
(as of 2/20/2018)
Cost of Gas as in 2018
28. 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;
29. 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;
30. FUNCTIONS
Functio
ns
Consta
nt
Transactio
nal
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; }
}
}
39. ERC223 TOKEN
ERC223 is backwards compatible with ERC20
merges the token transfer function among wallets and contracts into one
single function ‘transfer()’
does not allow token to be transferred to a contract that does not allow
token to be withdrawn
Improvements with ERC223
• Eliminates the problem of lost tokens which happens during the transfer of ERC20 tokens to a contract
(when people mistakenly use the instructions for sending tokens to a wallet). ERC223 allows users to send
their tokens to either wallet or contract with the same function transfer, thereby eliminating the potential
for confusion and lost tokens.
• Allows developers to handle incoming token transactions, and reject non-supported tokens (not possible
with ERC20)
• Energy savings. The transfer of ERC223 tokens to a contract is a one step process rather than 2 step
process (for ERC20), and this means 2 times less gas and no extra blockchain bloating.
40. ERC223 TOKEN INTERFACE
contract ERC223 {
uint public totalSupply;
function balanceOf(address who) public view returns (uint);
function name() public view returns (string _name);
function symbol() public view returns (string _symbol);
function decimals() public view returns (uint8 _decimals);
function totalSupply() public view returns (uint256 _supply);
function transfer(address to, uint value) public returns (bool ok);
function transfer(address to, uint value, bytes data) public
returns (bool ok);
function transfer(address to, uint value, bytes data, string
custom_fallback) public returns (bool ok);
event Transfer(address indexed from, address indexed to, uint
value, bytes indexed data);
}
41. ERC721
▪People love collecting
▪Allow smart contracts to operate as tradable tokens
▪Each ERC721 is unique (non-fungible)
▪Support “ownership” functions
▪Each token is referenced by unique id
▪Tokens can have metadata (attributes)
42. ERC721
contract ERC721 {
event Transfer(address indexed _from, address indexed
_to, uint256 _tokenId);
event Approval(address indexed _owner, address indexed
_approved, uint256 _tokenId);
function balanceOf(address _owner) public view returns
(uint256 _balance);
function ownerOf(uint256 _tokenId) public view returns
(address _owner);
function transfer(address _to, uint256 _tokenId) public;
function approve(address _to, uint256 _tokenId) public;
function takeOwnership(uint256 _tokenId) public;
}