getcompressedaccount
Retrieve compressed account information by address or hash. RPC method guide with use cases, tips and examples.
The getCompressedAccount RPC method retrieves information about a specific compressed account using either its address or hash. Information includes the compressed account state, balance, and metadata.
An ID to identify the request.
The version of the JSON-RPC protocol.
The name of the method to invoke.
Invalid request.
Unauthorized request.
Request was forbidden.
The specified resource was not found.
Exceeded rate limit.
The server encountered an unexpected condition that prevented it from fulfilling the request.
POST /getCompressedAccount HTTP/1.1
Host: mainnet.helius-rpc.com?api-key=<api_key>
Content-Type: application/json
Accept: */*
Content-Length: 137
{
"id": "test-account",
"jsonrpc": "2.0",
"method": "getCompressedAccount",
"params": {
"address": null,
"hash": "11111111111111111111111111111111"
}
}{
"context": {
"slot": 100
},
"value": null
}Common Use Cases
Checking Compressed Account Balance: Retrieve the lamport balance of any compressed account.
Verifying Account Existence: Check if a compressed account exists and has been initialized with data or lamports.
Inspecting Compressed Account Data: Access the stored data within a compressed account, including program-specific state.
Identifying Account Owner: Determine which program owns a compressed account to understand how its data should be interpreted.
Merkle Tree Information: Get the merkle tree context including leaf index and sequence number for proof generation.
Parameters
address(string, optional): The base-58 encoded address of the compressed account to query.hash(string, optional): The base-58 encoded 32-byte hash of the compressed account.
Note: Either address OR hash must be provided, but not both. If neither is provided, the request will fail.
Response
Returns compressed account information directly, or null if account not found:
hash(string): The 32-byte hash of the compressed account as a base-58 string.owner(string): The base-58 encoded public key of the program that owns this account.lamports(number): The number of lamports owned by the account.tree(string): The public key of the merkle tree storing this account.leafIndex(number): The leaf index of this account in the merkle tree.seq(number): The sequence number of this account.slotCreated(number): The slot when this account was created.data(object, optional): The account data object containing:discriminator(BN): The account discriminator as BN object.data(string): Base64 encoded account data.dataHash(string): The 32-byte hash of the account data.
Developer Tips
Address vs Hash: Use
addresswhen you know the account's derived address, andhashwhen you have the specific hash from a previous query or transactionHash Format: When using the
hashparameter, pass the hash value directly as returned from other RPC methods (likegetCompressedAccountsByOwner). Hash values must be used immediately after retrieval for accurate resultsError Handling: Always check if the returned value is
nullbefore accessing account properties, as the account may not existMerkle Context: The returned
leafIndex,tree, andseqfields are crucial for generating merkle proofs when updating or transferring compressed accountsData Interpretation: The
datafield contains program-specific information. Use thediscriminatorto identify the account type and parse thedataaccordinglyRate Limits: Be mindful of RPC rate limits when making frequent queries. Consider batching requests when possible
Hash Verification: The
dataHashfield allows you to verify the integrity of the account data without storing the full data on-chain
Examples
The below examples work - just make sure you installed the dependencies.
Example: Fetching Compressed Account by Address
Let's fetch information for a compressed account using its address.
curl -X POST https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getCompressedAccount",
"params": {
"address": "11111113R2cuenjG5nFubqX9Wzuukdin2YfGQVzu5"
}
}'import { Rpc, createRpc, createBN254, BN254, CompressedAccountWithMerkleContext } from '@lightprotocol/stateless.js';
async function getCompressedAccountInfo(): Promise<void> {
const rpc: Rpc = createRpc(
'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY',
'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY'
);
try {
// Query by address
const address: BN254 = createBN254('11111113R2cuenjG5nFubqX9Wzuukdin2YfGQVzu5', 'base58');
const accountInfo: CompressedAccountWithMerkleContext | null =
await rpc.getCompressedAccount(address);
if (!accountInfo) {
console.log('Compressed account not found.');
return;
}
console.log('Compressed Account Info:', {
hash: accountInfo.hash,
owner: accountInfo.owner,
lamports: accountInfo.lamports,
tree: accountInfo.tree,
leafIndex: accountInfo.leafIndex,
sequence: accountInfo.seq,
slotCreated: accountInfo.slotCreated,
dataHash: accountInfo.data?.dataHash,
});
} catch (error: unknown) {
console.error('Error fetching compressed account:', error);
}
}
getCompressedAccountInfo();// Current API: light-client 0.14.0
use light_client::rpc::LightClient;
use solana_sdk::bs58;
use anyhow::Result;
#[tokio::main]
async fn main() -> Result<()> {
let client = LightClient::new("https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY".to_string()).await?;
// Query compressed account by address
let address = "11111113R2cuenjG5nFubqX9Wzuukdin2YfGQVzu5";
match client.get_compressed_account(Some(address), None).await? {
Some(account_info) => {
println!("Compressed Account Info:");
println!(" Hash: {}", account_info.hash);
println!(" Owner: {}", account_info.owner);
println!(" Lamports: {}", account_info.lamports);
println!(" Tree: {}", account_info.tree);
println!(" Leaf Index: {}", account_info.leaf_index);
}
None => {
println!("Compressed account not found.");
}
}
Ok(())
}Example: Fetching Compressed Account by Hash
You can also query a compressed account using its hash.
import { Rpc, createRpc, bn } from '@lightprotocol/stateless.js';
import { PublicKey } from '@solana/web3.js';
async function getCompressedAccountByHash(): Promise<void> {
const rpc: Rpc = createRpc(
'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY',
'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY'
);
try {
// First, get compressed accounts to obtain a real hash
const owner = new PublicKey('4LhEEtzAhM6wEXJR2YQHPEs79UEx8e6HncmeHbqbW1w1');
const accounts = await rpc.getCompressedAccountsByOwner(owner);
if (accounts.items.length === 0) {
console.log('No compressed accounts found');
return;
}
// Query by hash using a real hash from the first account
const hash = bn(accounts.items[0].hash);
const accountInfo = await rpc.getCompressedAccount(undefined, hash);
if (accountInfo) {
console.log('Compressed Account Info:', {
hash: accountInfo.hash,
owner: accountInfo.owner,
lamports: accountInfo.lamports,
tree: accountInfo.tree,
leafIndex: accountInfo.leafIndex
});
} else {
console.log('Compressed account not found.');
}
} catch (error: unknown) {
console.error('Error:', error);
}
}
getCompressedAccountByHash();Last updated
Was this helpful?