Learn to use Blockchain in apps — Lab 3

Andreea-Elena Dragnoiu (Panait)
Game of Life
Published in
7 min readMar 5, 2020

In this lab, we will make a project using truffle framework (https://www.trufflesuite.com), we will develop a simple smart contract (in Solidity) using truffle, we will connect our smart contract with our local instance of blockchain (Ganache) and we will deploy our smart contract to a public blockchain test network.

(The second lab from Learn to use Blockchain in apps series can be found at: https://medium.com/@panaitandreea01/student-blockchain-labs-lab-2-dd78252a4067)

1. Set up personal Blockchain — Ganache

  • Instructions on how to install Ganache:

Create a workspace. Notice the 10 accounts with 100 ethers that are available in Ganache. More info about Ganache:

2. Configure environment for developing smart contracts

  • Install Node.js — Node Package Manager — NPM: https://nodejs.org/en
  • Truffle Framework (latest version): npm install -g truffle
  • Create new project:

mkdir BlockchainPrj

cd BlockchainPrj

  • Initialize Truffle project:

truffle init

  • Open Truffle project in VS Code with the command:

code .

  • Create package.json for installing project dependencies:

npm init -> for creating a package.json file

  • For our project, we will use the following packages:

— dotenv: npm i dotenv (https://www.npmjs.com/package/dotenv)

— ganache-cli: npm i -g ganache-cli (https://www.npmjs.com/package/ganache-cli)

— truffle (previous installed)

— @truffle/hdwallet-provider: npm i @truffle/hdwallet-provider (https://www.npmjs.com/package/@hardfork/truffle-hdwallet-provider)

At the time of writing, the latest version for dependencies used were:

For interacting with the frontend, we will use (in next lab)

— truffle-contract (https://www.npmjs.com/package/@galtproject/truffle-contract)

lite-server ( https://www.npmjs.com/package/lite-server)

3. Create smart contract (file with extension .sol) in the contracts folder.

Below is an example of a smart contract:

Compile smart contract: truffle compile

4. Connect to smart contract using Ganache: use truffle console to get info about the smart contract from Ganache

Configure truffle-config.js to use Ganache as the development network. Notice that the host and the port are those from Ganache, which can be seen in the settings section, on Server page (See below)

Modify truffle-config.js to use the Ganache network:

5. Deploy smart contract on blockchain: create file named 2_deploy_contracts.js, in migrations folder (the migrations folder is used for deployment, when the truffle migrate command is run, the compiler checks for the files that are in the migrations folder and executes each of them, one by one, starting with the file that begins with 1_ ; each file name from this folder should begin with a number and an _)

Trending Cryptocurrency Hub Articles:

1. Ethereum and Blockchain Technology

2. The CryptoKitties Economy

3. It’s Time to Take Back Control! How Pocketinns Will Democratize Trade

4. A Crypto that will Pay You

Then deploy smart contract on Ganache using the following command:

truffle migrate

After migrating the smart contracts, notice the accounts from Ganache !

One can communicate with the deployed smart contract in the truffle console, using the command:

truffle console

Then, for accessing the functions of our smart contract, we can use the following syntax:

MyContract.deployed().then((instance) => { app = instance } )

This access the deployed() function for our smart contract and creates an instance for the deployed smart contract, which we can later use in the truffle console, like:

6. Test smart contract: before deploying the smart contract on public blockchain, it is important to test it ! (But for our purpose, we can test the smart contract later)

TODO: we will test the smart contract next time

7. Connect to public blockchain (testnet)

In the .env file you can have something like:

MNEMONIC=your mnemonic seed phrase goes here

— sign up to Infura and create a project

— view the project, choose a blockchain network and copy the endpoint which will be used to configure the public blockchain network:

For Kovan network: https://kovan.infura.io/v3/PROJECT-ID

For Ropsten network: https://ropsten.infura.io/v3/PROJECT-ID

— Add in the .env file the Infura endpoint:

KOVAN_URL=YOUR_KOVAN_URL_WITH_PROJECT_ID

ROPSTEN_URL=YOUR_ROPSTEN_URL_WITH_PROJECT_ID

  • Update project settings (the truffle-config.js file) to connect to Ethereum node:
require('dotenv').config() //here we use the dotenv packageconst HDWalletProvider = require('@truffle/hdwallet-provider');module.exports = {
networks: {//the network for development is Ganache
development: { host: "127.0.0.1", // Localhost (default: none) port: 7545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) }, kovan: {//the public blockchain network provider: () => new HDWalletProvider(
process.env.MNEMONIC,
process.env.KOVAN_URL
),
gas: 5000000, gasPrice: 25000000000, network_id: 42 //kovan network id},
ropsten: { //it is commented in the truffle-config.js
provider: () => new HDWalletProvider(
process.env.MNEMONIC,
process.env.ROPSTEN_URL),
network_id: 3, // Ropsten's id gas: 5500000, // Ropsten has a lower block limit than mainnet confirmations: 2, // # of confs to wait between deployments. (default: 0) timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) skipDryRun: true // Skip dry run before migrations? (default: false for public nets )}..... //here are other parts from truffle-config.js}

Useful: a list for blockchain networks with their id https://ethereum.stackexchange.com/questions/17051/how-to-select-a-network-id-or-is-there-a-list-of-network-ids

  • Obtain ether (depending on the blockchain testnet that is used):

Copy an address (account) from the 10 accounts that are available in Ganache and use it in one of the following links, in order to get free test ethers (to deploy the smart contract on the chosen public network) from the following sites (note: there are more sites of this kind) :

  • Test connection to public blockchain:

truffle console — network ropsten

Interact with the network:

web3.eth.getBlock(‘latest’).then(console.log)

More details about web3.js, can be found at:

  • Deploy smart contracts to network:

Try to deploy on the Ropsten network:

truffle migrate - -network ropsten

(Deploy failed, I did not have ethers for Ropsten network.)

Exit from truffle console:

.exit

Try to deploy on the Kovan network:

truffle console - -network kovan

truffle migrate - -network kovan

Deploy successful :) !

Now you can see the above transactions public, in Etherscan !

https://kovan.etherscan.io/tx/0xf5e069124a9e8ebc5372259260d7ab483051f871fa5311be09c150fd91a04f40

https://kovan.etherscan.io/tx/0xc59ca68267d7c79096dc596cd7ff976457f0ed6aa6671775bf7e8462891e378f

Notice the “from” address ! Is your address from Ganache.

Don’t forget to give us your 👏 !

--

--