#21DaysSolidityChallenge Day 3: Creating Your Own ERC-20 Token in Solidity

Solidity Academy
Coinmonks

--

🚀 Buy me a coffee! ☕ http://buymeacoffee.com/solidity

👋 Welcome to Day 3 of our Solidity Code Challenge series! In this challenge, you’ll embark on a journey to create your very own ERC-20 token contract using Solidity. ERC-20 is a widely adopted standard for fungible tokens on the Ethereum blockchain, and by building this token, you’ll gain a solid understanding of token creation and management.

#21DaysSolidityChallenge 21 Days Solidity Coding Challenge

Oh, this magical link is just sooo tempting! đŸȘ„✚ Click away, my dear friend. 😉

đŸ€– Solidity and ERC-20 Tokens: A Quick Overview

Before we dive into the challenge, let’s briefly review some key concepts:

- Solidity: Solidity is a high-level programming language used to write smart contracts on the Ethereum blockchain. It’s designed to be similar to JavaScript, making it accessible to developers.

- ERC-20: ERC-20 is a technical standard used for creating fungible tokens on the Ethereum blockchain. Fungible tokens are identical and interchangeable, much like traditional currencies.

Now, let’s get started with the challenge!

Step 1: Setting Up Your Development Environment

Before we dive into coding, ensure you have the following tools and accounts ready:

1. Ethereum Wallet: You’ll need an Ethereum wallet like MetaMask to interact with the Ethereum blockchain.

2. Solidity Compiler: Have the Solidity compiler (solc) installed on your computer or use online Solidity development environments like Remix.

3. Test Network: Choose a test network (e.g., Ropsten, Rinkeby, or Kovan) to deploy and test your token contract without using real Ether.

4. Integrated Development Environment (IDE): Consider using an IDE like Visual Studio Code with Solidity extensions for a smoother coding experience.

Step 2: Writing the ERC-20 Token Smart Contract

Let’s create a Solidity smart contract for your ERC-20 token. We’ll name it `MyToken.sol`. This contract will include functions for transferring tokens, checking balances, and approving token transfers.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyToken {
string public name = "My Token";
string public symbol = "MT";
uint8 public decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
constructor(uint256 initialSupply) {
totalSupply = initialSupply * 10 ** uint256(decimals);
balanceOf[msg.sender] = totalSupply;
}
function transfer(address to, uint256 value) public returns (bool success) {
require(to != address(0), "Invalid address");
require(balanceOf[msg.sender] >= value, "Insufficient balance");
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
emit Transfer(msg.sender, to, value);
return true;
}
function approve(address spender, uint256 value) public returns (bool success) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function transferFrom(address from, address to, uint256 value) public returns (bool success) {
require(from != address(0), "Invalid sender address");
require(to != address(0), "Invalid recipient address");
require(balanceOf[from] >= value, "Insufficient balance");
require(allowance[from][msg.sender] >= value, "Allowance exceeded");
balanceOf[from] -= value;
balanceOf[to] += value;
allowance[from][msg.sender] -= value;
emit Transfer(from, to, value);
return true;
}
}

In this contract:

- We define a contract called `MyToken`.

- We declare several state variables, including the token’s name, symbol, decimals, and total supply.

- We use two mapping data structures:
— `balanceOf`: Maps addresses to token balances.
— `allowance`: Maps address pairs to approved token spending amounts.

- We implement three important events: `Transfer`, `Approval`, and `TransferFrom`.

- The constructor sets the initial total supply and assigns it to the contract creator.

- We implement functions for transferring tokens (`transfer`), approving token transfers (`approve`), and executing approved transfers (`transferFrom`).

Step 3: Compiling the Token Contract

Compile your ERC-20 token contract using the Solidity compiler. Use the following command in your terminal:

solc - bin - abi MyToken.sol

This command generates two essential artifacts:

- `MyToken.bin`: The bytecode that will be deployed to the Ethereum blockchain.

- `MyToken.abi`: The Application Binary Interface (ABI) specifying how to interact with the contract.

Step 4: Deploying Your ERC-20 Token Contract

To interact with your ERC-20 token contract, you need to deploy it to a test network. For this challenge, we’ll use the Ropsten test network. Follow these steps:

1. Open your Ethereum wallet (e.g., MetaMask) and switch to the Ropsten network.

2. Get some test Ether for Ropsten from a faucet. You can find a faucet by searching for “Ropsten faucet” online.

3. Deploy your contract using a tool like Remix or Truffle, or do it manually through a script. Here’s an example of deploying your contract using Remix:

- Go to Remix (https://remix.ethereum.org/).

- Click the “Solidity” tab.

- Click “Create” to create a new file, and paste your contract code.

- Click the “Deploy & Run Transactions” tab.

- Ensure that your environment is set to “Injected Web3” (if you’re using MetaMask).

- Click the “Deploy” button.

4. Confirm the deployment in your wallet, and your contract will be deployed to the Ropsten network.

Step 5: Interacting with Your ERC-20 Token Contract

Now that your contract is deployed, you can interact with it to transfer tokens, check balances, and approve token transfers.

Transferring Tokens

1. In Remix, go to the “Deployed Contracts” section.

2. Find your deployed `MyToken` contract and click on it.

3. You will see the `transfer` function under the contract details.

4. Enter the recipient’s address and the number of tokens you want to transfer.

5. Click the “transfer” button to initiate the transfer.

Checking Balances

1. Still in Remix, locate your deployed `MyToken` contract.

2. You will see the `balanceOf` function under the contract details.

3. Enter the address for which you want to check the balance.

4. Click the “balanceOf” button to view the balance.

Approving Token Transfers

1. In Remix, find your deployed `MyToken` contract.

2. You will see the `approve` and `transferFrom` functions under the contract details.

3. Use the `approve` function to approve a spender to transfer a certain number of tokens on your behalf.

4. Then, use the `transferFrom` function to execute the approved transfer.

🎉 Congratulations! You’ve successfully created and deployed your own ERC-20 token contract and interacted with it.

Conclusion 🌟

In this Day 3 challenge, you’ve gone beyond the basics and created your own ERC-20 token contract, complete with functions for transferring tokens, checking balances, and approving token transfers. This is a significant step in your journey to becoming a Solidity developer.

As you continue through this Solidity Code Challenge series, you’ll build on this foundation and explore more complex smart contract scenarios. Stay curious, keep coding, and embrace the endless possibilities of blockchain technology!

🚀 Happy coding! 🚀

📚 Resources 📚

--

--

Solidity Academy
Coinmonks
Writer for

Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/ SEND US Your Products to Review! solidity101@gmail.com