hirosystems / stacks-blockchain-api

API for the Stacks blockchain
https://stacks-blockchain-api.vercel.app
GNU General Public License v3.0
171 stars 108 forks source link

total transactions reported as 0 when an offset is used #2061

Closed rafaelcr closed 3 days ago

rafaelcr commented 2 weeks ago

Example: https://api.hiro.so/extended/v2/addresses/SP1JX8274K2R0MMP1VCX56C5DCERPP6EH5XHS68MR/transactions?limit=30&offset=30

{
  "limit": 30,
  "offset": 30,
  "total": 0,
  "results": [

  ]
}

When offset= is removed:

{
  "limit": 30,
  "offset": 0,
  "total": 30,
  "results": [
    {
      "tx": {
rafaelcr commented 2 weeks ago

Confirmed in v8.0.0

crypt0jan commented 2 weeks ago

Some context:

I came across this issue while coding a script that updates a local JSON file holding all the transactions of our Pool Operator. The script checks how many transactions I already saved in a JSON and how many are left to fetch. But after I downloaded 30 transactions, I was missing the latest 3 transactions I broadcasted. Upon debugging, I noticed that the total value changes from 30 to 0 if I add any offset parameter to the request.

It would be great to always show the total amount of transactions so developers can use a while loop to fetch up until total has been reached.

let lastTxs = [];
let total = 0;
let limitLastTxs = 10;
let totalSavedTxs = jsonTxData.transactions.length;
let offsetLastTxs = jsonTxData.transactions.length;

while (total <= totalSavedTxs) {
    try {
        await fetch(
            'https://api.hiro.so/extended/v2/addresses/' + 
            poolAddress + '/transactions?limit=' + limitLastTxs + 
            '&offset=' + offsetLastTxs, {
                method: 'get',
                headers: { 'Content-Type': 'application/json' }
            }
        )
        .then((response) => response.json())
        .then((data) => {
            //console.log('data', data);
            // Concatenate results with the previous fetch
            lastTxs = lastTxs.concat(data.results);
            // Update total
            total = data.total;
            // Update offset
            offsetLastTxs = (lastTxs.length + totalSavedTxs);
        });

        // Break if it is still totalSavedTxs === total
        if (total === totalSavedTxs || total === 0) break;
    } catch (e) {
        console.error('Could not fetch transactions from HIRO API.');
        throw new Error(e);
    }
}