Building PTBs from JSON¶
How a JSON argument such as ["u64", 42] or ["pure", "vector<u8>", "0x01"] becomes an input and a command in a programmable transaction block.
You will learn / You need
You will learn: the two argument encoders, the dispatch rules in parse_argument(), which forms add extra commands, how pure values are BCS-encoded, and the exact error messages for bad input.
You need: Writing request payloads and a basic idea of BCS. The
backend deep dive shows where this code is called.
Two encoders¶
| Encoder | File | Input form | Used for |
|---|---|---|---|
| Typed arguments | stealth_name_service/backend/testing_backend/src/ptb_args.rs |
["u64", 42], ["object", "0x…"], ["vector", "u8", [1, 2]], … |
Every argument in /api/SendPTB, and EVENT_HANDLER_ARGS_JSON in the static relayer. |
| Pure values | stealth_name_service/backend/testing_backend/src/pure_args.rs |
["pure", "<move type>", <json value>] |
Arbitrary BCS-encodable values, in particular nested vector<vector<u8>> such as the bridge's args. |
ptb_args.rs builds arguments on a ProgrammableTransactionBuilder, so some forms add commands to the
transaction. pure_args.rs only produces bytes; ptb_args.rs hands them to the builder as one pure input.
Dispatch¶
parse_argument() looks at the array length first.
flowchart TD
A["JSON argument"] --> B{"array?"}
B -- no --> E1["error: Expected array"]
B -- yes --> C{"length"}
C -- 2 --> D["parse_typed_argument()<br/>[type, value]"]
C -- 3 --> F["parse_complex_argument()<br/>[kind, type, value]"]
C -- other --> E2["error: Invalid array length"]
D --> G{"type"}
G -- "object / immutable" --> H["resolve object<br/>(RPC)"]
G -- scalar --> I["pure input"]
F --> J{"kind"}
J -- result --> K["NestedResult"]
J -- vector --> L["MakeMoveVec"]
J -- vecmap --> M["2 × MakeMoveVec<br/>+ vec_map::from_keys_values"]
J -- option --> N["option::some / none"]
J -- pure --> O["encode_pure()<br/>one pure input"]
The backend calls parse_argument_cached() instead. It differs only for ["immutable", id], which it
looks up in the per-network ImmutableObjectCache before resolving. See
the immutable object cache.
Two-element forms¶
get_argument_from_parsing() handles the scalar types; parse_object_argument() handles objects.
| Type tag | JSON value | Becomes | Notes |
|---|---|---|---|
string |
string | pure UTF-8 string | |
u8, u16, u32 |
number | pure integer | Read as u64, then narrowed with an as cast. An out-of-range number wraps silently. |
u64 |
number | pure u64 |
|
u128 |
number | pure u128 |
Read as u64 first, so values above u64::MAX are rejected. |
u256 |
decimal string | pure, 16 bytes | Parsed as u128. Values above u128::MAX are rejected. |
bool |
boolean | pure bool |
|
address |
0x… string |
pure address | |
object |
0x… id |
object input, mutable | Shared objects are passed mutable; owned objects as ImmOrOwned. |
immutable |
0x… id |
object input, read-only | Shared objects are passed with mutable: false. |
Every object argument costs one get_objects RPC to read its owner and version, unless the immutable
cache already holds it. resolve_object_arg() in src/grpc_ext.rs turns the owner into the right
ObjectArg.
Prefer pure for exact integers
The pure form rejects out-of-range values (Value … is out of range for u8) and accepts any
u256. The two-element forms are kept for existing payloads.
Three-element forms¶
parse_complex_argument() dispatches on the first element.
| Form | Commands added | What it does |
|---|---|---|
["result", i, j] |
none | Argument::NestedResult(i, j): output j of command i in this PTB. Indices are read as u64 and cast to u16. |
["vector", type, [values]] |
1 MakeMoveVec |
Each element becomes its own input via the two-element rules. The element type comes from get_move_type_from_string(). |
["vecmap", [ktype, vtype], [{k: v}, …]] |
2 MakeMoveVec + 1 MoveCall |
Builds a key vector and a value vector, then calls 0x2::vec_map::from_keys_values<K, V>. Each entry object must have exactly one pair; keys are JSON object keys, so they arrive as strings. |
["option", type, value] |
1 MoveCall (+1 for a vector) |
null calls 0x1::option::none<T>, anything else 0x1::option::some<T>. |
["pure", type, value] |
none | One pure input with the BCS bytes from encode_pure(). |
Command indices matter for result. A vector argument adds one command before the call that uses it;
a vecmap adds three. Count them when you point a later ["result", i, j] at an earlier call. The
payload guide has worked examples.
Quirks of option, from parse_complex_argument() in src/ptb_args.rs:
["option", "vector", [type, values]]builds the vector, then wraps it withoption::some<T>whereTis the element type, notvector<T>.nullvalues giveoption::none<T>.["option", "vecmap", …]returns the bareVecMapwith no option wrapper.
get_move_type_from_string() maps string to 0x1::string::String and object to
0x2::object::ID. It does not accept immutable; a vector of immutable objects fails with
Unsupported move type: immutable.
pure: BCS from a type and a value¶
src/pure_args.rs parses the type into a PureType, converts the JSON into a PureValue against it,
and serialises that with BCS.
flowchart LR
A["type string<br/>vector<vector<u8>>"] --> B["PureType::parse"]
C["JSON value"] --> D["to_pure_value"]
B --> D
D --> E["PureValue"]
E --> F["bcs::to_bytes"]
F --> G["pure_bytes(bytes, false)"]
| Type | Accepted JSON | Encoding |
|---|---|---|
u8 … u128 |
number, or decimal string | Little-endian fixed width. Out of range is an error. |
u256 |
number or decimal string | 32 bytes, little-endian, no length prefix. |
bool |
boolean | 1 byte. |
address |
0x… string |
32 raw bytes, no length prefix. |
string |
string | ULEB128 length + UTF-8. |
vector<T> |
array of T |
ULEB128 length + elements. Nesting depth is unlimited. |
vector<u8> |
array of numbers, or a shorthand | ULEB128 length + bytes. |
vector<u8> shorthands, from bytes_from_json():
| JSON | Bytes |
|---|---|
[1, 2, 3] |
the numbers |
"hello" |
UTF-8 of the string (not base64) |
"0x0102" |
hex-decoded |
{"base64": "AQI="} |
base64-decoded |
{"bcs": ["u64", 7]} |
BCS of the inner [type, value], recursively |
{"type": "0x2::iota::IOTA"} |
BCS of a vector<String> of canonical type names, as the relayer encodes type_args |
The bare-string rule differs from the event decoder, which reads a string vector<u8> as hex or base64.
The module comment in src/pure_args.rs records the asymmetry. pure_bytes(bytes, false) lets
identical values share one input; each argument still points at it by position.
Type arguments¶
Two encodings exist, for two different places.
| Where | Form | Code |
|---|---|---|
type_args of a call in /api/SendPTB |
JSON array of type strings such as "0x2::iota::IOTA" |
parse_type_args() in src/main.rs: TypeTag::from_str, then TypeInput. |
type_args inside a bridge intent |
vector<u8>: BCS of a vector<String> of canonical names without 0x |
type_args_to_bytes() and type_args_from_bytes() in src/bridge/event.rs. parse_canonical_type() adds the 0x back. |
The static relayer passes no type arguments: append_call() in src/bin/event_handler.rs uses
parse_argument() and an empty type list.
Errors¶
All errors surface as the backend's 500 body or as a build_call row in the relayer.
| Message | Cause |
|---|---|
Expected array for argument, got … |
The argument is not a JSON array. |
Invalid array length, expect 2 or 3, got N |
Wrong arity. |
Unsupported argument type: X |
Unknown two-element type tag. |
Unsupported command type: X |
Unknown three-element kind. |
Expected u8 value (and u16, u64, …) |
Value of the wrong JSON type. |
Invalid u256 value: … |
The u256 string does not parse as u128. |
Invalid address '…': … / Invalid object ID '…': … |
Malformed hex id. |
Unsupported move type: X |
Element type not allowed in vector/option. |
Each vecmap entry must have exactly one key-value pair |
Malformed vecmap entry. |
Unsupported pure type '…'; expected one of u8, …, vector<T> |
Unknown pure type. |
Expected an array for vector<T>, got … |
Depth mismatch between type and value. |
Value V is out of range for T |
pure integer overflow. |
Unsupported vector<u8> shorthand '…'; expected bcs, type, or base64 |
Unknown shorthand key. |
Invalid type argument '…': … |
A type_args string does not parse. |
Invalid target format, expected 'package_id::module::function', got '…' |
target from parse_target() in src/lib.rs. |
Configuration knobs¶
None. The encoders take no environment variables. EVENT_HANDLER_ARGS_JSON
feeds the same encoder in the static relayer.
Tests that pin the behaviour¶
In src/ptb_args.rs:
| Test | Pins |
|---|---|
immutable_object_id_matches_only_the_immutable_tag |
Only ["immutable", id] goes to the cache. |
immutable_object_cache_starts_empty |
A fresh cache is empty. |
a_cross_chain_request_lands_as_pure_inputs |
A bridge request's arguments become pure inputs. |
pure_is_dispatched_as_a_three_element_command |
["pure", …] reaches the pure encoder. |
a_malformed_pure_argument_is_rejected |
Bad pure input is an error, not a panic. |
In src/pure_args.rs:
| Test | Pins |
|---|---|
nested_byte_vectors_match_the_documented_literal |
vector<vector<u8>> bytes match the documented example. |
decoded_elements_are_the_bytes_the_destination_call_receives |
Round trip to the bytes Move sees. |
encoded_args_reproduce_the_golden_intent_args |
Agreement with the relayer's intent encoding. |
empty_type_args_encode_to_a_single_zero_byte |
Empty vector is 0x00. |
a_bare_string_is_utf8_bytes |
Bare-string shorthand. |
hex_base64_and_arrays_agree |
The three byte spellings give the same bytes. |
type_shorthand_matches_the_relayers_encoder |
{"type": …} equals type_args_to_bytes(). |
scalars_match_plain_bcs, u256_is_thirty_two_bytes, addresses_are_thirty_two_raw_bytes |
Scalar layouts. |
large_integers_are_accepted_as_decimal_strings |
Decimal strings for wide integers. |
deep_nesting_is_supported, non_byte_vectors_still_work |
Nested and non-byte vectors. |
rejects_an_unknown_type, rejects_a_depth_mismatch, rejects_out_of_range_and_malformed_values, rejects_malformed_shorthands |
Error paths. |
In src/bridge/event.rs: type_args_round_trip_through_canonical_strings.
In src/main.rs: type_args_are_parsed_into_the_move_call.