Efficiently Retrieve the Oldest Transaction Signature for a Solana Address with Billions of Transactions in Python

66 views Asked by At

I'm working on a project where I need to retrieve the oldest transaction signature for a given Solana address. However, I'm facing a challenge when dealing with addresses that have billions of transactions. My current code, which fetches signatures in batches, doesn't work efficiently for such addresses and could easily hit the rate limit.

Here's my current code:

import asyncio
from solana.rpc.async_api import AsyncClient
from typing import Optional
from solders.pubkey import Pubkey

SOLANA_RPC_URL = "https://api.mainnet-beta.solana.com/"  # Example RPC URL, adjust as necessary

async def get_oldest_transaction_signature(address: str) -> Optional[str]:
    async with AsyncClient(SOLANA_RPC_URL) as client:
        # Start with None to get the most recent signatures first
        before = None
        oldest_signature = None

        address = Pubkey.from_string(address)

        while True:
            # Fetch a batch of signatures
            signatures_response = await client.get_signatures_for_address(
                address, before=before, limit=1000
            )
            signatures = signatures_response.value
            if not signatures:
                break  # No more signatures found
            # Update the oldest signature
            oldest_signature = signatures[-1].signature
            # Set the 'before' parameter to the oldest signature in the current batch
            before = oldest_signature
    if oldest_signature:
        return str(oldest_signature)
    else:
        return None

# Usage example
async def main():
    address = "BXbmrFeRNUn13V5YYHnXe2Ws55H48hJuB7LyKqEyXLTh"  # Replace with the actual address
    oldest_signature = await get_oldest_transaction_signature(address)
    print(f"Oldest Transaction Signature: {oldest_signature}")

if __name__ == "__main__":
    asyncio.run(main())

The issue with this code is that it starts from the most recent signatures and keeps fetching batches of signatures until it reaches the oldest one. For addresses with billions of transactions, this approach is not feasible due to the large number of requests required and the potential of hitting the rate limit.

Is there any way to efficiently filter and retrieve only the first (oldest) transaction signature for a given address? I want my function to return the very oldest transaction signature without having to iterate through all the transactions.

NOTE that the given address is just a sample and does not contain billions of transactions but I am still getting rate limits because there are lots of transactions.

If we can just filter or sort it in descending order (oldest to latest).

Is there any function like that?

Any suggestions or alternative approaches would be greatly appreciated. Thank you in advance for your help!

0

There are 0 answers