Sei sulla pagina 1di 11

Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.

Global Open Versity


Blockchain DevOps Hands-on Labs Training Manual

Developing Smart Contract on Ethereum Private Blockchain using


Truffle Framework
Kefa Rabah
Global Open Versity, Vancouver Canada
krabah@globalopenversity.org
www.globalopenversity.org

Table of Contents Page No.

DEVELOPING SMART CONTRACT ON ETHEREUM PRIVATE BLOCKCHAIN USING TRUFFLE


FRAMEWORK 1

DEVELOPING SMART CONTRACT ON ETHEREUM PRIVATE BLOCKCHAIN USING TRUFFLE


FRAMEWORK 2

Introduction 2

Part 1: Install Blockchain Required Tools 2


Step 1: Installations of Tools 3
Step 2: Install Truffle 3
Step 3: Install Ganache 4

Part 2: Building Smart Contract with Truffle 4


Step 1: Create a Project 4
Step 2: Testing our Contract 8

Error running “truffle console” command: 9


Solution 9

Congratulations! 10

A Globalopenversity Open Access Technical Academic Publications


Delivering Cutting-edge Technology at your Fingertips in the 21st Century
1
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

Global Open Versity


Blockchain DevOps Hands-on Labs Training Manual

Developing Smart Contract on Ethereum Private Blockchain using


Truffle Framework
By Kefa Rabah, krabah@globalopenversity.org April 06, 2018 The Lake Institute

Introduction
Smart contract is a code that can be deployed inside blockchain. Once it is deployed, Users can trigger it
by using its address. In this tutorial, the smart contract is written with solidity language and deployed on
private ethereum blockchain network. The development steps are simplified by using Truffle framework.

To get yourself up to-speed, check our Blockchain Labs Tech Series to quick you a quick start:

1. https://www.scribd.com/document/373203852/Step-By-Step-Guide-Installing-Ethereum-Building-
a-Blockchain-on-Ubuntu-16-04-Linux-Server

2. https://www.scribd.com/document/374058743/Step-By-Step-Guide-Building-Deploying-a-Private-
Blockchain-Network-on-Windows

3. https://www.scribd.com/document/375670977/Step-By-Step-Guide-Build-Deploy-Ethereum-
Blockchain-Smart-Contract

Part 1: Install Blockchain Required Tools


The tools we will use
The most prominent tools at the moments are:
• Truffle: A development environment, testing framework and asset pipeline for Ethereum. In other
words, it helps you develop smart contracts, publish them, and test them, among other things. You
can read the docs of the Truffle suite for more informations.

• Ganache: It was called TestRPC before, if you have read a tutorial from a few months ago, chances
are they use TestRPC with Truffle, but it was renamed upon the integration of TestRPC within the
Truffle Suite. What Ganache does is simple, it creates a virtual Ethereum blockchain, and it generates
some fake accounts that we will use during development.
• Mist: It’s a browser for decentralized web apps. It seeks to be the equivalent of Chrome or Firefox, but
for Dapps. It’s still insecure and you shouldn’t use it with untrusted dapps as of yet.
• Ethereum wallet: It’s a version of Mist, but only opens one single dapp, the Ethereum Wallet. Mist and
Ethereum Wallet are just UI fronts. And we need a core that will connect us to an Ethereum
blockchain(It could be the real Ethereum blockchain, or a test one).
2
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

• Geth: Is the core application on your computer that will connect you to a blockchain. It can also start a
new one (in our case we will create a local test blockchain), create contract, mine ether etc.

We will start by using Truffle and Ganache, and then use Truffle with geth and Mist.

Step 1: Installations of Tools


1. The requirements for this tutorial are that you know what is and how to use a command-line tool, and
you are a bit familiar with NPM, which we must download and install first. At the time of writing this
article, we used “node-v9.8.0-x64.exe”

Step 2: Install Truffle


2. Open up a command-line, and type the next part:

npm install -g truffle

Fig. 1a

3. If all is OK, then type command truffle,

Fig. 1b

4. If something goes wrong we advise you to read more on Truffle’s docs.

3
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

Let’s start Truffle


5. First, create a new folder, and type the next line:

truffle init

6. It will initialize an empty truffle project.

Step 3: Install Ganache


7. Then, install Ganache’s command-line interface:

npm install -g ganache-cli

8. If all is OK, then type command ganache-cli,

Fig. 2

9. If you are unsure about something, here is Ganache’s Github page.

Note: there is a GUI for Ganache, but because we are such haxors, we will use the CLI.

Part 2: Building Smart Contract with Truffle

Step 1: Create a Project


1. We shall create a project under folder/directory named ethProject and change into the directory

$ mkdir ethProject

4
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

