A token, part 1: balances and transfer

Mint, read a balance, move value between accounts. This is the core every fungible token shares.

A fungible token is a mapping from address to amount, plus rules about who may change it. You already have every piece: composite keys from the mappings lesson, the integer pattern, abi.caller() and abi.require.

Three functions make a usable token:

  • mint(to, amount) — bring new units into existence and add to total supply
  • balance_of(addr) — read anyone's balance, free
  • transfer(to, amount) — move the caller's own units to someone else

Total supply is stored, not computed. There is no way to iterate every key in storage and sum balances — storage has no such operation, and a contract that needed one would cost unbounded gas. Keep a running total and update it in the same place you change balances.

Deduct before you credit, and check before you deduct. If transfer credits the recipient before verifying the sender can afford it, a single bad input mints tokens out of nothing. Order: require, deduct, credit, emit.

Your turn

Implement mint, balance_of, transfer and total_supply. Transfer must move the caller's tokens, reject overdrafts with b"insufficient", and emit Transfer.

Hints

Stuck? Ask

Answered by Animica's own free inference network. It is donated GPU capacity, so give it 20-30 seconds.