Tezex.Michelson (tezex v6.0.0)

Copy Markdown View Source

Build the Micheline value of a contract call from an Elixir term and the Michelson type it must have.

encode/2 takes the type as Micheline JSON, as the node returns it (see Tezex.Rpc.entrypoint/3), and returns Micheline JSON to send as a transaction's parameters.value. schema/1 shows the term encode/2 expects for a type.

Tezex.Micheline only serializes trees and doesn't know what they mean. The rules here are Michelson's: pair a b c is pair a (pair b c), mutez is a 64-bit signed integer, map keys are sorted in the protocol's order.

Values

typeElixir value
unit:unit
booltrue, false
intan integer
natan integer >= 0
mutezan integer from 0 to 2^63 - 1
stringa string of printable ASCII (codes 32 to 126) and newlines
bytesa binary of raw bytes, not hex: <<5, 1>>
bls12_381_*a binary of raw bytes, not hex: <<5, 1>>
chesta binary of raw bytes, not hex: <<5, 1>>
chest_keya binary of raw bytes, not hex: <<5, 1>>
sapling_transactiona binary of raw bytes, not hex: <<5, 1>>
address, contracta base58 string, optionally "KT1…%entrypoint" (%default is forbidden)
key_hasha base58 string
keya base58 string
signaturea base58 string
chain_ida base58 string
timestampa DateTime with whole seconds
lista list
seta list or a MapSet
map, big_mapa map
lambdathe code as Micheline JSON, passed through as is
sapling_state[], an empty state
optionnil (or a missing field) for None, any other value v for Some v; {:some, nil} for Some None when the type inside is an option
paira map when every field has a name and no two share one, a tuple otherwise
or{name, value}, or name for a unit branch; {:left, value} / {:right, value} for a branch without a name

never, operation and ticket take no value.

encode/2 checks a value's format, not its cryptographic validity: it doesn't check that a key or signature is a point on its curve, or that bls12_381_* bytes are a valid point. The node checks those and rejects the operation.

Names are % annotations. A pair field's name can be on the option (option %expiry nat) or on the type inside (option (nat %expiry)). An or branch's name is only on the branch: or (option (nat %x)) nat has no names.

pair

Fields are counted after flattening the nested pairs that have no name, so pair (nat %a) (pair (nat %b) (nat %c)) is %{a: 1, b: 2, c: 3}. Keys can be atoms or strings.

A tuple can be written nested or flat on the right, since pair a b c is the same type as pair a (pair b c): {1, 2, 3} or {1, {2, 3}}. The output is always nested: Pair 1 (Pair 2 3).

or

Names are found through the nested ors, named or not: in or (or (nat %a) (nat %b)) nat, {:a, 1} works, and in or (or %foo (nat %a) (nat %b)) nat, {:a, 1} and {:foo, {:a, 1}} both work. :left / :right pick a branch without a name. It's an error when they could also mean a branch named %left / %right.

map, set

Elements are sorted in the protocol's order for the key type, and two keys that encode to the same value are an error.

Examples

iex> type = %{
...>   "prim" => "pair",
...>   "args" => [
...>     %{"prim" => "address", "annots" => ["%holder"]},
...>     %{"prim" => "nat", "annots" => ["%amount"]}
...>   ]
...> }
iex> encode(type, %{holder: "tz1ZW1ZSN4ruXYc3nCon8EaTXp1t3tKWb9Ew", amount: 5})
{:ok, %{"prim" => "Pair", "args" => [%{"string" => "tz1ZW1ZSN4ruXYc3nCon8EaTXp1t3tKWb9Ew"}, %{"int" => "5"}]}}
iex> schema(type)
%{"holder" => :address, "amount" => :nat}

iex> type = %{
...>   "prim" => "or",
...>   "args" => [
...>     %{"prim" => "unit", "annots" => ["%tez"]},
...>     %{"prim" => "address", "annots" => ["%fa12"]}
...>   ]
...> }
iex> encode(type, :tez)
{:ok, %{"prim" => "Left", "args" => [%{"prim" => "Unit"}]}}
iex> schema(type)
{:or, ["tez", {"fa12", :address}]}

iex> {:error, error} = encode(%{"prim" => "nat"}, -1)
iex> Exception.message(error)
"invalid_value at the root: expected an integer >= 0, got: -1"

Summary

Types

Micheline JSON, decoded: a node map or a sequence.

What schema/1 returns

Functions

Encodes value as Micheline JSON of Michelson type type.

Same as encode/2 but returns the Micheline directly, and raises Tezex.Michelson.EncodeError when the value doesn't fit.

Shows the term encode/2 expects for type, see schema/0.

Types

micheline()

@type micheline() :: map() | [micheline()]

Micheline JSON, decoded: a node map or a sequence.

schema()

@type schema() :: atom() | String.t() | tuple() | %{required(String.t()) => schema()}

What schema/1 returns:

  • a scalar: its type name, :nat
  • a pair: a map of field name to schema when every field has a unique name, else a tuple of schemas
  • an or: {:or, branches}, where a branch is one of:
    • name: a named unit branch
    • {name, schema}: a named branch
    • :left / :right: an unnamed unit branch
    • {:left | :right, schema}: an unnamed branch

  • {:option, schema}, {:list, schema}, {:set, schema}, {:contract, parameter}, {:ticket, schema}
  • {:map, key, value}, {:big_map, key, value}, {:lambda, arg, return}
  • {:unsupported, prim}: a type tezex doesn't know
  • {:unsupported, type}: a malformed type, one that isn't a node map

Functions

encode(type, value)

@spec encode(map(), term()) ::
  {:ok, micheline()} | {:error, Tezex.Michelson.EncodeError.t()}

Encodes value as Micheline JSON of Michelson type type.

Returns {:error, %Tezex.Michelson.EncodeError{}} with the path to the value that doesn't fit.

encode!(type, value)

@spec encode!(map(), term()) :: micheline()

Same as encode/2 but returns the Micheline directly, and raises Tezex.Michelson.EncodeError when the value doesn't fit.

schema(type)

@spec schema(map()) :: schema()

Shows the term encode/2 expects for type, see schema/0.

Field and branch names are strings: they come from the contract, and atoms are never garbage collected.