Skip to main content

Simple Values

We will start by going through the basic types used in smart contracts:

  • Fixed-width numbers
  • Arbitrary width (big) numbers
  • Boolean values

Fixed-width numbers

Small numbers can be stored in variables of up to 64 bits. We use big endian encoding for all numbers in our projects.

Rust types: u8, u16, u32, usize, u64, i8, i16, i32, isize, i64.

Top-encoding: The same as for all numerical types, the minimum number of bytes that can fit their 2's complement, big endian representation.

Nested encoding: Fixed width big endian encoding of the type, using 2's complement.

important

A note about the types usize and isize: these Rust-specific types have the width of the underlying architecture, i.e. 32 on 32-bit systems and 64 on 64-bit systems. However, smart contracts always run on a wasm32 architecture, so these types will always be identical to u32 and i32 respectively. Even when simulating smart contract execution on 64-bit systems, they must still be serialized on 32 bits.

Examples

TypeNumberTop-level encodingNested encoding
u800x0x00
u810x010x01
u80x110x110x11
u82550xFF0xFF
u1600x0x0000
u160x110x110x0011
u160x11220x11220x1122
u3200x0x00000000
u320x110x110x00000011
u320x11220x11220x00001122
u320x1122330x1122330x00112233
u320x112233440x112233440x11223344
u6400x0x0000000000000000
u640x110x110x0000000000000011
u640x11220x11220x0000000000001122
u640x1122330x1122330x0000000000112233
u640x112233440x112233440x0000000011223344
u640x11223344550x11223344550x0000001122334455
u640x1122334455660x1122334455660x0000112233445566
u640x112233445566770x112233445566770x0011223344556677
u640x11223344556677880x11223344556677880x1122334455667788
usize00x0x00000000
usize0x110x110x00000011
usize0x11220x11220x00001122
usize0x1122330x1122330x00112233
usize0x112233440x112233440x11223344
i800x0x00
i810x010x01
i8-10xFF0xFF
i81270x7F0x7F
i8-0x110xEF0xEF
i8-1280x800x80
i16-10xFF0xFFFF
i16-0x110xEF0xFFEF
i16-0x11220xEEDE0xEEDE
i32-10xFF0xFFFFFFFF
i32-0x110xEF0xFFFFFFEF
i32-0x11220xEEDE0xFFFFEEDE
i32-0x1122330xEEDDCD0xFFEEDDCD
i32-0x112233440xEEDDCCBC0xEEDDCCBC
i64-10xFF0xFFFFFFFFFFFFFFFF
i64-0x110xEF0xFFFFFFFFFFFFFFEF
i64-0x11220xEEDE0xFFFFFFFFFFFFEEDE
i64-0x1122330xEEDDCD0xFFFFFFFFFFEEDDCD
i64-0x112233440xEEDDCCBC0xFFFFFFFFEEDDCCBC
i64-0x11223344550xEEDDCCBBAB0xFFFFFFEEDDCCBBAB
i64-0x1122334455660xEEDDCCBBAA9A0xFFFFEEDDCCBBAA9A
i64-0x112233445566770xEEDDCCBBAA99890xFFEEDDCCBBAA9989
i64-0x11223344556677880xEEDDCCBBAA9988780xEEDDCCBBAA998878
isize00x0x00000000
isize-10xFF0xFFFFFFFF
isize-0x110xEF0xFFFFFFEF
isize-0x11220xEEDE0xFFFFEEDE
isize-0x1122330xEEDDCD0xFFEEDDCD
isize-0x112233440xEEDDCCBC0xEEDDCCBC

Arbitrary width (big) numbers

For most smart contracts applications, number larger than the maximum uint64 value are needed. EGLD balances for instance are represented as fixed-point decimal numbers with 18 decimals. This means that to represent even just 100 EGLD we use the number 100*1018, which already exceeds the capacity of a regular 64-bit integer.

Rust types: BigUint, BigInt,

important

