-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add docs for solana ws and methods #1234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
victorbware
wants to merge
7
commits into
main
Choose a base branch
from
victor/add_solana_ws_docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f962e47
feat: add docs for solana ws and methods
victorbware 8cb84bf
fix: ws pricing
victorbware be18511
fix: linter
victorbware e38fafb
fix: contact
victorbware 868739c
fix: cleanup vote subscribe and repeating networks pannel
victorbware 3a1a9b6
fix: api key
victorbware 1e39020
fix: remove block slotupdate methods
victorbware File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...pi-reference/websockets/solana-subscription-api-endpoints/account-subscribe.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| --- | ||
| title: accountSubscribe | ||
| description: Subscribe to notifications when one Solana account's lamports or data change. | ||
| subtitle: Subscribe to notifications when one Solana account's lamports or data change. | ||
| slug: reference/account-subscribe | ||
| --- | ||
|
|
||
| The `accountSubscribe` method opens a stream that emits a notification any time the lamports or data of a specified account change. Pair it with [`accountUnsubscribe`](#unsubscribe) to stop receiving notifications. | ||
|
|
||
| # Parameters | ||
|
|
||
| * `pubkey`: `string` - Account pubkey, as a base-58 encoded string. | ||
|
|
||
| * `config` (optional): `object` - Configuration object containing: | ||
|
|
||
| * `commitment`: `string` - The commitment level to use. One of `processed`, `confirmed`, `finalized`. Defaults to `finalized`. | ||
| * `encoding`: `string` - Account data encoding. One of `base58`, `base64`, `base64+zstd`, `jsonParsed`. Defaults to `base58`. | ||
|
|
||
| # Request | ||
|
|
||
| <CodeGroup> | ||
| ```shell wscat | ||
| // initiate websocket stream first | ||
| wscat -c wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY --> | ||
|
|
||
| // then call subscription | ||
| {"jsonrpc":"2.0","id":1,"method":"accountSubscribe","params":["CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12",{"encoding":"jsonParsed","commitment":"finalized"}]} | ||
| ``` | ||
|
|
||
| ```javascript @solana/web3.js | ||
| import { Connection, PublicKey } from '@solana/web3.js' | ||
|
|
||
| const connection = new Connection( | ||
| 'https://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->', | ||
| { | ||
| wsEndpoint: 'wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->', | ||
| commitment: 'finalized' | ||
| } | ||
| ) | ||
|
|
||
| const accountPubkey = new PublicKey('CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12') | ||
|
|
||
| const subscriptionId = connection.onAccountChange( | ||
| accountPubkey, | ||
| (accountInfo, context) => { | ||
| console.log('Account update at slot', context.slot, { | ||
| lamports: accountInfo.lamports, | ||
| owner: accountInfo.owner.toBase58(), | ||
| dataLen: accountInfo.data.length | ||
| }) | ||
| }, | ||
| 'finalized' | ||
| ) | ||
|
|
||
| // To unsubscribe later: | ||
| // await connection.removeAccountChangeListener(subscriptionId) | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| # Result | ||
|
|
||
| <CodeGroup> | ||
| ```json result | ||
| // subscribe response | ||
| {"jsonrpc":"2.0","result":23784,"id":1} | ||
|
|
||
| // notification | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "method": "accountNotification", | ||
| "params": { | ||
| "result": { | ||
| "context": { "slot": 5199307 }, | ||
| "value": { | ||
| "data": ["11116bv5nS2h3y12kD1yUKeMZvGcKLSjQgX6BeV7u1FrjeJcKfsHRTPuR3oZ1EioKtYGiYxpxMG5vpbZLsbcBYBEmZZcMKaSoGx9JZeAuWf", "base58"], | ||
| "executable": false, | ||
| "lamports": 33594, | ||
| "owner": "11111111111111111111111111111111", | ||
| "rentEpoch": 635, | ||
| "space": 80 | ||
| } | ||
| }, | ||
| "subscription": 23784 | ||
| } | ||
| } | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| # Unsubscribe | ||
|
|
||
| Use `accountUnsubscribe` with the subscription id returned by `accountSubscribe` to cancel the stream. | ||
|
|
||
| * `subscription_id`: `number` - The subscription id to cancel. | ||
|
|
||
| <CodeGroup> | ||
| ```shell wscat | ||
| {"jsonrpc":"2.0","id":1,"method":"accountUnsubscribe","params":[subscription_id]} | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| ```json result | ||
| {"jsonrpc":"2.0","result":true,"id":1} | ||
| ``` | ||
|
|
||
| When using `@solana/web3.js`, call `connection.removeAccountChangeListener(subscriptionId)` instead of sending the raw JSON-RPC request. | ||
106 changes: 106 additions & 0 deletions
106
...t/api-reference/websockets/solana-subscription-api-endpoints/logs-subscribe.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| --- | ||
| title: logsSubscribe | ||
| description: Subscribe to Solana transaction log messages that match a log filter. | ||
| subtitle: Subscribe to Solana transaction log messages that match a log filter. | ||
| slug: reference/logs-subscribe | ||
| --- | ||
|
|
||
| The `logsSubscribe` method opens a stream that emits a notification any time a transaction is committed and its logs match the supplied filter. Pair it with [`logsUnsubscribe`](#unsubscribe) to stop receiving notifications. | ||
|
|
||
| # Parameters | ||
|
|
||
| * `filter`: filter criteria for log subscriptions. Accepts one of: | ||
|
|
||
| * `"all"` - subscribe to all transactions except simple vote transactions. | ||
| * `"allWithVotes"` - subscribe to all transactions including simple vote transactions. | ||
| * An object: `{ "mentions": ["<pubkey>"] }` - subscribe to transactions that mention exactly one of the provided base-58 encoded pubkeys. | ||
|
|
||
| * `config` (optional): `object` - Configuration object containing: | ||
|
|
||
| * `commitment`: `string` - The commitment level. One of `processed`, `confirmed`, `finalized`. Defaults to `finalized`. | ||
|
|
||
| # Request | ||
|
|
||
| <CodeGroup> | ||
| ```shell wscat | ||
| // initiate websocket stream first | ||
| wscat -c wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY --> | ||
|
|
||
| // then call subscription | ||
| {"jsonrpc":"2.0","id":1,"method":"logsSubscribe","params":[{"mentions":["11111111111111111111111111111111"]},{"commitment":"finalized"}]} | ||
| ``` | ||
|
|
||
| ```javascript @solana/web3.js | ||
| import { Connection, PublicKey } from '@solana/web3.js' | ||
|
|
||
| const connection = new Connection( | ||
| 'https://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->', | ||
| { | ||
| wsEndpoint: 'wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->', | ||
| commitment: 'finalized' | ||
| } | ||
| ) | ||
|
|
||
| const subscriptionId = connection.onLogs( | ||
| new PublicKey('11111111111111111111111111111111'), | ||
| (logs, context) => { | ||
| console.log('Logs at slot', context.slot, { | ||
| signature: logs.signature, | ||
| err: logs.err, | ||
| logs: logs.logs | ||
| }) | ||
| }, | ||
| 'finalized' | ||
| ) | ||
|
|
||
| // To unsubscribe later: | ||
| // await connection.removeOnLogsListener(subscriptionId) | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| # Result | ||
|
|
||
| <CodeGroup> | ||
| ```json result | ||
| // subscribe response | ||
| {"jsonrpc":"2.0","result":24040,"id":1} | ||
|
|
||
| // notification | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "method": "logsNotification", | ||
| "params": { | ||
| "result": { | ||
| "context": { "slot": 5208469 }, | ||
| "value": { | ||
| "signature": "5h6xBEauJ3PK6SWCZ1PGjBvj8vDdWG3KpwATGy1ARAXFSDwt8GFXM7W5Ncn16wmqokgpiKRLuS83KUxyZyv2sUYv", | ||
| "err": null, | ||
| "logs": [ | ||
| "Program 11111111111111111111111111111111 invoke [1]", | ||
| "Program 11111111111111111111111111111111 success" | ||
| ] | ||
| } | ||
| }, | ||
| "subscription": 24040 | ||
| } | ||
| } | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| # Unsubscribe | ||
|
|
||
| Use `logsUnsubscribe` with the subscription id returned by `logsSubscribe` to cancel the stream. | ||
|
|
||
| * `subscription_id`: `number` - The subscription id to cancel. | ||
|
|
||
| <CodeGroup> | ||
| ```shell wscat | ||
| {"jsonrpc":"2.0","id":1,"method":"logsUnsubscribe","params":[subscription_id]} | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| ```json result | ||
| {"jsonrpc":"2.0","result":true,"id":1} | ||
| ``` | ||
|
|
||
| When using `@solana/web3.js`, call `connection.removeOnLogsListener(subscriptionId)` instead of sending the raw JSON-RPC request. |
114 changes: 114 additions & 0 deletions
114
...pi-reference/websockets/solana-subscription-api-endpoints/program-subscribe.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| --- | ||
| title: programSubscribe | ||
| description: Subscribe to notifications for accounts owned by a Solana program. | ||
| subtitle: Subscribe to notifications for accounts owned by a Solana program. | ||
| slug: reference/program-subscribe | ||
| --- | ||
|
|
||
| The `programSubscribe` method opens a stream that emits a notification when the lamports or data of any account owned by a given program change. Pair it with [`programUnsubscribe`](#unsubscribe) to stop receiving notifications. | ||
|
|
||
| # Parameters | ||
|
|
||
| * `pubkey`: `string` - Program pubkey, as a base-58 encoded string. | ||
|
|
||
| * `config` (optional): `object` - Configuration object containing: | ||
|
|
||
| * `commitment`: `string` - The commitment level. One of `processed`, `confirmed`, `finalized`. Defaults to `finalized`. | ||
| * `encoding`: `string` - Account data encoding. One of `base58`, `base64`, `base64+zstd`, `jsonParsed`. Defaults to `base58`. | ||
| * `filters`: `array` (optional) - An array of filter objects (`memcmp` and/or `dataSize`) to narrow which program-owned accounts trigger notifications. | ||
|
|
||
| <Warning> | ||
| `programSubscribe` without filters can produce extremely high volumes of notifications. Use `dataSize` and `memcmp` filters to scope the stream to the accounts you care about. | ||
| </Warning> | ||
|
|
||
| # Request | ||
|
|
||
| <CodeGroup> | ||
| ```shell wscat | ||
| // initiate websocket stream first | ||
| wscat -c wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY --> | ||
|
|
||
| // then call subscription | ||
| {"jsonrpc":"2.0","id":1,"method":"programSubscribe","params":["TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",{"encoding":"jsonParsed","filters":[{"dataSize":165}]}]} | ||
| ``` | ||
|
|
||
| ```javascript @solana/web3.js | ||
| import { Connection, PublicKey } from '@solana/web3.js' | ||
|
|
||
| const connection = new Connection( | ||
| 'https://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->', | ||
| { | ||
| wsEndpoint: 'wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->', | ||
| commitment: 'finalized' | ||
| } | ||
| ) | ||
|
|
||
| const programId = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA') | ||
|
|
||
| const subscriptionId = connection.onProgramAccountChange( | ||
| programId, | ||
| ({ accountId, accountInfo }, context) => { | ||
| console.log('Program account update at slot', context.slot, { | ||
| account: accountId.toBase58(), | ||
| lamports: accountInfo.lamports, | ||
| owner: accountInfo.owner.toBase58() | ||
| }) | ||
| }, | ||
| 'finalized', | ||
| [{ dataSize: 165 }] | ||
| ) | ||
|
|
||
| // To unsubscribe later: | ||
| // await connection.removeProgramAccountChangeListener(subscriptionId) | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| # Result | ||
|
|
||
| <CodeGroup> | ||
| ```json result | ||
| // subscribe response | ||
| {"jsonrpc":"2.0","result":24040,"id":1} | ||
|
|
||
| // notification | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "method": "programNotification", | ||
| "params": { | ||
| "result": { | ||
| "context": { "slot": 5208469 }, | ||
| "value": { | ||
| "pubkey": "H4vnBqifaSACnKa7acsxstsY1iV1bvJNxsCY7enrd1hq", | ||
| "account": { | ||
| "data": ["11116bv5nS2h3y12kD1yUKeMZvGcKLSjQgX6BeV7u1FrjeJcKfsHRTPuR3oZ1EioKtYGiYxpxMG5vpbZLsbcBYBEmZZcMKaSoGx9JZeAuWf", "base58"], | ||
| "executable": false, | ||
| "lamports": 33594, | ||
| "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", | ||
| "rentEpoch": 636, | ||
| "space": 80 | ||
| } | ||
| } | ||
| }, | ||
| "subscription": 24040 | ||
| } | ||
| } | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| # Unsubscribe | ||
|
|
||
| Use `programUnsubscribe` with the subscription id returned by `programSubscribe` to cancel the stream. | ||
|
|
||
| * `subscription_id`: `number` - The subscription id to cancel. | ||
|
|
||
| <CodeGroup> | ||
| ```shell wscat | ||
| {"jsonrpc":"2.0","id":1,"method":"programUnsubscribe","params":[subscription_id]} | ||
| ``` | ||
| </CodeGroup> | ||
|
|
||
| ```json result | ||
| {"jsonrpc":"2.0","result":true,"id":1} | ||
| ``` | ||
|
|
||
| When using `@solana/web3.js`, call `connection.removeProgramAccountChangeListener(subscriptionId)` instead of sending the raw JSON-RPC request. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unsubscribe
wscatpayload is not valid JSON becausesubscription_idis emitted as an unquoted identifier ("params":[subscription_id]), so users who paste this directly into a WebSocket session will get a parse error instead of unsubscribing. This same pattern appears across the new Solana method pages, so these examples should use an actual numeric ID (for example,23784) or clearly-marked placeholder JSON that remains syntactically valid.Useful? React with 👍 / 👎.