How to deploy smart contracts on Shardeum using Truffle

How to Deploy ERC-20 Smart Contracts on Shardeum Using Truffle

Truffle is a world-class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM). The aim of these development environments is to make life as a developer easier while they focus on building their projects on blockchains.

Shardeum is an EVM based smart contract platform so any smart contracts you write on/for Ethereum can be deployed or migrated to Shardeum seamlessly. The benefit of using Shardeum is that it can process at a speed over 100k TPS with true decentralization and an estimated transaction cost of $0.01. With cheap and lighting fast transactions, you and your users are in for a treat when the network is expected to launch at the end of 2022.

There are various EVM friendly ways to deploy your smart contracts and DApps on Shardeum Liberty (alphanet). Truffle, Remix, Hardhat are some of the well known development environments you could use to develop your Web 3 applications. While we have discussed about deploying apps using Remix and Hardhat in our previous blogs, we would like to take you through how to leverage Truffle to create smart contracts and build your DApps.

Truffle Environment Setup

There are a few technical requirements for developers to start using Truffle. Install the following dependencies :

  1. NodeJS v12 or later (available here)
  2. Npm/Yarn Package Installer (included with Node)
  3. Windows, Linux or Mac OS X

Installing Truffle

Once the above dependencies are installed successfully. You can now install Truffle.

npm install -g truffle

To verify that Truffle is installed properly, type truffle version into the terminal (cmd).

Creating a Project

After ensuring Truffle is installed successfully, create your own project and name it something like “testToken”.

Create a new directory for your Truffle project

mkdir testToken
cd testToken

Initialize your project

truffle init

Once this operation is complete, you’ll now have a project structure with the following items:

  1. contracts/: Directory for Solidity contracts
  2. migrations/: Directory for scriptable deployment files
  3. test/: Directory for test files for testing your application and contracts
  4. truffle-config.js: Truffle configuration file
  5. build (visible after compile): Compiled Solidity contracts with bytecode and ABI

Create Contract

You can write your own smart contract, or use open-source ‘openzeppellin’ standard contracts and build on top of them. Since we are using ‘openzeppelin’ for our testToken, it requires the following dependency.

npm install @openzeppelin/contracts

After the dependency is installed, create a file named “testToken.sol” in the contract directory.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract testToken is ERC20 {
  constructor(uint256 initialSupply) ERC20("testTkn", "TST") { _mint(msg.sender, initialSupply);
  }
}

Compile Contract

To compile a Truffle project, change to the root directory (in our case testToken directory) and run the command in the terminal.

truffle compile

Coding Migrations

To deploy our testToken contract on Shardeum Liberty, we have to create a migration to get the contract on the network. Create a file in the migrations folder named “2_deploy.js”.

var test = artifacts.require("testToken");

module.exports = function(deployer) {
  // deployment
  deployer.deploy(test, '10000000000000000000000');
};

Configuring Truffle For Shardeum Liberty (Testnet)

  1. Go to truffle-config.js (located in root directory)
  2. Update with Shardeum Liberty details (available here)
const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard port (default: none)
      network_id: "*",       // Any network (default: none)
    },
    liberty: {
      provider: () => new HDWalletProvider(mnemonic, `https://liberty10.shardeum.org/`),
      network_id: 8080,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true
    }
  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "^0.8.0", // A version or constraint - Ex. "^0.5.0"
    }
  }
}

Note: Make sure to add your mnemonic or private key and add it to a separate file “.secret” (ensure to never upload this file to GitHub or GitLab).

Also make sure, you have @truffle/hdwallet-provider dependency installed. If not, please install it using the below command.

truffle migrate --network liberty

The contract will be deployed on Shardeum Liberty and it should look like this:

Starting migrations...
======================
> Network name:    'liberty'
> Network id:      8080
> Block gas limit: 20000000000 (0x4a817c800)

2_deploy.js
===========

   Deploying 'testToken'
   ---------------------
   > transaction hash:    0x9a3fcdb6d517d7cf0ee69f8076d020e1bba8cdd01378cc34eaf1030a7fdfc273
   > Blocks: 0            Seconds: 8
   > contract address:    0x4d63Ba5f3E48dbE7f2b1e459C74BE94B8d61e919
   > block number:        11
   > block timestamp:     1438271100
   > account:             0xFa0B6609cd5d8fC19A1aC16311da1466FaF09978
   > balance:             99.964878389908455424
   > gas used:            1170047 (0x11da7f)
   > gas price:           20 gwei
   > value sent:          0.00429 ETH
   > total cost:          0.02769094 ETH

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

Summary
=======
> Total deployments:   1
> Final cost:          0.02769094 ETH

Congratulations! You have successfully deployed ERC20 Smart Contract using Truffle. Now you can interact with the smart contract by building a DApp and create your own blockchain project providing various services and products. 🙂


Content/opinions expressed in this publication are those of the author(s). They do not necessarily purport to reflect the opinions or views of Shardeum Foundation.

About the Author : Shailesh Khote has been in blockchain development since Jan 2021. He worked with solidity smart contracts for DEX, NFTs, Yield Farming, IDOs, and Yield Aggregation and his core competencies include Solidity, web3.js and ReactJs. You can follow him on Twitter

Leave a Comment

Your email address will not be published.

Stay updated with Shardeum updates

 

Close
Scroll to Top