How to Deploy and Operate an ERC20 Token on Ethereum: A Complete Tutorial
This tutorial walks you through installing geth, synchronizing the Rinkeby testnet, creating accounts, writing and compiling Solidity contracts, deploying an ERC20 token, and performing common operations such as balance queries, transfers, approvals, and delegated transfers using the console and web3.js.
Introduction
In recent years blockchain projects, especially smart contracts, have become extremely popular. If you have never heard of Ethereum or smart contracts you are missing out. This guide shows you how to deploy a smart contract from zero to one.
Synchronization
All Ethereum data is stored on the blockchain. To read or write data you must first synchronize a full node locally because there is no direct API to query remote nodes.
Ethereum provides a Go client called geth. Install it on macOS with Homebrew:
brew tap ethereum
brew install ethereumSynchronize the Rinkeby testnet using fast mode:
geth --rinkeby --syncmode "fast" --cache=1024 --rpc --rpcapi "eth,net,web3,debug"During synchronization the console displays IPC and HTTP addresses; these will be needed later.
Create Account
In Ethereum each account is an address. Open the console and create a new wallet:
# This method cannot create an account
# geth attach http://127.0.0.1:8545
# This method can create an account
personal.newAccount("your_password")The command returns the new address, which is stored in the keystore directory.
You can list all accounts with:
eth.accountsSolidity Basics
Ethereum executes bytecode on the Ethereum Virtual Machine (EVM). Solidity is the most widely used language for writing EVM bytecode.
Key language features:
Inheritance : contracts can inherit from multiple parents using the is keyword.
Functions : declared with function, may have returns and visibility modifiers ( public, external, private, internal).
State mutability : view, pure, payable.
Constructor : defined with constructor and runs once during deployment.
Example of a simple contract:
pragma solidity ^0.4.23;
contract Smile {
function smile() public pure returns (uint) {
return 1;
}
}ERC20 Token Standard
Most Ethereum tokens follow the ERC20 interface, which defines a set of functions and events that wallets and exchanges understand.
contract ERC20 {
uint256 public totalSupply;
function balanceOf(address who) public view returns (uint256 value);
function transfer(address to, uint256 value) public returns (bool ok);
function approve(address spender, uint256 value) public returns (bool ok);
function allowance(address owner, address spender) public view returns (uint256 value);
function transferFrom(address from, address to, uint256 value) public returns (bool ok);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}The fields name, symbol, and decimals describe the token.
Deploying a Contract
There are several ways to deploy contracts:
Using solc
Install the Solidity compiler:
brew upgrade
brew tap ethereum/ethereum
brew install solidity
brew linkapps solidityCompile your source file:
solc MyToken.sol --overwrite --optimize --bin --abi -o buildThe .bin file contains the deployment bytecode, and the .abi file contains the contract interface.
Deploy with web3.js
Load the ABI and bytecode, then send a transaction:
var abi = [...] // ABI JSON array
var bin = "0x60806040..."; // deployment bytecode
var MyToken = web3.eth.contract(abi).new({from: myAddress, gas: 0x40e5, gasPrice: 10e9, data: bin});After the transaction is mined, the console prints the contract address.
Other Tools
Remix : an online IDE for Solidity that can compile and deploy directly.
Truffle : a development framework. Install with npm install -g truffle, then run truffle compile and truffle migrate. Truffle works with a geth RPC endpoint.
Interacting with a Deployed Token
Assume the token address is 0x08d8468a6b332ff48f7bccd8888e4a660ee42dbb. Define helper variables:
var token = "0x08d8468a6b332ff48f7bccd8888e4a660ee42dbb";
var addr1 = "0xda00ff72703fcc97d167696359fc02328508302f"; // owner
var addr2 = "0x767976f6b20655243bba20497fcd8d9f4eb4ee39"; // recipient
var addr3 = "0xd1ab0a8ffb68b076de20f06def43964748a7854a"; // third party
eth.gasPrice = 11e9; // set gas price (11 gwei)
web3.eth.defaultAccount = addr1;Load the standard ERC20 ABI:
var stdAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"}, ... ]; // truncated for brevityQuery balance:
var contract = web3.eth.contract(stdAbi).at(token);
contract.balanceOf(addr1); // returns 100000000000000000000 (100 tokens with 18 decimals)
web3.fromWei(contract.balanceOf(addr1), "ether"); // returns 100Transfer 1 token from the default account to addr2: contract.transfer(addr2, 1e18); Approve addr2 to spend 14 tokens on behalf of addr1:
contract.approve(addr2, 14e18);
contract.allowance(addr1, addr2); // should return 14e18Perform a delegated transfer (addr2 moves 1 token from addr1 to addr3):
web3.eth.defaultAccount = addr2;
contract.transferFrom(addr1, addr3, 1e18);
web3.eth.defaultAccount = addr1; // restore defaultAlternative way to send a transaction from a non‑default account:
contract.transfer.sendTransaction(addr2, 1e18, {from: addr1, gasPrice: 10e9});Other Resources
Android users can test tokens with the Toshi wallet (download Toshi‑4.2.apk ).
Reference links:
go‑ethereum (geth) repository
OpenZeppelin Solidity library
Solidity function guide
Rinkeby address example
Deployed token on Etherscan
(Please use blockchain technology in a lawful, compliant, and responsible manner.)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qizhuo Club
360 Mobile tech channel sharing practical experience and original insights from 360 Mobile Security and other teams across Android, iOS, big data, AI, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
