Skip to main content

Data encoding/decoding

To optimize how data is sent and received over the network, Gear uses the parity-scale-codec - a Rust implementation of the SCALE Codec. This codec is used by the Substrate nodes' internal runtime. SCALE is a lightweight format that enables the serialization and deserialization of data. Encoding (and decoding) data using SCALE makes it highly suitable for resource-constrained execution environments like blockchain runtimes and low-power/low-memory devices.

To use SCALE codec in your program, you should add it in Cargo.toml:


// ...
codec = { package = "parity-scale-codec", version = "3.1.2", default-features = false, features = ["derive", "full"] }
use codec::{Decode, Encode};

#[derive(Encode, Decode)]
enum MyType {
MyStruct { field: ... },

We only need the Encode and Decode trait when using wrapped methods from gstd, such as: msg::send, msg::reply, send_for_reply etc. In methods like send_byte or reply_bytes we operate with a set of bytes, so nothing needs to be decoded/encoded.

Learn more about SCALE Codec here.


scale-info is a library to describe Rust types, intended for providing information about the structure of encodable SCALE types.

The definitions provide third party tools (e.g. a UI client) with information about how they are able to decode types agnostic of language. The interface that uses scale-info for Gear programs is called the metadata macro. It defines incoming and outgoing types for all necessary entry points and allows contracts and the client part to understand each other.

To use metadata in contract:


// ...
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }
// We define all incoming and outgoing data types in advance

gstd::metadata! {
title: "gear program",
input: String,
input: MyAction,
output: Vec<u8>,
input: StateQuery,
output: StateReply,

Learn more about scale-info here