Solana Kit

repository·main·Indexed 20 days ago

https://github.com/anza-xyz/kit

A modern JavaScript SDK for building Solana applications across Node.js, web, and React Native. Designed as the successor to @solana/web3.js 2.x, it focuses on tree-shakability, functional composition, and zero dependencies. The kit includes packages such as @solana/accounts for account representation and decoding, @solana/addresses for address validation and PDA derivation, and @solana/assertions for verifying environment cryptographic capabilities.

Tokens
264.4K
Snippets
768
Records
929
Agent score
70%

What's inside anza-xyz-kit

  1. Overview of @solana/example-react-app features

    main

    The @solana/example-react-app demonstrates how to integrate @solana/kit and @solana/react into a web application. Key capabilities demonstrated include:

    • Wallet Connectivity: Connecting to one or more browser wallets that support the Wallet Standard.
    • Balance Management: Fetching and subscribing to the balance of the currently selected wallet.
    • Message Signing: Signing arbitrary messages using a wallet account.
    • Lamport Transfers: Performing transfers from a selected wallet to any other connected wallet.
  2. Overview of @solana/rpc-spec-types

    main
    The @solana/rpc-spec-types package provides core type definitions used for both RPC and RPC Subscriptions specifications. It is designed to be used standalone or as part of the larger @solana/kit ecosystem. It defines the fundamental structures for requests and responses, as well as the interfaces for transforming them.
  3. Overview of @solana/codecs sub-packages

    main

    The @solana/codecs ecosystem is divided into specialized packages to manage different types of data serialization:

    • @solana/codecs-core: The foundation. Provides core types and helper functions for creating, composing, transforming, and adjusting the size of codecs (e.g., padding, offsetting, reversing).
    • @solana/codecs-numbers: Codecs for various numeric types, including integers and decimal numbers.
    • @solana/codecs-strings: Codecs for strings with different encodings (UTF-8) and size strategies (Base 58, Base 64, Base 16, etc.).
    • @solana/codecs-data-structures: Helpers for complex structures like objects (structs), enums, arrays, maps, tuples, and unions.
    • @solana/options: Provides Rust-like Options support, including codecs and helpers for managing optional values.
  4. Overview of @solana/react

    main
    The @solana/react package provides a suite of React hooks designed to simplify building Solana applications. It provides bindings for the Kit client and hooks for managing RPC requests, subscriptions, and tracked data within a React component lifecycle.
  5. Overview of @solana/accounts

    main

    The @solana/accounts package provides types and helper methods for representing, fetching, and decoding Solana accounts. It offers a unified definition for accounts regardless of their retrieval method and supports both encoded (raw bytes) and decoded (structured data) representations.

    Key features include:

    • Unified account definitions.
    • Support for MaybeAccount to handle accounts that may or may not exist on-chain.
    • Helpers for fetching, parsing, and decoding accounts.
    • Assertion utilities to verify account existence.
    // Fetch.
    const myAddress = address('1234..5678');
    const myAccount = await fetchEncodedAccount(rpc, myAddress);
    myAccount satisfies MaybeEncodedAccount<'1234..5678'>;
    
    // Assert.
    assertAccountExists(myAccount);
    myAccount satisfies EncodedAccount<'1234..5678'>;
    
    // Decode.
    type MyAccountData = { name: string; age: number };
    const myDecoder: Decoder<MyAccountData> = getStructDecoder([
        ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
        ['age', getU32Decoder()],
    ]);
    const myDecodedAccount = decodeAccount(myAccount, myDecoder);
    myDecodedAccount satisfies Account<MyAccountData, '1234..5678'>;
  6. Use @solana/rpc-api to interact with Solana clusters

    main

    The @solana/rpc-api package provides TypeScript types for all Solana JSON RPC methods and utilities to create an RPC implementation. It allows you to interact with specific clusters (Devnet, Testnet, or Mainnet) using type-safe method calls.

    Each RPC method is defined as a type. When using a Rpc instance, you call methods and use the .send() suffix to execute the request.

    // Example pattern for calling an RPC method
    const something: Something = await rpc.getSomething(address('95DpK3y3GF7U8s1k4EvZ7xqyeCkhsHeZaE97iZpHUGMN')).send();
  7. Use @solana/rpc-parsed-types for Solana JSON-RPC responses

    main
    The @solana/rpc-parsed-types package provides TypeScript definitions for the parsed objects returned by Solana JSON-RPC methods. This allows developers to have type safety when working with the structured data returned by the Solana HTTP RPC API. It can be used as a standalone package or accessed via the main @solana/kit package.
  8. Use @solana/offchain-messages for message encoding and decoding

    main
    The @solana/offchain-messages package provides utilities to encode and decode messages following the offchain message specification (SRFC). This is used to create signed messages that can be verified off-chain without requiring an on-chain transaction. You can use this package standalone or as part of the @solana/kit bundle.
  9. Use @solana/instruction-plans for multi-instruction transaction planning

    main
    The @solana/instruction-plans package provides types and functions designed to help you plan transactions that consist of multiple instructions. This is useful for orchestrating complex sequences of operations within a single transaction. You can use this package as a standalone dependency or access it via the @solana/kit umbrella package.
  10. Use @solana/plugin-core for modular Kit clients

    main
    @solana/plugin-core provides the utilities required to build modular Kit clients that support a plugin-based architecture. This allows you to extend the core functionality of a client by adding specialized plugins. While you can use @solana/plugin-core as a standalone dependency, it is also included as part of the main @solana/kit package.
  11. Use @solana/program-client-core for building Solana program clients

    main
    The @solana/program-client-core package provides the essential types and utilities required to build Solana program clients. It is primarily designed to be used by the JavaScript Codama renderer to generate program clients that are compatible with the Solana Kit ecosystem.
  12. What is a Codec?

    main

    A Codec is an object that provides a bidirectional interface for transforming data between a structured type and a Uint8Array. It abstracts away the underlying serialization strategy and is highly composable, allowing you to build complex binary layouts from simple building blocks.

    A Codec<From, To> has two primary methods:

    • encode(value: From): Uint8Array
    • decode(bytes: Uint8Array): To

    Note that To can be a more specific type than From (e.g., encoding a number but decoding to a bigint).

    import {
        Codec,
        addCodecSizePrefix,
        getUtf8Codec,
        getU32Codec,
        getStructCodec,
    } from '@solana/kit';
    
    type Person = { name: string; age: number };
    
    const getPersonCodec = (): Codec<Person> =>
        getStructCodec([
            ['name', addCodecSizePrefix(getUtf8Codec(), getU32Codec())],
            ['age', getU32Codec()],
        ]);
    
    const personCodec = getPersonCodec();
    const encodedPerson = personCodec.encode({ name: 'John', age: 42 });
    const decodedPerson = personCodec.decode(encodedPerson);