Kit
Functions

createDecoder

Call Signature

createDecoder<TTo, TSize>(decoder): FixedSizeDecoder<TTo, TSize>

Creates a Decoder by filling in the missing decode function using the provided read function.

Instead of manually implementing decode, this utility leverages the existing read function and the size properties to generate a complete decoder. The provided decode method will read from a Uint8Array at the given offset and return the decoded value.

If the fixedSize property is provided, a FixedSizeDecoder will be created, otherwise a VariableSizeDecoder will be created.

Type Parameters

Type ParameterDescription
TToThe type of the decoded value.
TSize extends numberThe fixed size of the encoded value in bytes (for fixed-size decoders).

Parameters

ParameterTypeDescription
decoderOmit<FixedSizeDecoder<TTo, TSize>, "decode">A decoder object that implements read, but not decode. - If the decoder has a fixedSize property, it is treated as a FixedSizeDecoder. - Otherwise, it is treated as a VariableSizeDecoder.

Returns

FixedSizeDecoder<TTo, TSize>

A fully functional Decoder with both read and decode methods.

Examples

Creating a custom fixed-size decoder.

const decoder = createDecoder({
    fixedSize: 4,
    read: (bytes, offset) => {
        const value = bytes[offset];
        return [value, offset + 4];
    },
});
 
const value = decoder.decode(new Uint8Array([42, 0, 0, 0]));
// 42

Creating a custom variable-size decoder:

const decoder = createDecoder({
    read: (bytes, offset) => {
        const decodedValue = new TextDecoder().decode(bytes.subarray(offset));
        return [decodedValue, bytes.length];
    },
});
 
const value = decoder.decode(new Uint8Array([104, 101, 108, 108, 111]));
// "hello"

Remarks

Note that, while createDecoder is useful for defining more complex decoders, it is more common to compose decoders together using the various helpers and primitives of the @solana/codecs package.

Here are some alternative examples using codec primitives instead of createDecoder.

// Fixed-size decoder for unsigned 32-bit integers.
const decoder = getU32Decoder();
const value = decoder.decode(new Uint8Array([42, 0, 0, 0]));
// 42
 
// Variable-size decoder for 32-bytes prefixed UTF-8 strings.
const decoder = addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder());
const value = decoder.decode(new Uint8Array([5, 0, 0, 0, 104, 101, 108, 108, 111]));
// "hello"
 
// Variable-size decoder for custom objects.
type Person = { name: string; age: number };
const decoder: Decoder<Person> = getStructDecoder([
    ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
    ['age', getU32Decoder()],
]);
const value = decoder.decode(new Uint8Array([3, 0, 0, 0, 66, 111, 98, 42, 0, 0, 0]));
// { name: "Bob", age: 42 }

See

Call Signature

createDecoder<TTo>(decoder): VariableSizeDecoder<TTo>

Creates a Decoder by filling in the missing decode function using the provided read function.

Instead of manually implementing decode, this utility leverages the existing read function and the size properties to generate a complete decoder. The provided decode method will read from a Uint8Array at the given offset and return the decoded value.

If the fixedSize property is provided, a FixedSizeDecoder will be created, otherwise a VariableSizeDecoder will be created.

Type Parameters

Type ParameterDescription
TToThe type of the decoded value.

Parameters

ParameterTypeDescription
decoderOmit<VariableSizeDecoder<TTo>, "decode">A decoder object that implements read, but not decode. - If the decoder has a fixedSize property, it is treated as a FixedSizeDecoder. - Otherwise, it is treated as a VariableSizeDecoder.

Returns

VariableSizeDecoder<TTo>

A fully functional Decoder with both read and decode methods.

Examples

Creating a custom fixed-size decoder.

const decoder = createDecoder({
    fixedSize: 4,
    read: (bytes, offset) => {
        const value = bytes[offset];
        return [value, offset + 4];
    },
});
 
const value = decoder.decode(new Uint8Array([42, 0, 0, 0]));
// 42

Creating a custom variable-size decoder:

const decoder = createDecoder({
    read: (bytes, offset) => {
        const decodedValue = new TextDecoder().decode(bytes.subarray(offset));
        return [decodedValue, bytes.length];
    },
});
 
const value = decoder.decode(new Uint8Array([104, 101, 108, 108, 111]));
// "hello"

Remarks

Note that, while createDecoder is useful for defining more complex decoders, it is more common to compose decoders together using the various helpers and primitives of the @solana/codecs package.

Here are some alternative examples using codec primitives instead of createDecoder.

// Fixed-size decoder for unsigned 32-bit integers.
const decoder = getU32Decoder();
const value = decoder.decode(new Uint8Array([42, 0, 0, 0]));
// 42
 
// Variable-size decoder for 32-bytes prefixed UTF-8 strings.
const decoder = addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder());
const value = decoder.decode(new Uint8Array([5, 0, 0, 0, 104, 101, 108, 108, 111]));
// "hello"
 
// Variable-size decoder for custom objects.
type Person = { name: string; age: number };
const decoder: Decoder<Person> = getStructDecoder([
    ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
    ['age', getU32Decoder()],
]);
const value = decoder.decode(new Uint8Array([3, 0, 0, 0, 66, 111, 98, 42, 0, 0, 0]));
// { name: "Bob", age: 42 }

See

Call Signature

createDecoder<TTo>(decoder): Decoder<TTo>

Creates a Decoder by filling in the missing decode function using the provided read function.

Instead of manually implementing decode, this utility leverages the existing read function and the size properties to generate a complete decoder. The provided decode method will read from a Uint8Array at the given offset and return the decoded value.

If the fixedSize property is provided, a FixedSizeDecoder will be created, otherwise a VariableSizeDecoder will be created.

Type Parameters

Type ParameterDescription
TToThe type of the decoded value.

Parameters

ParameterTypeDescription
decoderOmit<FixedSizeDecoder<TTo, number>, "decode"> | Omit<VariableSizeDecoder<TTo>, "decode">A decoder object that implements read, but not decode. - If the decoder has a fixedSize property, it is treated as a FixedSizeDecoder. - Otherwise, it is treated as a VariableSizeDecoder.

Returns

Decoder<TTo>

A fully functional Decoder with both read and decode methods.

Examples

Creating a custom fixed-size decoder.

const decoder = createDecoder({
    fixedSize: 4,
    read: (bytes, offset) => {
        const value = bytes[offset];
        return [value, offset + 4];
    },
});
 
const value = decoder.decode(new Uint8Array([42, 0, 0, 0]));
// 42

Creating a custom variable-size decoder:

const decoder = createDecoder({
    read: (bytes, offset) => {
        const decodedValue = new TextDecoder().decode(bytes.subarray(offset));
        return [decodedValue, bytes.length];
    },
});
 
const value = decoder.decode(new Uint8Array([104, 101, 108, 108, 111]));
// "hello"

Remarks

Note that, while createDecoder is useful for defining more complex decoders, it is more common to compose decoders together using the various helpers and primitives of the @solana/codecs package.

Here are some alternative examples using codec primitives instead of createDecoder.

// Fixed-size decoder for unsigned 32-bit integers.
const decoder = getU32Decoder();
const value = decoder.decode(new Uint8Array([42, 0, 0, 0]));
// 42
 
// Variable-size decoder for 32-bytes prefixed UTF-8 strings.
const decoder = addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder());
const value = decoder.decode(new Uint8Array([5, 0, 0, 0, 104, 101, 108, 108, 111]));
// "hello"
 
// Variable-size decoder for custom objects.
type Person = { name: string; age: number };
const decoder: Decoder<Person> = getStructDecoder([
    ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
    ['age', getU32Decoder()],
]);
const value = decoder.decode(new Uint8Array([3, 0, 0, 0, 66, 111, 98, 42, 0, 0, 0]));
// { name: "Bob", age: 42 }

See

On this page