koral
Menu

Examples / Source walkthrough

Small programs.
Visible behavior.

Start with a real source example. Understand what each declaration means before building a larger program.

01 / Typed Counter

A state update in Kotlin.

package example
import dev.koral.api.*

@Account("counter")
data class Counter(val count: ULong)

@Instruction
fun increment(state: Counter, by: ULong): Counter =
    Counter(checkedAdd(state.count, by))
Download typed-counter.kt ↗

This is the bundled source file, not a browser simulation. Compile it through the local CLI described in the getting-started guide.

Describe the state.

The @Account declaration identifies the owned state type. Counter stores one immutable ULong field named count. An instruction returns the next value of that state.

The supported state model is deliberately restricted. This preview is not a general Kotlin runtime or arbitrary object serialization system.

Make the update explicit.

The @Instruction function takes the current Counter and an unsigned increment. checkedAdd expresses checked unsigned arithmetic. Returning Counter(...) describes the state after the update.

Counter(checkedAdd(state.count, by))

The instruction logic is only part of a Solana program. Account order, authorization, initialization, and runtime checks remain important. Read the emitted ABI and test behavior locally.

Know the boundaries.

The current compiler supports a restricted owned ULong state model, checked arithmetic, and selected account operations. General Kotlin libraries, arbitrary CPI, and complete DEX programs are outside the current launch scope.

A Counter example is a starting point for inspecting the compilation path. It does not establish production readiness or general protocol performance.