際際滷

際際滷Share a Scribd company logo
BLOCKCHAIN AND SMART
CONTRACTS SESSION 2
Hands-on introduction for
Software Developers and
Architects
PLAN FOR TODAY
Understand basic Etherium concepts
Install Etherium Test Network
Install truffle code development environment
Create your first contract
Compile
Test on the test network
CONCEPTS
? Accounts
? Contracts
? Messages
TWO TYPES OF ACCOUNTS
Accou
nt
Externally Owned
(user accounts)
Contra
ctAccount
StorageBalance Code
<>
Externally Owned Account
StorageBalance
Account
StorageBalance Code
<>
Contract
StorageBalance Code
<>
CONTRACTS
Contracts are accounts that contain code
Store the state
? Example: account
balances
Serve as external
account with special
access polices
? 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
MESSAGES
Contract A Contract B
Message Call
? Source
? Target
? Data Payload
? Ether
? Gas
? Return Data
Non-contract
Account
Message CallsendTransaction
Call
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);
? }
? }
CONTRACT LANGUAGES
Solidity
Serpent
LLL
Bytecodes
Etherium
Virtual Machine
(EVM)
REGISTRARS (ETHEREUM NAME
SERVICE, ENS)
?Send ether to an address in Metamask (soon: MEW, Mist, Bitfinex).
?Use it inside your own contracts to resolve names of other contracts and addresses.
?Store contract ABIs for easy lookup using the ethereum-ens library
?Address of a Swarm site
Use Cases
Registry
Regista
r
Resolve
r
ETHER
? Wei: 1
? Ada: 1000
? Fentoether: 1000
? Kwei: 1,000
? Mwei: 1,000,000
? Babbage:
1,000,000
? Pictoether: 1,000,000
? Shannon:
1,000,000,000
? Gwei: 1,000,000,000
? Nano: 1,000,000,000
? Szabo:
1,000,000,000,000
? Micro:
1,000,000,000,000
? Microether:
1,000,000,000,000
? Finney:
1,000,000,000,000,000
? Milli: 1,000,000,000,000,000
? Milliether:
1,000,000,000,000,000
? Ether:
1,000,000,000,000,000,000
Wei = 10-18
Ether
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
2 MINUTE INTRODUCTION
TO SOLIDITY
Data Types
Functions
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;
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;
FUNCTIONS
Functions
Constant
Transactional
Function Visibility
External
Public
Internal
Private
HANDS-ON Code time
PREREQUISITES
Linux (Ubuntu)
Network Connection
INSTALL TRUFFLE FRAMEWORK
?$ sudo npm install -g truffle
?$ mkdir solidity-experiments
?$ cd solidity-experiments/
?$ truffle init
Truffle
Framework
INSTALL AND START TEST
NETWORK
?$ sudo npm
install -g
ethereumjs-
testrpc
?$ testrpc &
Etherium
test
network
CONFIGURE TRUFFLE
? module.exports = {
? networks: {
? development: {
? host: "localhost",
? port: 8545,
? network_id: "*"
? }
? }
? }
Update
truffle.js
DEVELOPING SMART CONTRACT
WITH TRUFFLE
Write Code
Create
deployment
script
Migrate/Deploy
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!';
}
}
DEPLOYMENT SCRIPT
var Hello1 =
artifacts.require("./Hello.sol");
module.exports =
function(deployer) {
deployer.deploy(Hello1);
};
DEPLOY CONTRACT ON TEST
NETWORK$ truffle console
truffle(development)> truffle migrate --reset
Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
...
0xb6bbeaaf3649ecb38d548cba96f681682dad9e0225726924fbee3ce3
6eff94e3
Migrations: 0xc08c46796ba0edc0bebbbd0d90868c010055cb0e
Saving successful migration to network...
...
0x16fe364b9f2c3e8f07fa1ebd6b84b8ad9b4e750d8698a7e920d824eb
d019dd80
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying Hello...
...
0xfe120836b2d7395bd988104feff018fe352f93555f71003bbf1a6467
1cca9ba1
Hello: 0x2b649a87d20ce1ac3b6a0218e911165fa0f095f0
Saving successful migration to network...
...
0x1a09073a3b3f7996f3d63a81a99d8cd09198ad7b467f35f7ddc500a
4291332b9
Saving artifacts...
truffle(development)>
TEST CONTRACT
truffle(development)> var he =
Hello.at(Hello.address)
Undefined
truffle(development)> he.sayHello()
'Hello World!'
STAY IN TOUCH
Gene Leybzon https://www.linkedin.com/in/leybzon/
https://www.meetup.com/members/90744
20/
https://www.leybzon.com

