Tutorial·10 मिनट का पठन·Solingo द्वारा

ERC-20 Token Tutorial — Solidity में अपना पहला Token बनाएं

अपना पहला ERC-20 token create, test और deploy करने के लिए step-by-step guide। Scratch से mainnet तक।

# ERC-20 Token Tutorial — Solidity में अपना पहला Token बनाएं

अपना ERC-20 token बनाना किसी भी Solidity developer के लिए foundational skill है। चाहे आप governance token बना रहे हों, reward system, या सिर्फ सीख रहे हों, ERC-20 को समझना essential है।

इस tutorial में, हम scratch से एक ERC-20 token build करेंगे, thoroughly test करेंगे और testnet पर deploy करेंगे। अंत में, आपके पास production-ready token contract होगा।

Prerequisites

शुरू करने से पहले, सुनिश्चित करें कि आपके पास है:

  • Basic Solidity knowledge (variables, functions, mappings)
  • एक wallet (MetaMask recommended)
  • कुछ testnet ETH (Sepolia recommended)
  • Foundry installed (curl -L https://foundry.paradigm.xyz | bash)

ERC-20 क्या है?

ERC-20 Ethereum पर सबसे widely adopted token standard है। यह common interface define करता है जो सभी tokens को implement करना होता है, जो wallets, exchanges और dApps के साथ compatibility ensure करता है।

Standard 6 core functions और 2 events specify करता है:

Functions:

  • totalSupply(): Total token supply
  • balanceOf(address account): Address का balance get करें
  • transfer(address to, uint256 amount): Tokens transfer करें
  • approve(address spender, uint256 amount): Spending approve करें
  • allowance(address owner, address spender): Allowance check करें
  • transferFrom(address from, address to, uint256 amount): Behalf पर transfer करें

Events:

  • Transfer(address indexed from, address indexed to, uint256 value)
  • Approval(address indexed owner, address indexed spender, uint256 value)

Scratch से ERC-20 Build करना

आइए step by step basic ERC-20 token implement करें:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

contract BasicToken {

string public name;

string public symbol;

uint8 public decimals;

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(string memory _name, string memory _symbol, uint256 _initialSupply) {

name = _name;

symbol = _symbol;

decimals = 18; // अधिकांश tokens के लिए standard

totalSupply = _initialSupply * 10**decimals;

balanceOf[msg.sender] = totalSupply;

emit Transfer(address(0), msg.sender, totalSupply);

}

function transfer(address to, uint256 amount) public returns (bool) {

require(to != address(0), "Transfer to zero address");

require(balanceOf[msg.sender] >= amount, "Insufficient balance");

balanceOf[msg.sender] -= amount;

balanceOf[to] += amount;

emit Transfer(msg.sender, to, amount);

return true;

}

function approve(address spender, uint256 amount) public returns (bool) {

require(spender != address(0), "Approve to zero address");

allowance[msg.sender][spender] = amount;

emit Approval(msg.sender, spender, amount);

return true;

}

function transferFrom(address from, address to, uint256 amount) public returns (bool) {

require(from != address(0), "Transfer from zero address");

require(to != address(0), "Transfer to zero address");

require(balanceOf[from] >= amount, "Insufficient balance");

require(allowance[from][msg.sender] >= amount, "Insufficient allowance");

balanceOf[from] -= amount;

balanceOf[to] += amount;

allowance[from][msg.sender] -= amount;

emit Transfer(from, to, amount);

return true;

}

}

Testing with Foundry

Foundry के साथ comprehensive tests लिखें:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import "forge-std/Test.sol";

import "../src/BasicToken.sol";

contract BasicTokenTest is Test {

BasicToken token;

address alice = address(0x1);

address bob = address(0x2);

function setUp() public {

token = new BasicToken("MyToken", "MTK", 1000000);

}

function testTransfer() public {

token.transfer(alice, 1000);

assertEq(token.balanceOf(alice), 1000);

}

function testApproveAndTransferFrom() public {

token.approve(alice, 500);

vm.prank(alice);

token.transferFrom(address(this), bob, 500);

assertEq(token.balanceOf(bob), 500);

}

function testFuzz_transfer(uint256 amount) public {

vm.assume(amount <= token.totalSupply());

token.transfer(alice, amount);

assertEq(token.balanceOf(alice), amount);

}

}

Deployment

Sepolia testnet पर deploy करें:

forge create --rpc-url $SEPOLIA_RPC_URL \

--private-key $PRIVATE_KEY \

--constructor-args "MyToken" "MTK" 1000000 \

--verify --etherscan-api-key $ETHERSCAN_API_KEY \

src/BasicToken.sol:BasicToken

Production Enhancements

Real production token के लिए, add करें:

  • Minting और Burning:
  • function mint(address to, uint256 amount) public onlyOwner {
    

    totalSupply += amount;

    balanceOf[to] += amount;

    emit Transfer(address(0), to, amount);

    }

    function burn(uint256 amount) public {

    require(balanceOf[msg.sender] >= amount);

    balanceOf[msg.sender] -= amount;

    totalSupply -= amount;

    emit Transfer(msg.sender, address(0), amount);

    }

  • Pausable functionality
  • Access control (OpenZeppelin Ownable)
  • Events for all critical actions
  • निष्कर्ष

    आपने सफलतापूर्वक एक ERC-20 token बनाया, test किया और deploy किया! यह Solidity development में एक major milestone है।

    अगले कदम:

    • Solingo पर advanced token features practice करें
    • NFTs के लिए ERC-721 explore करें
    • DeFi protocols में integration सीखें

    ---

    अतिरिक्त Resources:

    Practice में लगाने के लिए तैयार हैं?

    Solingo पर interactive exercises के साथ इन concepts को apply करें।

    मुफ्त में शुरू करें