Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction 2
Congratulations! 10
www.globalopenversity.org
Global Open Versity Developing Smart contract on Ethereum Blockchain using Truffle Framework v1.2
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
• 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.
Fig. 1a
Fig. 1b
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
truffle init
Fig. 2
Note: there is a GUI for Ganache, but because we are such haxors, we will use the CLI.
$ 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
$ 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.
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
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
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id
}
}
};
9. Now from our ethProject shell, we can now deploy our contract to the blockchain:
$ truffle compile
$ truffle migrate
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...
$ truffle console
{ [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)> storeData.toString()
'36'
14. That it is, you’re done with this section and this project
$ 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
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
2. You should now be able to run "truffle console" command without any issue, as shown below:
$ truffle console
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.
-----------------------------------------------
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.
10
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org