4

development environment : C#, Windows

Connecting to solana-mainnet web server using wss address.

        wss = new WebSocketSharp.WebSocket(this.Address);
        wss.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;

        wss.OnMessage += (sender, e) =>
        {
            if (e.IsBinary)
            {
                HandleMessage(Opcode.Binary, e.RawData);
            }
            else if(e.IsText)
            {
                HandleMessage(Opcode.Text, e.RawData);
            }
        };
        wss.OnOpen += (sender, e) =>
        {
            HandleOpen();
        };
        wss.OnError += (sender, e) =>
        {
            HandleError(e.Exception);
        };
        wss.OnClose += (sender, e) =>
        {
            HandleClose(e.Code, e.Reason);
        };

OnMessage occurs when the solana balance changes. (At this time, I check solana balance.)

However, if there is a change in the spl-token balance, there is no event.

Therefore, I am periodically checking the spl-token.

This is very inefficient

how to detect spl-token balance change?

Best regards,

0

1 Answer 1

3

Solana JSON RPC API offers a way to subscribe to an account to receive notifications when the Lamports or data for a given account's public key changes.

https://docs.solana.com/developing/clients/jsonrpc-api#accountsubscribe

This includes any type of account, a token account is basically a data account initialized with a specific standard.

https://github.com/solana-labs/solana-program-library/blob/master/token/program/src/state.rs#L83

Here's an example of subscribing to CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12. It doesn't matter what type of account it is.

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "accountSubscribe",
  "params": [
    "CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12",
    {
      "encoding": "base64",
      "commitment": "finalized"
    }
  ]
}

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.