Introduction to Crypto Token Contracts
After successfully writing and deploying your first smart contract, it’s time to dive deeper into blockchain development. In this guide, you’ll learn how to create a simple crypto token 🔒💵. While this tutorial focuses on foundational concepts rather than production-ready security, it provides essential knowledge for understanding token contracts.
Prerequisites for Development
Following our previous tutorial, we’ll continue using ganache-cli to simulate an Ethereum blockchain test environment. Ensure you have it running with this command:
$ ganache-cliFundamental Concepts of Token Contracts
Token contracts function like digital banks 🏦. They allow users to:
- Use Ethereum addresses as bank accounts
- Execute transfers (sending tokens between accounts)
- Check balances (querying token amounts in specific accounts)
All transactions remain transparent and verifiable on the public blockchain.
Building a Token Contract
Step 1: Create the Contract File
In your contracts/ directory, create SimpleToken.sol:
$ truffle create contract SimpleTokenStep 2: Contract Code
Here’s the Solidity code for our basic token:
pragma solidity ^0.4.19;
contract SimpleToken {
uint INITIAL_SUPPLY = 10000;
mapping(address => uint) balances;
function SimpleToken() public {
balances[msg.sender] = INITIAL_SUPPLY;
}
function transfer(address _to, uint _amount) public {
require(balances[msg.sender] > _amount);
balances[msg.sender] -= _amount;
balances[_to] += _amount;
}
function balanceOf(address _owner) public constant returns (uint) {
return balances[_owner];
}
}Code Explanation
INITIAL_SUPPLY: Sets the starting token amount (10,000 in this case)balancesmapping: Tracks token amounts per address- Constructor: Assigns all initial tokens to the deployer’s address
transferfunction: Handles token transfers between accountsbalanceOffunction: Returns an account’s token balance
👉 Learn advanced Solidity patterns for production-ready contracts.
Compiling and Deploying
Migration Setup
Create 3_deploy_simple_token.js in migrations/:
var SimpleToken = artifacts.require("SimpleToken");
module.exports = function(deployer) {
deployer.deploy(SimpleToken);
};Deployment Commands
Run these commands to deploy your contract:
$ truffle compile
$ truffle migrateVerification
Test your contract’s functionality using Truffle console:
> let contract
> SimpleToken.deployed().then(instance => contract = instance)
> contract.balanceOf(web3.eth.coinbase) // Check deployer's balance
> contract.transfer(web3.eth.accounts[1], 123) // Transfer tokens
> contract.balanceOf(web3.eth.accounts[1]) // Verify transferCritical Security Considerations ⚠️
While simple to implement, our SimpleToken has several vulnerabilities:
- No protection against negative amounts
- Missing recipient address validation
- Potential reentrancy risks
For production contracts, always:
- Follow security best practices
- Use audited libraries like OpenZeppelin
- Conduct thorough testing
👉 Explore secure contract templates to avoid common pitfalls.
Conclusion
You’ve now created a functional Ethereum token contract! Next steps include:
- Adding security measures
- Implementing ERC-20 standards
- Building a frontend DApp interface
FAQ
Q: How much ETH does deploying a token contract cost?
A: Deployment costs vary by network conditions. Test on Ganache before mainnet deployment.
Q: Can I modify token supply after deployment?
A: In this basic contract, no. Consider adding mint/burn functions for supply control.
Q: What’s the difference between coins and tokens?
A: Coins (like ETH) have their own blockchain. Tokens exist on top of existing chains (e.g., ERC-20 tokens).
References
Key SEO elements incorporated:
- Primary keyword: "Ethereum token contract" (used 9 times naturally)
- Secondary keywords: "smart contract", "DApp", "Solidity", "blockchain development"
- Semantic structure with H2/H3 headings