solana_kit_codecs 0.3.0
solana_kit_codecs: ^0.3.0 copied to clipboard
Umbrella package re-exporting all codec sub-packages for the Solana Kit Dart SDK.
solana_kit_codecs #
Umbrella package that re-exports all Solana Kit codec sub-packages through a single import.
This is a Dart port of @solana/codecs from the Solana TypeScript SDK.
Installation #
Install the package directly:
dart pub add solana_kit_codecs
If your app uses several Solana Kit packages together, you can also depend on the umbrella package instead:
dart pub add solana_kit
Inside this monorepo, Dart workspace resolution uses the local package automatically.
Documentation #
- Package page: https://pub.dev/packages/solana_kit_codecs
- API reference: https://pub.dev/documentation/solana_kit_codecs/latest/
- Workspace docs: https://openbudgetfun.github.io/solana_kit/
- Package catalog entry: https://openbudgetfun.github.io/solana_kit/reference/package-catalog#solana_kit_codecs
- Source code: https://github.com/openbudgetfun/solana_kit/tree/main/packages/solana_kit_codecs
For architecture notes, getting-started guides, and cross-package examples, start with the workspace docs site and then drill down into the package README and API reference.
Usage #
Instead of importing each codec sub-package individually, import solana_kit_codecs to get everything at once:
import 'package:solana_kit_codecs/solana_kit_codecs.dart';
This single import gives you access to all codec functionality from:
- solana_kit_codecs_core -- Core interfaces (
Encoder,Decoder,Codec) and composition utilities (combineCodec,transformCodec,fixCodecSize,addCodecSizePrefix,addCodecSentinel,offsetCodec,padLeftCodec,padRightCodec,reverseCodec, etc.) - solana_kit_codecs_numbers -- Numeric codecs for integers and floats (
getU8Codec,getU16Codec,getU32Codec,getU64Codec,getU128Codec,getI8Codec, ...,getF32Codec,getF64Codec,getShortU16Codec) - solana_kit_codecs_strings -- String codecs (
getUtf8Codec,getBase58Codec,getBase16Codec,getBase64Codec,getBase10Codec,getBaseXCodec,getBaseXResliceCodec) - solana_kit_codecs_data_structures -- Composite data structure codecs (
getStructCodec,getArrayCodec,getTupleCodec,getBooleanCodec,getNullableCodec,getMapCodec,getSetCodec,getDiscriminatedUnionCodec,getLiteralUnionCodec,getBitArrayCodec,getBytesCodec,getConstantCodec,getUnitCodec,getHiddenPrefixCodec,getHiddenSuffixCodec) - solana_kit_options -- Rust-like
Option<T>type and codec (some,none,getOptionCodec,unwrapOption,unwrapOptionRecursively)
Example: encoding a Solana account layout #
import 'dart:typed_data';
import 'package:solana_kit_codecs/solana_kit_codecs.dart';
// Define an account data layout (similar to a Borsh schema).
final accountCodec = getStructCodec([
('isInitialized', getBooleanCodec()),
('authority', fixCodecSize(getBase58Codec(), 32)),
('balance', getU64Codec()),
('label', addCodecSizePrefix(getUtf8Codec(), getU32Codec())),
('tags', getArrayCodec(
addCodecSizePrefix(getUtf8Codec(), getU32Codec()),
size: PrefixedArraySize(getU16Codec()),
)),
]);
// Encode account data.
final encoded = accountCodec.encode({
'isInitialized': true,
'authority': '11111111111111111111111111111111',
'balance': BigInt.from(1000000000),
'label': 'My Account',
'tags': ['defi', 'staking'],
});
// Decode account data.
final decoded = accountCodec.decode(encoded);
// decoded['isInitialized'] == true
// decoded['balance'] == BigInt.from(1000000000)
// decoded['label'] == 'My Account'
// decoded['tags'] == ['defi', 'staking']
Example: encoding a Rust-like enum #
import 'package:solana_kit_codecs/solana_kit_codecs.dart';
// Define an instruction enum.
final instructionCodec = getDiscriminatedUnionCodec([
('initialize', getStructCodec([
('authority', fixCodecSize(getBase58Codec(), 32)),
('amount', getU64Codec()),
])),
('transfer', getStructCodec([
('amount', getU64Codec()),
])),
('close', getUnitCodec()),
]);
// Encode a 'transfer' instruction.
final bytes = instructionCodec.encode({
'__kind': 'transfer',
'amount': BigInt.from(500),
});
// Decode.
final decoded = instructionCodec.decode(bytes);
// decoded['__kind'] == 'transfer'
// decoded['amount'] == BigInt.from(500)
Example: working with Option types #
import 'package:solana_kit_codecs/solana_kit_codecs.dart';
// Encode an optional u64 field.
final optionalBalance = getOptionCodec(getU64Codec());
optionalBalance.encode(some(BigInt.from(42))); // [0x01, ...8 bytes...]
optionalBalance.encode(none<BigInt>()); // [0x00]
// Decode and pattern match.
final result = optionalBalance.decode(bytes);
switch (result) {
case Some(:final value):
print('Balance: $value');
case None():
print('No balance set');
}
Re-exported packages #
| Package | Description |
|---|---|
solana_kit_codecs_core |
Core interfaces and composition utilities |
solana_kit_codecs_numbers |
Integer and float codecs |
solana_kit_codecs_strings |
String and base encoding codecs |
solana_kit_codecs_data_structures |
Struct, array, enum, and other composite codecs |
solana_kit_options |
Rust-like Option<T> type and codec |
Example #
Use example/main.dart as a runnable starting point for solana_kit_codecs.
- Import path:
package:solana_kit_codecs/solana_kit_codecs.dart - This section is centrally maintained with
mdtto keep package guidance aligned. - After updating shared docs templates, run
docs:updatefrom the repo root.
Maintenance #
- Validate docs in CI and locally with
docs:check. - Keep examples focused on one workflow and reference package README sections for deeper API details.