More Related Content

Similar to Hands on with smart contracts (20)

Blockchain, Ethereum and Business Applications
Blockchain, Ethereum and Business ApplicationsBlockchain, Ethereum and Business Applications
Blockchain, Ethereum and Business Applications
Matthias Zimmermann
?
Ethereum
EthereumEthereum
Ethereum
V C
?
Security in the blockchain
Security in the blockchainSecurity in the blockchain
Security in the blockchain
Bellaj Badr
?
1. ibm blockchain explained
1. ibm blockchain explained1. ibm blockchain explained
1. ibm blockchain explained
Diego Alberto Tamayo
?
Building Apps with Ethereum Smart Contract
Building Apps with Ethereum Smart ContractBuilding Apps with Ethereum Smart Contract
Building Apps with Ethereum Smart Contract
Vaideeswaran Sethuraman
?
Blockchain
BlockchainBlockchain
Blockchain
Rishabh Sharma
?
Algorand Smart Contracts
Algorand Smart ContractsAlgorand Smart Contracts
Algorand Smart Contracts
ssusercc3bf81
?
Understanding Algorand's smart contract language
Understanding Algorand's smart contract language   Understanding Algorand's smart contract language
Understanding Algorand's smart contract language
Vanessa Lo?i?
?
Ethereum
EthereumEthereum
Ethereum
Brian Yap
?
Blockchain for Developers
Blockchain for DevelopersBlockchain for Developers
Blockchain for Developers
Shimi Bandiel
?
IP Addressing.ppt
IP Addressing.pptIP Addressing.ppt
IP Addressing.ppt
jAIREN1
?
Ethereum Solidity Fundamentals
Ethereum Solidity FundamentalsEthereum Solidity Fundamentals
Ethereum Solidity Fundamentals
Eno Bassey
?
廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf
廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf
廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf
slideshare779123
?
Part1-Intro-Apps.pptx
Part1-Intro-Apps.pptxPart1-Intro-Apps.pptx
Part1-Intro-Apps.pptx
Olivier Bonaventure
?
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
I.I.S. G. Vallauri - Fossano
?
A living programming environment for a living blockchain
A living programming environment for a living blockchainA living programming environment for a living blockchain
A living programming environment for a living blockchain
Santiago Bragagnolo
?
A living programming environment for blockchain
A living programming environment for blockchainA living programming environment for blockchain
A living programming environment for blockchain
Pharo
?
lecture7 blockchain ethereum mechanics 101
lecture7 blockchain ethereum mechanics 101lecture7 blockchain ethereum mechanics 101
lecture7 blockchain ethereum mechanics 101
HariPurnama5
?
Ethereum.pptx
Ethereum.pptxEthereum.pptx
Ethereum.pptx
INAMULLAH699891
?
6. TinyOS_2.pdf
6. TinyOS_2.pdf6. TinyOS_2.pdf
6. TinyOS_2.pdf
Jesus Cordero
?
Blockchain, Ethereum and Business Applications
Blockchain, Ethereum and Business ApplicationsBlockchain, Ethereum and Business Applications
Blockchain, Ethereum and Business Applications
Matthias Zimmermann
?
Ethereum
EthereumEthereum
Ethereum
V C
?
Security in the blockchain
Security in the blockchainSecurity in the blockchain
Security in the blockchain
Bellaj Badr
?
Algorand Smart Contracts
Algorand Smart ContractsAlgorand Smart Contracts
Algorand Smart Contracts
ssusercc3bf81
?
Understanding Algorand's smart contract language
Understanding Algorand's smart contract language   Understanding Algorand's smart contract language
Understanding Algorand's smart contract language
Vanessa Lo?i?
?
Blockchain for Developers
Blockchain for DevelopersBlockchain for Developers
Blockchain for Developers
Shimi Bandiel
?
IP Addressing.ppt
IP Addressing.pptIP Addressing.ppt
IP Addressing.ppt
jAIREN1
?
Ethereum Solidity Fundamentals
Ethereum Solidity FundamentalsEthereum Solidity Fundamentals
Ethereum Solidity Fundamentals
Eno Bassey
?
廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf
廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf
廣吭心,@乂Windows議Potatoes湊際阻! 盾裂5N児豢MS-RPCE議好返隈.pdf
slideshare779123
?
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
I.I.S. G. Vallauri - Fossano
?
A living programming environment for a living blockchain
A living programming environment for a living blockchainA living programming environment for a living blockchain
A living programming environment for a living blockchain
Santiago Bragagnolo
?
A living programming environment for blockchain
A living programming environment for blockchainA living programming environment for blockchain
A living programming environment for blockchain
Pharo
?
lecture7 blockchain ethereum mechanics 101
lecture7 blockchain ethereum mechanics 101lecture7 blockchain ethereum mechanics 101
lecture7 blockchain ethereum mechanics 101
HariPurnama5
?

