Docs Técnicas
A Simple Contract
This contract stores a single counter.
This contract stores a single counter.
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:
contract Name:The contract name contributes to the canonical contract id generated by artifact compilation.
Storage Rules
The storage: block declares persistent fields:
storage:
value: u64 = 0Fields 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:
@construct
def initialize(owner: Address):
self.owner = ownerThe constructor runs once. If a contract declares one, every other entrypoint is blocked until construction succeeds.
Read-Only Entry Points
Views are read-only:
@view
def get() -> u64:
return self.valueA view cannot write storage, emit events, or produce ledger effects.
Mutating Entry Points
Transactions can mutate state:
@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.