OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Validate owners and lamports in a saved Solana getMultipleAccounts response

Python

Read a saved Solana getMultipleAccounts JSON-RPC response, preserve each value’s request-order index, identify null accounts, and validate the owner and non-negative integer lamports of every present account.

Python code excerpt: Validate owners and lamports in a saved Solana getMultipleAccounts response. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

This offline checker handles one specific review task: inspect the account entries in a saved getMultipleAccounts response without decoding account data or contacting an RPC service. Solana documents that this method returns account state in request order and that its result contains a value array whose entries can be null. The checker labels entries by their zero-based position in that array; it does not infer account addresses that are not included in the saved response.

Before you start

  • Intended runner: Python 3.12; no third-party packages or installation are required.
  • Input must be a saved JSON response shaped like a JSON-RPC object with a result object containing a value array, or the same JSON supplied through stdin using the - argument.
  • Owner validation checks only that owner is a non-empty string; lamports validation requires a non-negative Python integer and rejects booleans. It does not validate public-key syntax, decode account data, or verify the response against a request.

Build and check

  1. Save the code as check_accounts.py using Python 3.12.
  2. Save a getMultipleAccounts response as a local JSON file, or prepare it for stdin.
  3. Run the script with the file path or `-` to read stdin.
  4. Review the printed indices against the response value-array order; a null entry is reported rather than treated as a missing index.
  5. If the process exits nonzero, inspect stderr for input or response-shape errors, or the report for invalid account fields.

Limits to understand

  • The zero-based index identifies an entry in result.value; the response alone may not provide the original requested public keys.
  • This checks only response structure, owner string presence, and lamports type/range; it does not validate account data or establish that a response came from Solana.
  • A syntactically valid JSON document can still be large or consume substantial parsing resources; use an appropriately bounded trusted local fixture.
  • No timeout is needed because the script performs no network calls.

What this example adds

The method’s ordered value array becomes an easy-to-review JSON report: each entry retains its zero-based position, nulls are explicit, and malformed present accounts are marked invalid with reasons. Input and shape failures produce a diagnostic on stderr and a nonzero exit status; account-level validation failures still print the full report and also return nonzero. This is a local-file utility, not a Solana RPC client or a test of the external service.

Run it

Save the script as check_accounts.py, then run `python3.12 check_accounts.py saved-response.json`. To supply the saved response on stdin, run `python3.12 check_accounts.py - < saved-response.json`. The program performs no network requests, retries, or writes to files; it reads the chosen local input and writes its report to stdout.

Example input

{"jsonrpc":"2.0","id":1,"result":{"context":{"slot":341197247},"value":[{"owner":"11111111111111111111111111111111","lamports":42,"data":["","base64"]},null,{"owner":"ExampleOwner","lamports":0}]}}

Expected output

[
  {
    "index": 0,
    "status": "valid",
    "owner": "11111111111111111111111111111111",
    "lamports": 42
  },
  {
    "index": 1,
    "status": "null"
  },
  {
    "index": 2,
    "status": "valid",
    "owner": "ExampleOwner",
    "lamports": 0
  }
]
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

Python example
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 OpenViST contributors
import argparse
import json
import pathlib
import sys


def main():
    parser = argparse.ArgumentParser(
        description="Check owner and lamports in a saved getMultipleAccounts response."
    )
    parser.add_argument("file", help="Saved JSON response file, or - to read stdin")
    args = parser.parse_args()

    try:
        if args.file == "-":
            text = sys.stdin.read()
        else:
            text = pathlib.Path(args.file).read_text(encoding="utf-8")
    except OSError as exc:
        print(f"error: cannot read input: {exc}", file=sys.stderr)
        return 1

    try:
        document = json.loads(text)
    except json.JSONDecodeError as exc:
        print(f"error: invalid JSON: {exc}", file=sys.stderr)
        return 1

    if not isinstance(document, dict):
        print("error: top-level JSON value must be an object", file=sys.stderr)
        return 1
    if "error" in document:
        print("error: response contains an RPC error", file=sys.stderr)
        return 1

    result = document.get("result")
    if not isinstance(result, dict):
        print("error: result must be an object", file=sys.stderr)
        return 1
    values = result.get("value")
    if not isinstance(values, list):
        print("error: result.value must be an array", file=sys.stderr)
        return 1

    rows = []
    invalid = False
    for index, account in enumerate(values):
        if account is None:
            rows.append({"index": index, "status": "null"})
            continue
        if not isinstance(account, dict):
            rows.append({"index": index, "status": "invalid", "reason": "account must be an object or null"})
            invalid = True
            continue

        owner = account.get("owner")
        lamports = account.get("lamports")
        problems = []
        if not isinstance(owner, str) or not owner:
            problems.append("owner must be a non-empty string")
        if isinstance(lamports, bool) or not isinstance(lamports, int) or lamports < 0:
            problems.append("lamports must be a non-negative integer")

        if problems:
            rows.append({"index": index, "status": "invalid", "reason": "; ".join(problems)})
            invalid = True
        else:
            rows.append({"index": index, "status": "valid", "owner": owner, "lamports": lamports})

    print(json.dumps(rows, indent=2))
    return 1 if invalid else 0


if __name__ == "__main__":
    sys.exit(main())
What to verify after running
  • Run the synthetic example and confirm it prints indices 0, 1, and 2 in order, with index 1 marked null.
  • Try an empty value array and confirm the report is an empty JSON array.
  • Try malformed JSON and confirm an invalid-JSON diagnostic goes to stderr with a nonzero exit status.
  • Try a missing file and confirm a read error goes to stderr with a nonzero exit status.
  • Try a present account with a missing or empty owner, a negative lamports value, a floating-point lamports value, and a boolean lamports value; confirm each is marked invalid and the process exits nonzero.
  • Try a top-level array, absent result.value, or a response containing an error field; confirm each is rejected rather than reported as an account list.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-11.

    Executed 6 synthetic local fixture cases in isolated CPython 3.12.15 on GitHub Actions, without network access. Declared exit codes, output assertions and input integrity checks passed. Live services, installed third-party packages and production behavior remain outside scope. Editorial approval is still required.