DEV Community

Cover image for How To: Deploy Smart Contracts on the Energi Blockchain
Johnson Ogwuru
Johnson Ogwuru

Posted on

How To: Deploy Smart Contracts on the Energi Blockchain

For this tutorial, we would be making use of the following technologies:

  1. Solidity
  2. Javascript
  3. Node JS
  4. Truffle
  5. Energi Blockchain
  6. Editor => vscode

Definitions:

  1. Solidity is an object-oriented programming language for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum.

  2. Truffle is a world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.

  3. Energi Blockchain Energi is a next-generation Proof of Stake (PoS) cryptocurrency that combines smart contract capabilities, decentralised governance, and a self-funding treasury.


Installation:

  1. Follow the instruction found on the Energi wiki to download and install the Energi core Node on your device. I am using a mac, so i would make use of the mac installation documentation, found here, you can find download and installation instruction for other platforms like linux and windows in the documentation.
  2. Install Truffle globally by running this command in your terminal npm install -g truffle. For mac users, if having permission issues, add sudo before the command like this, sudo npm install -g truffle and after inputing your password when prompted, truffle will be downloaded.
  3. In vscode install the following solidity extension, this extension helps with syntax highlighting, snippets and linting using solhint when writing smart contracts with solidity.

Getting Started:

In this article, we would write a basic smart contract and deploy it on the energi test network.

To get started, i would be creating an empty folder in my desktop named energi-app.
mkdir energi-app

After this cd into the folder we just created and create a truffle project by running the init command.
cd energi-app
truffle init

johnson@Johnsons-MBP desktop % mkdir energi-app
johnson@Johnsons-MBP desktop % cd energi-app
johnson@Johnsons-MBP energi-app % truffle init

Starting unbox...
=================

✔ Preparing to download box
✔ Downloading
✔ cleaning up temporary files
✔ Setting up box

Unbox successful, sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test

johnson@Johnsons-MBP energi-app %
Enter fullscreen mode Exit fullscreen mode

Now we have our project setup, if you have done everything properly, when you open the energi-app in vscode, the structure should look like what we have in the image below.

Alt Text

When you look at the project folder, you see a folder named contracts, oh yeah🤓, that's where we would save our contracts.
Now inside of the contracts folder create a file Hello.sol, inside of the file add the following solidity code.

pragma solidity >=0.4.2 <0.7.0;

contract Hello {
  function sayHello() public pure returns(string memory) {
    return 'Hello There';
  }
}
Enter fullscreen mode Exit fullscreen mode

This is a very basic solidity smart contract code, this tutorial won't be focused on discussing solidity or smart contracts, to learn more visit the solidity documentation.

To prepare our smart contract for deployment, we need to add one more file to our project, this is the file that would specify which contracts to deploy. Navigate to the migrations folder and create a new file 2_migrate_hello.js.
The numbering on the file is important, it's how truffle knows which contracts to deploy when.

Note that for every independent contract you create, you would need to create a migration file for. But for contracts dependent on each other, you can batch their migration together in the same migration file.

Add the following code in the 2_migrate_hello.js migration file created.

const Hello = artifacts.require("./Hello.sol")

module.exports = function (deployer) {
  deployer.deploy(Hello);
}
Enter fullscreen mode Exit fullscreen mode

Now to deploy our contract on the Energi test-network, you need to follow the steps below:

  1. Start Energi node, but this time make use of this command if you are on a mac
    $HOME/energi3/bin/energi3 --testnet --rpc.
    This would start an rpc enabled testnet, using the Energi core node app you downloaded.
    The RPC link exposed by default would be 127.0.0.1:49796

  2. Navigate to the truffle-config.js file, and replace the code there with the one attached below:

const HDWalletProvider = require('@truffle/hdwallet-provider');

const fs = require('fs');
// const mnemonic = fs.readFileSync(".secret").toString().trim();
const privateKey = '0x1...';

module.exports = {
  networks: {
    energiTestnet: {
      provider: () => new HDWalletProvider(privateKey, `http://127.0.0.1:49796`),
      network_id: 49797,    
      gas: 5500000,
      from: '0xa57ed899Cd9587952Cbf284c9459DB4fF6DEe53A',
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    },
  },
}
Enter fullscreen mode Exit fullscreen mode

Edit the from property in the network object to the address you want to deploy the contract with, and also change the private key value to the private key of the account you use in the from property.
Also make sure that the account you use for this, is being controlled by the node you started initially, and it has some funds, 3tnrg(test nrg) won't be bad. You can request some tnrg from the Energi team on Discord


Note, in the truffle config file, you can either use a private key or mnemonic phrase, whichever works for you, but don't be like me, make sure you store this information securely, you can use a .env file, and make sure you don't commit it to a public repository.


Install the hdwallet-provider by running the following command
yarn init
yarn add @truffle/hdwallet-provider

Then run the following command to deploy your contract, make sure you are in your project directory.
truffle deploy --network=energiTestnet

If all goes well, you should be able to see this output in your terminal.

johnson@Johnsons-MBP energi-app % truffle deploy --reset --network=energiTestnet

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'energiTestnet'
> Network id:      49797
> Block gas limit: 40000000 (0x2625a00)


1_initial_migration.js
======================

   Replacing 'Migrations'
   ----------------------
   > transaction hash:    0xc88f6525e15caf806f755b16f682a8f7c7a37f91051f833f766410da0a4bdbb2
   > Blocks: 1            Seconds: 24
   > contract address:    0xa16FC529F5C493c12031657BF93A07e9DBfB6538
   > block number:        194105
   > block timestamp:     1592907159
   > account:           0xa57ed899Cd9587952Cbf284c9459DB4fF6DEe53A
   > balance:             3631.499979394
   > gas used:            188483 (0x2e043)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00376966 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00376966 ETH


2_migrate_hello.js
==================

   Replacing 'Hello'
   -----------------
   > transaction hash:    0xa9f76b3df7a191d56d769655428916f8c0128ecdd026bf6af26ef3a1b9403127
   > Blocks: 0            Seconds: 76
   > contract address:    0xA309F47d94768c2357780E8b55dC77981494a995
   > block number:        194107
   > block timestamp:     1592907306
   > account:             0xa57ed899Cd9587952Cbf284c9459DB4fF6DEe53A
   > balance:             3631.496548394
   > gas used:            129549 (0x1fa0d)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00259098 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00259098 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.00636064 ETH


johnson@Johnsons-MBP energi-app % 
Enter fullscreen mode Exit fullscreen mode

Because i have compiled this before, it won't display the compilation message, but would rather say it's been compiled before, but on yours, you would be able to watch it compile before migration starts.

Lastly, let's check our deployed smart contract on the Energi test-network block-explorer. From my deploy log, my hello smart contract address is 0xA309F47d94768c2357780E8b55dC77981494a995. When you get to the Energi test-network block explorer, search for the smart contract address. For me this is what i see, my smart contract deployed, and chilling on the Energi test-network.

Alt Text


Finally, to deploy to the main net, all we have to do is remove the testnet we used when starting the node and start the node with this command instead.
$HOME/energi3/bin/energi3 --rpc

In this article we have covered the basics of deploying on Energi, with truffle. For further readings and information about the Energi Blockchain check out the documentation, found here. Reach out to me also if you have any questions here or on Twitter.

You can find the repository for the project here

Top comments (0)