How to setup a Blockchain Environment Locally using Truffle & Ganache in 2024

I am writing this blog because it was a bit of struggle setting Truffle up and I thought it could help some new blockchain developers. It was quite hard figuring out what to install and how to setup the config file correctly so the deployment of the contract works as expected. Nonetheless, let’s get to it.

  1. First you need to install Ganache from the official website.

    Once installed, start Ganache. It will run a local Ethereum blockchain with a set of predefined accounts and private keys.

  2. Then install truffle with npm install -g truffle
  3. Then create a new folder where you want to have your truffle application mkdir HelloWorldBlockchain
  4. Go into your newly created folder cd HellowWorldBlockchain
  5. Run truffle init to initialize the app
  6. Compile your truffle app using truffle compile

    Everything works fine, because we have no contracts in place yet.

  7. So create a new Contract under /contracts called HelloWorld.sol
    // Hello World contract
    pragma solidity >=0.4.22 <0.9.0;
    
    contract HelloWorld {
        string public greeting;
    
        // Constructor to initialize the greeting message
        constructor() {
            greeting = "Hello, World!";
        }
    
        // Function to get the current greeting message
        function getGreeting() public view returns (string memory) {
            return greeting;
        }
    
        // Function to set a new greeting message
        function setGreeting(string memory _newGreeting) public {
            greeting = _newGreeting;
        }
    }
  8. You also need a migration file, they are sorted alphanumerically, so name it 1_deploy.js. By using migration scripts, you can manage the evolution of your smart contract system in a structured and repeatable manner, just like you would with database migrations.
    const HelloWorld = artifacts.require("HelloWorld");
    
    module.exports = function (deployer) {
      deployer.deploy(HelloWorld);
    };
  9. Before compiling your code and running the migration, you have to specify your development environment in truffle.config.js under networks. Un-comment the development and set it up like so:
        development: {
         host: "127.0.0.1",     // Localhost (default: none)
         port: 7545,            // Standard Ethereum port (default: none)
         network_id: "*",       // Any network (default: none)
        },
  10. You also have to change the solc(solidity compiler) under compilers to version: “0.8.19”, here is a reason why you need to do that. But in simple terms default solc version 0.8.21 is a head of the network that you’re trying to deploy your code to.
      compilers: {
        solc: {
          version: "0.8.19", 
        }
      }
  11. Now run the compiler truffle compile
  12. And tou can finally run your truffle migration using this script: truffle migrate --network development. And you should see the following

    Compiling your contracts...
    ===========================
    > Everything is up to date, there is nothing to compile.
    
    
    Starting migrations...
    ======================
    > Network name:    'development'
    > Network id:      5777
    > Block gas limit: 6721975 (0x6691b7)
    
    
    1_deploy.js
    ===========
    
       Deploying 'HelloWorld'
       ----------------------
       > transaction hash:    0x89e362d7f480eb82b3484d4c504f55562d441364ba6a1e9f51a5f9a93df52af2
       > Blocks: 0            Seconds: 0
       > contract address:    0xec6F9413b93C14F930F2aE917c282D9Aaaa56d42
       > block number:        1
       > block timestamp:     1708858480
       > account:             0xACF685726DA72f62a72DDF40C484954194789e62
       > balance:             99.998359507
       > gas used:            486072 (0x76ab8)
       > gas price:           3.375 gwei
       > value sent:          0 ETH
       > total cost:          0.001640493 ETH
    
       > Saving artifacts
       -------------------------------------
       > Total cost:         0.001640493 ETH
    
    Summary
    =======
    > Total deployments:   1
    > Final cost:          0.001640493 ETH
  13. Check it in your Ganache under Contracts, it should say deployed.
See also  How to make AWS S3 bucket files public

How to test your deployed contract code:

  1. By running: truffle console --network development
  2. Loading the Contract let HelloWorld = artifacts.require("HelloWorld");
  3. Instantiating your function let instance = await HelloWorld.deployed();
  4. Running your function let greeting = await instance.getGreeting();
  5. And finally logging the result console.log(greeting);

So there you have it, that’s how to setup your own Blockchain Environemnt Locally and How to deploy your first Solidity Contract into said blockchain. Honestly it wasn’t as easy as I had expected it would be, there were a few hurdles along the way. If you prefer using Foundry then check out my setup guide here.

Man smelling truffles, mmm
Man smelling truffles, mmm

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *