Blockchain Hands On Tutorial
Blockchain Hands On Tutorial
3
Smart Contracts
• In the form of code
• Stored on a blockchain
• Executes under given conditions
- K. Delmolino, M. Arnett, A. E. Kosba, A. Miller, and E. Shi, “Step by Step Towards Creating a Safe Smart Contract:
Lessons and Insights from a Cryptocurrency Lab,” IACR Cryptology ePrint Archive, vol. 2015, p. 460, 2015. 4
Smart Contracts Example (1/3)
Blockchain
• Owner creates the contract
• Contract replicates among all the nodes
Owner
Creat Contract
e
Tenant
5
Smart Contracts Example (2/3)
Blockchain
• Tenant deposits to the contract
• Contract’s State changes on all the nodes
Owner
Tenant
Deposit
6
Smart Contracts Example (3/3)
Blockchain
• Owner checks the contract’s balance
• Contract’s state is fetched from one node
Owner
Check Ba
lance
Tenant
7
Smart Contracts
1. Developing a simple contract
2. Compiling the contract
3. Deploying the contract
4. Interacting with the contract
5. Adding more functions to our code to make it more practical
8
Open Remix : remix.ethereum.org
• An open source tool for writing, compiling and testing Solidity contracts
9
Solidity
• Object-oriented
• Contract-oriented
• High-level language
• Influenced by C++, Python, and JavaScript
• Target Ethereum Virtual Machine (EVM)
Serpent as an Alternative?
• Low-level language
• Complex compiler
10
Start Coding
• Setter and Getter: Set and get the information.
Variable
Getter function
Setter function
11
Compile the Contract
• Compile tab: Start to compile button
12
Set Deployment Parameters (1/2)
• Run tab: Environment = JavaScript VM
13
Set Deployment Parameters (2/2)
• JavaScript VM: All the transactions will be executed in a sandbox blockchain in the
browser. Nothing will be persisted and a page reload will restart a new blockchain
from scratch, the old one will not be saved.
• Injected Provider: Remix will connect to an injected web3 provider. Mist and
Metamask are example of providers that inject web3, thus they can be used with
this option.
• Web3 Provider: Remix will connect to a remote node. You will need to provide
the URL address to the selected provider: geth, parity or any Ethereum client.
• Gas Limit: The maximum amount of gas that can be set for all the instructions of a
contract.
• Value: Input some ether with the next created transaction (wei = 10-18 of ether).
14
Types of Blockchain Deployment
• Private: e.g., Ganache sets a personal Ethereum blockchain for
running tests, executing commands, and inspecting the state while
controlling how the chain operates.
• Public Test (Testnet): Like Ropsten, Kovan and Rinkeby which are
existing public blockchains used for testing and which do not use real
funds. Use faucet for receiving initial virtual funds.
• Public Real (Mainnet): Like Bitcoin and Ethereum which are used for
real and which available for everybody to join.
15
Deploy the Contract on the Private Blockchain of
Remix
• Run tab: Deploy button
16
Interact with the Contract
• Setter = Red Button: Creates transaction
• Getter= Blue Button: Just gives information
18
Receive ether (1/2)
• Transfer money to the contract
Payable keyword
allows receiving
ether
Hidden Code:
Address(this).balance += msg.value; We can get the
balance of the
contract 19
Receive ether (2/2)
Input the value as wei
1
(10-18 of ether)
We want to save
the address of the
contract creator
21
Withdraw funds
• Modifier: Conditions you want to
test in other functions
• First the modifier will execute, then
the invoked function
24
MyEtherWallet
• add your custom network that you want to test your contracts on
25
Import your RPC server address and the port number from
Ganache to MyEtherWallet
26
MyEtherWallet
• Contracts tab: Deploy Contract
27
Remix
• Type your contract and compile it
28
Remix
Click on Details Button: access ByteCode to import it to MyEtherWallet
29
Ganache
Access your private key for signing your contract in MyEtherWallet.
30
MyEtherWallet
1. Paste the
contract’s ByteCode
from Remix
4. Click Unlock
32
Ganache
You can see now you have one transaction for your address and your
balance has been changed because of the amount of gas you paid for
creating the contract.
33
Interacting with the smart contract
Read
Generate the transaction
34
Ganache
Transactions tab: Copy the created contract address
35
Remix
Copy the ABI
(ABI is the interface that tells MyEtherWallet how to interact with the
contract)
36
MyEtherWallet
Contracts tab:
Interact with Contract = Paste the contract address from Ganache and
the ABI from Remix
37
MyEtherWallet
You now can interact with the contract by selecting a function and
invoking it
38
MyEtherWallet
If you select the getValue function you will receive the value
without paying any gas
(There is no operation cost for getting information)
39
MyEtherWallet
If you choose a function that updates the state of the contract,
you will need to pay gas for it in a transaction.
40
Create Custom Ethereum Blockchain
• Instead of using Ganache with its default properties for private
blockchain you can run your own blockchain
• Install Geth: One of the implementations of Ethereum written in Go
• Create the genesis block
• Create storage of the blockchain
• Deploy blockchain nodes
• Connect MyEtherWallet to your blockchain to interact with it
41
Geth help
42
Genesis block
• The first block in the chain and a json file that stores the configuration
of the chain
44
Inside the Blockchain Folder
• geth folder: Store your database
• keystore: Store your Ethereum accounts
45
Start the Ethereum peer node
• Start the blockchain
46
Blockchain started
• Type
admin.nodeInfo
to get the
information
about your
current node
47
Create an account
• Type personal.newAccount to create as many accounts as you need
48
Mining
• Type miner.start() to start mining
49
Thank You!
Any Questions?
Mohammad H. Tabatabaei
[email protected]
50