Reader small image

You're reading from  Learn Ethereum - Second Edition

Product typeBook
Published inAug 2023
PublisherPackt
ISBN-139781804616512
Edition2nd Edition
Concepts
Right arrow
Authors (3):
Xun (Brian) Wu
Xun (Brian) Wu
author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

Zhihong Zou
Zhihong Zou
author image
Zhihong Zou

Zhihong Zou is an innovative technology leader with over 20 years of experience delivering cutting-edge enterprise solutions. He has led transformative solutions in telecommunications, healthcare, and government that leverage blockchain, AI/ML, and modern cloud architectures to solve complex business challenges. As an insightful visionary and trusted advisor, Zhihong is passionate about driving digital transformation and future-proofing organizations. He holds an M.Sc degree in computational mathematics and an M.Sc in computer science. Zhihong has authored multiple papers on numerical computing and two popular editions of the book Learn Ethereum. In his free time, Zhihong enjoys sharing his blockchain expertise as an advocate and writer.
Read more about Zhihong Zou

Dongying Song
Dongying Song
author image
Dongying Song

Dongying Song has been a noteworthy leader in Blockchain, Ethereum, big data, ML, data science, and the cloud application development space for over 8 years. As an experienced software engineer and data scientist, she has worked for top-tier banks and pharmaceutical and technology companies. Dongying holds a master's degree in statistics from Columbia University and majored in mathematics during her undergraduate years. Her recent activities focus on Blockchain, Ethereum, and smart contract developments.
Read more about Dongying Song

View More author details
Right arrow

Developing Your Own Cryptocurrency

In this chapter, we will provide an overview of smart contract open source libraries and review ERC token standards. Then, we will create a new cryptocurrency called MyERC20Token based on the ERC-20 token standard using Solidity. We will also create a non-fungible DigitalArtERC721Token token for a decentralized art marketplace based on the ERC-721 standard. At the end of this chapter, we will discuss another popular Non-Fungible Token (NFT) token standard, ERC-1155, and learn about how to create an ERC-1155 NFT token. After reading through this chapter, you should be able to create your own cryptocurrency.

In this chapter, we will explore the popular tools and frameworks that you will come across frequently while starting to learn Ethereum. This will help you get a grasp on the big picture of the Ethereum ecosystem and apply this knowledge to Ethereum project development.

The topics that we will cover in this chapter are as follows:

    ...

Technical requirements

For all the source code for this book, please refer to the following GitHub link: https://github.com/PacktPublishing/Learn-Ethereum-Second-Edition

Understanding token standards

In Chapter 5, Deep Research and Latest Developments in Ethereum, we delved into the ERC-20 and ERC-721 token standards. ERC-20 is the predominant technical standard on the Ethereum blockchain for issuing tokens, which function as an interchangeable digital currency. Before the ERC token standard, numerous Initial Coin Offerings (ICOs), start-ups, and Decentralized Applications (DApps) created their tokens with many different standards. Since the release of the ERC-20 standard, things have changed and become much more streamlined.

The benefits of ERC-20 tokens include the following:

  • Reducing the risk of contracts breaking
  • Reducing the complexity of token interactions
  • Allowing for lots of different use cases to emerge on top of these standards
  • Uniform and quicker transactions
  • Confirming transactions more efficiently
  • Enhancing token liquidity

However, ERC-20 tokens do not have built-in regulatory limitations by design...

Setting up an Ethereum development environment

To start with smart contract development, you will need to install development tools. Let me introduce you to a popular DApp development tool—Truffle. Refer to the upcoming section for guidance on acquiring the necessary Ethereum development tools and launching a local private blockchain environment, which is typically utilized to execute and deploy your smart contract on a local blockchain.

Working with Truffle

Truffle is a comprehensive Ethereum DApp development tool that facilitates the creation, compilation, testing, and deployment of smart contracts and DApps within a development environment.

When utilizing Truffle, frontend development can be executed through HTML, CSS, and JavaScript, while smart contracts are written in Solidity. The web3.js API is used for interaction between the UI and smart contract. Truffle Boxes are pre-defined code templates that offer beneficial modules, Solidity contracts and libraries,...

Creating an ERC-20 token – MyERC20Token

Now that we have set up our working environment, we can now start to add code as we walk through the explanation. In this section, we will create our cryptocurrency, called MyERC20Token. For this project, we will use the Truffle framework and Solidity language. The idea of MyERC20Token is to use the ERC-20 token standard and issue our token.

