Reader small image

You're reading from  The Essential Guide to Web3

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781801813471
Edition1st Edition
Concepts
Right arrow
Author (1)
Vijay Krishnan
Vijay Krishnan
author image
Vijay Krishnan

Vijay Krishnan, an experienced expert in blockchain and Web3, holds the position of head of SysOps for Linea, a Layer 2 blockchain at Consensys. With a rich background in blockchain, he's guided Fortune 100 companies from ideation to product build. With over 50 projects and 300+ use cases to his credit, Vijay has left an indelible mark on the industry. Prior to Consensys, he pioneered the AWS Partner Blockchain ecosystem and led IBM North America's Blockchain Practice. With two decades on Wall Street, he possesses deep financial and tech insights. Vijay, a leader and subject matter expert, empowers clients in DeFi, NFTs, gaming, and the metaverse. Beyond work, he's a mentor, speaker, author, and organic farmer. His knowledge and innovation continue to shape the blockchain and Web3 landscape.
Read more about Vijay Krishnan

Right arrow

Creating Your First Token

The ERC-20 token specification is the widely accepted standard for Ethereum tokens, making it the go-to choice for most Ethereum contracts in use today. In this chapter, you’ll learn how to create your own Ethereum token by following this established ERC-20 standard. This is a short chapter but an exciting one.

In this chapter, we’re going to cover the following main topics:

  • Writing your first ERC-20 token smart contract
  • Deploying and testing the ERC-20 token smart contract

Technical requirements

Before you dive into creating your ERC-20 token, you’ll need to ensure that you have the following technical requirements set up and ready:

  • Operating system (OS): You can choose any OS that you are comfortable with. The examples in this guide use MacOS, but it is up to your preference.
  • GitHub CLI: Make sure you have the GitHub command line interface (CLI) installed and access GitHub. This is essential for version control and collaboration.
  • Visual Studio Code (VS Code): Install VS Code, a popular code editor that makes smart contract development more convenient.
  • Node.js: Ensure you have Node.js installed. This runtime environment is necessary for running JavaScript-based applications.
  • Node Package Manager (npm): This comes bundled with Node.js. It is used for managing packages and dependencies in your projects.
  • Hardhat: Install Hardhat, a development environment for Ethereum that simplifies smart contract development and testing...

Writing your first ERC-20 token smart contract

Creating your first token on the Ethereum network can be an exciting and rewarding experience. By following these simple steps, you can have your own custom token up and running in no time.

Here are the steps, but you should already be familiar with them from Chapter 8:

  1. Choose a token standard: Select a token standard that suits your needs, such as ERC-20. ERC-20 is suitable for fungible tokens (tokens with equal value).
  2. Write the smart contract: Using the Solidity programming language, write a smart contract that implements the chosen token standard. This contract will define the token’s properties, such as name, symbol, and total supply, as well as its functionalities.
  3. Test the smart contract: Before deploying the contract to the Ethereum network, test it on a local development environment or a testnet. This step helps identify and fix potential bugs and issues in the contract.
  4. Deploy the smart contract...

Deploying and testing the ERC-20 token smart contract

You are already familiar with the steps to test a smart contract, but here is another method of testing the MySimpleToken contract.

The tests for the MySimpleToken smart contract are in the test folder and a file named MySimpleToken.test.js, which contains the following test cases:

  • Test case 1 to create a token with a name
  • Test case 2 to create a token with a symbol
  • Test case 3 to check that tokens have the correct number of decimals
  • Test case 4 to check whether the token has a valid total supply
  • Test case 5 to check if users are able to query account balances
  • Test case 6 to transfer the right amount of tokens to/from an account
  • Test case 7 to see if the smart contract emits a transfer event with the right arguments
  • Test case 8 to allow for allowance approvals and queries
  • Test case 9 to emit an approval event with the right arguments
  • Test case 10 to allow an approved sender to transfer...

Summary

We covered various topics related to Ethereum tokens and smart contract testing. The key takeaways are that we learned about the six functions and two events required for an ERC-20-compliant smart contract and provided explanations and examples for each. We covered the process of creating an ERC-20 token contract, which is fungible in nature, with decent complexity (MySimpleToken) and we provided step-by-step instructions for testing its various methods using the Hardhat. Throughout the conversation, we provided detailed explanations, examples, and code snippets to help illustrate the concepts and guide users in creating, deploying, and testing token contracts. Overall, this discussion offers a solid foundation for understanding tokenomics, Ethereum tokens, and smart contract development and testing.

In the next chapter, we will cover non-fungible token standards in detail.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Essential Guide to Web3
Published in: Nov 2023Publisher: PacktISBN-13: 9781801813471
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Vijay Krishnan

Vijay Krishnan, an experienced expert in blockchain and Web3, holds the position of head of SysOps for Linea, a Layer 2 blockchain at Consensys. With a rich background in blockchain, he's guided Fortune 100 companies from ideation to product build. With over 50 projects and 300+ use cases to his credit, Vijay has left an indelible mark on the industry. Prior to Consensys, he pioneered the AWS Partner Blockchain ecosystem and led IBM North America's Blockchain Practice. With two decades on Wall Street, he possesses deep financial and tech insights. Vijay, a leader and subject matter expert, empowers clients in DeFi, NFTs, gaming, and the metaverse. Beyond work, he's a mentor, speaker, author, and organic farmer. His knowledge and innovation continue to shape the blockchain and Web3 landscape.
Read more about Vijay Krishnan