SlideShare a Scribd company logo
1 of 53
Training
nuuneoi
Agenda
• Introduction to Blockchain
• Introduction to Smart Contract Platform
• Smart Contract 101
• Coding with Solidity
• Deploy to Ethereum Network
• Work with Ethereum’s Smart Contract though web3
• Extras
Introduction to
Blockchain
nuuneoi
What is Blockchain? (Speedy Version)
“The Decentralized Database”
What is Blockchain? (Speedy Version)
“The Decentralized Database”
What is Blockchain? (Speedy Version)
“The Decentralized Database”
What is Blockchain? (Speedy Version)
“The Decentralized Database”
What is Blockchain? (Speedy Version)
“The Decentralized Database”
What is Blockchain? (Speedy Version)
“The Decentralized Database”
Introduction to
Smart Contract Platform
nuuneoi
What is Smart Contract Platform?
• Since Blockchain is the decentralized database. How about we let it keep the
“executable code” inside instead than just a simple transaction?
What is Smart Contract Platform?
• Everyone hold the same source code in the decentralized way.
How the code is executed?
• Same as the way transaction is made in Blockchain 1.0 we make a
transaction to call the code instead of making a money transferring tx.
• We call it “Blockchain 2.0”
What else Smart Contract could do?
• Store the variables (States)
• Transaction 1: a = 10
• Transaction 2: a = 20
Ethereum
nuuneoi
Ethereum Networks
Main Net
Network ID: 1
Consensus: PoW
Ropsten
Network ID: 3
Consensus: PoW
Client: Cross-Client
Rinkeby
Network ID: 4
Consensus: PoA
Client: Geth
Kovan
Network ID: 42
Consensus: PoA
Client: Parity
• Since the Ethereum is open sourced. There could be unlimited number of
networks out there and each of them are run separately.
Creating a Wallet
• Install MetaMask
• Chrome plugin
Faucet
• Get Ethereum for FREE! … (Sort of)
• https://faucet.kovan.network/ (Github account required)
Creating another Wallet
• Do it in MetaMask
Make the first transaction
• Let’s transfer some Ethereum from first account to the second one.
• Learn more a bit about Etherscan.
Smart Contract
Programming with
Solidity
nuuneoi
Ethereum Smart Contract
pragma solidity ^0.4.18;
contract SimpleContract {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public {
// Cap balance to be [0, 10000]
require(newBalance <= 10000);
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
IDE: Remix
• https://remix.ethereum.org/
Solidity Helloworld
pragma solidity ^0.4.18;
contract SimpleContract {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public {
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
Coding: Constructor
pragma solidity ^0.4.18;
contract SimpleContract {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public {
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
constructor will be called
only once when deployed
Deploying
• Live Demo: Do it on remix
Working with Smart Contract through web3
• ABI
• BYTECODE
Working with Smart Contract through web3
• ABI
[ { "constant": false, "inputs": [ { "name": "newBalance", "type":
"uint256" } ], "name": "setBalance", "outputs": [], "payable": false,
"stateMutability": "nonpayable", "type": "function" }, { "inputs": [],
"payable": false, "stateMutability": "nonpayable", "type": "constructor" },
{ "constant": true, "inputs": [], "name": "getBalance", "outputs": [ {
"name": "", "type": "uint256" } ], "payable": false, "stateMutability":
"view", "type": "function" } ]
• BYTECODE
608060405234801561001057600080fd5b506103e860005560bf806100256000396000f3006
0806040526004361060485763ffffffff7c0100000000000000000000000000000000000000
00000000000000000060003504166312065fe08114604d578063fb1669ca146071575b60008
0fd5b348015605857600080fd5b50605f6088565b60408051918252519081900360200190f3
5b348015607c57600080fd5b506086600435608e565b005b60005490565b6000555600a1656
27a7a72305820cf77c0639acc98ef9acefd6386fe697d2d71d7478f3b9d459d8778557e3061
4a0029
Working with Smart Contract through web3
ABI
Working with Smart Contract through web3
• Live Coding: HTML + Javascript + jquery + web3.js + MetaMask
• Boilerplate: https://goo.gl/jychkY
Testing
$ npm install –g http-server
$ http-server .
Understand Gas Price
• See it on etherscan
Coding: Types
• Live Coding:
• bool
• int, uint, int8, uint8, int16, uint16, …, int256, uint256
• address
• fixed, ufixed
• byte, bytes
• string
• struct
• mapping
• enum
• []
Coding: Inheritance
• “is” contract A {
uint a;
function getA() public view returns(uint) {
return a;
}
}
contract B is A {
uint b;
function getBsumA() public view returns(uint) {
return b + a;
}
}
Coding: pure function
function func(uint x, uint y) public pure returns (uint) {
return x * (y + 42);
}
Coding: view function
uint a;
function func() public view returns (uint) {
return a;
}
Coding: pure & view function through read fn
uint a;
function func() public view returns (uint) {
return a;
}
Coding: pure & view function through read fn
uint a;
function func() public view returns (uint) {
return a;
}
function func2() public view returns (uint) {
return func();
}
Coding: pure & view function through write fn
uint a;
uint b;
function func() public view returns (uint) {
return a;
}
function func3(uint someVal) public returns (uint) {
b = someVal;
return func() + b;
}
Coding: require
function setBalance(uint newBalance) public {
// Check if newBalance is in [0, 10000]
require(newBalance <= 10000);
// Set new balance
balance = newBalance;
}
Coding: Visibility
• public – Can be either called internally or via messages.
• private – Only visible for the contract they are defined and
not in the derived contract.
• internal – Can only be accessed internally (i.e. from within
the current contract or contracts deriving from it)
• external - Can be called from other contracts and via
transactions. Cannot be called internally.
Coding: modifier
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
https://goo.gl/ZLThph
Coding: modifier
pragma solidity ^0.4.18;
contract SimpleContract is Ownable {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public onlyOwner {
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
https://goo.gl/ZLThph
Coding: Events
pragma solidity ^0.4.18;
contract SimpleContract is Ownable {
event BalancedUpdated(uint balance);
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public onlyOwner {
// Set new balance
balance = newBalance;
// Fire Event
emit BalanceUpdated(newBalance);
}
function getBalance() public view returns(uint) {
return balance;
}
}
Coding: Events
pragma solidity ^0.4.18;
contract SimpleContract is Ownable {
event BalancedUpdated(uint balance);
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public onlyOwner {
// Set new balance
balance = newBalance;
// Fire Event
emit BalanceUpdated(newBalance);
}
function getBalance() public view returns(uint) {
return balance;
}
}
var event = SimpleContract.BalanceUpdated();
event.watch(function(error, result) {
// Will be called once there is new event emittted
});
Coding: payable
function register() public payable {
registeredAddresses[msg.sender] = true;
}
Redeploying
• Live Demo: See the difference [new contract address].
• Test the event.
Extras
nuuneoi
What is ERC-20?
// https://github.com/ethereum/EIPs/issues/20
interface ERC20Interface {
function totalSupply() public view returns (uint supply);
function balanceOf(address _owner) public view returns (uint balance);
function transfer(address _to, uint _value) public returns (bool success);
function transferFrom(address _from, address _to, uint _value) public returns (bool success);
function approve(address _spender, uint _value) public returns (bool success);
function allowance(address _owner, address _spender) public view returns (uint remaining);
function decimals() public view returns(uint digits);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
• ERC-20 is just a contract implemented the interface defined by ERC-20
standard.
Truffle: Solidity Framework
• Highly recommended
• Testable code
• Incremental deployment
• etc.
Store file on IPFS
• https://ipfs.io/
• Upload through Infura
Alternative Public Blockchain
• EOSIO: Smart Contract
• TomoChain: Smart Contract (Ethereum-Forked)
• NEM: Smart Contract
• Stellar: Payment (Non Turing Completed)
Create your own Smart Contract blockchain
• Hyperledger Fabric
• Use Golang to write Smart Contract

More Related Content

What's hot

What's hot (20)

[WSO2 API Manager Community Call] Mastering JWTs with WSO2 API Manager
[WSO2 API Manager Community Call] Mastering JWTs with WSO2 API Manager[WSO2 API Manager Community Call] Mastering JWTs with WSO2 API Manager
[WSO2 API Manager Community Call] Mastering JWTs with WSO2 API Manager
 
Kubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy AgentKubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy Agent
 
Introduction to helm
Introduction to helmIntroduction to helm
Introduction to helm
 
Reactive stream processing using Akka streams
Reactive stream processing using Akka streams Reactive stream processing using Akka streams
Reactive stream processing using Akka streams
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Everything You Need To Know About Persistent Storage in Kubernetes
Everything You Need To Know About Persistent Storage in KubernetesEverything You Need To Know About Persistent Storage in Kubernetes
Everything You Need To Know About Persistent Storage in Kubernetes
 
Docker intro
Docker introDocker intro
Docker intro
 
Session 5 - NGSI-LD Advanced Operations | Train the Trainers Program
Session 5 -  NGSI-LD Advanced Operations | Train the Trainers ProgramSession 5 -  NGSI-LD Advanced Operations | Train the Trainers Program
Session 5 - NGSI-LD Advanced Operations | Train the Trainers Program
 
Demystifying Service Mesh
Demystifying Service MeshDemystifying Service Mesh
Demystifying Service Mesh
 
Write smart contract with solidity on Ethereum
Write smart contract with solidity on EthereumWrite smart contract with solidity on Ethereum
Write smart contract with solidity on Ethereum
 
Microservice 4.0 Journey - From Spring NetFlix OSS to Istio Service Mesh and ...
Microservice 4.0 Journey - From Spring NetFlix OSS to Istio Service Mesh and ...Microservice 4.0 Journey - From Spring NetFlix OSS to Istio Service Mesh and ...
Microservice 4.0 Journey - From Spring NetFlix OSS to Istio Service Mesh and ...
 
OpenStack vs VMware vCloud
OpenStack vs VMware vCloudOpenStack vs VMware vCloud
OpenStack vs VMware vCloud
 
Polygot Java EE on the GraalVM
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVM
 
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaEvent Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
 
Integrating Fiware Orion, Keyrock and Wilma
Integrating Fiware Orion, Keyrock and WilmaIntegrating Fiware Orion, Keyrock and Wilma
Integrating Fiware Orion, Keyrock and Wilma
 
Kong, Keyrock, Keycloak, i4Trust - Options to Secure FIWARE in Production
Kong, Keyrock, Keycloak, i4Trust - Options to Secure FIWARE in ProductionKong, Keyrock, Keycloak, i4Trust - Options to Secure FIWARE in Production
Kong, Keyrock, Keycloak, i4Trust - Options to Secure FIWARE in Production
 
Universal metrics with Apache Beam
Universal metrics with Apache BeamUniversal metrics with Apache Beam
Universal metrics with Apache Beam
 
Delivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWSDelivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWS
 
OpenStack Architecture
OpenStack ArchitectureOpenStack Architecture
OpenStack Architecture
 
Getting started with Docker
Getting started with DockerGetting started with Docker
Getting started with Docker
 

Similar to Smart Contract programming 101 with Solidity #PizzaHackathon

09 Application Design
09 Application Design09 Application Design
09 Application Design
Ranjan Kumar
 

Similar to Smart Contract programming 101 with Solidity #PizzaHackathon (20)

Solidity Security and Best Coding Practices
Solidity Security and Best Coding PracticesSolidity Security and Best Coding Practices
Solidity Security and Best Coding Practices
 
Solidity Simple Tutorial EN
Solidity Simple Tutorial ENSolidity Simple Tutorial EN
Solidity Simple Tutorial EN
 
Score (smart contract for icon)
Score (smart contract for icon) Score (smart contract for icon)
Score (smart contract for icon)
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Dex and Uniswap
Dex and UniswapDex and Uniswap
Dex and Uniswap
 
Mvc 4
Mvc 4Mvc 4
Mvc 4
 
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
 
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Hems
HemsHems
Hems
 
Thread
ThreadThread
Thread
 
Textile
TextileTextile
Textile
 
That’s My App - Running in Your Background - Draining Your Battery
That’s My App - Running in Your Background - Draining Your BatteryThat’s My App - Running in Your Background - Draining Your Battery
That’s My App - Running in Your Background - Draining Your Battery
 
From CRUD to messages: a true story
From CRUD to messages: a true storyFrom CRUD to messages: a true story
From CRUD to messages: a true story
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
 
09 Application Design
09 Application Design09 Application Design
09 Application Design
 
Durable functions 2.0 (2019-10-10)
Durable functions 2.0 (2019-10-10)Durable functions 2.0 (2019-10-10)
Durable functions 2.0 (2019-10-10)
 
NHibernate Configuration Patterns
NHibernate Configuration PatternsNHibernate Configuration Patterns
NHibernate Configuration Patterns
 

More from Sittiphol Phanvilai

GTUG Bangkok 2011 Android Ecosystem Session
GTUG Bangkok 2011 Android Ecosystem SessionGTUG Bangkok 2011 Android Ecosystem Session
GTUG Bangkok 2011 Android Ecosystem Session
Sittiphol Phanvilai
 

More from Sittiphol Phanvilai (11)

Firebase Dev Day Bangkok: Keynote
Firebase Dev Day Bangkok: KeynoteFirebase Dev Day Bangkok: Keynote
Firebase Dev Day Bangkok: Keynote
 
Introduction to Firebase [Google I/O Extended Bangkok 2016]
Introduction to Firebase [Google I/O Extended Bangkok 2016]Introduction to Firebase [Google I/O Extended Bangkok 2016]
Introduction to Firebase [Google I/O Extended Bangkok 2016]
 
What’s new in aNdroid [Google I/O Extended Bangkok 2016]
What’s new in aNdroid [Google I/O Extended Bangkok 2016]What’s new in aNdroid [Google I/O Extended Bangkok 2016]
What’s new in aNdroid [Google I/O Extended Bangkok 2016]
 
I/O Rewind 215: What's new in Android
I/O Rewind 215: What's new in AndroidI/O Rewind 215: What's new in Android
I/O Rewind 215: What's new in Android
 
I/O Rewind 2015 : Android Design Support Library
I/O Rewind 2015 : Android Design Support LibraryI/O Rewind 2015 : Android Design Support Library
I/O Rewind 2015 : Android Design Support Library
 
The way Tech World is heading to and how to survive in this fast moving world
The way Tech World is heading to and how to survive in this fast moving worldThe way Tech World is heading to and how to survive in this fast moving world
The way Tech World is heading to and how to survive in this fast moving world
 
Mobile Dev Talk #0 Keynote & Mobile Trend 2015
Mobile Dev Talk #0 Keynote & Mobile Trend 2015Mobile Dev Talk #0 Keynote & Mobile Trend 2015
Mobile Dev Talk #0 Keynote & Mobile Trend 2015
 
Tech World 2015
Tech World 2015Tech World 2015
Tech World 2015
 
Mobile Market : Past Present Now and Then
Mobile Market : Past Present Now and ThenMobile Market : Past Present Now and Then
Mobile Market : Past Present Now and Then
 
How to deal with Fragmentation on Android
How to deal with Fragmentation on AndroidHow to deal with Fragmentation on Android
How to deal with Fragmentation on Android
 
GTUG Bangkok 2011 Android Ecosystem Session
GTUG Bangkok 2011 Android Ecosystem SessionGTUG Bangkok 2011 Android Ecosystem Session
GTUG Bangkok 2011 Android Ecosystem Session
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Recently uploaded (20)

Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 

Smart Contract programming 101 with Solidity #PizzaHackathon

  • 2. Agenda • Introduction to Blockchain • Introduction to Smart Contract Platform • Smart Contract 101 • Coding with Solidity • Deploy to Ethereum Network • Work with Ethereum’s Smart Contract though web3 • Extras
  • 4. What is Blockchain? (Speedy Version) “The Decentralized Database”
  • 5. What is Blockchain? (Speedy Version) “The Decentralized Database”
  • 6. What is Blockchain? (Speedy Version) “The Decentralized Database”
  • 7. What is Blockchain? (Speedy Version) “The Decentralized Database”
  • 8. What is Blockchain? (Speedy Version) “The Decentralized Database”
  • 9. What is Blockchain? (Speedy Version) “The Decentralized Database”
  • 10. Introduction to Smart Contract Platform nuuneoi
  • 11. What is Smart Contract Platform? • Since Blockchain is the decentralized database. How about we let it keep the “executable code” inside instead than just a simple transaction?
  • 12. What is Smart Contract Platform? • Everyone hold the same source code in the decentralized way.
  • 13. How the code is executed? • Same as the way transaction is made in Blockchain 1.0 we make a transaction to call the code instead of making a money transferring tx. • We call it “Blockchain 2.0”
  • 14. What else Smart Contract could do? • Store the variables (States) • Transaction 1: a = 10 • Transaction 2: a = 20
  • 16. Ethereum Networks Main Net Network ID: 1 Consensus: PoW Ropsten Network ID: 3 Consensus: PoW Client: Cross-Client Rinkeby Network ID: 4 Consensus: PoA Client: Geth Kovan Network ID: 42 Consensus: PoA Client: Parity • Since the Ethereum is open sourced. There could be unlimited number of networks out there and each of them are run separately.
  • 17. Creating a Wallet • Install MetaMask • Chrome plugin
  • 18. Faucet • Get Ethereum for FREE! … (Sort of) • https://faucet.kovan.network/ (Github account required)
  • 19. Creating another Wallet • Do it in MetaMask
  • 20. Make the first transaction • Let’s transfer some Ethereum from first account to the second one. • Learn more a bit about Etherscan.
  • 22. Ethereum Smart Contract pragma solidity ^0.4.18; contract SimpleContract { uint balance; constructor() public { // Set initial balance as 1000 balance = 1000; } function setBalance(uint newBalance) public { // Cap balance to be [0, 10000] require(newBalance <= 10000); // Set new balance balance = newBalance; } function getBalance() public view returns(uint) { return balance; } }
  • 24. Solidity Helloworld pragma solidity ^0.4.18; contract SimpleContract { uint balance; constructor() public { // Set initial balance as 1000 balance = 1000; } function setBalance(uint newBalance) public { // Set new balance balance = newBalance; } function getBalance() public view returns(uint) { return balance; } }
  • 25. Coding: Constructor pragma solidity ^0.4.18; contract SimpleContract { uint balance; constructor() public { // Set initial balance as 1000 balance = 1000; } function setBalance(uint newBalance) public { // Set new balance balance = newBalance; } function getBalance() public view returns(uint) { return balance; } } constructor will be called only once when deployed
  • 26. Deploying • Live Demo: Do it on remix
  • 27. Working with Smart Contract through web3 • ABI • BYTECODE
  • 28. Working with Smart Contract through web3 • ABI [ { "constant": false, "inputs": [ { "name": "newBalance", "type": "uint256" } ], "name": "setBalance", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "constant": true, "inputs": [], "name": "getBalance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" } ] • BYTECODE 608060405234801561001057600080fd5b506103e860005560bf806100256000396000f3006 0806040526004361060485763ffffffff7c0100000000000000000000000000000000000000 00000000000000000060003504166312065fe08114604d578063fb1669ca146071575b60008 0fd5b348015605857600080fd5b50605f6088565b60408051918252519081900360200190f3 5b348015607c57600080fd5b506086600435608e565b005b60005490565b6000555600a1656 27a7a72305820cf77c0639acc98ef9acefd6386fe697d2d71d7478f3b9d459d8778557e3061 4a0029
  • 29. Working with Smart Contract through web3 ABI
  • 30. Working with Smart Contract through web3 • Live Coding: HTML + Javascript + jquery + web3.js + MetaMask • Boilerplate: https://goo.gl/jychkY
  • 31. Testing $ npm install –g http-server $ http-server .
  • 32. Understand Gas Price • See it on etherscan
  • 33. Coding: Types • Live Coding: • bool • int, uint, int8, uint8, int16, uint16, …, int256, uint256 • address • fixed, ufixed • byte, bytes • string • struct • mapping • enum • []
  • 34. Coding: Inheritance • “is” contract A { uint a; function getA() public view returns(uint) { return a; } } contract B is A { uint b; function getBsumA() public view returns(uint) { return b + a; } }
  • 35. Coding: pure function function func(uint x, uint y) public pure returns (uint) { return x * (y + 42); }
  • 36. Coding: view function uint a; function func() public view returns (uint) { return a; }
  • 37. Coding: pure & view function through read fn uint a; function func() public view returns (uint) { return a; }
  • 38. Coding: pure & view function through read fn uint a; function func() public view returns (uint) { return a; } function func2() public view returns (uint) { return func(); }
  • 39. Coding: pure & view function through write fn uint a; uint b; function func() public view returns (uint) { return a; } function func3(uint someVal) public returns (uint) { b = someVal; return func() + b; }
  • 40. Coding: require function setBalance(uint newBalance) public { // Check if newBalance is in [0, 10000] require(newBalance <= 10000); // Set new balance balance = newBalance; }
  • 41. Coding: Visibility • public – Can be either called internally or via messages. • private – Only visible for the contract they are defined and not in the derived contract. • internal – Can only be accessed internally (i.e. from within the current contract or contracts deriving from it) • external - Can be called from other contracts and via transactions. Cannot be called internally.
  • 42. Coding: modifier contract Ownable { address public owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); function Ownable() public { owner = msg.sender; } modifier onlyOwner() { require(msg.sender == owner); _; } function transferOwnership(address newOwner) public onlyOwner { require(newOwner != address(0)); emit OwnershipTransferred(owner, newOwner); owner = newOwner; } } https://goo.gl/ZLThph
  • 43. Coding: modifier pragma solidity ^0.4.18; contract SimpleContract is Ownable { uint balance; constructor() public { // Set initial balance as 1000 balance = 1000; } function setBalance(uint newBalance) public onlyOwner { // Set new balance balance = newBalance; } function getBalance() public view returns(uint) { return balance; } } contract Ownable { address public owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); function Ownable() public { owner = msg.sender; } modifier onlyOwner() { require(msg.sender == owner); _; } function transferOwnership(address newOwner) public onlyOwner { require(newOwner != address(0)); emit OwnershipTransferred(owner, newOwner); owner = newOwner; } } https://goo.gl/ZLThph
  • 44. Coding: Events pragma solidity ^0.4.18; contract SimpleContract is Ownable { event BalancedUpdated(uint balance); uint balance; constructor() public { // Set initial balance as 1000 balance = 1000; } function setBalance(uint newBalance) public onlyOwner { // Set new balance balance = newBalance; // Fire Event emit BalanceUpdated(newBalance); } function getBalance() public view returns(uint) { return balance; } }
  • 45. Coding: Events pragma solidity ^0.4.18; contract SimpleContract is Ownable { event BalancedUpdated(uint balance); uint balance; constructor() public { // Set initial balance as 1000 balance = 1000; } function setBalance(uint newBalance) public onlyOwner { // Set new balance balance = newBalance; // Fire Event emit BalanceUpdated(newBalance); } function getBalance() public view returns(uint) { return balance; } } var event = SimpleContract.BalanceUpdated(); event.watch(function(error, result) { // Will be called once there is new event emittted });
  • 46. Coding: payable function register() public payable { registeredAddresses[msg.sender] = true; }
  • 47. Redeploying • Live Demo: See the difference [new contract address]. • Test the event.
  • 49. What is ERC-20? // https://github.com/ethereum/EIPs/issues/20 interface ERC20Interface { function totalSupply() public view returns (uint supply); function balanceOf(address _owner) public view returns (uint balance); function transfer(address _to, uint _value) public returns (bool success); function transferFrom(address _from, address _to, uint _value) public returns (bool success); function approve(address _spender, uint _value) public returns (bool success); function allowance(address _owner, address _spender) public view returns (uint remaining); function decimals() public view returns(uint digits); event Approval(address indexed _owner, address indexed _spender, uint _value); } • ERC-20 is just a contract implemented the interface defined by ERC-20 standard.
  • 50. Truffle: Solidity Framework • Highly recommended • Testable code • Incremental deployment • etc.
  • 51. Store file on IPFS • https://ipfs.io/ • Upload through Infura
  • 52. Alternative Public Blockchain • EOSIO: Smart Contract • TomoChain: Smart Contract (Ethereum-Forked) • NEM: Smart Contract • Stellar: Payment (Non Turing Completed)
  • 53. Create your own Smart Contract blockchain • Hyperledger Fabric • Use Golang to write Smart Contract