Back in 2022, I created a token called MetaKey ($MKT) on BNB Smart Chain. This week, while preparing for my Blockchain Technology exam, I went back and read that contract line by line.
The source is verified, so you can read along on BscScan or Sourcify.
Here's what's actually in it, what I understand now that I didn't then, and one bug I only noticed this time.
The deployment
| Deployed | 24 May 2022, block 18,078,267 |
| Compiler | solc 0.4.26 (pragma solidity ^0.4.24), optimizer off |
| Deploy cost | 1,912,599 gas at 5 gwei, about 0.0096 BNB |
| Runtime bytecode | 7,967 bytes |
| Decimals | 9 |
| Supply at deploy | 1,000,000,000 MKT |
The contract is one file and a classic inheritance chain from that era:
SafeMath (library) Ownable -> Pausable ERC20Basic -> ERC20 -> StandardToken StandardToken + Pausable -> PausableToken -> Metakey The constructor
constructor(string memory _name, string memory _symbol, uint256 _decimals, uint256 _supply, address tokenOwner) public { name = _name; symbol = _symbol; decimals = _decimals; totalSupply = _supply * 10**_decimals; balances[tokenOwner] = totalSupply; owner = tokenOwner; emit Transfer(address(0), tokenOwner, totalSupply); } The deployment passed "MetaKey", "MKT", 9, 1000000000 and the deployer's address. So totalSupply became 1,000,000,000 × 10⁹ = 10¹⁸ base units.
That's the first thing that clicked this time: decimals are only a display hint. The contract never stores 1.5 MKT. It stores 1,500,000,000 base units, and wallets move the decimal point when they show it.
There's also a small spec mismatch I never noticed. EIP-20 defines decimals() as returning uint8, but this contract declares uint public decimals, which is a uint256. It works in practice because both are ABI-encoded as one 32-byte word and 9 fits either way, but a strict client could reject it.
A balance is just a mapping entry
mapping(address => uint256) balances; function transfer(address _to, uint256 _value) public returns (bool) { require(tokenBlacklist[msg.sender] == false); require(_to != address(0)); require(_value <= balances[msg.sender]); balances[msg.sender] = balances[msg.sender].sub(_value); balances[_to] = balances[_to].add(_value); emit Transfer(msg.sender, _to, _value); return true; } Nothing moves. A transfer is two writes to a mapping plus an event that wallets and explorers index.
The .sub() and .add() come from SafeMath, and that's an era detail worth knowing. Before Solidity 0.8, arithmetic silently wrapped on overflow, so most tokens pulled in a library like this. Since 0.8.0 the compiler checks overflow for you and reverts. One more pre-0.8 quirk: SafeMath here uses assert, which in 0.4.x compiles to the INVALID opcode and burns all remaining gas on failure. require reverts and refunds what's left.
approve, transferFrom and the race condition
approve + transferFrom is how a contract spends tokens on your behalf. The well-known problem: if you change an allowance from 100 to 50 with approve, the spender can watch the mempool, spend the old 100 first, then spend the new 50.
This contract also ships increaseApproval and decreaseApproval, which adjust the allowance relative to its current value. That's the usual mitigation, and I didn't understand why they were there in 2022.
The admin surface
This is the part I care about most now. Here's everything the owner can do:
| Function | Who can call it | What it does |
|---|---|---|
mint(account, amount) | owner | Creates new tokens. No cap. |
pause() / unpause() | owner | Freezes transfers and approvals for everyone. |
blackListAddress(addr, bool) | owner | Blocks an address from sending. |
transferOwnership(newOwner) | owner | Hands all of the above to someone else. |
There's no renounceOwnership(), and transferOwnership rejects address(0). So once deployed, the only way to give these powers up is to transfer ownership to an address nobody controls.
You can check the mint permission yourself without the source. eth_call simulates a call without sending a transaction:
"""Read the MetaKey (MKT) contract straight from BNB Smart Chain. No API key, no libraries.""" import json import urllib.request RPC = "https://bsc-dataseed.binance.org/" MKT = "0x99143d0B257F18966fB2773bbFbD170A8256784d" def call(data, sender=None): tx = {"to": MKT, "data": data} if sender: tx["from"] = sender body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "eth_call", "params": [tx, "latest"]}).encode() req = urllib.request.Request(RPC, body, {"Content-Type": "application/json", "User-Agent": "read-mkt"}) return json.load(urllib.request.urlopen(req)) def text(hex_result): # ABI-encoded string: offset, length, bytes raw = bytes.fromhex(hex_result[2:]) return raw[64:64 + int.from_bytes(raw[32:64], "big")].decode() def word(value): # left-pad an address or number to 32 bytes return value.lower().replace("0x", "").rjust(64, "0") name = text(call("0x06fdde03")["result"]) # name() symbol = text(call("0x95d89b41")["result"]) # symbol() decimals = int(call("0x313ce567")["result"], 16) # decimals() supply = int(call("0x18160ddd")["result"], 16) # totalSupply() owner = "0x" + call("0x8da5cb5b")["result"][-40:] # owner() print(f"{name} ({symbol}), {decimals} decimals") print(f"total supply: {supply} base units = {supply // 10**decimals:,} {symbol}") print(f"owner: {owner}") # Simulate mint(someone, 1) from a random address and from the owner. eth_call sends nothing. stranger = "0x1111111111111111111111111111111111111111" mint = "0x40c10f19" + word(stranger) + word("1") print("mint as stranger:", "reverts" if "error" in call(mint, stranger) else "succeeds") print("mint as owner: ", "reverts" if "error" in call(mint, owner) else "succeeds") Output:
MetaKey (MKT), 9 decimals total supply: 1000000000000000000 base units = 1,000,000,000 MKT owner: 0xd3a85d5064d0db781465746a950504f6ffd980c7 mint as stranger: reverts mint as owner: succeeds The hex strings are function selectors: the first 4 bytes of keccak256("name()"), keccak256("mint(address,uint256)") and so on. That's all an ABI call really is.
The bug I missed
The blacklist check in transferFrom looks at the wrong address:
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { require(tokenBlacklist[msg.sender] == false); // checks the spender require(_to != address(0)); require(_value <= balances[_from]); // _from is never checked require(_value <= allowed[_from][msg.sender]); ... } msg.sender in transferFrom is the spender, not the holder. And approve doesn't check the blacklist at all. So a blacklisted address can still approve a second address, and that second address can move the tokens out with transferFrom. The blacklist only blocks the direct path.
The fix is to check _from (and arguably _to) as well. Better still, don't ship a blacklist unless you really need one.
Burning
function burn(uint256 _value) public { _burn(msg.sender, _value); } function _burn(address _who, uint256 _value) internal { require(_value <= balances[_who]); balances[_who] = balances[_who].sub(_value); totalSupply = totalSupply.sub(_value); emit Burn(_who, _value); emit Transfer(_who, address(0), _value); } Anyone can burn their own tokens. Burning lowers totalSupply and emits a Transfer to address(0), which is how explorers show it. Because transfer() rejects address(0), burn() is the only real way to destroy tokens here.
And here's the funny part: we never called it. To support the price, we "burned" 500,000,000 MKT, half the supply, by sending it to a fresh address, 0x61d0…5848. That address has never made a single transaction.
But totalSupply() still returns 1,000,000,000 MKT today, because a plain transfer doesn't touch it. Every explorer still counts those tokens. Strictly speaking they're locked, not destroyed: whoever holds that address's private key could move them.
The well-known dead address 0x000000000000000000000000000000000000dEaD has the same supply problem, but at least nobody plausibly holds its key. Calling burn() would have done it properly in one line.
What I understand now: burning changes supply. It does nothing for demand.
BEP-20 or ERC-20?
Pretty much every token on BNB Smart Chain gets called BEP-20. The BEP-20 spec actually adds one function on top of ERC-20, getOwner(), which returns the token owner. MKT doesn't implement it, so a call to getOwner() reverts. Strictly speaking, MKT is an ERC-20 contract that happens to live on BNB Chain.
What I'd do differently today
- Solidity ^0.8 and OpenZeppelin. Checked arithmetic by default, and
ERC20plusERC20Burnableinstead of a hand-assembled template. - No admin powers I don't need. Fixed supply, no mint, no pause, no blacklist. If an admin is truly needed, a multisig behind a timelock, not one wallet.
- Tests before deploy. The blacklist bug above is exactly what a two-line test catches.
- Write down the admin surface. Anyone holding the token should be able to see, in one table, what the owner can do.
We don't maintain MetaKey anymore, but the contract is still on-chain and still readable, which turned out to be the most useful thing about it. Revisiting your own old code is a good way to find out what you've actually learned since.
- Contract: BscScan
- X: @MetakeyToken
- Instagram: @metakeytoken


