Skip to main content
Version: 1.4.0

Cheat Sheet

Contract, view and test
namespace C {
export type storage = int;
@entry
const increment = (action: int, store: storage) : [list <operation>, storage] => [list([]), store + action];
@entry
const decrement = (action: int, store: storage) : [list <operation>, storage] => [list([]), store - action];
@view
const get_storage = (must_be_positive: bool, storage: int): int => {
if (must_be_positive && storage < 0) {
return failwith("Negative value in storage");
} else {
return storage;
}
}
};
const testC = do {
let initial_storage = 42;
let originated = Test.originate(contract_of(C), initial_storage, 0tez);
let p : parameter_of C = Increment(1);
Test.transfer_exn(originated.addr, p, 1mutez);
return assert(Test.get_storage(originated.addr) == initial_storage + 1);
}
Strings
const name: string = "Tezos";
Characters
const t: string = "t";
Integers
const i: int = 42;
Natural numbers
const n: nat = 7n;
Unit
const u: unit = unit;
Boolean
const has_drivers_license: bool = false
const adult: bool = true
Boolean Logic
const booleanLogic: bool =
(!true) ==
false ==
(false && true) ==
(false || false)
Mutez (micro tez)
const tez_amount: tez = 42tez
const tez_amount2: tez = tez_amount + 7mutez // == 42000007mutez
Address
const tz1address: address =
"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address;
const kt1address: address =
"KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD" as address;
String
const my_str : string = "Hello World!";
Verbatim string
const verbatim_str : string = `verbatim string`;
Addition
const add_int: int = 3 + 4;
const add_nat: nat = 3n + 4n;
Multiplication & Division
const mul_int: int = 3 * 4;
const mul_nat: nat = 3n * 4n;
const div_int: int = 10 / 5;
const div_nat: nat = 10n / 5n; // can fail (division by zero), check your inputs first.
Modulo
const mod_nat: nat = 10 % 3; // can fail (division by zero), check your inputs first.
Tuples
type name = [string, string];
const winner: name = ["John", "Doe"];
const firstName: string = winner[0];
const lastName: string = winner[1];
Types
type age = int
type name = string
Include (prefer import)
#include "library.jsligo"
Import (better)
#import "library.jsligo" "MyLibrary"
const foo = MyLibrary.bar;
Functions (short form)
const add = (a: int, b: int): int =>
a + b;
Functions (long form)
const add = (a: int, b: int): int => {
let c = a;
let d = b;
return c + d
};
If/else Statement
function if_statement (age : nat): string {
if (age >= 16n) return "yes" else return "no"
}
Options
type middle_name = option<string>;
const a_middle_name : middle_name = Some("Foo");
const no_middle_name : middle_name = None();
Variable Binding
const age: int = 5
Type Annotations
const someAddress: address =
"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address;
Variants (label + optional value)
type action =
["Increment", int]
| ["Decrement", int]
| ["Reset"];
Matching on variant cases
let a: action = Increment(5)
const result: int = match(a) {
when(Increment(n)): n + 1;
when(Decrement(n)): n - 1;
when(Reset()): 0;
}
Records / Plain Old Data Objects
type person = {
age: int,
name: string
}
const john : person = {
age: 18,
name: "john doe"
}
const name_: string = john.name
Maps
type prices = map<nat, tez>;
const prices: prices = Map.literal(list([
[10n, 60mutez],
[50n, 30mutez],
[100n, 10mutez]
]));
const price: option<tez> = Map.find_opt(50n, prices)
const prices2: prices = Map.update(200n, Some (5mutez), prices)
Contracts & Accounts
const destinationAddress: address =
"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address;
const contract : contract<unit> =
match(Tezos.get_contract_opt(Tezos.get_sender()) as option<contract<unit>>) {
when(Some(contract)): contract;
when(None()): failwith("no contract or wrong contract type")
}
Transactions
const payment: operation =
Tezos.transaction(unit, 100mutez, contract);
Exception/Failure
const fail = (u: unit) : unit =>
failwith("a failure message")
Comb layout (default)
type animal =
@layout("comb")
| ["Elephant"]
| ["Dog"]
| ["Cat"];
Tree layout
type animal =
@layout("tree")
| ["Elephant"]
| ["Dog"]
| ["Cat"];
Namespace (auto-inferred type)
namespace FA0_inferred {
type storage = int;
@entry const add = (s : int, k : int) : [list<operation>, int] => [list([]), s + k];
@entry const extra = (s : int, k : int) : [list<operation>, int] => [list([]), s - k];
}
Interface
interface FA0_INTF {
type storage;
@entry const add : (s : int, k : storage) => [list<operation>, storage];
}
Extending Interface
interface FA0_EXT_INTF extends FA0_INTF {
type storage;
@entry const add : (s : int, k : storage) => [list<operation>, storage];
}
Namespace impmlementing
namespace FA0 implements FA0_INTF {
type storage = int;
@entry const add = (s : int, k : int) : [list<operation>, int] => [list([]), s + k];
@entry const extra = (s : int, k : int) : [list<operation>, int] => [list([]), s - k];
}
Extending namespace

Not available in JsLIGO, use CameLIGO.