Recently uploaded (20)

RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptxRAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
JenTeruel1
?
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
?
Multi objective genetic approach with Ranking
Multi objective genetic approach with RankingMulti objective genetic approach with Ranking
Multi objective genetic approach with Ranking
namisha18
?
Water Industry Process Automation & Control Monthly - March 2025.pdf
Water Industry Process Automation & Control Monthly - March 2025.pdfWater Industry Process Automation & Control Monthly - March 2025.pdf
Water Industry Process Automation & Control Monthly - March 2025.pdf
Water Industry Process Automation & Control
?
decarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptxdecarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptx
gonzalezolabarriaped
?
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Daniel Donatelli
?
google_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPTgoogle_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPT
JayeshShete1
?
Mathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptxMathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptx
ppkmurthy2006
?
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
?
How to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using ArduinoHow to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using Arduino
CircuitDigest
?
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
NgocThang9
?
Syntax Directed Definitions Synthesized Attributes and Inherited Attributes
Syntax Directed Definitions  Synthesized Attributes  and  Inherited AttributesSyntax Directed Definitions  Synthesized Attributes  and  Inherited Attributes
Syntax Directed Definitions Synthesized Attributes and Inherited Attributes
GunjalSanjay
?
Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...
dhanashree78
?
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
Thane Heins NOBEL PRIZE WINNING ENERGY RESEARCHER
?
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
?
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
?
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
ASHISHDESAI85
?
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
?
Industrial Construction shed PEB MFG.pdf
Industrial Construction shed PEB MFG.pdfIndustrial Construction shed PEB MFG.pdf
Industrial Construction shed PEB MFG.pdf
PLINTH & ROOFS
?
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
?
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptxRAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
JenTeruel1
?
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
?
Multi objective genetic approach with Ranking
Multi objective genetic approach with RankingMulti objective genetic approach with Ranking
Multi objective genetic approach with Ranking
namisha18
?
decarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptxdecarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptx
gonzalezolabarriaped
?
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Daniel Donatelli
?
google_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPTgoogle_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPT
JayeshShete1
?
Mathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptxMathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptx
ppkmurthy2006
?
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
?
How to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using ArduinoHow to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using Arduino
CircuitDigest
?
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
NgocThang9
?
Syntax Directed Definitions Synthesized Attributes and Inherited Attributes
Syntax Directed Definitions  Synthesized Attributes  and  Inherited AttributesSyntax Directed Definitions  Synthesized Attributes  and  Inherited Attributes
Syntax Directed Definitions Synthesized Attributes and Inherited Attributes
GunjalSanjay
?
Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...
dhanashree78
?
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
?
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
?
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
ASHISHDESAI85
?
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
?
Industrial Construction shed PEB MFG.pdf
Industrial Construction shed PEB MFG.pdfIndustrial Construction shed PEB MFG.pdf
Industrial Construction shed PEB MFG.pdf
PLINTH & ROOFS
?
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
?

