Skip to content

x402agent/SolanaOS / skills/solana-dev

solana-dev

Flagged

Use when user asks to "build a Solana dapp", "write an Anchor program", "create a token", "debug Solana errors", "set up wallet connection", "test my Solana program", "deploy to devnet", or "explain Solana concepts" (rent, accounts, PDAs, CPIs, etc.). End-to-end Solana development playbook covering wallet connection, Anchor/Pinocchio programs, Codama client generation, LiteSVM/Mollusk/Surfpool testing, and security checklists. Integrates with the Solana MCP server for live documentation search. Prefers framework-kit (@solana/client + @solana/react-hooks) for UI, wallet-standard-first connection (incl. ConnectorKit), @solana/kit for client/RPC code, and @solana/web3-compat for legacy boundaries.

references/kit/codecs.md

View on GitHub
---
title: Codecs Reference
description: Data encoding and decoding patterns for numbers, strings, structs, arrays, enums, discriminated unions, and common account/instruction codecs.
---

# Solana Kit Codecs Reference

## Direction

- **`encode()`**: values → `Uint8Array`
- **`decode()`**: `Uint8Array` → values

## Codec Types

```ts
// Full codec
const codec: Codec<number> = getU32Codec();
codec.encode(42);     // Uint8Array
codec.decode(bytes);  // number

// Encoder only (tree-shaking)
const encoder: Encoder<number> = getU32Encoder();

// Decoder only
const decoder: Decoder<number> = getU32Decoder();
```

## Number Codecs

```ts
// Unsigned
getU8Codec();   // 1 byte
getU16Codec();  // 2 bytes (little-endian default)
getU32Codec();  // 4 bytes
getU64Codec();  // 8 bytes (bigint)
getU128Codec(); // 16 bytes (bigint)

// Signed
getI8Codec();  getI16Codec();  getI32Codec();  getI64Codec();

// Float
getF32Codec();  getF64Codec();

// Big-endian
getU16Codec({ endian: Endian.Big });
```

## String Codecs

```ts
// UTF-8 (variable)
const utf8 = getUtf8Codec();

// With size prefix (common)
const prefixed = addCodecSizePrefix(getUtf8Codec(), getU32Codec());

// Fixed size
const fixed = fixCodecSize(getUtf8Codec(), 32);

// Base58 (addresses)
const base58 = getBase58Codec();
```

## Struct Codec

```ts
type MyStruct = { id: number; name: string; balance: bigint };

const codec = getStructCodec([
  ['id', getU32Codec()],
  ['name', addCodecSizePrefix(getUtf8Codec(), getU32Codec())],
  ['balance', getU64Codec()],
]);

const bytes = codec.encode({ id: 1, name: 'Alice', balance: 1000n });
const data = codec.decode(bytes);
```

## Array & Tuple

```ts
// Array with size prefix
const arr = addCodecSizePrefix(getArrayCodec(getU32Codec()), getU32Codec());

// Fixed array
const fixed = fixCodecSize(getArrayCodec(getU32Codec()), 10);

// Tuple
const point = getTupleCodec([getI32Codec(), getI32Codec()]);
```

## Enum

```ts
enum Direction { Up, Down, Left, Right }
const codec = getEnumCodec(Direction);
codec.encode(Direction.Up);  // [0]
```

## Discriminated Union

```ts
type Shape =
  | { __kind: 'circle'; radius: number }
  | { __kind: 'rectangle'; width: number; height: number };

const codec = getDiscriminatedUnionCodec([
  ['circle', getStructCodec([['radius', getU32Codec()]])],
  ['rectangle', getStructCodec([
    ['width', getU32Codec()],
    ['height', getU32Codec()],
  ])],
]);
```

## Boolean & Nullable

```ts
const bool = getBooleanCodec();        // 1 byte
const u32Bool = getBooleanCodec({ size: 4 });

const nullable = getNullableCodec(getU32Codec());
nullable.encode(null);  // [0]
nullable.encode(42);    // [1, 42, 0, 0, 0]
```

## Options (Rust Option)

```ts
import { some, none, isSome, getOptionCodec } from '@solana/options';

const opt = getOptionCodec(getU32Codec());
opt.encode(some(42));  // [1, 42, 0, 0, 0]
opt.encode(none());    // [0]
```

## Bytes

```ts
const bytes = getBytesCodec();
const fixed32 = fixCodecSize(getBytesCodec(), 32);  // pubkeys
```

## Composition Helpers

```ts
// Size prefix
const prefixed = addCodecSizePrefix(getUtf8Codec(), getU32Codec());

// Fixed size
const fixed = fixCodecSize(getUtf8Codec(), 32);

// Transform
const upper = transformCodec(
  getUtf8Codec(),
  (v) => v.toUpperCase(),  // before encode
  (v) => v.toLowerCase(),  // after decode
);

// Offset
const offset = offsetCodec(getU32Codec(), { preOffset: 4 });
```

## Common Patterns

### Account Decoder

```ts
const tokenDecoder: Decoder<TokenAccount> = getStructDecoder([
  ['mint', fixDecoderSize(getBytesDecoder(), 32)],
  ['owner', fixDecoderSize(getBytesDecoder(), 32)],
  ['amount', getU64Decoder()],
  ['delegate', getOptionDecoder(fixDecoderSize(getBytesDecoder(), 32))],
  ['state', getU8Decoder()],
]);
```

### Instruction Encoder

```ts
const transferEncoder: Encoder<{ discriminator: number; amount: bigint }> =
  getStructEncoder([
    ['discriminator', getU8Encoder()],
    ['amount', getU64Encoder()],
  ]);

const data = transferEncoder.encode({ discriminator: 3, amount: 1000000n });
```
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall