Skip to main content

The Contract

How It Works

Map Declaration

Maps have key and value type parameters. This one maps accounts to balance amounts.

Checking Membership

Use member(key) to check if a key exists before lookup.

Looking Up Values

Use lookup(key) to get the value for a key.

Inserting or Updating

Use insert(key, value) to add or update an entry.

Safe Arithmetic

Compact arithmetic widens the result type, so the sum must be asserted to fit and then explicitly cast back to Uint<128> before storing. See Overflow Protection.
setBalance and addBalance have no authentication. Anyone can overwrite any account’s balance. The pattern here is for illustration; production token contracts must gate writes with the Access Control keypair pattern.

Common Patterns

Initialize with Default

Create an entry if it doesn’t exist.

Nested Maps

Maps can contain other Maps for complex relationships (used in approval systems).

Iterating Over Maps

Compact doesn’t support iterating over Map keys directly. Track keys separately if needed.

What’s Next

Transfer Function

Build a token transfer system

ERC20 Token

See Maps in a complete token