createDecoder
Call Signature
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 Parameter | Description |
|---|---|
TTo | The type of the decoded value. |
TSize extends number | The fixed size of the encoded value in bytes (for fixed-size decoders). |
Parameters
| Parameter | Type | Description |
|---|---|---|
decoder | Omit<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.
Creating a custom variable-size decoder:
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.
See
- Decoder
- FixedSizeDecoder
- VariableSizeDecoder
- getStructDecoder
- getU32Decoder
- getUtf8Decoder
- addDecoderSizePrefix
Call Signature
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 Parameter | Description |
|---|---|
TTo | The type of the decoded value. |
Parameters
| Parameter | Type | Description |
|---|---|---|
decoder | 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
VariableSizeDecoder<TTo>
A fully functional Decoder with both read and decode methods.
Examples
Creating a custom fixed-size decoder.
Creating a custom variable-size decoder:
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.
See
- Decoder
- FixedSizeDecoder
- VariableSizeDecoder
- getStructDecoder
- getU32Decoder
- getUtf8Decoder
- addDecoderSizePrefix
Call Signature
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 Parameter | Description |
|---|---|
TTo | The type of the decoded value. |
Parameters
| Parameter | Type | Description |
|---|---|---|
decoder | | Omit<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.
Creating a custom variable-size decoder:
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.