Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A minimal token

Shows: tell, do not write (1) · internal messages (3) · async message for strangers (9) · receive without failing (6)

Goal

A token with balances and transfers in one screen of code: the native token’s design (lee/state_machine/core/src/native_token.rs) with minting added.

Actors

ActorWhat it holdsAuthorized by
(holder, token)a balancethe holder
(ISSUER, token)a balance, and the right to mintthe issuer

One actor per holder. No central ledger.

Messages

#[derive(BorshSerialize, BorshDeserialize)]
enum Message {
    /// Needs the holder's authorization.
    Transfer { to: AccountId, amount: u128 },
    /// Needs the issuer's authorization, at the issuer's actor.
    Mint { to: AccountId, amount: u128 },
    /// Only from another actor of this program.
    Credit(u128),
}

Handler

fn handle_message(input: &ReceiveInput, message: Message) -> Response {
    let me = input.receiver;
    let balance = decode_balance(&input.pre_state);     // empty state reads as zero
    let token_actor = |account_id| Actor::new(account_id, me.program_account_id);

    match message {
        Message::Transfer { to, amount } => {
            assert!(input.is_authorized(), "not your balance");
            let rest = balance.checked_sub(amount).expect("insufficient balance");
            Response::set_state(encode_balance(rest))
                .send_async(token_actor(to), &Message::Credit(amount))
        }
        Message::Mint { to, amount } => {
            assert!(input.is_authorized() && me.account_id == ISSUER, "only the issuer mints");
            Response::keep_state().send_async(token_actor(to), &Message::Credit(amount))
        }
        Message::Credit(amount) => {
            assert!(input.from_own_program(), "a credit comes from the token itself");
            let total = balance.checked_add(amount).expect("balance overflows");
            Response::set_state(encode_balance(total))
        }
    }
}

What to notice

The sender debits itself and tells the recipient. No handler ever touches two balances. Outside Mint, supply is conserved, because the only way to send Credit is one of the two arms above.

Credit trusts its sender, not a signature: from_own_program() is its only check on who sent it.

The credit is an AsyncMessage: to a public recipient the transaction declares, it lands in the same transaction; to a private recipient it waits in their mailbox. The handler does not know which.

Receiving needs no setup: an empty state reads as zero, so the first credit creates the balance.

The sender does not need the recipient to be online. A private recipient needs no state yet, but a public sender’s credit goes out in the clear, so the address needs its recovery binding on chain or introduced by the same privacy-preserving transaction; a public recipient must be declared, and one that does not exist yet must also be admitted.

Public and private

SenderRecipientWhat an observer sees
PublicPubliceverything
PublicPrivatesender, amount, destination address
PrivatePublicamount, recipient; the sender is hidden or a pseudonym
PrivatePrivatea sealed message

The program has no code for these cases: they come from which accounts the user picks.

Going further

  • The shipped token (logos-blockchain/lez-programs: programs/token) adds token definitions with an optional mint authority, NFTs and burning. It is not an actor program: it pins lee_core at v0.2.5-rc2, the whole-account interface, so its Transfer names both holdings, creates the recipient’s holding when it is empty, and refuses one of another definition or kind. It sends no messages.
  • Custody by other programs. Transfer needs only a grant over the balance it debits, so a program can move tokens it holds on a PDA by granting the seed (pattern 13).
  • The same check is the footgun. A grant does not say who sent the Transfer. Read what it does not mean before copying this handler.