$ cd ethproject

Note: for windows users use appropriate way to create and change into the directory/folder

2. Inside the directory ethProject, issue the command:

$ truffle init

Note: you will see that truffle created the file structure to us.

Fig. 3a

Fig. 3b

3. Go to the contracts folder and create "Storage.sol" file and then write the code for the smart
contract in it.

pragma solidity ^0.4.8;


contract Storage {
uint256 storedData;
function set(uint256 data) {
storedData = data;
}
function get() constant returns (uint256) {
return storedData;
}
}

4. Now go to the "migrations/2_deploy_contracts.js" and modify it to look like as follows:

var Storage = artifacts.require("./Storage.sol");

5
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

module.exports = function(deployer) {
deployer.deploy(Storage);
};

Note: it is important to write the ‘2’ because Truffle will execute the migrations in the order of the
prefix.

Note: now that we got the basic set up on we need to deploy it to a blockchain. On my previous
Blockchain Labs Tech Series, we ran our own private network with information on how to do it is found
here.

5. For simplistic sake, let’s use testrpc which does the job well for testing development purposes.

ganache-cli

OR

testrpc

Fig. 4

Let’s check if we are able to call the contract functions.

6. Back to the shell, we can now deploy our contract to the blockchain:

7. Next, let’s open the truffle.js file using text editor of your choice:
6
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

nano truffle.js

and do add the following to it

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id
}
}
};

8. Save and exit ^O/Yes followed by ^X.

9. Now from our ethProject shell, we can now deploy our contract to the blockchain:

$ truffle compile

$ truffle migrate

Note: the contract should have been deployed.

7
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

$ truffle migrate
Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0xb8489a2bdf78a684606df3be0f2b646f9272f8df945d9c6744fe60bbf9f6a81f
Migrations: 0x0af7febb76456af1959860db7c5af0d30467d2e1
Saving successful migration to network...
... 0x17f3fa25f06800a9fbc17aaabba05cedbae9e60f4b4e366b579176eba5affb69
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying Storage...
... 0x1b328b03b9ef10c72fef38fe6d79efd0e7a31ef147c3d7a2a5c135faa1bc149b
Storage: 0xe11ed418c16d16e8df9a614fe88751546cf47317
Saving successful migration to network...
... 0x0e1c998aa351e20f832a3129c0302405b55d76cd342db34b19446ca845deaa81
Saving artifacts...

10. Let’s check if we are able to call the contract functions.

$ truffle console

11. Issue ^C to exit.

Step 2: Testing our Contract


12. To test our project under the truffle console, issue the command:

truffle(development)> Storage.deployed().then(instance =>


instance.get.call()).then(result => storeData = result)

{ [String: '0'] s: 1, e: 0, c: [ 0 ] }
8
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

Repeat as follows:

truffle(development)> storeData.toString()
'0'

13. Alright, let’s see now we if can set storeData to be value 36.

truffle(development)> Storage.deployed().then(instance =>


instance.set.sendTransaction(36)).then(result => newStorageData = result)
'0xc5e2f9c9da4cf9f563c8e59073d5b6ca9458f112a6dcfc14aaea7c16a99422d4'

truffle(development)> Storage.deployed().then(instance =>


instance.get.call()).then(result => storeData = result)
{ [String: '36'] s: 1, e: 1, c: [ 36 ] }

truffle(development)> storeData.toString()
'36'

14. That it is, you’re done with this section and this project

Error running “truffle console” command:

$ truffle console
No network available. Use `truffle develop` or add network to truffle.js config.

Solution
1. Use your desired text editor to open truffle.js file

$ nano truffle.js

and do add the following code to it

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
9
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2

network_id: "*" // Match any network id


}
}
};

2. You should now be able to run "truffle console" command without any issue, as shown below:

$ truffle console

15. Issue ^C to exit.

Congratulations!
Congrats on finishing the tutorial and having deployed smart contract using truffle framework. You have
also learned how to install Truffle framework, and Ganache command line tool. You also learned how to
write and deploy smart contract using truffle.

Available from Scribd.com:

A Creative Common Publication:

-----------------------------------------------
Kefa Rabah is the Founder of The Lake Institute. Kefa is knowledgeable in several fields of Science &
Technology, Information Security Compliance, Blockchain Technology, Distributed Ledger Technology
(DLT) and Project Management, and Renewable Energy Systems. He is also the founder of Global Open
Versity, a place to enhance your educating and career goals using the latest innovations and
technologies.

Fellow us on Twitter: The Lake Institute and Kefa Rabah

A Globalopenversity Open Access Technical Academic Publications


Delivering Cutting-edge Technology at your Fingertips in the 21st Century

10
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org

Potrebbero piacerti anche