Kit
Functions

createEncoder

Call Signature

createEncoder<TFrom, TSize>(encoder): FixedSizeEncoder<TFrom, TSize>

Creates an Encoder by filling in the missing encode function using the provided write function and either the fixedSize property (/api/functions/for FixedSizeEncoders) or the getSizeFromValue function (/api/functions/for VariableSizeEncoders).

Instead of manually implementing encode, this utility leverages the existing write function and the size helpers to generate a complete encoder. The provided encode method will allocate a new Uint8Array of the correct size and use write to populate it.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TSize extends numberThe fixed size of the encoded value in bytes (for fixed-size encoders).

Parameters

ParameterTypeDescription
encoderOmit<FixedSizeEncoder<TFrom, TSize>, "encode">An encoder object that implements write, but not encode. - If the encoder has a fixedSize property, it is treated as a FixedSizeEncoder. - Otherwise, it is treated as a VariableSizeEncoder.

Returns

FixedSizeEncoder<TFrom, TSize>

A fully functional Encoder with both write and encode methods.

Examples

Creating a custom fixed-size encoder.

const encoder = createEncoder({
    fixedSize: 4,
    write: (value: number, bytes, offset) => {
        bytes.set(new Uint8Array([value]), offset);
        return offset + 4;
    },
});
 
const bytes = encoder.encode(42);
// 0x2a000000

Creating a custom variable-size encoder:

const encoder = createEncoder({
    getSizeFromValue: (value: string) => value.length,
    write: (value: string, bytes, offset) => {
        const encodedValue = new TextEncoder().encode(value);
        bytes.set(encodedValue, offset);
        return offset + encodedValue.length;
    },
});
 
const bytes = encoder.encode("hello");
// 0x68656c6c6f

Remarks

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

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

// Fixed-size encoder for unsigned 32-bit integers.
const encoder = getU32Encoder();
const bytes = encoder.encode(42);
// 0x2a000000
 
// Variable-size encoder for 32-bytes prefixed UTF-8 strings.
const encoder = addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder());
const bytes = encoder.encode("hello");
// 0x0500000068656c6c6f
 
// Variable-size encoder for custom objects.
type Person = { name: string; age: number };
const encoder: Encoder<Person> = getStructEncoder([
    ['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
    ['age', getU32Encoder()],
]);
const bytes = encoder.encode({ name: "Bob", age: 42 });
// 0x03000000426f622a000000

See

Call Signature

createEncoder<TFrom>(encoder): VariableSizeEncoder<TFrom>

Creates an Encoder by filling in the missing encode function using the provided write function and either the fixedSize property (/api/functions/for FixedSizeEncoders) or the getSizeFromValue function (/api/functions/for VariableSizeEncoders).

Instead of manually implementing encode, this utility leverages the existing write function and the size helpers to generate a complete encoder. The provided encode method will allocate a new Uint8Array of the correct size and use write to populate it.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.

Parameters

ParameterTypeDescription
encoderOmit<VariableSizeEncoder<TFrom>, "encode">An encoder object that implements write, but not encode. - If the encoder has a fixedSize property, it is treated as a FixedSizeEncoder. - Otherwise, it is treated as a VariableSizeEncoder.

Returns

VariableSizeEncoder<TFrom>

A fully functional Encoder with both write and encode methods.

Examples

Creating a custom fixed-size encoder.

const encoder = createEncoder({
    fixedSize: 4,
    write: (value: number, bytes, offset) => {
        bytes.set(new Uint8Array([value]), offset);
        return offset + 4;
    },
});
 
const bytes = encoder.encode(42);
// 0x2a000000

Creating a custom variable-size encoder:

const encoder = createEncoder({
    getSizeFromValue: (value: string) => value.length,
    write: (value: string, bytes, offset) => {
        const encodedValue = new TextEncoder().encode(value);
        bytes.set(encodedValue, offset);
        return offset + encodedValue.length;
    },
});
 
const bytes = encoder.encode("hello");
// 0x68656c6c6f

Remarks

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

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

// Fixed-size encoder for unsigned 32-bit integers.
const encoder = getU32Encoder();
const bytes = encoder.encode(42);
// 0x2a000000
 
// Variable-size encoder for 32-bytes prefixed UTF-8 strings.
const encoder = addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder());
const bytes = encoder.encode("hello");
// 0x0500000068656c6c6f
 
// Variable-size encoder for custom objects.
type Person = { name: string; age: number };
const encoder: Encoder<Person> = getStructEncoder([
    ['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
    ['age', getU32Encoder()],
]);
const bytes = encoder.encode({ name: "Bob", age: 42 });
// 0x03000000426f622a000000

See

Call Signature

createEncoder<TFrom>(encoder): Encoder<TFrom>

Creates an Encoder by filling in the missing encode function using the provided write function and either the fixedSize property (/api/functions/for FixedSizeEncoders) or the getSizeFromValue function (/api/functions/for VariableSizeEncoders).

Instead of manually implementing encode, this utility leverages the existing write function and the size helpers to generate a complete encoder. The provided encode method will allocate a new Uint8Array of the correct size and use write to populate it.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.

Parameters

ParameterTypeDescription
encoderOmit<FixedSizeEncoder<TFrom, number>, "encode"> | Omit<VariableSizeEncoder<TFrom>, "encode">An encoder object that implements write, but not encode. - If the encoder has a fixedSize property, it is treated as a FixedSizeEncoder. - Otherwise, it is treated as a VariableSizeEncoder.

Returns

Encoder<TFrom>

A fully functional Encoder with both write and encode methods.

Examples

Creating a custom fixed-size encoder.

const encoder = createEncoder({
    fixedSize: 4,
    write: (value: number, bytes, offset) => {
        bytes.set(new Uint8Array([value]), offset);
        return offset + 4;
    },
});
 
const bytes = encoder.encode(42);
// 0x2a000000

Creating a custom variable-size encoder:

const encoder = createEncoder({
    getSizeFromValue: (value: string) => value.length,
    write: (value: string, bytes, offset) => {
        const encodedValue = new TextEncoder().encode(value);
        bytes.set(encodedValue, offset);
        return offset + encodedValue.length;
    },
});
 
const bytes = encoder.encode("hello");
// 0x68656c6c6f

Remarks

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

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

// Fixed-size encoder for unsigned 32-bit integers.
const encoder = getU32Encoder();
const bytes = encoder.encode(42);
// 0x2a000000
 
// Variable-size encoder for 32-bytes prefixed UTF-8 strings.
const encoder = addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder());
const bytes = encoder.encode("hello");
// 0x0500000068656c6c6f
 
// Variable-size encoder for custom objects.
type Person = { name: string; age: number };
const encoder: Encoder<Person> = getStructEncoder([
    ['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
    ['age', getU32Encoder()],
]);
const bytes = encoder.encode({ name: "Bob", age: 42 });
// 0x03000000426f622a000000

See

On this page