How to get Paginated Transactions on Solana

Transaction records for any crypto wallet can range from a few hundred to a couple of million. This blog illustrates how we can get paginated transaction data on Solana using SHYFT APIs.

Transaction records for any crypto wallet can range from a few hundred to a couple of million. This blog illustrates how we can get paginated transaction data on Solana using SHYFT APIs.

Transactions on Solana, are a collection of atomic instructions which the Solana Runtime executes. A Transaction also contains an array of signatures, followed by a message. When signed and submitted to the Solana Runtime, it verifies the number of signatures that matches the message and then executes the instruction in a one-by-one fashion. Any operation performed on the Blockchain be it creating or minting tokens, performing marketplace trading or transferring tokens, is submitted in form of transactions to the Solana Runtime, and while building dApps we may also require to display a list of transactions that are taking place with respect to one particular user’s wallet. Contrary to the traditional and tedious transaction fetching method, SHYFT provides a very easy-to-use API-based solution for getting Transactions from one particular user wallet and paginating the transaction data. Let’s find out how we can achieve this using SHYFT APIs.

Read SHYFT API Documentation here.

Before getting started

To get started, we will need a few things.

Authentication: Getting your Shyft API key

x-api-key is an authentication parameter, which gives you access to SHYFT APIs. You can get your own API Key from the SHYFT website. Just signup with your email id here and you can get it for free.

Phantom Wallet

We will need the Phantom wallet browser extension, you can download it from the link below.

Chrome/Brave.

Firefox.

Once done, set up your Phantom wallet account. On-screen tips are available, which will guide you through setting up and getting started. You can also find a detailed guide related to this here.

Paginated Transactions

The number of transactions for any crypto wallet can range from a few tens to even a few million, depending upon the Web3 activity that the wallet owner performs or the wallet is associated with. Now, for any application which is attempting to display all the transaction data associated with a single wallet, fetching all the transactions at once may be time-consuming, and for a user who is viewing those records, it may be particularly difficult to locate one particular transaction from this huge heap of data. Thus, displaying paginated data, or listing the transactions data page-wise makes more sense.

But before fetching already paginated transactions, we will need a few details, such as the wallet address whose transactions we are about to fetch, the number of transactions to fetch at once, etc. This can be collected from the front end of the application using form fields or dropdowns. Here is a small example of how we have collected these details.

Press enter or click to view image in full size

Getting details of the wallet and number of transaction to be fetched

Getting Transaction Details: The API call

Once we have the wallet address and the cluster details, we are ready to make the API request for getting transactions. The SHYFT API endpoint:

GET https://api.shyft.to/sol/v1/wallet/transaction_history

This API accepts x-api-key parameter in the header, which is an authorization parameter used by SHYFT for validating its users. You can get your own **x-api-key** from the SHYFT website for free here.

Parameters required for this API call

  • network : Selects the Solana network cluster, which can be either devnet, testnet or mainnet-beta. The transactions from this cluster will be fetched.
  • wallet: Accepts the wallet address of the wallet whose Transactions are being fetched.
  • tx_num : Number of transactions to be fetched at once. This is an optional parameter that accepts a number, let’s say X, the API will fetch X number of transactions with the most recent being the first.
  • before_tx_signature : Tx signature before which X number of transactions will be fetched, with the latest one being at first.

This is a GET request so we will have to append the parameters to the endpoint itself.

https://api.shyft.to/sol/v1/wallet/transaction_history?network=devnet&wallet=YOUR_WALLET_ADDRESS&tx_num=5&before_tx_signature=TXN_FROM_WHICH_X_NO_OF_TXN_WILL_BE_FETCHED

Read SHYFT API Documentation here.

Once successfully executed, the response will return the number of transactions mentioned in tx_num and their details.

Please note that this is a sample response and a few fields have been trimmed down for better readability. You can view the entire response structure here.

