Version: Next

Cheat Sheet

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 hasDriversLicense: bool = False;
const adult: bool = True;
Boolean Logic
const booleanLogic: bool =
(not True) =
False =
(False and True) =
(False or False);
Mutez (micro tez)
const tez: tez = 42tez;
Address
const tz1address: address =
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address);
const kt1address: address =
("KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD": address);
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;
Modulo
const mod_nat: nat = 10 mod 3;
Tuples
type name is (string * string);
const winner: name = ("John", "Doe");
const firstName: string = winner.0;
const lastName: string = winner.1;
Types
type age is int;
type name is string
Includes

#include "library.ligo"

Functions (short form)
function add (const a : int ; const b : int) : int is
a + b
Functions (long form)
function add (const a : int ; const b : int) : int is
block {
const result: int = a + b;
} with result
If Statement
function if_statement (const age : int) : int is
block {
var id: int := -1;
if age < 16 then {
failwith ("Too young to drive");
} else {
id := 1;
}
} with id
Options
type middleName is option(string);
const middleName : middleName = Some("Foo");
const middleName : middleName = None;
Assignment
const age: int = 5;
Assignment on an existing variable
caution

This feature is not supported at the top-level scope, you can use it e.g. within functions. Works for Records and Maps as well.

function assignment_existing (const age : int) : int is
block {
var x : int := 2;
x := 3;
} with x
Type Annotations
const someAddress: address =
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address);
Variants
type action is
| Increment of int
| Decrement of int;
Variant *(pattern)* matching
function main
(const action : action; const input : int) : int is
(case action of
Increment (n) -> input + 1
| Decrement (n) -> input - 1
end)
Records
type person is record
age: int;
name: string;
end
const john : person = record
age = 18;
name = "john doe";
end
const name: string = john.name;
Maps
type prices is map(nat, tez);
const prices: prices = map
10n -> 60mutez;
50n -> 30mutez;
100n -> 10mutez;
end
const price: option(tez) = prices[50n];
function mutate (const u: unit) : unit is block {
prices[200n] := 10mutez;
} with unit;
Contracts & Accounts
const destinationAddress: address =
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address);
const contract : contract (unit) = (
case (Tezos.get_contract_opt (Tezos.sender) : option(contract(unit))) of
Some (contract) -> contract
| None ->
(failwith ("No contract.")
: contract (unit))
end);
Transactions
const payment: operation =
Tezos.transaction(unit, 100mutez, contract);
Exception/Failure
function fail (const u: unit) : unit is
failwith("a failure message")