Compact<u32>
Compact 104 v393About This Type
Variable-length encoded wrapper around u32.
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
- u32
- SCALE Encoding
- Variable-length integer (1, 2, or 4 bytes depending on value)
SCALE Encoding
- Rule
- Variable-length integer encoding of u32. Uses fewer bytes for smaller values.
- Size
- variable (1–5 bytes)
Examples
Small value: 6 = 6
0x1818 Single-byte mode: 6 << 2 = 24 = 0x18Medium value: 100 = 100
0x910191 01 Two-byte mode: (100 << 2) | 0x01 = 401 = 0x0191, stored LELarger value: 1,000,000 = 1,000,000
0x02000f400202 00 0f 40 02 Four-byte mode: (1,000,000 << 2) | 0x02Code Examples
import { TypeRegistry, compactToU8a } from "@polkadot/types";
const registry = new TypeRegistry();
// Encode as Compact<u32>
const value = registry.createType("Compact<u32>", 100);
console.log("Hex:", value.toHex());
// Or use the low-level utility
const bytes = compactToU8a(100);
console.log("Bytes:", bytes);Referenced By (15)
#103 LastRuntimeUpgradeInfo via spec_version#169 Proxy::Call via kill_pure.height#169 Proxy::Call via kill_pure.ext_index#363 Crowdloan::Call via create.end#363 Crowdloan::Call via contribute.crowdloan_id#363 Crowdloan::Call via withdraw.crowdloan_id#363 Crowdloan::Call via finalize.crowdloan_id#363 Crowdloan::Call via refund.crowdloan_id#363 Crowdloan::Call via dissolve.crowdloan_id#363 Crowdloan::Call via update_min_contribution.crowdloan_id#363 Crowdloan::Call via update_end.crowdloan_id#363 Crowdloan::Call via update_end.new_end#363 Crowdloan::Call via update_cap.crowdloan_id#375 Header via number#385 CheckNonce
Type Information
- Type ID
- 104
- Kind
- Compact
- Runtime
- v393