{
    "success": true,
    "message": "Last 5 transaction fetched successfully",
    "result": [
        {
            "blockTime": 1668678102,
            "meta": {
                "err": null,
                "fee": 5000,
                "innerInstructions": [
                    {
                        "index": 0,
                        "instructions": [
                            {
                                "parsed": {
                                    "info": {
                                        "extensionTypes": [
                                            "immutableOwner"
                                        ],
                                        "mint": "BzxTNv26TbUYJSqiRQdJKzGrQoLHiykmyNi89mDE3aEg"
                                    },
                                    "type": "getAccountDataSize"
                                },
                                "program": "spl-token",
                                "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
                            },
                            {
                                "parsed": {
                                    "info": {
                                        "lamports": 2039280,
                                        "newAccount": "JBDkv1TutxEpGpUVVWa1V5wCvYk8QwZmy5ssHjvMyW8C",
                                        "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                                        "source": "7wgBXTi3HHFmBf1MUn7PWm3oYoQYSuUj1LGvT7k9fsPB",
                                        "space": 165
                                    },
                                    "type": "createAccount"
                                },
                                "program": "system",
                                "programId": "11111111111111111111111111111111"
                            },
                            .......... //Trimmed Down
                        ]
                    }
                ],
                "logMessages": [
                    "Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]",
                    ....
                    "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success"
                ],
                "postBalances": [
                    16992583360,
                    ........ //Trimmed Down
                ],
                "postTokenBalances": [
                    {
                        "accountIndex": 1,
                        "mint": "BzxTNvV6TbUYJSqiRQdJKzGrQoLHiykmyNi89mDE3aEg",
                        "owner": "7wgBXTi3HHFmBf1MUn7PWm3oYoQYSuUj1LGvT7k9fsPB",
                        "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                        "uiTokenAmount": {
                            "amount": "0",
                            "decimals": 0,
                            "uiAmount": null,
                            "uiAmountString": "0"
                        }
                    },
                    {
                        "accountIndex": 2,
                        "mint": "BzxTNvV6TbUYJSqiRQdJKzGrQoLHiykmyNi89mDE3aEg",
                        "owner": "HgQy5bqJd3GcjqakukhfMpqSjF7jEYxGiqAqh4QtTuHF",
                        "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                        "uiTokenAmount": {
                            "amount": "1",
                            "decimals": 0,
                            "uiAmount": 1,
                            "uiAmountString": "1"
                        }
                    }
                ],
                "preBalances": [
                    16994627640,
                    2039280,
                    0,
                    1,
                    731913600,
                    1461600,
                    339663400,
                    1009200,
                    934087680
                ],
                "preTokenBalances": [
                    {
                        "accountIndex": 1,
                        "mint": "BzxTNvV6TbUYJSqiRQdJKzGrQoLHiykmyNi89mDE3aEg",
                        "owner": "7wgBXTi3HHFmBf1MUn7PWm3oYoQYSuUj1LGvT7k9fsPB",
                        "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                        "uiTokenAmount": {
                            "amount": "1",
                            "decimals": 0,
                            "uiAmount": 1,
                            "uiAmountString": "1"
                        }
                    }
                ],
                "rewards": [],
                "status": {
                    "Ok": null
                }
            },
            "slot": 176154064,
            "transaction": {
                "message": {
                    "accountKeys": [
                        {
                            "pubkey": "7wgBXTi3HHFmBf1MUn7PWm3oYoQYSuUj1LGvT7k9fsPB",
                            "signer": true,
                            "source": "transaction",
                            "writable": true
                        },
                        .......... //Trimmed Down
                    ],
                    "addressTableLookups": null,
                    "instructions": [
                        {
                            "parsed": {
                                "info": {
                                    "account": "JBDkv1TutxEpGpUVVWa1V5wCvYk8QwZmy5ssHjvMyW8C",
                                    "mint": "BzxTNvV6TbUYJSqiRQdJKzGrQoLHiykmyNi89mDE3aEg",
                                    "source": "7wgBXTi3HHFmBf1MUn7PWm3oYoQYSuUj1LGvT7k9fsPB",
                                    "systemProgram": "11111111111111111111111111111111",
                                    "tokenProgram": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                                    "wallet": "HgQy5bqJd3GcjqakukhfMpqSjF7jEYxGiqAqh4QtTuHF"
                                },
                                "type": "create"
                            },
                            "program": "spl-associated-token-account",
                            "programId": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
                        },
                        .......... //Trimmed Down
                    ],
                    "recentBlockhash": "9puBn1WkPMAv2hQG8D4aEH2i4YjGiSBexYb8J6MFMbUg"
                },
                "signatures": [
                    "2tF8HmsvRYooui2r38iLc9Bq2LDM8KXLie5STPTuHVXVW4H14pbnrMVrvj8C8oHVAei5WYRDJfzpgPirSXvBHSUc"
                ]
            }
        },
  }
 }
]

The result field in the response will contain the array of transactions and their details. To know more about the response structure, or the details of this API call, please refer to our dev docs here.

The list of transactions can be presented to the front-end user along with the required fields in the following manner, and can also be displayed filtered from the front end.

Displaying a Paginated List of Transactions

Bonus Tip: The Previous and Next Button

We know we need the before_tx_signature in order to fetch the transactions older than the current batch of transactions fetched. Now for fetching transactions using the next and previous buttons, we have to keep track of the last transaction fetched in the current batch. Now, this can be done in several ways, one of which can be using a stack data structure. Every time we fetch a batch of transactions using the API, we can push the signature of the last(oldest) transaction fetched in the stack. The next button would require us to fetch the transaction older than the tx_signature at the top of the stack. And the previous button would require us to pop out an element from the stack and then fetch the transactions older than the tx_signature at the top of the stack.

So that’s almost everything about getting and displaying paginated transactions using SHYFT APIs. If you liked this blog, don’t forget to check out our other blogs on Transferring multiple NFTs on Solana or Creating and minting tokens on Solana using SHYFT APIs. We hope you like the experience of building dApps on Solana using SHYFT APIs. Happy Hacking!

Resources

SHYFT API Documentation

Shyft Website

Get API Key

GitHub

Join our Discord

Try out our APIs on Swagger UI