A Hands-On Tutorial: Working With Smart Contracts in Ethereum
A Hands-On Tutorial: Working With Smart Contracts in Ethereum
2018
A hands-on tutorial:
Working with Smart Contracts in
Ethereum
by Roman Vitenberg
Joint work with Mohammad H. Tabatabaei from the University of Oslo
1
30.08.2018
2
30.08.2018
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
3
30.08.2018
Start Coding
• Setter and Getter: Set and get the information.
Variable
Getter function
Setter function
4
30.08.2018
• Gas Limit: The maximum amount of gas that can be set for all the transactions of
a contract.
• Value: The amount of value for the next created transaction (wei = 10-18 of ether).
5
30.08.2018
10
11
6
30.08.2018
12
Additional features
• Saving the address of the contract creator
• Limiting the users’ access to functions
• Transfering funds from an account to the contract
• Withdrawing funds from the contract to an account
13
7
30.08.2018
Constructor
• A function with the name of the contract
• Will be called at the creation of the instance of the contract
We want to save
the address of the
contract creator
14
Modifier
• Conditions you want to test in other functions
• First the modifier will execute, then the invoked function
15
8
30.08.2018
Payable keyword
allows receiving
ether
17
9
30.08.2018
Withdraw funds
• Transfer ether from the contract to
the user account
10
30.08.2018
Run Ganache
21
11
30.08.2018
MyEtherWallet
• add your custom network that you want to test your contracts on
22
Import your RPC server address and the port number from
Ganache to MyEtherWallet
23
12
30.08.2018
MyEtherWallet
• Contracts tab: Deploy Contract
24
Remix
• Type your contract and compile it
25
13
30.08.2018
Remix
Click on Details Button: access ByteCode to import it to MyEtherWallet
26
Ganache
Access your private key for signing your contract in MyEtherWallet.
27
14
30.08.2018
MyEtherWallet
1. Paste the
contract’s ByteCode
from Remix
4. Click Unlock
MyEtherWallet
Click on Sign Transaction button to deploy your contract
29
15
30.08.2018
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.
30
31
16
30.08.2018
Ganache
Transactions tab: Copy the created contract address
32
Remix
Click on Details button: Copy the ABI
(ABI is the interface that tells MyEtherWallet how to interact with the
contract)
33
17
30.08.2018
MyEtherWallet
Contracts tab:
Interact with Contract = Paste the contract address from Ganache and
the ABI from Remix
34
MyEtherWallet
You now can interact with the contract by selecting a function and
invoking it
35
18
30.08.2018
MyEtherWallet
If you select the getValue function you will receive the value
without paying any gas
(There is no operation cost for getting information)
36
MyEtherWallet
If you choose a function that updates the state of the contract,
you will need to pay gas for it in a transaction.
37
19
30.08.2018
MyEtherWallet
Now if you try getValue function again, you will see the change.
38
39
20
30.08.2018
40
Geth
• An Ethereum program written in Go
41
21
30.08.2018
Geth help
42
Genesis block
• The first block in the chain and a json file that stores the configuration
of the chain
22
30.08.2018
44
45
23
30.08.2018
46
Blockchain started
• Type
admin.nodeInfo
to get the
information
about your
current node
47
24
30.08.2018
Create an account
• Type personal.newAccount to create as many accounts as you need
48
Mining
• Type miner.start() to start mining
49
25
30.08.2018
Thank you
50
26