Subscribing to Accounts

Subscribe requests related to streaming real-time account updates

To receive account updates we use the accounts filter of the Subscribe request. The account filter has the following structure.

{
  "slots": {},
  "accounts": {
      "accountLabel": {
        "account": string[], //account updates for these accounts 
        "owner": string[], //account updates for account with these owners
	"filters": {
	  "memcmp": {bytes and offset}  | undefined;
	  "datasize": string | undefined;
          "tokenAccountState": boolean | undefined;
        } 
  },
  "transactions": {},
  "blocks": {},
  "blocksMeta": {},
  "accountsDataSlice": [],
  "commitment": CommitmentLevel.CONFIRMED //commitment level, can be finalized or processed as well
}

Similar to the transactions filter, the first field under the accounts filter is the accountLabel, a user-defined label that helps you identify updates, especially when working with multiple filters. The account field specifies an array of account addresses for streaming account updates. The owner field streams updates for accounts owned by the specified addresses. Additionally, you can refine the data further by adding filters based on memcmp values, such as bytes and dataSize.

The "key" at the start of all filters, (e.g. accountLabel, txnLabel) is a client-assigned label and can be set to any user-defined names.

Subscribe to account updates for a program

{
  "slots": {},
  "accounts": {
    "pumpfun": {
      "owner": ["6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P"] //account updates will be streamed for accounts with this owner
    }
  },
  "transactions": {},
  "blocks": {},
  "blocksMeta": {},
  "accountsDataSlice": [],
  "commitment": CommitmentLevel.PROCESSED
}

To stream all account updates for a program, use the account field under the accounts filter. The owner field should specify the program's address, as all accounts belonging to the program are owned by the program address itself.

Subscribe to account updates of an address

{
  "slots": {
    "slots": {}
  },
  "accounts": {
    "sol/usdc": {
      "account": ["9AnFgHoXFysVcuFFX7QztDmzuH8r5ZFvyP4sYwn1XTj9"] // pool address when streaming pool updates
    }
  },
  "transactions": {},
  "blocks": {},
  "blocksMeta": {},
  "accountsDataSlice": [],
  "commitment": CommitmentLevel.CONFIRMED
};

The account array accepts the pool address for which updates are being streamed. This is especially useful when monitoring the state of specific accounts, such as those in liquidity pools, to track changes in real time.

Subscribe to account updates using Memory compare (memcmp)

memcmp filters allow you to match specific portions of binary data within accounts, helping you include only those account updates that meet specific criteria. This makes them especially useful for targeting specific states or values in program-owned accounts. For instance, you can use a memcmp filter to track liquidity pool balances, monitor token ownership, or identify program-specific flags. A memcmp filter typically specifies three key parameters: offset, which defines the starting byte position in the account data to compare; bytes, the value to match at the specified offset; and Encoding (optional), which determines how the bytes are encoded, such as base64 or base58.

{
  "slots": {},
  "accounts": {
    "raydium": {
      "account": [],
      "filters": [
        {
          "memcmp": {
            "offset": LIQUIDITY_STATE_LAYOUT_V4.offsetOf('marketProgramId').toString(), 
            "base58": "srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX"
          }
        }
      ],
      "owner": ["675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"] 
    }
  },
  "transactions": {},
  "blocks": {},
  "blocksMeta": {
    "block": []
  },
  "accountsDataSlice": [],
  "commitment": CommitmentLevel.PROCESSED,
  "entry": {},
  "transactionsStatus": {}
}

For instance, this subscribe request streams updates for Raydium pool accounts with a marketProgramId equal to serum. The filter targets the marketProgramId field of the account, ensuring updates are streamed only for matching accounts.

Last updated