These types are managed by MultiversX VM, in many cases the contract never sees the data, only a handle. This is to reduce the burden on the smart contract.

Top-encoding: The same as for all numerical types, the minimum number of bytes that can fit their 2's complement, big endian representation.

Nested encoding: Since these types are variable length, we need to encode their length, so that the decodes knows when to stop decoding. The length of the encoded number always comes first, on 4 bytes (usize/u32). Next we encode:

  • For BigUint the big endian bytes
  • For BigInt the shortest 2's complement number that can unambiguously represent the number. Positive numbers must always have the most significant bit 0, while the negative ones 1. See examples below.

Examples

TypeNumberTop-level encodingNested encodingExplanation
BigUint00x0x00000000The length of 0 is considered 0.
BigUint10x010x00000001011 can be represented on 1 byte, so the length is 1.
BigUint2560x01000x000000020100256 is the smallest number that takes 2 bytes.
BigInt00x0x00000000Signed 0 is also represented as zero-length bytes.
BigInt10x010x0000000101Signed 1 is also represented as 1 byte.
BigInt-10xFF0x00000001FFThe shortest 2's complement representation of -1 is FF. The most significant bit is 1.
BigUint1270x7F0x000000017F
BigInt1270x7F0x000000017F
BigUint1280x800x0000000180
BigInt1280x00800x000000020080The most significant bit of this number is 1, so to avoid ambiguity an extra 0 byte needs to be prepended.
BigInt2550x00FF0x0000000200FFSame as above.
BigInt2560x01000x000000020100256 requires 2 bytes to represent, of which the MSB is 0, no more need to prepend a 0 byte.

Boolean values

Booleans are serialized the same as a byte (u8) that can take values 1 or 0.

Rust type: bool

Values

TypeValueTop-level encodingNested encoding
booltrue0x010x01
boolfalse0x0x00

Byte slices and ASCII strings

Byte slices are technically a special case of the list types, but they are usually thought of as basic types. Their encoding is, in any case, consistent with the rules for lists of "byte items".

important

Strings are treated from the point of view of serialization as series of bytes. Using Unicode strings, while often a good practice in programming, tends to add unnecessary overhead to smart contracts. The difference is that Unicode strings get validated on input and concatenation.

We consider best practice to use Unicode on the frontend, but keep all messages and error messages in ASCII format on smart contract level.

Rust types: ManagedBuffer, BoxedBytes, &[u8], Vec<u8>, String, &str.

Top-encoding: The byte slice, as-is.

Nested encoding: The length of the byte slice on 4 bytes, followed by the byte slice as-is.

Examples

TypeValueTop-level encodingNested encodingExplanation
&'static [u8]b"abc"0x6162630x00000003616263ASCII strings are regular byte slices of buffers.
ManagedBufferManagedBuffer::from("abc")0x6162630x00000003616263Use Vec for a buffer that can grow.
BoxedBytesBoxedBytes::from( b"abc")0x6162630x00000003616263BoxedBytes are just optimized owned byte slices that cannot grow.
Vec<u8>b"abc".to_vec()0x6162630x00000003616263Use Vec for a buffer that can grow.
&'static str"abc"0x6162630x00000003616263Unicode string (slice).
String"abc".to_string()0x6162630x00000003616263Unicode string (owned).
Note

Inside contracts, ManagedBuffer is the only recommended type for generic bytes.


Address

MultiversX addresses are 32 byte arrays, so they get serialized as such, both in top and nested encodings.


Token identifiers

MultiversX ESDT token identifiers are of the form XXXXXX-123456, where the first part is the token ticker, 3 to 20 characters in length, and the last is a random generated number.

They are top-encoded as is, the exact bytes and nothing else.

Because of their variable length, they need to be serialized like variable length byte slices when nested, so the length is explicitly encoded at the start.

TypeValueTop-level encodingNested encoding
TokenIdentifierABC-1234560x4142432d3132333435360x0000000A4142432d313233343536