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
| type | Elixir value |
|---|---|
unit | :unit |
bool | true, false |
int | an integer |
nat | an integer >= 0 |
mutez | an integer from 0 to 2^63 - 1 |
string | a string of printable ASCII (codes 32 to 126) and newlines |
bytes | a binary of raw bytes, not hex: <<5, 1>> |
bls12_381_* | a binary of raw bytes, not hex: <<5, 1>> |
chest | a binary of raw bytes, not hex: <<5, 1>> |
chest_key | a binary of raw bytes, not hex: <<5, 1>> |
sapling_transaction | a binary of raw bytes, not hex: <<5, 1>> |
address, contract | a base58 string, optionally "KT1…%entrypoint" (%default is forbidden) |
key_hash | a base58 string |
key | a base58 string |
signature | a base58 string |
chain_id | a base58 string |
timestamp | a DateTime with whole seconds |
list | a list |
set | a list or a MapSet |
map, big_map | a map |
lambda | the code as Micheline JSON, passed through as is |
sapling_state | [], an empty state |
option | nil (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 |
pair | a 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
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.
Types
Micheline JSON, decoded: a node map or a sequence.
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 namedunitbranch{name, schema}: a named branch:left/:right: an unnamedunitbranch{: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
@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.
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.
Field and branch names are strings: they come from the contract, and atoms are never garbage collected.