Deploying ERC-20 Smart Contracts on Shardeum with Truffle

Nikhil KumarNikhil Kumar
9 min read

1. Revolutionizing Blockchain Development

Truffle stands as a pinnacle in the realm of blockchain development environments. It's more than just a toolbox, it's a comprehensive ecosystem, including a development environment, testing framework, and asset pipeline, tailored for blockchain projects harnessing the Ethereum Virtual Machine (EVM). The primary objective of such development environments is to simplify the lives of developers, allowing them to pour their energy into creating remarkable projects on the blockchain.

Enter Shardeum, an EVM-based smart contract platform that offers seamless compatibility with Ethereum. This means any smart contracts you've painstakingly crafted for Ethereum can effortlessly find a new home on Shardeum. The real perk? Shardeum boasts an astonishing transaction processing speed of over 100,000 transactions per second (TPS), all while maintaining the core principles of decentralization. Plus, you can expect transaction costs to hover around a mere $0.01. With such economical and lightning-fast transactions, you and your users are in for a delightful experience when the network officially launches, which is slated for the end of 2022.

Now, let's dive into the myriad EVM-friendly methods you have at your disposal to deploy your smart contracts and decentralized applications (DApps) on Shardeum's Sphinx betanet. While we've previously explored deploying apps using Remix and Hardhat in our blogs, today, we're here to guide you through harnessing the power of Truffle. Discover how you can create smart contracts and construct your DApps with ease using this robust development environment.

2. What's in a Name? Understanding ERC-20 Tokens

Before we delve into the fascinating world of deploying ERC-20 tokens and smart contracts on Shardeum through Truffle, it's crucial to grasp the fundamental concept of what an ERC-20 token truly represents. ERC-20 tokens are the heartbeat of the Ethereum network, stemming from the ERC-20 token standard meticulously crafted by the Ethereum platform itself. This standard serves as a guiding light, illuminating the path for the creation and operation of fungible tokens within the Ethereum ecosystem.

Now, what makes ERC-20 tokens so remarkable? Well, it's all in the rules. The ERC-20 standard establishes a set of rules and regulations governing how these tokens can be transferred, the processes involved in approving transactions utilizing them, and even the total supply of these tokens. Picture it this way: each set of ERC-20 tokens can be likened to a unique species, distinguished by its ticker symbol, like ABC or XYZ. The real magic lies in their fungibility, signifying that every token within a particular set is virtually indistinguishable from another token within the same set. In other words, it's a world of interchangeable tokens, where one can seamlessly take the place of another.

3. Setting up Your Development Environment

Before you embark on your journey into the realm of Shardeum smart contracts with Truffle, let's ensure you've got the right tools in your developer's toolkit. To wield the power of Truffle effectively, you'll need to set up your environment correctly. Here's a handy checklist of the technical requirements:

  1. NodeJS v12 or Beyond: First on the list is NodeJS, the engine that fuels your development aspirations. Make sure you have version 12 or a later iteration. Don't have it yet? No worries, you can grab it here.

  2. Npm/Yarn Package Installer: Every developer's trusty sidekick, you'll need npm (Node Package Manager) or Yarn to handle your package management needs. The good news? If you've installed NodeJS as suggested, npm comes bundled in. Yarn is also a fantastic choice. Take your pick!

  3. Operating System: Truffle isn't picky about where it sets up shop. Whether you're a Windows aficionado, a Linux enthusiast, or a Mac aficionado, Truffle is here for you, ready to turn your ideas into blockchain realities.

With these prerequisites in place, you're all set to dive into the exciting world of Truffle and Shardeum. Let the coding adventures begin!

4. Crafting Your Truffle Project: A Step-by-Step Guide

Now that you've got your development environment primed and ready, it's time to bring your vision to life with Truffle. Follow these steps to set up your Truffle project:

  1. Installing Truffle: Assuming you've checked off the prerequisites mentioned earlier, you're just a few keystrokes away from installing Truffle. Open your terminal (cmd) and enter the magic words:

     install -g truffle
    

    Hit Enter, and let npm work its magic. This command will whisk Truffle onto your system.

  2. Verification: To ensure that Truffle has made itself at home, type the following into your terminal:

     truffle version
    

    If you see version details, congratulations, Truffle is now part of your toolkit.

  3. Creating Your Project: With Truffle in place, let's birth your project. Give it a name that speaks to your ambitions. For instance, let's call it "testToken." Enter this command:

     truffle init testToken
    

    Truffle will diligently set up the scaffolding for your project.

You've now laid the foundation for your Shardeum adventure. Your project is ready for your brilliance!

5. Inside Your Truffle Project: A Peek into the Structure

Welcome to your newly minted Truffle project! Here's what your project structure looks like and what each element does:

  1. contracts/: This directory is where your Solidity smart contracts will reside. You'll write, compile, and manage your contracts here.

  2. migrations/: Think of this as a deployment script vault. It holds files that help you deploy your contracts to the blockchain. Truffle runs these scripts in a specific order to ensure your contracts are deployed correctly.

  3. test/: As the name suggests, this is where you'll create test files for your contracts. Testing is crucial to ensure your code functions as expected.

  4. truffle-config.js: This configuration file is where you set up your development environment. You specify things like which blockchain network to deploy to and how to connect to it.

  5. build/ (visible after compile): This directory stores the compiled output of your Solidity contracts. It contains the bytecode and Application Binary Interface (ABI) needed for contract interaction.

