Compact<u64>

Compact 12 v393

About This Type

Variable-length encoded wrapper around u64.

Compact encoding uses fewer bytes for small values. Values 0–63 use 1 byte, 64–16,383 use 2 bytes, and larger values use 4 bytes.

Encoding: Two least-significant bits encode the mode: 00=single-byte, 01=two-byte, 10=four-byte, 11=big-integer.

Compact Wrapper

Wraps
u64
SCALE Encoding
Variable-length integer (1, 2, or 4 bytes depending on value)

SCALE Encoding

Rule
Variable-length integer encoding of u64. Uses fewer bytes for smaller values.
Size
variable (1–5 bytes)

Examples

Small value: 6 = 6
0x18
18 Single-byte mode: 6 << 2 = 24 = 0x18
Medium value: 100 = 100
0x9101
91 01 Two-byte mode: (100 << 2) | 0x01 = 401 = 0x0191, stored LE
Larger value: 1,000,000 = 1,000,000
0x02000f4002
02 00 0f 40 02 Four-byte mode: (1,000,000 << 2) | 0x02

Code Examples

import { TypeRegistry, compactToU8a } from "@polkadot/types";

const registry = new TypeRegistry();

// Encode as Compact<u64>
const value = registry.createType("Compact<u64>", 100);
console.log("Hex:", value.toHex());

// Or use the low-level utility
const bytes = compactToU8a(100);
console.log("Bytes:", bytes);

Referenced By (115)

and 95 more...

Type Information

Type ID
12
Kind
Compact
Runtime
v393