3

I'm trying to use the Solana RPC to listen to incoming transactions on multiple accounts at once. The logsSubscribe documentation states that an object with an array of Pubkeys (as base-58 encoded strings) can be set as filter. I have tried this but whenever using an array with more than one public key, it gives the error 'Invalid Request: Only 1 address supported'. The error seems to be obvious, but reading the documentation you should be able to use multiple public keys.

If there's no way to listen to account changes for multiple accounts using logsSubscribe then is there a different method?

The RPC request:

    let subscription = JSON.stringify({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "logsSubscribe",
        "params": [{
            "mentions": ["addressOne", "addressTwo"]
        }, {
            "commitment": "finalized"
        }]
    })

    socket.send(subscription);

The output:

{
  jsonrpc: '2.0',
  error: {
    code: -32602,
    message: 'Invalid Request: Only 1 address supported'
  },
  id: 1
}

2 Answers 2

1

If you want to listen to multiple accounts, you'll have to do it through multiple different subscriptions, since RPC only supports one at a time currently. The interface was likely written to be as generic as possible, but was only wired up to manage one key per request.

Note that logsSubscribe isn't listening to changes to accounts, just mentions of the account. If you want to listen to changes, you'll need to use accountSubscribe.

Either way, the documentation should probably be updated to specify that only one pubkey is supported in logsSubscribe.

0

I experienced another issue with the following code (it printed both addresses, so notifying me that it called both onAccountChange methods), but it stuck, and none of them had been called when one of the accounts had a change.

const accounts = [
    new PublicKey("B91...vN8"),
    new PublicKey("DCw...EWc"),
]
accounts.forEach((account) => {
    console.log("watching", account.toBase58());
    connection.onAccountChange(account, () => console.log("change detected in", account.toBase58()));
});

So, to fix this issue, I had to create other connection objects (even with identical arguments).

const getConnection = () => new Connection(clusterApiUrl("mainnet-beta"), "confirmed");
accounts.forEach((account) => {
    console.log("watching", account.toBase58());
    getConnection().onAccountChange(account, () => console.log("change detected in", account.toBase58()));
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.