Creating a Token: Writing a Token Smart Contract

·

Ethereum introduced significant improvements over the Bitcoin blockchain, with one of its standout features being the ability to develop Turing-complete programs—smart contracts. Among these, Ethereum tokens have become the most widely used. Today, we'll explore how to write a smart contract for tokens.

Simple Methods to Issue Tokens

Some may assume that issuing tokens is highly complex, especially for those without programming experience. However, several automated token-generation tools now exist, making it possible for anyone to create their own token without coding knowledge.

Token Factory

Token Factory is a website that actively assists in generating tokens. Simply input the key parameters of your token, and the site will automatically create it for you.

One-Click Token

Developed domestically, One-Click Token is another token-generation platform. Like Token Factory, it only requires you to enter the token's parameters to generate it. Note that it requires integration with the MetaMask wallet.

My Token Deployer

This platform exclusively serves the Rinkeby test network. It allows you to input only the token name and symbol.

Introduction to Development Tools

While automated token-generation tools exist, developers often prefer having full control over their code. Before diving into token development, let's review the essential tools required.

Truffle

Truffle is an Ethereum development framework originally conceived during a blockchain hackathon. It has since evolved into a developer-friendly tool with features such as:

OpenZeppelin

As blockchain technology gains traction, hackers have increasingly targeted smart contracts, exposing vulnerabilities—some of which can render tokens worthless. Writing secure token contracts is now more critical than ever.

Fortunately, the community offers tools to mitigate these risks. OpenZeppelin is a reusable smart contract framework providing battle-tested templates that accelerate development while ensuring security.

Ganache

Developed by the Truffle team, Ganache is a GUI version of testrpc (now rebranded as ganache-cli). It enables users to create a local blockchain environment for development and testing via an intuitive interface.

Project Initialization

With the tools introduced, let’s proceed to write our token’s smart contract. First, we need to initialize the project using Truffle.

CommandDescription
mkdir mytokenCreates an empty directory
cd mytokenNavigates into the directory
truffle unbox tutorialtokenInitializes the project using a template

We’ve used the tutorialtoken template—a specialized boilerplate for token smart contracts. This template includes deployment scripts, front-end code, and other essentials, significantly reducing manual effort. The resulting directory structure is:

├── contracts
│   ├── Migrations.sol
├── migrations
│   ├── 1_initial_migration.js
├── src
│   ├── css
│   ├── fonts
│   ├── index.html
│   └── js
├── test
└── truffle.js

Key directories include contracts (for Solidity files) and migrations (for deployment scripts). The truffle.js file handles environment configuration. For more details, refer to the official documentation.

Writing the Contract Code

After initializing the project, we integrate OpenZeppelin as the contract’s base class. First, install OpenZeppelin:

yarn add openzeppelin-solidity

Next, create Mytoken.sol in the contracts directory with the following code:

pragma solidity ^0.4.24;
import 'openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';

contract Mytoken is StandardToken {
    string public constant name = "My token";
    string public constant symbol = "MT";
    uint8 public constant decimals = 18;
    uint256 public constant INITIAL_SUPPLY = 10000000000000 * (10 ** uint256(decimals));

    constructor() public {
        totalSupply_ = INITIAL_SUPPLY;
        balances[msg.sender] = INITIAL_SUPPLY;
    }
}

Using OpenZeppelin simplifies our code dramatically. By inheriting from StandardToken, we only need to specify the token’s core details: name, symbol, decimals, and initial supply. Here, the total supply is set to 10¹⁴ tokens with a precision of 10¹⁸ (matching Ethereum’s Ether).

Compiling the Contract

Once the contract is written, compile it using:

truffle compile

This generates a build directory containing JSON files for each compiled Solidity file:

build
└── contracts
    ├── Mytoken.json
    ├── StandardToken.json
    └── ...

The Mytoken.json file includes detailed metadata about the smart contract.

Conclusion

This guide covered the basics of writing a token smart contract. With frameworks like Truffle and libraries like OpenZeppelin, the process is streamlined, secure, and efficient.

👉 Explore advanced token deployment strategies to take your project further.


FAQs

Q: Do I need programming experience to issue a token?
A: No. Tools like Token Factory allow token creation without coding, but developers prefer custom solutions for control and security.

Q: How secure are OpenZeppelin contracts?
A: OpenZeppelin’s contracts are extensively audited and widely used in production, offering robust security.

Q: Can I test my token before deploying it?
A: Yes. Ganache provides a local blockchain for testing, ensuring your contract works as expected before going live.

Q: What’s the purpose of the decimals parameter?
A: It defines the smallest divisible unit of your token, similar to how Ether uses 18 decimal places.

Q: Is Truffle necessary for Ethereum development?
A: While not mandatory, Truffle simplifies tasks like compiling, testing, and deploying contracts.

👉 Learn more about optimizing smart contracts for better performance and lower gas fees.