Transaction History (Continued)

A more advanced implementation of the endpoint.

The endpoint we just saw in the previous page is paginated.

In this tutorial we'll learn how to retrieve more than 10 items using proper pagination.

The basic idea is to implement a loop that will keep iterating until hasNextPage is false and there are no further pages to call.

Here's an example of how you'd implement such a function:

const axios = require('axios');

const NOVES_API_KEY = process.env.NOVES_API_KEY;

async function getAllTransactions(chain, accountAddress, options = {}) {
    const baseURL = 'https://translate.noves.fi';
    let endpoint = `${baseURL}/evm/${chain}/txs/${accountAddress}`;
    let allTransactions = [];
    let hasNextPage = true;

    while (hasNextPage) {
        try {
            const response = await axios.get(endpoint, {
                params: options,
                headers: {
                    apiKey: NOVES_API_KEY,
                },
            });
            const data = response.data;

            allTransactions = allTransactions.concat(data.items);

            hasNextPage = data.hasNextPage;

            if (hasNextPage && data.nextPageUrl) {
                endpoint = data.nextPageUrl;
            } else {
                hasNextPage = false;
            }
        } catch (error) {
            console.error('Error fetching transactions:', error);
            return null;
        }
    }

    return allTransactions;
}

// Replace with the desired chain
const chain = 'eth';

// Replace with the target wallet address
const account = '0x9B1054d24dC31a54739B6d8950af5a7dbAa56815';

// Set startTimestamp and endTimestamp
const startTimestamp = 1674845277;
const endTimestamp = 1698328961;

// Optional parameters
const options = {
    viewAsAccountAddress: account,
    startTimestamp: startTimestamp,
    endTimestamp: endTimestamp,
    pageNumber: 1,
};

getAllTransactions(chain, account, options).then((data) => {
    console.log(JSON.stringify(data, null, 2));
});

Note how in this case we're setting time boundaries. You will want to do this always when iterating through multiple pages.

Note on performance:

In the context of a wallet application, you'll typically have a local cache for transactions that have already been fetched.

To make the transaction history functionality as performant (and cheap) as possible, make sure to only call the /txs endpoint with a startTimestamp or startBlock that matches the timestamp of the last transaction you already have cached.

This way you avoid re-fetching data you already have.

Note on perspective address and account abstraction wallets:

An important concept when classifying transactions with Translate API is that of perspective. You can learn more about it here.

The perspective address is set in the viewAsAccountAddress, and for most wallets it will simply be wallet address of the user that executes the transactions.

In a ERC 4337 (Account Abstraction) wallet, the transactions are executed by a bundler, but the viewAsAccountAddress that you will want to pass is the address of the sender in the userOp. This is the same as the contract address of the smart contract wallet for a particular user.

Translate API is fully compatible with ERC-4337 transactions. You can read our associated doc here.