πOla Examples
Ola contracts allow users to write complex business logic that will be deployed to Ola's l2 network, and cross-contract calls can be written between different contracts just like solidity and rust.
Fibonacci
The following example shows a recursive and non-recursive ola smart contract implementation of Fibonacci numbers.
contract Fibonacci {
fn main() {
fib_non_recursive(10);
}
fn fib_recursive(u32 n) -> (u32) {
if (n <= 2) {
return 1;
}
return fib_recursive(n -1) + fib_recursive(n -2);
}
fn fib_non_recursive(u32 n) -> (u32) {
u32 first = 0;
u32 second = 1;
u32 third = 1;
for (u32 i = 2; i <= n; i++) {
third = first + second;
first = second;
second = third;
}
return third;
}
}
Person
The following shows a simple book contract that contains a book structure, assigns a value to the book structure and reads the status of the book.
Cross-contract invocation
The following example demonstrates the usage of cross-contract invocation.
Caller contract
Callee contract
Multiple files
For better project organisation and clearer logic, it is common to split the contents of a file into multiple files. ola language supports the import of another contract within a contract through the import keyword.
An example of a multi-file contract is shown below.
Contract RectangularCalculator
Contract ShapeCalculator
Contract Calculator
Last updated