Hands on with smart contracts

  • 1. BLOCKCHAIN AND SMART CONTRACTS SESSION 2 Hands-on introduction for Software Developers and Architects
  • 2. PLAN FOR TODAY Understand basic Etherium concepts Install Etherium Test Network Install truffle code development environment Create your first contract Compile Test on the test network
  • 4. TWO TYPES OF ACCOUNTS Accou nt Externally Owned (user accounts) Contra ctAccount StorageBalance Code <> Externally Owned Account StorageBalance Account StorageBalance Code <> Contract StorageBalance Code <>
  • 5. CONTRACTS Contracts are accounts that contain code Store the state ? Example: account balances Serve as external account with special access polices ? 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. MESSAGES Contract A Contract B Message Call ? Source ? Target ? Data Payload ? Ether ? Gas ? Return Data Non-contract Account Message CallsendTransaction 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) ?Send ether to an address in Metamask (soon: MEW, Mist, Bitfinex). ?Use it inside your own contracts to resolve names of other contracts and addresses. ?Store contract ABIs for easy lookup using the ethereum-ens library ?Address of a Swarm site Use Cases Registry Regista r Resolve r
  • 10. ETHER ? Wei: 1 ? Ada: 1000 ? Fentoether: 1000 ? Kwei: 1,000 ? Mwei: 1,000,000 ? Babbage: 1,000,000 ? Pictoether: 1,000,000 ? Shannon: 1,000,000,000 ? Gwei: 1,000,000,000 ? Nano: 1,000,000,000 ? Szabo: 1,000,000,000,000 ? Micro: 1,000,000,000,000 ? Microether: 1,000,000,000,000 ? Finney: 1,000,000,000,000,000 ? Milli: 1,000,000,000,000,000 ? Milliether: 1,000,000,000,000,000 ? Ether: 1,000,000,000,000,000,000 Wei = 10-18 Ether
  • 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
  • 12. 2 MINUTE INTRODUCTION TO SOLIDITY Data Types Functions
  • 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;
  • 18. INSTALL TRUFFLE FRAMEWORK ?$ sudo npm install -g truffle ?$ mkdir solidity-experiments ?$ cd solidity-experiments/ ?$ truffle init Truffle Framework
  • 19. INSTALL AND START TEST NETWORK ?$ sudo npm install -g ethereumjs- testrpc ?$ testrpc & Etherium test network
  • 20. CONFIGURE TRUFFLE ? module.exports = { ? networks: { ? development: { ? host: "localhost", ? port: 8545, ? network_id: "*" ? } ? } ? } Update truffle.js
  • 21. DEVELOPING SMART CONTRACT WITH TRUFFLE Write Code Create deployment script Migrate/Deploy
  • 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!'; } }
  • 23. DEPLOYMENT SCRIPT var Hello1 = artifacts.require("./Hello.sol"); module.exports = function(deployer) { deployer.deploy(Hello1); };
  • 24. DEPLOY CONTRACT ON TEST NETWORK$ truffle console truffle(development)> truffle migrate --reset Using network 'development'. Running migration: 1_initial_migration.js Deploying Migrations... ... 0xb6bbeaaf3649ecb38d548cba96f681682dad9e0225726924fbee3ce3 6eff94e3 Migrations: 0xc08c46796ba0edc0bebbbd0d90868c010055cb0e Saving successful migration to network... ... 0x16fe364b9f2c3e8f07fa1ebd6b84b8ad9b4e750d8698a7e920d824eb d019dd80 Saving artifacts... Running migration: 2_deploy_contracts.js Deploying Hello... ... 0xfe120836b2d7395bd988104feff018fe352f93555f71003bbf1a6467 1cca9ba1 Hello: 0x2b649a87d20ce1ac3b6a0218e911165fa0f095f0 Saving successful migration to network... ... 0x1a09073a3b3f7996f3d63a81a99d8cd09198ad7b467f35f7ddc500a 4291332b9 Saving artifacts... truffle(development)>
  • 25. TEST CONTRACT truffle(development)> var he = Hello.at(Hello.address) Undefined truffle(development)> he.sayHello() '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

  • #5: Every account has a persistent key-value store mapping 256-bit words to 256-bit words called storage
  • #7: 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)
  • #9: [add bytecode example here]
  • #10: https://github.com/ethereum/ens https://medium.com/the-ethereum-name-service/a-developers-guide-to-ens-concepts-7004eea8a073
  • #12: http://ether.fund/tool/gas-fees https://etherscan.io/chart/gasprice https://ethgasstation.info/calculatorTxV.php
  • #14: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
  • #15: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
  • #16: Add example(s) of function definitions
  • #23: http://leybzon.blogspot.com/2018/01/hello-world-from-solidity.html
  • #24: http://leybzon.blogspot.com/2018/01/hello-world-from-solidity.html
  • #25: http://leybzon.blogspot.com/2018/01/hello-world-from-solidity.html
  • #26: http://leybzon.blogspot.com/2018/01/hello-world-from-solidity.html