The accountSubscribe
method in Solana allows developers to receive real-time notifications about changes to a specific account on the Solana blockchain. This subscription-based method provides updates whenever the monitored account's data is modified.
Parameters
publicKey
- The public key of the account to monitor, as a base-58 encoded string<commitment>
- (optional) The level of commitment desired when querying state. Default is "finalized".encoding
- (optional) Data encoding for the account data:"base58"
- Base-58 encoding (default)"base64"
- Base-64 encoding"base64+zstd"
- Compressed Base-64 encoding"jsonParsed"
- JSON format (if program supports it)
Response
result
- An integer subscription id (needed to unsubscribe)subscription
- An object containing account information:lamports
- Number of lamports assigned to the accountowner
- Base-58 encoded public key of the program this account has been assigned todata
- Account data, encoded according to specified encoding parameterexecutable
- Boolean indicating if the account contains a programrentEpoch
- The epoch at which this account will next owe rent
accountSubscribe
code examples
accountSubscribe
code examplesThis example subscribes to changes in a token account.
Note that subscriptions require a WebSocket connection and WebSocket cat for you to use this method in the console.
Install WebSocket cat with:
npm install -g wscat
$ wscat -c YOUR_CHAINSTACK_WEBSOCKET_ENDPOINT
# Wait for the connection to be established
Connected (press CTRL+C to quit)
> {"jsonrpc":"2.0","id":1,"method":"accountSubscribe","params":["39azUYFWPz3VHgKCf3VChUwbpURdCHRxjWVowf5jUJjg",{"encoding":"jsonParsed","commitment":"finalized"}]}
Use case
The accountSubscribe
method in Solana is essential for applications that need to monitor account state changes in real-time. Common use cases include:
- Token tracking: Monitor token account balances for changes to update user interfaces instantly
- Program monitoring: Track program-owned accounts to detect state changes in smart contracts
- Wallet integration: Keep wallet balances and account states synchronized with the blockchain
- DeFi applications: Monitor liquidity pools, lending positions, or other DeFi-related accounts
- Gaming: Track in-game asset accounts and player state changes in real-time
By subscribing to account changes, applications can provide responsive user experiences without constantly polling the blockchain for updates. This is particularly useful for applications requiring real-time updates like trading interfaces, gaming platforms, or wallet applications.