Cheat Sheet
Contract, view and test
module C = struct
type storage = int
[@entry]
let increment (action: int) (store: storage) : operation list * storage =
[], store + action
[@entry]
let decrement (action: int) (store: storage) : operation list * storage =
[], store - action
[@view]
let get_storage (must_be_positive: bool) (storage: int) : int =
if must_be_positive && storage < 0 then
failwith "Negative value in storage"
else
storage
end
let testC =
let initial_storage = 42 in
let originated = Test.originate (contract_of C) initial_storage 0tez in
let p : C parameter_of = Increment 1 in
let _ = Test.transfer_exn originated.addr p 1mutez in
assert (Test.get_storage originated.addr = initial_storage + 1)
let name : string = "Tezos"
Characters
let t : string = "t"
Integers
let i : int = 42
Natural numbers
let n : nat = 7n
Unit
let u : unit = unit
Boolean
let has_drivers_license : bool = false
let adult : bool = true
Boolean Logic
let booleanLogic : bool =
(not true) =
false =
(false && true) =
(false || false)
Mutez (micro tez)
let tez : tez = 42tez
let tez : tez = 7mutez
Address
let tz1address : address =
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)
let kt1address : address =
("KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD" : address)
String
let my_str : string = "Hello World!"
Verbatim string
let verbatim_str : string = {|verbatim string|}
Addition
let add_int : int = 3 + 4
let add_nat : nat = 3n + 4n
Multiplication & Division
let mul_int : int = 3 * 4
let mul_nat : nat = 3n * 4n
let div_int : int = 10 / 5
let div_nat : nat = 10n / 5n
Modulo
let mod_nat : nat = 10 mod 3
Tuples
type name = string * string
let winner : name = "John", "Doe"
let firstName : string = winner.0
let lastName : string = winner.1
Types
type age = int
type name = string
Include (prefer import)
#include "library.mligo"
Import (better)
#import "library.mligo" "MyLibrary"
let foo = MyLibrary.bar
Functions
let add (a : int) (b : int) : int =
a + b
If Statement
let can_drive (age : nat) : string =
if age >= 16n then "yes" else "no"
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
let someAddress : address =
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)
Variants
type action =
Increment of int
| Decrement of int
Variant (pattern) matching
let a : action = Increment 5
let result : int =
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 destinationAddress : address =
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)
let contract : unit contract =
match (Tezos.get_contract_opt (Tezos.get_sender ()) : unit contract option) with
Some contract -> contract
| None -> (failwith "no contract" : unit contract)
Transactions
let payment : operation =
Tezos.transaction unit 100mutez contract
Exception/Failure
let 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
Module definition (auto-inferred type)
module FA0_inferred = struct
type t = unit
[@entry] let transfer (_ : unit) (_ : t) : operation list * t = [], ()
end
Module Type
module type FA0_SIG = sig
type t
[@entry] val transfer : unit -> t -> operation list * t
end
Extending Module Type
module type FA0Ext_SIG = sig
include FA0_SIG
[@entry] val transfer2 : unit -> t -> operation list * t
end
Module definition
module FA0 : FA0_SIG = struct
type t = unit
[@entry] let transfer (_ : unit) (_ : t) : operation list * t = [], ()
end
Extending module definition
module FA0Ext : FA0Ext_SIG = struct
include FA0
[@entry] let transfer2 (a : unit) (b : t) = transfer a b
end
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);
}
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.