Skip to main content

Program Declaration

Every Leo program must declare a program scope with a unique program identifier. The program identifier follows the format name.aleo.

Program Structure

A Leo program consists of several components, declared in the following order:
  1. Imports (optional)
  2. Program scope (required)
  3. Mappings (optional)
  4. Records and Structs (optional)
  5. Functions (required)
  6. Finalize functions (optional)

Complete Example

Program Components

Mappings

Mappings define on-chain storage with a key-value structure:

Records

Records define private, owned data structures:

Structs

Structs define composite data types:

Functions

Functions define the executable logic of your program:

Imports

Leo programs can import other programs to reuse functionality:
Imported programs must be available in your project’s dependencies or published on the Aleo network.

Self References

Within a function, you can access the caller’s address using self.caller:

Block Context

Finalize functions can access blockchain context through the block object:

Program Naming

Valid Names

Program names must:
  • Be lowercase
  • Contain only alphanumeric characters and underscores
  • End with .aleo
  • Be unique on the Aleo network

Multiple Programs

A Leo project typically contains:
  • One main program in src/main.leo
  • Optional imported programs
  • Build configuration in program.json

Project Structure

Program Constants

You can define program-level constants:

Best Practices

1. Clear Naming

Use descriptive names for programs, functions, and variables:

2. Organize by Functionality

Group related functions and types together:

3. Document Public Interfaces

Add comments to explain function behavior:

4. Separate Concerns

Use the finalize pattern to separate off-chain and on-chain logic:

Next Steps

Data Types

Explore Leo’s type system

Functions

Learn about function declarations

Imports

Import other programs

Mappings

Use on-chain storage