Introduction to Token Creation on Ethereum
One of Ethereum's most popular features is the ability to create custom tokens. All tokens issued on Ethereum follow the ERC-20 standard, the same standard used by Ether itself. This compatibility ensures your tokens will work seamlessly with any wallet or exchange that supports Ethereum.
Developing an ERC-20 Smart Contract
Setting Up the Development Environment
We'll use Remix IDE (http://remix.ethereum.org/) for our development environment. Start by creating a new file named jasonToken.sol (you can choose any name).
The ERC-20 Interface Contract
pragma solidity ^0.5.0;
contract ERC20Interface {
function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}This interface defines the mandatory functions for any ERC-20 token:
totalSupply: Returns the total token supplybalanceOf: Returns the balance of a specific addresstransfer: Enables token transfers between walletstransferFrom: Facilitates automated transfersapprove: Verifies transactions against total supplyallowance: Cancels transactions with insufficient funds
Implementing SafeMath for Secure Calculations
Add the SafeMath contract below the ERC20 Interface code:
contract SafeMath {
function safeAdd(uint a, uint b) public pure returns (uint c) {
c = a + b;
require(c >= a);
}
function safeSub(uint a, uint b) public pure returns (uint c) {
require(b <= a);
c = a - b;
}
function safeMul(uint a, uint b) public pure returns (uint c) {
c = a * b;
require(a == 0 || c / a == b);
}
function safeDiv(uint a, uint b) public pure returns (uint c) {
require(b > 0);
c = a / b;
}
}SafeMath prevents overflow errors by reverting transactions when arithmetic operations exceed safe limits.
Creating the Token Contract
Now we'll implement our JasonToken contract, which will mint 50 million tokens:
contract JasonToken is ERC20Interface, SafeMath {
string public name;
string public symbol;
uint8 public decimals;
uint256 public _totalSupply;
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
constructor() public {
name = "JasonToken";
symbol = "Jas";
decimals = 1;
_totalSupply = 50000000;
balances[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}
function totalSupply() public view returns (uint) {
return _totalSupply - balances[address(0)];
}
function balanceOf(address tokenOwner) public view returns (uint balance) {
return balances[tokenOwner];
}
function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
return allowed[tokenOwner][spender];
}
function approve(address spender, uint tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] = safeSub(balances[msg.sender], tokens);
balances[to] = safeAdd(balances[to], tokens);
emit Transfer(msg.sender, to, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
balances[from] = safeSub(balances[from], tokens);
allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
balances[to] = safeAdd(balances[to], tokens);
emit Transfer(from, to, tokens);
return true;
}
}Compiling and Deploying the Contract
Deployment Configuration
Set Remix to use "Injected Provider - MetaMask" and ensure you're connected to the Goerli test network. Verify you have sufficient ETH for deployment.
Deployment Process
- Select the correct contract (JasonToken) before deploying
- Monitor deployment status via MetaMask Activity
- View detailed information on Etherscan
๐ Learn more about smart contract deployment best practices
Verifying Contract Deployment
Once deployed:
- Confirm the contract appears in your wallet
- Check transaction details on Etherscan
- Verify the token balance in your connected wallet
Adding Your Token to MetaMask
To make your token visible in MetaMask:
- Click "Add Token"
- Enter your contract address
- Confirm token details
๐ Discover advanced Ethereum wallet management techniques
Token Distribution
Now you're ready to:
- Send tokens to other Ethereum wallets
- List on decentralized exchanges
- Build applications using your custom token
FAQ Section
What is the ERC-20 standard?
ERC-20 is a technical standard for creating fungible tokens on Ethereum, ensuring compatibility across wallets and exchanges.
Why use SafeMath?
SafeMath prevents arithmetic overflow/underflow vulnerabilities that could lead to token loss or creation.
How much does it cost to deploy a token?
Deployment costs vary based on network congestion, but typically range from $50-$200 in gas fees.
Can I change token parameters after deployment?
No, token name, symbol, and total supply are immutable once deployed.
How do I get test ETH for deployment?
Use Goerli faucets to obtain free test ETH for development purposes.
What's the difference between decimals and total supply?
Decimals determine divisibility (e.g., 18 decimals = 1 token = 10^18 smallest units), while total supply is the whole number of tokens created.