Here is a list of token features we are going to build:

  • The token name is MyERC20Token, with the symbol MTK.
  • The token must follow the ERC-20 standard.
  • The token must specify the initial supply during its creation.
  • The token owner can assign someone to the admin role.
  • The admin can add an account to a whitelist and remove the account from the whitelist.
  • The admin can lock or unlock the coins of anyone’s account through a whitelist.
  • The admin can transfer the administrator role to any other address.
  • Anyone who is on the whitelist must be able to buy or sell...

Defining and implementing the ERC-20 interface

The ERC-20 interface defines a set of rules that a token must follow in order to be traded on the Ethereum network. There are a total of six mandatory functions and three optional functions that a token contract must implement. We define the ERC-20 token interface as follows. MyERC20Token will implement the ERC-20 interface:

interface ERC20 {
    function transfer(address to, uint256 value) external returns (bool);
    function approve(address spender, uint256 value) external returns (bool);
    function transferFrom(address from, address to, uint256 value) external returns (bool);
    function totalSupply() external view returns (uint256);
    function balanceOf(address who) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
   &...

Creating an ERC-721 token – DigitalArtERC721Token

For artists, when they want to sell their products, it is often hard to connect with dealers and collectors. Even if they manage to connect with dealers and collectors, the party on the other side may not be interested in buying the artist’s product. Selling their art to the next owner may take a long time due to the many layers of the middleman. The entire process is not transparent and not efficient. Blockchain, however, is a technology that can provide you with opportunities to make the process more transparent.

An art gallery is a great example of an ERC-721 token use case. ERC-721 tokens are NFTs and essentially have a unique identity that can be used to represent digital ownership. The token itself is genetically traceable. When a digital art owner assigns a unique token ID, along with the original author’s information, the art token can be used to track the entire transaction history when product ownership...

Creating an ERC-1155 token – ERC1155NFTToken

In MyERC20Token and DigitalArtERC721Token, we create all required smart contracts on our own, which we can learn and understand and customize these contracts. In this section, we will use one of the most popular open source smart contract frameworks, OpenZeppelin, to build our ERC1155MultiNFTToken.

OpenZeppelin is a library that provides reusable, battle-tested, modular templates. The libraries have many trusted smart contracts for complex smart contract creation, including ERC-20, ERC-21, ERC-1155, and many other OpenZeppelin contracts.

NFT digital assets are often saved in the InterPlanetary File System (IPFS). The ERC-1155 token defines a token URI to store token JSON metadata, which has NFT token information, including the image location.

An NFT’s metadata is often saved in the IPFS. Next, we will upload NFT images to the IPFS.

Uploading NFT images

We will first create 5 NFT collections. The images are under...

Summary

In this chapter, we covered the most popular ERC token standards – ERC-20, ERC-721, and ERC-1155. We set up a Truffle project and developed an ERC-20 token called MYERC20Token. We also implemented the ERC-20 interface with many additional token functions. Next, we continued to implement ERC-721 for a digital art NFT, which we used for an art trade. Finally, we covered another popular NFT token standard, ERC-1155, and walked through the OpenZeppelin ERC-1155 contract to get a better understanding.

In the next chapter, we will continue our learning journey and start to learn about Ethereum tools and unit tests.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Ethereum - Second Edition
Published in: Aug 2023Publisher: PacktISBN-13: 9781804616512
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

Authors (3)

author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

author image
Zhihong Zou

Zhihong Zou is an innovative technology leader with over 20 years of experience delivering cutting-edge enterprise solutions. He has led transformative solutions in telecommunications, healthcare, and government that leverage blockchain, AI/ML, and modern cloud architectures to solve complex business challenges. As an insightful visionary and trusted advisor, Zhihong is passionate about driving digital transformation and future-proofing organizations. He holds an M.Sc degree in computational mathematics and an M.Sc in computer science. Zhihong has authored multiple papers on numerical computing and two popular editions of the book Learn Ethereum. In his free time, Zhihong enjoys sharing his blockchain expertise as an advocate and writer.
Read more about Zhihong Zou

author image
Dongying Song

Dongying Song has been a noteworthy leader in Blockchain, Ethereum, big data, ML, data science, and the cloud application development space for over 8 years. As an experienced software engineer and data scientist, she has worked for top-tier banks and pharmaceutical and technology companies. Dongying holds a master's degree in statistics from Columbia University and majored in mathematics during her undergraduate years. Her recent activities focus on Blockchain, Ethereum, and smart contract developments.
Read more about Dongying Song