0% found this document useful (0 votes)
294 views

A Hands-On Tutorial: Working With Smart Contracts in Ethereum

This document provides an overview of different tools for working with smart contracts in Ethereum, including Remix, Ganache, MyEtherWallet, and Geth. It discusses the functionality each tool provides, and recommends which tools to use for different purposes such as developing contracts, deploying contracts, interacting with contracts, and monitoring contract execution. The document then walks through hands-on examples for developing a simple smart contract in Remix, compiling, deploying to a private blockchain, interacting with the contract, and adding additional functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
294 views

A Hands-On Tutorial: Working With Smart Contracts in Ethereum

This document provides an overview of different tools for working with smart contracts in Ethereum, including Remix, Ganache, MyEtherWallet, and Geth. It discusses the functionality each tool provides, and recommends which tools to use for different purposes such as developing contracts, deploying contracts, interacting with contracts, and monitoring contract execution. The document then walks through hands-on examples for developing a simple smart contract in Remix, compiling, deploying to a private blockchain, interacting with the contract, and adding additional functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

30.08.

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

Different tools provide different functionality


Tools
Remix Ganache MyEtherWallet Geth
Activities 1 3
1 Configure the Blockchain - - - +
2 Deploy the Blockchain Not
Persistent
+ - + 2 4
3 Develop the contract + - - +
4 Compile the contract + - - + 5
5 Create user account + + + +
6 Deploy the contract + - + +
6
Create the UI for
7 + - + +
interacting
8 Run the client + - + + 8 7
Interact with the contract &
9 + - + +
have fun
9
10 Monitor the execution - + - +
https://remix.ethereum.org/
http://truffleframework.com/ganache/ 10
1
https://github.com/kvhnuke/etherwallet/releases/tag/v3.21.06

1
30.08.2018

Use which tool for what purpose? (1/2)


• Use Geth for everything?
• Powerful but command-line only

• What should I use?


• As a starting point for developing contracts – mostly Remix

• What cannot Remix do?


• Configure the blockchain
• Create real (non-test) user accounts and transfer funds between user
accounts
• Monitor the execution
• Other advanced operations
2

Use which tool for what purpose? (2/2)


• Why use Ganache?
• To inspect and monitor the execution
• To visualize certain elements in a better way

• Why use MyEtherWallet?


• To create a personal wallet (real user account) and transfer funds between
user accounts

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

Open Remix : remix.ethereum.org


• An open source tool for writing, compiling and testing Solidity contracts

3
30.08.2018

Start Coding
• Setter and Getter: Set and get the information.

Variable

Getter function

Setter function

Compile the Contract


• Compile tab: Start to compile button

4
30.08.2018

Set Environment (1/2)


• Run tab: Environment = JavaScript VM

Set Environment (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 transactions of
a contract.
• Value: The amount of value for the next created transaction (wei = 10-18 of ether).

5
30.08.2018

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: Like Ropsten, Kovan and Rinkeby which are existing public
blockchains used for testing and which do not use real funds.
• Public Real: Like Bitcoin and Ethereum which are used for real and
which available for everybody to join.

10

Deploy the Contract on the Private Blockchain of


Remix
• Run tab: Deploy button

11

6
30.08.2018

Interact with the Contract


• Setter = Red Button: Creates transaction
• Getter= Blue Button: Just gives information

Press getValue to see the initial amount 1

Input a value and press setValue button


2
to create and confirm the transaction
Press getValue again to see the result 3

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

Only the contract


creator is permitted
to set value

15

8
30.08.2018

Receive ether (1/2)


• Transfer money to the contract

Payable keyword
allows receiving
ether

We can get the


balance of the
contract
16

Receive ether (2/2)


Input the value as wei
1
(10-18 of ether)

Click the receiveFunds button to


2 transfer the money to the
contract

17

9
30.08.2018

Withdraw funds
• Transfer ether from the contract to
the user account

Transfer some money from the


contract to the mentioned
account
18

Solidity version More details in the source code:


https://github.com/ramyhardan/proof-of-existence

An event can be listened to by any client

Getters are automatically generated for public vars

The modifier code can be prepended to any function

Constructor is executed when the contract is created

The core function of this contract, allows its owner


to confirm a document id with a certain content

ProofCreated event is emitted to the blockchain

View/constant functions are read-only and do not cost gas


19

10
30.08.2018

Now deploying a smart contract on an


external blockchain
Tools 1 3
Remix Ganache MyEtherWallet Geth
Activities
1 Configuring the Blockchain - - - + 2 4
Not
2 Deploying the Blockchain Persistent
+ - +
3 Developing the contract + - - + 5
4 Compiling the contract + - - +
5 Creating user account + + + + 6
6 Deploying the contract + - + +
Creating the UI for
7 + - + +
interacting 8 7
8 Run the client + - + +
Interact with the contract & 9
9 + - + +
have fun
10 Monitoring the execution - + - +
10
20

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

2. Gas Limit will


automatically be
calculated

3. Paste your private


key from Ganache

4. Click Unlock

5. Now you have


access to your wallet 28

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

Interacting with the smart contract

Extract the contract address Extract the ABI (Application Binary


from Ganache Interface) of the code from Remix

Interact with the contract in MyEtherWallet


(Import the contract address and the ABI into the MyEtherWallet)

Select a function Write Pay some gas


Read

Generate the transaction

Receive the result

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

Create your own 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

39

20
30.08.2018

Homebrew (package manager for mac)


• Install homebrew with the command from its website: https://brew.sh/

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

• Create and store the file as genesis.json


43

22
30.08.2018

Create the storage of the blockchain


• Go to the directory of the genesis.json file
• Specify directory of your blockchain
• Create the storage from the genesis block

Folder name of your


blockchain

44

Inside the Blockchain Folder


• geth folder: Store your database
• keystore: Store your Ethereum accounts

45

23
30.08.2018

Start the Ethereum peer node


• Start the blockchain

• Networkid provides privacy for your network.


• Other peers joining your network must use the same networkid.

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

• See the created account(s)

48

Mining
• Type miner.start() to start mining

49

25
30.08.2018

Thank you

50

26

You might also like