Blockchain & Solidity Program Lab Manual
ISBN 9788119221646

Highlights

Notes

  

Prog. 18:: Working with Smart Contracts in Ethereum

Smart Contracts

In the form of code

Stored on a blockchain

Executes under given conditions

Owner creates the contract

Contract replicates among all the nodes

Tenant deposits to the contract

Contract’s State changes on all the nodes

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

OpenRemix:remix.ethereum.org

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

Solidity

Object-oriented

Contract-oriented

High-level language

Influenced by C++,Python, and JavaScript

Target Ethereum Virtual Machine(EVM)

Serpentas an Alternative?

Low-level language

Complex compiler

StartCoding

Setter and Getter: Set and get the information.

Compile the Contract

Compile tab: Start to compile button Run tab: Environment=JavaScript VM

JavaScriptVM: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.

InjectedProvider:Remix will connect to an injected web3 provider.Mistand Metamask are example of providers that inject web3,thustheycanbeusedwith this option.

Web3Provider:Remixwillconnecttoaremotenode.Youwillneedtoprovide the URLaddress to the selected provider: geth, parity or any Ethereum client.

GasLimit:Themaximumamountofgasthatcanbesetforalltheinstructionsofa contract.

Value:Inputsomeetherwiththenextcreatedtransaction(wei=10-18ofether).

TypesofBlockchainDeployment

Private: e.g., Ganache sets a personal Ethereum blockchain for running tests, executing commands, and inspecting the state while controlling how the chain operates.

PublicTest(Testnet):Like Ropsten,Kovan and Rinkeby which are existing public blockchains used for testing and which donot 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.

Interact with the Contract

Setter=RedButton:Createstransaction

Getter=BlueButton:Justgivesinformation

Additional features

Transferring funds from an account to the contract

Saving the address of the contract creator

Limiting the users’ access to functions

Withdrawing funds from the contract to an account

Transfer money to the contract

Constructor

• Will be called at the creation of the instance of the contract

Withdraw funds

Modifier:Conditions you want to test in other functions

First the modifier will execute, then function

Now deploying a smart contract on an external blockchain

Remix

Ganache

MyEtherWallet

Geth

1

Configuring the Blockchain

-

-

-

+

2

DeployingtheBlockchain

Not Persisten

t

+

-

+

3

Developingthecontract

+

-

-

+

4

Compilingthecontract

+

-

-

+

5

Creating user account

+

+

+

+

6

Deployingthecontract

+

-

+

+

7

CreatingtheUIfor interacting

+

-

+

+

8

Run theclient

+

-

+

+

9

Interactwiththecontract& have fun

+

-

+

+

10

Monitoringtheexecution

-

+

-

+

Run Ganache

MyEtherWallet

Add your custom network that you want to test your contracts on

Import your RPC server address and the port number from Ganache toMyEtherWallet

MyEtherWallet

Contracts tab:Deploy Contract

Type your contract and compile it

Click on Details Button:access ByteCode to import it to MyEtherWallet

Ganache

Access your private key for signing your contract in MyEtherWallet

    1. Paste the contract’s ByteCode fromRemix

    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

Click on Sign Transaction button to deploy your contract

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.

Interacting with the smart contract

Ganache Transactions tab:Copy the created contract address

Remix

Copy the ABI (ABI is the interface that tells MyEtherWallet how to interact with the contract)

Contracts tab:

Interact with Contract=Paste the contract address from Ganache and the ABI from Remix

You now can interact with the contract by selecting a function and invoking it

If you select the getValue function you will receive the value without paying any gas

(There is no operation cost for getting information)

If you choose a function that updates the state of the contract, you will need to pay gas for it in a transaction.

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

Creates to rage of the blockchain

Deploy blockchain odes

Connect MyEtherWallet to your blockchain to interact with it

Geth help

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

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

Inside the Blockchain Folder

Geth folder:Store your database

keystore:Store your Ethereum accounts

Start the Ethereum peer node

Start the blockchain

Network id provides privacy for your network.

Other peers joining your network must use the same network id.

Blockchain started

Type admin.nodeInfo to get the information about your current node.

Create an account

Type personal.newAccount to create as many accounts as you need

See the created account(s)

Mining

Typeminer.start() to start mining

Typeminer.stop() to stop mining