Greetings, I'm Heorhii. Today, let's unravel the intricate architecture of Aleo Leo programs, exploring their structural elements and delving into the advantages they bring to blockchain development.
Aleo Leo program structure. A Leo program is a meticulous compilation of declarations organized within its file. These encompass the Program scope, constants, imports, transition functions, helper functions, structs, records, mappings, and finalize functions. These declarations are locally accessible but can be imported from other Leo files if needed.
import foo.leo; program hello.aleo { // ... declarations ... }
https://developer.aleo.org/leo/language#layout-of-a-leo-program
Advantages of Aleo Leo program Layout:
Structured clarity: the organized layout enhances code readability and comprehension.
Modular design: declarations are compartmentalized, promoting modular development and code reuse.
Privacy assurance: Leo's visibility options allow for fine-grained control over data exposure.
On-chain control: finalize functions provide a secure mechanism for on-chain state mutation.
Program scope. The program scope is a nexus of code and data residing at a specific program ID on the Aleo blockchain. It encapsulates mappings, records, structs, transition functions, helper functions, and finalize functions.
import foo.leo;
program hello.aleo {
mapping balances: address => u64;
record token {
owner: address,
amount: u64,
}
struct message {
sender: address,
object: u64,
}
transition mint_public(
public receiver: address,
public amount: u64,
) -> token {
return token {
owner: receiver,
amount,
} then finalize(receiver, amount);
}
finalize mint_public(
public receiver: address,
public amount: u64,
) {
let current_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
Mapping::set(account, receiver, current_amount + amount);
}
function compute(a: u64, b: u64) -> u64 {
return a + b;
}
}

Constants. Constants are immutable values that must be assigned when declared. They can be defined globally or within function scopes.
program foo.aleo {
const FOO: u8 = 1u8;
function bar() -> u8 {
const BAR: u8 = 2u8;
return FOO + BAR;
}
}
Import. Imports facilitate the inclusion of external declarations into the current file. These dependencies should reside in the imports directory.
import foo.leo; // Import all `foo.leo` declarations into the `hello.aleo` program.
program hello.aleo { }
Structs. Structs define data types with named components.
struct array3 {
a0: u32,
a1: u32,
a2: u32,
}
Records. Records, similar to structs, hold data components but with added visibility options—constant, public, or private.
record token {
// The token owner.
owner: address,
// The token amount.
amount: u64,
}
Arrays, Tuples, and more. Leo supports static arrays, tuples, and their combinations. These data structures can be used flexibly in your programs.
// Initalize a boolean array of length 4
let arr: [bool; 4] = [true, false, true, false];
// Nested Array
let nested: [[bool; 2]; 2] = [[true, false], [true, false]];
// Array of Structs
struct bar {
data: u8,
}
let arr_of_structs: [bar; 2] = [bar { data: 1u8 }, bar { data: 2u8 }];
// Access the field of a struct within an array
transition foo(a: [bar; 8]) -> u8 {
return a[0u8].data;
}
// Struct that contains an array
struct bat {
data: [u8; 8],
}
// Record that contains an array
record floo {
owner: address,
data: [u8; 8],
}
// Declare a mapping that contains an array value
mapping data: address => [bool; 8];
// Iterate over an array using a for loop and sum the values within
transition sum_with_loop(a: [u64; 4]) -> u64 {
let sum: u64 = 0u64;
for i: u8 in 0u8..4u8 {
sum += a[i];
}
return sum;
}
Transition functions. Transition functions are the core of Leo programs. They compute values and must be in the current program scope.
program test.aleo {
transition baz(foo: u8, bar: u8) -> u8 {
let a: (u8, u8) = (foo, bar);
let result: u8 = a.0 + a.1;
return result;
}
}
Helper functions. Helper functions contain expressions and statements for internal computations. They cannot produce records and are called by other functions.
function foo(
a: field,
b: field,
) -> field {
return a + b;
}
Finalize functions. Finalize functions, linked to transition functions, run computations on-chain, updating public state within mappings.
finalize transfer_public_to_private(public sender: address, public amount: u64) { // ... on-chain state mutation ... }
Lern more about Aleo Leo:
Conclusion. In conclusion, Aleo Leo's well-defined program structure and robust features empower developers to build sophisticated and privacy-centric blockchain applications. Dive in, explore, and elevate your blockchain development experience with Aleo Leo.
To know more, join now!
Aleo Twitter
Aleo Discord
Aleo Website
List of Aleo and Leo code and resourses
Prepared by Colliseum

