Objectives
By the end of this lesson you should be able to:- Construct a minimal token and deploy to testnet
- Identify the properties that make a token a token
Implementing a Token
The minimal elements needed for a token are pretty basic. Start by creating a contract calledMinimalToken
. Add a mapping
to relate user addresses to the number of tokens they possess. Finally, add a variable to track totalSupply
:
Reveal code
Reveal code
constructor
that initializes the totalSupply
at 3000 and assigns ownership to the contract creator:
Reveal code
Reveal code

Reveal code
Reveal code

Transferring Tokens
We can set an initial distribution of tokens and we can see balances, but we’re still missing a way to allow the owners of these tokens to share them or spend them. To remediate this, all we need to do is add a function that can update the balances of each party in the transfer. Add afunction
called transfer
that accepts an address
of _to
and a uint
for the _amount
. You don’t need to add anything for _from
, because that should only be msg.sender
. The function should subtract the _amount
from the msg.sender
and add it to _to
:
Reveal code
Reveal code

Note:
here is misleading. In the EVM, payable
only refers to transfers of the primary token used to pay gas fees: ETH, Base ETH, Sepolia ETH, Matic, etc. It does not refer to the balance of our simple token.
Instead, the transaction is reverting because of the built-in overflow/underflow protection. It’s not a great programming practice to depend on this, so add an error for InsufficientTokens
that returns the newSenderBalance
.
error
.
Destroying Tokens
Tokens can be effectively destroyed by accident, or on purpose. Accidental destruction happens when someone sends a token to an unowned wallet address. While it’s possible that some day, some lucky person will create a new wallet and find a pleasant surprise, the most likely outcome is that any given randomly chosen address will never be used, thus no one will ever have the ability to use or transfer those tokens. Luckily, there are some protections here. Similar to credit card numbers, addresses have a built-in checksum that helps protect against typos. Try it out by trying to transfer tokens to the second Remix address, but change the first character in the address fromA
to B
. You’ll get an error:
0x0000000000000000000000000000000000000000
. This address is unowned and unownable, making it mathematically impossible to retrieve any tokens that are sent to it. Redeploy and try it out by sending 1000 tokens to the zero address.
The totalSupply
remains unchanged, and the balance of the zero address is visible, but those tokens are stuck there forever.
The zero address currently has a balance of more than 11,000 ETH, worth over 20 million dollars! Its total holding of burned assets is estimated to be worth more than 200 million dollars!!!