Search code examples
solana

API for TPS and other cluster stats?


Is there any API that I can call to get the current cluster stats for mainnet or any cluster in fact?

I saw on the Solana Explorer source code that they are calculating it manually, but I was kinda hoping there would be an API as well?

https://github.com/solana-labs/solana/blob/master/explorer/src/components/TpsCard.tsx


Solution

  • In your case you'll want https://docs.solana.com/developing/clients/jsonrpc-api#getrecentperformancesamples, and you can even see how it's being used in the Solana Explorer.

    With curl, you can do:

    curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
      {"jsonrpc":"2.0", "id":1, "method":"getRecentPerformanceSamples", "params": [1]}
    '
    

    Which gives:

    {
      "jsonrpc": "2.0",
      "result": [
        {
          "numSlots": 126,
          "numTransactions": 126,
          "samplePeriodSecs": 60,
          "slot": 348125
        }
    }
    

    Then you can figure out TPS by doing numTransactions / samplePeriodSecs.

    Reference code at https://github.com/solana-labs/solana/blob/6d1b6bdd7cff9a4404d00811825493ed5ac1b074/explorer/src/providers/stats/solanaClusterStats.tsx#L105