Cheat Sheet
Strings
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 =
(!true) ==
false ==
(false && true) ==
(false || false)
Mutez (micro tez)
let tez: tez = 42tez
let tez2: tez = 7mutez
Address
let tz1address: address =
"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address;
let kt1address: address =
"KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD" as address;
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 % 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
Includes
#include "library.jsligo"
Functions (short form)
let add = (a: int, b: int): int =>
a + b;
Functions (long form)
let add = (a: int, b: int): int => {
let c = a;
let d = b;
return c + d
};
If Statement
function if_statement (age : nat): string {
if (age >= 16n) return "yes" else return "no"
}
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
let someAddress: address =
"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address;
Variants
type action =
["Increment", int]
| ["Decrement", int];
Variant *(pattern)* matching
let a: action = Increment(5)
let result: int = match(a) {
when(Increment(n)): n + 1;
when(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(list([
[10n, 60mutez],
[50n, 30mutez],
[100n, 10mutez]
]));
let price: option<tez> = Map.find_opt(50n, prices)
let prices2: prices = Map.update(200n, Some (5mutez), prices)
Contracts & Accounts
let destinationAddress: address =
"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address;
let contract : contract<unit> =
match(Tezos.get_contract_opt(Tezos.get_sender()) as option<contract<unit>>) {
when(Some(contract)): contract;
when(None()): (failwith("no contract") as contract<unit>)
}
Transactions
let payment: operation =
Tezos.transaction(unit, 100mutez, contract);
Exception/Failure
let fail = (u: unit) : unit =>
failwith("a failure message")
contract_of and parameter_of
namespace C {
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];
};
const testC = () => {
let initial_storage = 42;
let orig = Test.originate(contract_of(C), initial_storage, 0tez);
let p : parameter_of C = Increment(1);
Test.transfer_exn(orig.addr, p, 1mutez);
return assert(Test.get_storage(orig.addr) == initial_storage + 1);
}