Build and Deploy a Custom ERC20 Token on a Local Ethereum Chain with Truffle and OpenZeppelin
This step‑by‑step guide shows how to set up a Mac development environment, create a Truffle project, install OpenZeppelin, write and compile a simple ERC20 token contract, deploy it to a Ganache private Ethereum network, and interact with it via MetaMask.
Prepare Development Environment (Mac)
Tools required: Node.js (v9.11.1) and npm (v5.6.0), Truffle (v4.1.5, Solidity 0.4.21), Ganache (v1.1.0).
Create a Truffle Project
Initialize with the tutorial token unbox: truffle unbox tutorialtoken The generated directory follows the standard Truffle layout: contracts/ – Solidity contract files (*.sol) migrations/ – Deployment scripts test/ – Test scripts truffle.js – Truffle configuration file
Install OpenZeppelin Contracts
npm install zeppelin-solidityWrite ERC20 Smart Contract
Create contracts/FakeToken.sol with the following content:
pragma solidity ^0.4.17;
import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
contract FakeToken is StandardToken {
string public name = 'FakeToken';
string public symbol = 'TT';
uint8 public decimals = 2;
uint public INITIAL_SUPPLY = 12000;
function FakeToken() public {
totalSupply_ = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}
}Compile the Contract
truffle compileTypical output shows compilation of FakeToken.sol and Migrations.sol and writes artifacts to ./build/contracts. Warnings about missing visibility can be ignored for this tutorial.
Deploy the Contract
Create migrations/2_deploy_contracts.js:
var FakeToken = artifacts.require("FakeToken");
module.exports = function(deployer) {
deployer.deploy(FakeToken);
};Start Ganache (default RPC port 7545) and run: truffle migrate The migration creates the contract address and four transactions (four blocks).
Interact via Front‑End and MetaMask
Update the front‑end RPC endpoint from port 9545 to Ganache’s 7545:
App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:7545');Replace references to TutorialToken.json with FakeToken.json in the UI scripts.
Configure MetaMask to connect to the local Ganache network and import the generated accounts. The first account holds the initial token supply.
Start the local HTTP server: npm run dev Test token transfer:
Select the first MetaMask account and refresh the page.
Enter the second account’s address in the “Recipient” field.
Enter a transfer amount (decimals = 2, so the amount is truncated to two decimal places).
Click “Transfer” and confirm the transaction in the MetaMask popup.
Ganache shows a new transaction and the second account receives the tokens.
To add the token to MetaMask:
Open MetaMask, go to the “TOKENS” tab, and click “Add Token”.
Enter the contract address displayed in Ganache.
MetaMask automatically fills in the token symbol and decimals.
Confirm to finish.
The newly created ERC20 token is fully functional on the local Ethereum network and visible in the MetaMask wallet.
Reference: http://truffleframework.com/tutorials/robust-smart-contracts-with-openzeppelin
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.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
