0.1.47 • Published 1 year ago

@arcium-hq/reader v0.1.47

Weekly downloads
-
License
GPL-3.0-only
Repository
github
Last release
1 year ago

Arcium Reader SDK

The Arcium Reader SDK is a TypeScript library designed for passively observing and querying the state of the Arcium program on Solana. It allows you to fetch information about Arcium accounts and track the status of computations without requiring a signer.

Installation

npm install @arcium-hq/reader

or

yarn add @arcium-hq/reader

or

pnpm add @arcium-hq/reader

Usage

Getting the Read-only Program

To interact with the reader functions, you first need a read-only instance of the Arcium Anchor program. This doesn't require a wallet or signer.

import * as anchor from "@coral-xyz/anchor";
import { getArciumProgramReadonly } from "@arcium-hq/reader";

// Assuming you have an AnchorProvider configured (e.g., from anchor.AnchorProvider.env())
const provider = anchor.getProvider();
const arciumProgram = getArciumProgramReadonly(
  provider as anchor.AnchorProvider
);

Fetching Account Information

The reader SDK provides functions to fetch and deserialize various Arcium account types.

1. Fetching MXE Accounts:

import { getMXEAccAddresses, getMXEAccInfo } from "@arcium-hq/reader";

// Get all MXE account public keys
const mxeAccPubkeys = await getMXEAccAddresses(provider.connection);

// Get detailed info for a specific MXE account
if (mxeAccPubkeys.length > 0) {
  const mxeAccInfo = await getMXEAccInfo(arciumProgram, mxeAccPubkeys[0]);
  console.log("MXE Account Info:", mxeAccInfo);
  // Access fields like mxeAccInfo.computationDefinitions, mxeAccInfo.cluster, etc.
}

2. Fetching Cluster Accounts:

import { getClusterAccAddresses, getClusterAccInfo } from "@arcium-hq/reader";

// Get all Cluster account public keys
const clusterAccPubkeys = await getClusterAccAddresses(provider.connection);

// Get detailed info for a specific Cluster account
if (clusterAccPubkeys.length > 0) {
  const clusterInfo = await getClusterAccInfo(
    arciumProgram,
    clusterAccPubkeys[0]
  );
  console.log("Cluster Account Info:", clusterInfo);
  // Access fields like clusterInfo.mxes, clusterInfo.nodes, etc.
}

3. Fetching ArxNode Accounts:

import { getArxNodeAccAddresses, getArxNodeAccInfo } from "@arcium-hq/reader";

// Get all ArxNode account public keys
const arxNodeAccPubkeys = await getArxNodeAccAddresses(provider.connection);

// Get detailed info for a specific ArxNode account
if (arxNodeAccPubkeys.length > 0) {
  const arxNodeInfo = await getArxNodeAccInfo(
    arciumProgram,
    arxNodeAccPubkeys[0]
  );
  console.log("ArxNode Account Info:", arxNodeInfo);
  // Access fields like arxNodeInfo.clusterMemberships, etc.
}

Tracking Computation Status

You can subscribe to events related to a specific MXE (identified by its program ID) to track the lifecycle of computations it processes.

import {
  subscribeComputations,
  unsubscribeComputations,
  ArciumEventData,
  ArciumEventName,
} from "@arcium-hq/reader";
import { PublicKey } from "@solana/web3.js";

// Assuming `mxeProgramId` is the PublicKey of the MXE program you want to monitor
const mxeProgramId = new PublicKey("YOUR_MXE_PROGRAM_ID");

const eventLog = [];

// Define a callback function to handle events
const eventCallback = (
  eventData: ArciumEventData,
  eventName: ArciumEventName
) => {
  console.log(`Received event: ${eventName}`, eventData);
  eventLog.push({ event: eventData, name: eventName });
};

// Start subscribing
console.log(`Subscribing to computation events for ${mxeProgramId.toBase58()}`);
const subscriptionId = await subscribeComputations(
  connection,
  mxeProgramId,
  eventCallback
);

console.log(`Subscription active with ID: ${subscriptionId}`);

// ... later, when you want to stop listening ...

// Unsubscribe
await unsubscribeComputations(connection, subscriptionId);
console.log(`Unsubscribed from computation events.`);