0
0
Blockchain / Solidityprogramming~30 mins

ERC-20 fungible token standard in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a Simple ERC-20 Fungible Token
📖 Scenario: You want to create your own digital currency on the Ethereum blockchain. This currency will follow the ERC-20 standard, which means it can be easily used by wallets and exchanges.
🎯 Goal: Build a simple ERC-20 token contract that tracks balances, allows transfers, and lets users approve others to spend tokens on their behalf.
📋 What You'll Learn
Create a contract named SimpleToken
Define state variables for name, symbol, decimals, and totalSupply
Create a mapping called balances to track token balances
Create a mapping called allowances to track approved spending
Implement transfer, approve, and transferFrom functions
Emit Transfer and Approval events appropriately
💡 Why This Matters
🌍 Real World
ERC-20 tokens are the most common type of tokens on Ethereum. They represent digital assets like currencies, points, or shares.
💼 Career
Understanding ERC-20 tokens is essential for blockchain developers working on decentralized finance (DeFi), NFTs, and token-based applications.
Progress0 / 4 steps
1
Set up the basic contract and state variables
Write a Solidity contract named SimpleToken. Inside it, declare public variables name as "MyToken", symbol as "MTK", decimals as 18, and totalSupply as 1000000 * 10 ** 18. Also, create a public mapping balances from address to uint256.
Blockchain / Solidity
Need a hint?

Start by declaring the contract and the variables exactly as named.

2
Initialize the total supply and add allowances mapping
Add a constructor that assigns the entire totalSupply to the contract deployer's balances. Also, declare a public nested mapping allowances from address to address to uint256.
Blockchain / Solidity
Need a hint?

Use msg.sender to assign the total supply to the deployer.

3
Implement transfer, approve, and transferFrom functions
Write three public functions: transfer that moves tokens from the caller to another address, approve that allows a spender to spend tokens on the caller's behalf, and transferFrom that allows a spender to transfer tokens from an owner to a recipient. Make sure to update balances and allowances accordingly and return bool success.
Blockchain / Solidity
Need a hint?

Use require to check balances and allowances. Emit events after state changes.

4
Test the transfer function output
Add a simple test in the contract that transfers 100 tokens from the deployer to address 0x0000000000000000000000000000000000000001 and then print the balance of that address using a public view function. Write a public view function balanceOf that returns the balance of a given address. Then call balanceOf for 0x0000000000000000000000000000000000000001 and print the result.
Blockchain / Solidity
Need a hint?

Write a balanceOf function and a testTransfer function that transfers 100 tokens and returns the new balance.