solana_kit_rpc_spec_types 0.2.1 copy "solana_kit_rpc_spec_types: ^0.2.1" to clipboard
solana_kit_rpc_spec_types: ^0.2.1 copied to clipboard

RPC spec type definitions for the Solana Kit Dart SDK.

solana_kit_rpc_spec_types #

pub package docs CI coverage

RPC spec type definitions for the Solana Kit Dart SDK.

This is the Dart port of @solana/rpc-spec-types from the Solana TypeScript SDK.

Installation #

Install with:

dart pub add solana_kit_rpc_spec_types

If you are working within the solana_kit monorepo, the package resolves through the Dart workspace. Otherwise, specify a version or path as needed.

Documentation #

Usage #

RpcRequest #

The RpcRequest class describes an RPC request with a method name and parameters.

import 'package:solana_kit_rpc_spec_types/solana_kit_rpc_spec_types.dart';

final request = RpcRequest<List<Object?>>(
  methodName: 'getBalance',
  params: ['83astBRguLMdt2h5U1Tbd4hU5SkfAWRkzG2HPM88BREAK'],
);

print(request.methodName); // 'getBalance'
print(request.params); // ['83astBRguLMdt2h5U1Tbd4hU5SkfAWRkzG2HPM88BREAK']

Request transformers #

An RpcRequestTransformer is a function that transforms an RpcRequest before it is sent. This enables patterns like applying defaults, renaming methods, or serializing values.

import 'package:solana_kit_rpc_spec_types/solana_kit_rpc_spec_types.dart';

// A transformer that prefixes all method names.
RpcRequestTransformer prefixTransformer = (request) {
  return RpcRequest(
    methodName: 'custom_${request.methodName}',
    params: request.params,
  );
};

final original = RpcRequest<Object?>(
  methodName: 'getSlot',
  params: [],
);
final transformed = prefixTransformer(original);
print(transformed.methodName); // 'custom_getSlot'

RPC response types #

The RpcResponseData sealed class represents the data in an RPC response, which is either a successful result or an error.

import 'package:solana_kit_rpc_spec_types/solana_kit_rpc_spec_types.dart';

// A successful response.
const success = RpcResponseResult<int>(id: '1', result: 42);
print(success.result); // 42

// An error response.
const error = RpcResponseError<int>(
  id: '1',
  error: RpcErrorResponsePayload(
    code: -32601,
    message: 'Method not found',
  ),
);
print(error.error.code); // -32601
print(error.error.message); // 'Method not found'

// Pattern matching on sealed types.
void handleResponse(RpcResponseData<int> data) {
  switch (data) {
    case RpcResponseResult<int>(:final result):
      print('Got result: $result');
    case RpcResponseError<int>(:final error):
      print('Got error: ${error.message}');
  }
}

Response transformers #

An RpcResponseTransformer transforms a raw response before returning it to the caller. It receives both the response and the original request for context.

import 'package:solana_kit_rpc_spec_types/solana_kit_rpc_spec_types.dart';

// A transformer that doubles numeric results.
RpcResponseTransformer<Object?> doubleTransformer = (response, request) {
  if (response is int) {
    return response * 2;
  }
  return response;
};

Creating JSON-RPC messages #

The createRpcMessage function builds a spec-compliant JSON-RPC 2.0 message with an auto-incrementing string ID.

import 'package:solana_kit_rpc_spec_types/solana_kit_rpc_spec_types.dart';

final message = createRpcMessage(RpcRequest(
  methodName: 'getSlot',
  params: [],
));
print(message);
// {id: '0', jsonrpc: '2.0', method: 'getSlot', params: []}

// Each call gets a new ID.
final message2 = createRpcMessage(RpcRequest(
  methodName: 'getBalance',
  params: ['address123'],
));
print(message2['id']); // '1'

BigInt-aware JSON parsing #

The parseJsonWithBigInts and stringifyJsonWithBigInts functions handle the fact that Solana RPC responses can contain integers larger than what IEEE 754 doubles can represent. All non-floating-point numbers are parsed as BigInt.

import 'package:solana_kit_rpc_spec_types/solana_kit_rpc_spec_types.dart';

// Parse JSON with BigInt support.
final parsed = parseJsonWithBigInts('{"balance": 9007199254740993, "rate": 1.5}');
final map = parsed as Map<String, Object?>;
print(map['balance'] is BigInt); // true
print(map['balance']); // 9007199254740993 (as BigInt, no precision loss)
print(map['rate'] is double); // true (floating-point preserved as double)

// Stringify with BigInt support.
final json = stringifyJsonWithBigInts({
  'balance': BigInt.parse('9007199254740993'),
  'rate': 1.5,
});
print(json); // {"balance":9007199254740993,"rate":1.5}
// Note: BigInt values are rendered as raw integers, not quoted strings.

Optional Isolate JSON Decoding #

For large Solana RPC payloads, you can offload BigInt-aware JSON parsing to a background isolate.

import 'package:solana_kit_rpc_transport_http/solana_kit_rpc_transport_http.dart';

final transport = createHttpTransportForSolanaRpc(
  url: 'https://api.mainnet-beta.solana.com',
  decodeSolanaJsonInIsolate: true,
  solanaJsonIsolateThreshold: 262144,
);

For direct parsing, use parseJsonWithBigIntsAsync(...) with runInIsolate: true.

API Reference #

Classes #

  • RpcRequest<TParams> -- Describes an RPC request with methodName (String) and params (TParams).
  • RpcResponseData<T> -- Sealed class representing an RPC response: either RpcResponseResult<T> or RpcResponseError<T>.
  • RpcResponseResult<T> -- A successful RPC response with id and result.
  • RpcResponseError<T> -- An error RPC response with id and error.
  • RpcErrorResponsePayload -- Error payload with code (int), message (String), and optional data.

Functions #

  • createRpcMessage<TParams>(RpcRequest<TParams> request) -- Creates a JSON-RPC 2.0 message map with auto-incrementing ID.
  • parseJsonWithBigInts(String json) -- Parses JSON, converting all integer values to BigInt while preserving floating-point numbers as double.
  • parseJsonWithBigIntsAsync(String json, {bool runInIsolate = false, int isolateThreshold = 262144}) -- Async variant that can offload large payload parsing to a background isolate.
  • stringifyJsonWithBigInts(Object? value) -- Converts a value to JSON, rendering BigInt values as unquoted large integers.

Typedefs #

  • RpcRequestTransformer -- RpcRequest<Object?> Function(RpcRequest<Object?> request) -- Transforms an RPC request before sending.
  • RpcResponseTransformer<T> -- T Function(Object? response, RpcRequest<Object?> request) -- Transforms an RPC response before returning.

Example #

Use example/main.dart as a runnable starting point for solana_kit_rpc_spec_types.

  • Import path: package:solana_kit_rpc_spec_types/solana_kit_rpc_spec_types.dart
  • This section is centrally maintained with mdt to keep package guidance aligned.
  • After updating shared docs templates, run docs:update from 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.
0
likes
160
points
151
downloads

Publisher

unverified uploader

Weekly Downloads

RPC spec type definitions for the Solana Kit Dart SDK.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

solana_kit_errors

More

Packages that depend on solana_kit_rpc_spec_types