Voltar para Documentação

Docs Técnicas

A Simple Contract

This contract stores a single counter.

O conteúdo abaixo vem das fontes técnicas do repositório e é prerenderizado no site para leitura direta por pessoas, crawlers e agentes.

This contract stores a single counter.

python
contract Counter:
    storage:
        value: u64 = 0
        owner: Address

    @construct
    def initialize(owner: Address):
        self.owner = owner

    @view
    def get() -> u64:
        return self.value

    @tx
    def set(value: u64):
        require(ctx.caller == self.owner, "only_owner")
        self.value = value
        emit CounterSet(value)

Source Layout

Every source file starts with:

python
contract Name:

The contract name contributes to the canonical contract id generated by artifact compilation.

Storage Rules

The storage: block declares persistent fields:

python
storage:
    value: u64 = 0

Fields may have defaults. If a field has no default, the runtime initializes it to the type default.

For Address fields, that default is only a pre-construction sentinel. The runtime rejects writing the empty/zero address back into storage or using it in accounting effects.

Construction

Constructors use @construct:

python
@construct
def initialize(owner: Address):
    self.owner = owner

The constructor runs once. If a contract declares one, every other entrypoint is blocked until construction succeeds.

Read-Only Entry Points

Views are read-only:

python
@view
def get() -> u64:
    return self.value

A view cannot write storage, emit events, or produce ledger effects.

Mutating Entry Points

Transactions can mutate state:

python
@tx
def set(value: u64):
    require(ctx.caller == self.owner, "only_owner")
    self.value = value
    emit CounterSet(value)

If a transaction fails, storage is rolled back.

The runtime executes this contract locally. The ledger still decides whether the resulting effects are admissible for commit.