Serializers#

Containers#

class starknet_py.serialization.TupleDataclass#

Dataclass that behaves like a tuple at the same time. Used when data has defined order and names. For instance in case of named tuples or function responses.

as_dict() Dict#

Creates a regular dict from TupleDataclass.

as_tuple() Tuple#

Creates a regular tuple from TupleDataclass.

Factory functions#

starknet_py.serialization.serializer_for_function(abi_function: Function) FunctionSerializationAdapterV0#

Create FunctionSerializationAdapter for serializing function inputs and deserializing function outputs.

Parameters:

abi_function – parsed function’s abi.

Returns:

FunctionSerializationAdapter.

starknet_py.serialization.serializer_for_event(event: EventV0 | EventV1 | EventV2) PayloadSerializer#

Create serializer for an event.

Parameters:

event – parsed event.

Returns:

PayloadSerializer that can be used to (de)serialize events.

starknet_py.serialization.serializer_for_type(cairo_type: CairoType) CairoDataSerializer#

Create a serializer for cairo type.

Parameters:

cairo_type – CairoType.

Returns:

CairoDataSerializer.

starknet_py.serialization.serializer_for_payload(payload: Dict[str, CairoType]) PayloadSerializer#

Create PayloadSerializer for types listed in a dictionary. Please note that the order of fields in the dict is very important. Make sure the keys are provided in the right order.

Parameters:

payload – dictionary with cairo types.

Returns:

PayloadSerializer that can be used to (de)serialize events/function calls.

Specific serializers#

class starknet_py.serialization.CairoDataSerializer#

Base class for serializing/deserializing data to/from calldata.

deserialize(data: List[int]) DeserializationType#

Transform calldata into python value.

Parameters:

data – calldata to deserialize.

Returns:

defined DeserializationType.

serialize(data: SerializationType) List[int]#

Transform python data into calldata.

Parameters:

data – data to serialize.

Returns:

calldata.

class starknet_py.serialization.FeltSerializer#

Serializer for field element. At the time of writing it is the only existing numeric type.

class starknet_py.serialization.ArraySerializer#

Serializer for arrays. In abi they are represented as a pointer to a type. Can serialize any iterable and prepends its length to resulting list. Deserializes data to a list.

Examples: [1,2,3] => [3,1,2,3] [] => [0]

class starknet_py.serialization.NamedTupleSerializer#

Serializer for tuples with named fields. Can serialize a dictionary, a named tuple and TupleDataclass. Deserializes data to a TupleDataclass.

Example: {“a”: 1, “b”: 2} => [1,2]

class starknet_py.serialization.StructSerializer#

Serializer of custom structures. Can serialize a dictionary. Deserializes data to a dictionary.

Example: {“a”: 1, “b”: 2} => [1,2]

class starknet_py.serialization.TupleSerializer#

Serializer for tuples without named fields. Can serialize any iterable. Deserializes data to a python tuple.

Example: (1,2,(3,4)) => [1,2,3,4]

class starknet_py.serialization.Uint256Serializer#

Serializer of Uint256. In Cairo it is represented by structure {low: Uint128, high: Uint128}. Can serialize an int. Deserializes data to an int.

Examples: 0 => [0,0] 1 => [1,0] 2**128 => [0,1] 3 + 2**128 => [3,1]

class starknet_py.serialization.PayloadSerializer#

Serializer for payloads like function arguments/function outputs/events. Can serialize a dictionary. Deserializes data to a TupleDataclass.

Example: {“a”: 1, “b”: 2} => [1,2]

deserialize(data: List[int]) DeserializationType#

Transform calldata into python value.

Parameters:

data – calldata to deserialize.

Returns:

defined DeserializationType.

serialize(data: SerializationType) List[int]#

Transform python data into calldata.

Parameters:

data – data to serialize.

Returns:

calldata.

class starknet_py.serialization.FunctionSerializationAdapter#

Class serializing *args and **kwargs by adapting them to function inputs.

serialize(*args, **kwargs) List[int]#

Method using args and kwargs to match members and serialize them separately.

Returns:

Members serialized separately in SerializedPayload.

Exceptions#

class starknet_py.serialization.CairoSerializerException#

Exception thrown by CairoSerializer.

class starknet_py.serialization.InvalidTypeException#

Exception thrown when invalid type was provided.

class starknet_py.serialization.InvalidValueException#

Exception thrown when invalid value was provided.