Cheat Sheet
Contract, view and test
namespace C {
export type storage = int;
@entry
const increment = (action: int, store: storage) : [list <operation>, storage] => [[], store + action];
@entry
const decrement = (action: int, store: storage) : [list <operation>, storage] => [[], 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, 0mav);
let p : parameter_of C = Increment(1);
Test.transfer_exn(originated.addr, p, 1mumav);
return assert(Test.get_storage(originated.addr) == initial_storage + 1);
}
Strings
const name: string = "Mavryk";
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)
Mumav (micro mav)
const mav_amount: mav = 42mav
const mav_amount2: mav = mav_amount + 7mumav // == 42000007mumav
Address
const tz1address: address = "mv18Cw7psUrAAPBpXYd9CtCpHg9EgjHP9KTe";
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 = 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 = "mv18Cw7psUrAAPBpXYd9CtCpHg9EgjHP9KTe";
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, mav>;
const prices: prices = Map.literal([
[10n, 60mumav],
[50n, 30mumav],
[100n, 10mumav]
]);
const price: option<mav> = Map.find_opt(50n, prices)
const prices2: prices = Map.update(200n, Some (5mumav), prices)
Contracts & Accounts
const destinationAddress: address = "mv18Cw7psUrAAPBpXYd9CtCpHg9EgjHP9KTe";
const contract : contract<unit> =
match(Mavryk.get_contract_opt(Mavryk.get_sender()) as option<contract<unit>>) {
when(Some(contract)): contract;
when(None()): failwith("no contract or wrong contract type")
}
Transactions
const payment: operation =
Mavryk.transaction(unit, 100mumav, 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
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 {
export 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];
}
Extending namespace
Not available in JsLIGO, use CameLIGO.