Skip to main content
Version: Next

Cheat Sheet

Contract, view and test
type storage = int;
class C {
@entry
increment = (action: int, store: storage) : [list <operation>, storage] => [[], store + action];
@entry
decrement = (action: int, store: storage) : [list <operation>, storage] => [[], store - action];
@view
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 = (() => {
const initial_storage = 42;
const originated = Test.Originate.contract(contract_of(C), initial_storage, 0 as tez);
const p : parameter_of<C> = ["Increment" as "Increment", 1];
Test.Typed_address.transfer_exn(originated.taddr, p, 1 as mutez);
return Assert.assert(Test.Typed_address.get_storage(originated.taddr) == initial_storage + 1);
})()
Strings
const name: string = "Tezos";
Characters
const t: string = "t";
Integers
const i: int = 42;
Natural numbers
const n: nat = 7 as nat;
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 = 42 as tez
const tez_amount2: tez = tez_amount + (7 as mutez) // == (42000007 as mutez)
Address
const tz1address: address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
const kt1address: address = "KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD";
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 = (3 as nat) + (4 as nat);
Multiplication & Division
const mul_int: int = 3 * 4;
const mul_nat: nat = (3 as nat) * (4 as nat);
const div_int: int = 10 / 5;
const div_nat: nat = (10 as nat) / (5 as nat);
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)
Import (better)
Functions (short form)
const add = (a: int, b: int): int =>
a + b;
Functions (long form)
function add (a: int, b: int): int {
const c = a;
const d = b;
return c + d
};
If/else Statement
function if_statement (age : nat): string {
if (age >= (16 as nat)) return "yes"; else return "no";
}
Options
type middle_name = option<string>;
const a_middle_name : middle_name = ["Some" as "Some", "Foo"];
const no_middle_name : middle_name = ["None" as "None"];
Variable Binding
const age: int = 5
Type Annotations
const someAddress: address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
Variants (label + optional value)
type action =
["Increment", int]
| ["Decrement", int]
| ["Reset"];
Matching on variant cases
const a: action = ["Increment" as "Increment", 5];
const result: int =
$match(a, {
"Increment": n => n + 1,
"Decrement": n => n - 1,
"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([
[10 as nat, 60 as mutez],
[50 as nat, 30 as mutez],
[100 as nat, 10 as mutez]
]);
const price: option<tez> = Map.find_opt(50 as nat, prices)
const prices2: prices =
Map.update(200 as nat, ["Some" as "Some", 5 as mutez], prices)
Contracts & Accounts
const destinationAddress: address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
const contract : contract<unit> =
$match(Tezos.get_contract_opt(Tezos.get_sender()) as option<contract<unit>>, {
"Some": contract => contract,
"None": () => failwith("no contract or wrong contract type")
})
Transactions
const payment: operation =
Tezos.Operation.transaction(unit, 100 as mutez, 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] => [[], s + k];
// @entry
const extra = (s : int, k : int) : [list<operation>, int] => [[], s - k];
}
Interface
type storage = int;
interface FA0_INTF {
//@entry
add : (s : int, k : storage) => [list<operation>, storage];
}
Extending an interface
interface FA0_EXT_INTF extends FA0_INTF {
// @entry
add : (s : int, k : storage) => [list<operation>, storage];
}
Implementing an interface
class FA0 implements FA0_INTF {
@entry
add = (s : int, k : int) : [list<operation>, int] => [[], s + k];
@entry
extra = (s : int, k : int) : [list<operation>, int] => [[], s - k];
}