More

    Learn Truffle and Ganache – How to Create and Deploy a Smart Contract

    Learning a new technology often means learning a new framework, programming language, IDE, or deployment method. And the blockchain is no different.

    In this tutorial, I am going to show you how to get started with Truffle, a Node.js blockchain framework, in Visual Studio Code.

    How to Install Truffle

    To install Truffle you need to have Node and NPM along with Python setup on your machine.

    If you do not have them already, you can install them from their official websites (Node and Python). Once you’re done with that, you are all set to install Truffle.

    We will use npm to install Truffle. Enter the following command in your command prompt:

    npm install -g truffle

    While the installation runs, if you come across the following error, I have got you covered:

    gyp ERR! stack Error: Could not find any Visual Studio installation to use

    Google shows a bunch of solutions for the above error. What actually worked for me was installing Visual Studio along with ‘Desktop Development with C++’.

    After you download Visual Studio and run the installer you will see the following screen:

    Under the ‘Desktop and mobile’ section, check ‘Desktop Developement with C++’ and continue with the installation process.

    Once this is done you can run the Truffle installation command again.

    To verify if Truffle is installed successfully, run:

    truffle version

    in your command prompt. You should see an output like the image below:

    Congratulations! You have installed Truffle.

    How to Use Truffle in Visual Studio Code

    Visual Studio Code comes with it’s own extension for Truffle. We will install it to make our work easier.

    In the marketplace search bar, type “Truffle for VS Code” and click on install (similar to the image below).

    VS Code requires you to have other extensions for it to work, so just check the ones you don’t have installed and continue the setup:
    Untitled

    If you do not see the Truffle logo in the left bar you might have to restart VS Code.

    How to Set Up Ganache in VS Code

    Ganache comes with the Truffle suite to deploy DApps.

    Click on the ‘Networks’ > ‘Create a new network’ in Truffle explorer.

    From the dropdown box select ‘Ganache service’.

    Select type ‘local’ or ‘fork’. Since this a local setup I will select ‘local’.

    Next, you will be asked to enter your ‘local project’s name’. Enter any name of your choice and hit enter.

    Your network setup is complete.

    To start the network, right click on the network name and click on ‘Start Ganache’.

    When you start the Ganache service, you will see a command line output as follows:
    Screenshot-2022-07-16-200504

    The output displays a set of things to speed up. We do not need to worry about them just yet.

    How to Start a Tuffle Project

    To start a project in Truffle, go into a directory and type the init command.

    truffle init

    This will create a new Truffle project.

    How to Create a Contract in Truffle

    The following commands creates a contract in Truffle:

    truffle create contract <contract name>

    Here we created a contract named ‘SimpleStorage’.

    How to Run Tests in Truffle

    To run test in Truffle, just enter this command:

    truffle test

    All the tests will be run one by one.

    How to Deploy in Truffle

    We will use Ganache to deploy in Truffle.

    truffle develop

    You use the above command to start the deployment process.

    Ganache has some accounts and private key ready for this purpose.

    In the above image at the bottom you will see truffle (develop)> console.

    Type migrate --reset in the console.

    You will see that the initial migrations are made and the deployment process starts. In the end you will get a summary (the cost and number of deployments) of the deployment.

    We progress more quickly when we can learn from each other’s mistake. I have just started learning Blockchain, and it took me a while to get the setup running. So here I am sharing my findings. Happy learning!

    This content was originally published here.

    Latest articles

    spot_imgspot_img

    Related articles

    Leave a reply

    Please enter your comment!
    Please enter your name here

    spot_imgspot_img