Your first contract: storage

A contract is a Python module whose functions the chain can call. State lives in storage — and storage holds bytes, nothing else.

An Animica contract is a Python module. There is no class to inherit, no constructor, no decorators. Functions you define at the top level are the functions the chain can call.

State lives in storage, a key/value store scoped to your contract. Both keys and values are bytes:

storage.set(b"greeting", b"hello")
storage.get(b"greeting", b"")   ->  b"hello"

That "bytes only" rule is the first thing that trips people up coming from Solidity or from ordinary Python. There is no storage.set(b"n", 5) — an integer is not bytes, and the VM will refuse it.

A missing key reads as b"", not None. storage.get(key, b"") returns the default you pass. Passing b"" and comparing against b"" is the idiom used throughout the Animica standard library, and it means you never have to write a None check.

from stdlib import storage

def set_greeting(text: bytes) -> None:
    storage.set(b"greeting", bytes(text))

def greeting() -> bytes:
    return storage.get(b"greeting", b"")

Your turn

Write a contract that stores a name and reads it back. set_name(name) stores it; name() returns it, or b"" if nothing was ever stored.

Hints

Stuck? Ask

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