With this structure in place, you're all set to start developing, deploying, and testing your smart contracts on Shardeum using Truffle. The blockchain world is your oyster!

6. Creating Your Smart Contract

Now, let's delve into creating your smart contract. You have two options here: you can either write your custom contract or leverage open-source "OpenZeppelin" standard contracts and build on top of them. For our "testToken" example, we'll use OpenZeppelin.

First things first, you need to install the OpenZeppelin contracts dependency. Run this command in your project directory:

npm install @openzeppelin/contracts

Once the dependency is installed successfully, it's time to craft your smart contract. In the contracts directory of your project, create a file named "testToken.sol."

Here's a simple example of what your "testToken.sol" smart contract might look like:

solidityCopy code// 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);
  }
}

Compiling Your Contract

With your smart contract in place, it's essential to compile it before moving forward. To compile a Truffle project, navigate to the root directory (in our case, the "testToken" directory) using your terminal or command prompt.

Now, run the following command to compile your project:

truffle compile

This will process your contract, generating the bytecode and Application Binary Interface (ABI) required for deployment and interaction. Once compilation is successful, you're ready to proceed with deploying your contract on Shardeum Sphinx Dapp using Truffle.

7. Coding Migrations and Configuring Truffle for Shardeum Sphinx Dapp

Now that we've created our smart contract, the next steps are coding migrations and configuring Truffle for Shardeum Sphinx Dapp (Testnet). These actions are crucial for deploying your contract on Shardeum. Let's break it down:

1. Coding Migrations:

To deploy our "testToken" contract on Shardeum Sphinx Dapp, we need to create a migration file. In your project's "migrations" folder, create a JavaScript file named "2_deploy.js." Inside this file, include the following code:

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

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

This code defines the deployment process for your contract. It deploys the "testToken" contract with an initial supply of tokens set to '10000000000000000000000'.

2. Configuring Truffle for Shardeum Sphinx Dapp (Testnet):

To deploy your contract on Shardeum Sphinx Dapp's Testnet, you need to configure Truffle accordingly. Locate the "truffle-config.js" file in your project's root directory and update it with the following configuration:

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"
    }
  }
}

Important Notes:

  • Add your mnemonic or private key to a separate file named ".secret." Ensure you never upload this file to GitHub or GitLab for security reasons.

  • Make sure you have the "@truffle/hdwallet-provider" dependency installed. If not, you can install it using the following command:

npm i @truffle/hdwallet-provider

With these configurations in place, you're all set to deploy your "testToken" contract on Shardeum Sphinx Dapp's Testnet. The final step is to execute the deployment command, as explained in the previous response.

8. Deploying Your Contract:

You're now ready to deploy your ERC-20 smart contract on Shardeum Sphinx Dapp using Truffle. Here's how:

  1. Open your terminal and navigate to the "testToken" directory (replace "testToken" with your project's name if different).

  2. Execute the following command:

truffle migrate --network sphinx

This command initiates the deployment process. Your contract will be deployed on Shardeum Sphinx Dapp.

Deployment Details:

Upon successful deployment, you'll receive important information about your contract deployment:

  • Network Information:

    • Network name: 'Shardeum Sphinx Dapp 1.X'

    • Network id: 8081

    • Block gas limit: 20000000000 (0x4a817c800)

  • Contract Deployment:

    • Deployment script: 2_deploy.js

    • Deployed contract: 'testToken'

    • Transaction hash: 0x9a3fcdb6d517d7cf0ee69f8076d020e1bba8cdd01378cc34eaf1030a7fdfc273

    • Blocks: 0 (Time taken: 8 seconds)

    • Contract address: 0x4d63Ba5f3E48dbE7f2b1e459C74BE94B8d61e919

    • Block number: 11

    • Block timestamp: 1438271100

    • Account: 0xFa0B6609cd5d8fC19A1aC16311da1466FaF09978

    • Balance: 99.964878389908455424 ETH

    • Gas used: 1170047 (0x11da7f)

    • Gas price: 20 gwei

    • Value sent: 0.00429 ETH

    • Total cost: 0.02769094 ETH

Congratulations, You've Deployed an ERC-20 Smart Contract!

With your contract successfully deployed, you're now equipped to interact with it. You can start building your decentralized applications (DApps) and create unique blockchain projects that offer various services and products.

9. Conclusion:

I hope this guide has provided you with valuable insights into what ERC-20 tokens are and how to deploy smart contracts and ERC-20 tokens on Shardeum using Truffle. At Shardeum, we're committed to addressing the scalability challenges faced by most blockchains today. Our solutions aim to empower web3 projects and applications to thrive without compromising security or decentralization. We invite you to collaborate with us and be part of the ever-growing Shardeum community as you embark on your web3 journey!

If you are interested in learning more about Shardeum or getting involved in the project, you can join the community on Telegram channel, follow the Twitter page and join the Discord channel. You can also find more information on the Shardeum Website.

Shardeum Rewards Developers for Their Contributions.

Shardeum, the decentralized application platform, is committed to rewarding developers for their contributions to the project. The company offers a variety of community reward programs.

To learn more about the available community reward programs, please visit the Community Reward Programs page.

Thank you for Reading! ❤️

12
Subscribe to my newsletter

Read articles from Nikhil Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nikhil Kumar
Nikhil Kumar