tutorials

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

This blog will show you how to deploy a ERC-20 smart contract on EVM based Shardeum blockchain using Truffle to build your DApps on the...

< Back
Back to top
  • Share

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 Sphinx (betanet). 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.

What is an ERC-20 Token?

Before we learn how to deploy ERC-20 tokens and smart contracts on Shardeum using Truffle, let’s find out what an ERC-20 token is. ERC-20 tokens are fungible tokens that are created on the Ethereum network, based upon the ERC-20 token standard brought forth by the platform. This token standard lays down rules regarding how ERC-20 tokens can be transferred, how transactions using them are approved, and the entire supply of an ERC-20 token, among other things. 

Each set of ERC-20 tokens is differentiated by ticker symbols like ABC or XYZ, and since they are fungible, it means each token in a certain set is interchangeable with another token from the same set.

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 Sphinx Dapp, 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 Sphinx Dapp (Testnet)

  1. Go to truffle-config.js (located in root directory)
  2. Update with Shardeum Sphinx Dapp 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)
    },
    sphinx: {
      provider: () => new HDWalletProvider(mnemonic, `https://dapps.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.

npm i @truffle/hdwallet-provider

Deploying on Shardeum Sphinx Dapp

To deploy our testToken contract run this command in the testToken directory:

truffle migrate --network sphinx

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

Starting migrations...
======================
> Network name:    'Shardeum Sphinx Dapp 1.X'
> Network id:      8081
> 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. 🙂

Conclusion:

We hope this post has given you some insight into what ERC-20 tokens are, and how to Truffle deploy smart contracts and ERC-20 tokens on Shardeum!

At Shardeum, we aim to solve the scalability trilemma plaguing most blockchains today, and blocking the road for web3 projects and applications to go mainstream. Our scalability solutions would allow blockchains to become more flexible without compromising on either security or decentralization. We hope you will collaborate with us to make your dream web3 project a reality, and join the ever-growing Shardeum community while you’re at it!

Popular Searches

List of Layer 1 Crypto Projects  |  Types of DDoS Attack  |  Advantages and Disadvantages of Blockchain Technology  |  What is Blockchain Technology  |  What are the Features of Blockchain  |  What is a Blockchain Node  | How to Buy Land in Metaverse  |  What is Crypto Metaverse  |  Can the Blockchain be Hacked  |  Benefits of Blockchain  |  What is Proof of Stake Blockchain  |  What is Cloud Mining  |  How to Mint Crypto Coins  |  SB Token  |  NFT Smart Contract  |  Enterprise Blockchain  |  Types of Cryptocurrency Scams  |  How to Make and Sell NFT  |  What is Gas in Cryptocurrency  |  Stack Mobile


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

The Shard

Sign up for The Shard community newsletter

Stay updated on major developments about Shardeum.