Skip to content

Conversation

@Shubbu03
Copy link

Problem

The Gill React library lacked a convenient way for developers to monitor program logs in real-time. Developers needed to manually set up RPC subscriptions and manage the complex lifecycle of WebSocket connections to watch for program-specific logs, which required significant boilerplate code and error handling.

Summary of Changes

Added useWatchProgramLogs React hook in packages/react/src/hooks/program-logs.ts

  • Provides real-time monitoring of Solana program logs using RPC subscriptions
  • Supports filtering by log type (all, error, success)
  • Configurable commitment level and maximum log items
  • Automatic cleanup and error handling
  • Returns logs array, error state, connection status, and clear function

Key Features:

  • Uses logsSubscribe with program mention filter for efficient log monitoring
  • Implements proper subscription lifecycle management with cleanup
  • Provides TypeScript types for ProgramLog with signature, error, logs, and slot
  • Handles subscription errors gracefully with user-friendly error messages
  • Maintains a rolling buffer of logs with configurable maximum items
  • Integrates seamlessly with existing useSolanaClient hook

Fixes

Issue #280

@changeset-bot
Copy link

changeset-bot bot commented Sep 27, 2025

⚠️ No Changeset found

Latest commit: e0c6d31

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Shubbu03 Shubbu03 marked this pull request as ready for review October 2, 2025 10:49
Copy link
Collaborator

@tobeycodes tobeycodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: can we use react query for storage of logs? https://tkdodo.eu/blog/using-web-sockets-with-react-query

question: does this handle deduplication when the hook is used twice at the same time and shared cache?

request: this is not done use program logs watcher in core

try {
// Use Solana logs subscription with a program mention filter
// @solana/kit provides logsSubscribe.
const sub = (rpcSubscriptions as any).logsSubscribe(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request: can we remove any?


export type ProgramLog = {
signature: string;
err: unknown | null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request: can we improve this type?


export function useWatchProgramLogs(
programId: Address | string | undefined,
opts: { filter?: LogFilter; commitment?: Commitment; maxItems?: number } = {},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request: lets call this options and move the types to a type expression above the function

try {
unsubscribeRef.current();
} catch {
// ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why do we have a try/catch and then ignore the error?

};
}, [programId, commitment, filter, maxItems, rpcSubscriptions]);

return { logs, error, clear, isConnected } as const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request: do not use as const. this should be typed correctly

@catmcgee
Copy link
Collaborator

@Shubbu03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants