ligo/gitlab-pages/docs/api/cheat-sheet.md

8.5 KiB

id title
cheat-sheet Cheat Sheet
Primitive Example
Strings "Tezos"
Characters "t"
Integers 42, 7
Natural numbers 42n, 7n
Unit unit
Boolean
const hasDriversLicense: bool = False;
const adult: bool = True;
Boolean Logic
(not True) == False == (False and True) == (False or False)
Mutez (micro tez) 42mutez, 7mutez
Address "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx", "KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD"
Addition 3 + 4, 3n + 4n
Multiplication & Division 3 * 4, 3n * 4n, 10 / 5, 10n / 5n
Modulo 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
  block { skip } with 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
if age < 16 
then fail("Too young to drive.");
else const new_id: int = prev_id + 1;
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

⚠️ 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.
age := 18;, p.age := 21
Type Annotations ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)
Variants
type action is
| Increment of int
| Decrement of int
Variant (pattern) matching
const a: action = Increment(5);
case a of
| Increment(n) -> n + 1
| Decrement(n) -> n - 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];

prices[200n] := 5mutez;
Contracts & Accounts
const destinationAddress : address = "tz1...";
const contract : contract(unit) = get_contract(destinationAddress);
Transactions
const payment : operation = transaction(unit, amount, receiver);
Exception/Failure failwith("Your descriptive error message for the user goes here.")
Primitive Example
Strings "Tezos"
Characters "t"
Integers 42, 7
Natural numbers 42n, 7n
Unit unit
Boolean
let has_drivers_license: bool = false
let adult: bool = true
Boolean Logic
(not true) = false = (false && true) = (false || false)
Mutez (micro tez) 42mutez, 7mutez
Address ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address), ("KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD": address)
Addition 3 + 4, 3n + 4n
Multiplication & Division 3 * 4, 3n * 4n, 10 / 5, 10n / 5n
Modulo 10 mod 3
Tuples
type name = (string * string)
let winner: name = "John", "Doe"
let first_name: string = winner.0
let last_name: string = winner.1
Types type age = int, type name = string
Includes #include "library.mligo"
Functions
let add (a : int) (b : int) : int = a + b 
If Statement
let new_id: int = if age < 16 
then failwith("Too young to drive.")
else prev_id + 1
Options
type middle_name = string option
let middle_name : middle_name = Some "Foo"
let middle_name : middle_name = None
Variable Binding let age: int = 5
Type Annotations ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)
Variants
type action =
| Increment of int
| Decrement of int
Variant (pattern) matching
let a: action = Increment 5
match a with
| Increment n -> n + 1
| Decrement n -> n - 1
Records
type person = {
  age: int ;
  name: string ;
}

let john : person = {
  age = 18;
  name = "John Doe";
}

let name: string = john.name
Maps
type prices = (nat, tez) map

let prices : prices = Map.literal [
  (10n, 60mutez);
  (50n, 30mutez);
  (100n, 10mutez)
]

let price: tez option = Map.find_opt 50n prices

let prices: prices = Map.update 200n (Some 5mutez) prices
Contracts & Accounts
let destination_address : address = "tz1..."
let contract : unit contract =
Tezos.get_contract destination_address
Transactions
let payment : operation = 
Tezos.transaction unit amount receiver
Exception/Failure failwith("Your descriptive error message for the user goes here.")
Primitive Example
Strings "Tezos"
Characters "t"
Integers 42, 7
Natural numbers 42n, 7n
Unit unit
Boolean
let has_drivers_license: bool = false;
let adult: bool = true;
Boolean Logic
(not true) = false = (false && true) = (false || false)
Mutez (micro tez) 42mutez, 7mutez
Address ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address), ("KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD": address)
Addition 3 + 4, 3n + 4n
Multiplication & Division 3 * 4, 3n * 4n, 10 / 5, 10n / 5n
Modulo 10 mod 3
Tuples
type name = (string, string);
let winner: name = ("John", "Doe");
let first_name: string = winner[0];
let last_name: string = winner[1];
Types type age = int;, type name = string;
Includes #include "library.mligo"
Functions
let add = (a: int, b: int) : int => a + b; 
If Statement
let new_id: int = if (age < 16) {
failwith("Too young to drive.");
} else { prev_id + 1; }
Options
type middle_name = option(string);
let middle_name : middle_name = Some("Foo");
let middle_name : middle_name = None;
Variable Binding let age: int = 5;
Type Annotations ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)
Variants
type action =
| Increment(int)
| Decrement(int);
Variant (pattern) matching
let a: action = Increment(5);
switch(a) {
| Increment(n) => n + 1
| Decrement(n) => n - 1;
}
Records
type person = {
  age: int,
  name: string
}

let john : person = {
  age: 18,
  name: "John Doe"
};

let name: string = john.name;
Maps
type prices = map(nat, tez);

let prices : prices = Map.literal([
  (10n, 60mutez),
  (50n, 30mutez),
  (100n, 10mutez)
]);

let price: option(tez) = Map.find_opt(50n, prices);

let prices: prices = Map.update(200n, Some (5mutez), prices);
Contracts & Accounts
let destination_address : address = "tz1...";
let contract : contract(unit) =
Tezos.get_contract(destination_address);
Transactions
let payment : operation = 
Tezos.transaction (unit, amount, receiver);
Exception/Failure failwith("Your descriptive error message for the user goes here.");