Deploy smart contract to a local blockchain

Sep 7, 2023 by Arnaud Stoz | 396 views

Blockchain Smart Contract Ethereum

https://cylab.be/blog/280/deploy-smart-contract-to-a-local-blockchain

When starting to develop a smart contract it can sometimes be difficult and confusing how to deploy them and test them. You have the well known online tools like remix but sometimes you prefer to do stuff locally.

Technology stack

To develop and deploy your first smart contract we will use the following tools:

  • an text editor of your choice (vscodium, vim,....)
  • Truffle : framework for developing smart contract
  • ganache : fire up quickly a personal and local blockchain

Installation

To install truffle and ganache you will need to have npm installed.

Once you have npm installed you can install truffle and ganache with the following command:

npm install -g truffle ganache

Start your project

First create a new directory in the location of your choice

mkdir hello_world && cd hello_world

Then we will use truffle to simplify the development, testing and deployment of smart contract. To start a new project using truffle run

truffle init

It will create 3 different folders and 1 file:

  • contracts : will contain the source code of your smart contract
  • migrations : in truffle terminology migrate is the action of compiling and deploying a smart contract. This folder will contain code to deploy a smart contract to the blockchain.
  • test : will contain test code for your smart contract
  • truffle-config.js : the truffle configuration file.

Start your local blockchain and connect truffle to it

Now that you have successfully written your first smart contract, it's time to start your local blockchain to test it.

To start the blockchain just run

ganache

If everything works as expected you should see an output similar to the following ganache_output.png

By default ganache sets ups 10 accounts all with 100 ETH. The only information really important here is the latest one, RPC listening port and address, the other are out of the scope of this blog post.

Once the blockchain is up and running we need to configure truffle to use that blockchain. Open the file truffle-config.js, uncomment the following block and make sure that it contains the correct value.

networks: {
........
   development {
          host : "127.0.0.1",
          port :  8545,
          network_id: "*"
   }
.......
}

Once you have done the modification run truffle migrate and look for the output. If you have a CONNECTION ERROR check again your truffle-config.js file.

Writing hello World smart contract

In the contracts folder create a new file called hello_world.sol. Smart contract are written in a language called solidity.

Add the following code to the file

// SPDX-License-Identifier:  GPL-3.0-or-later
pragma solidity >=0.5.0 <0.9.0;

contract HelloWorld{
    string private greeting;

    constructor() {
        greeting =  "Hello World!";
    }

    function getGreeting() public view returns(string memory){
        return greeting;
    }

    function setGreeting(string memory _message) public{
        greeting = _message;
    } 
}

The purpose of this blog post is not to explain smart contract development so in short this smart contract does 3 things:

  • when deployed on the blockchain initialize a greeting message "Hello World!"
  • allow everybody to retrieve the greeting message
  • allow everybody to change the greeting message

Deploy your smart contract to the blockchain

In the migrations folder create a file called 1_helloWorld_migration.js

It is very important to start your filename with the number 1 (if it is the first file in this directory) otherwise truffle will not be able to deploy your smart contract.

add this code in the file

const HelloWorld = artifacts.require("HelloWorld");

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

Now deploy your smart contract by running truffle migrate. You should see an output similar to this: deploy_output.png

If you are using ganache UI you might face compatibility issue when deploying smart contract. In this case I recommend using ganache from the cli

Congratulation your contract has been published on your local blockchain!

Interact with your contract

To interact with your contract use the truffle console command. Then run

> let instance = await HelloWorld.deployed()
> instance.getGreeting() //show the greeting 
> instance.setGreeting("My new greeting") //set greeting

Congratulations you have now interacted with your contract!

If you want to interact more with the blockchain using truffle console and have a look at web3.

This blog post is licensed under CC BY-SA 4.0

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept