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 the SCALE codec in a program, add it to Cargo.toml:

[dependencies]

// ...
codec = { package = "parity-scale-codec", version = "3.6", default-features = false }
use codec::{Decode, Encode};

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

The Encode and Decode traits are needed when using wrapped methods from gstd, such as: msg::load, msg::send, msg::reply, msg::send_for_reply, etc.

In methods like msg::load_bytes, msg::send_bytes, or msg::reply_bytes, operations are performed with a set of bytes, so nothing needs to be decoded/encoded.

Learn more about the SCALE Codec here.

scale-info‚Äč

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 to decode types agnostic of language. The interface that uses scale-info for Gear programs is called metadata. It defines incoming and outgoing types for all necessary entry points and allows programs and the client part to understand each other.

info

Learn more about using metadata in a program.

To use scale-info in a project:

[dependencies]

// ...
scale-info = { version = "2.9", default-features = false, features = ["derive"] }

Learn more about scale-info here.