Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rafaelcr opened this issue Aug 26, 2024 · 8 comments · Fixed by #2073
Open

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

rafaelcr opened this issue Aug 26, 2024 · 8 comments · Fixed by #2073
Assignees
Labels

Comments

@rafaelcr
Copy link
Collaborator

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 rafaelcr added the bug Something isn't working label Aug 26, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in API Board Aug 26, 2024
@rafaelcr
Copy link
Collaborator Author

Confirmed in v8.0.0

@crypt0jan
Copy link

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);
    }
}

@andresgalante andresgalante moved this from 🆕 New to 📋 Backlog in API Board Sep 3, 2024
@andresgalante andresgalante assigned rafaelcr and tippenein and unassigned rafaelcr Sep 3, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in API Board Sep 9, 2024
@blockstack-devops
Copy link

🎉 This issue has been resolved in version 8.1.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@blockstack-devops
Copy link

🎉 This issue has been resolved in version 8.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@crypt0jan
Copy link

This issue was not entirely solved, unfortunately. See attached pictures more info.

Basically, when I have 5 transactions but request transactions with an offset of 4, I see 1 transaction (good) and a total count of 5 (good). But when I use an offset of 5, the total drops to 0 (not good, should remain 5).

Image
Image

@tippenein
Copy link
Collaborator

Thanks @crypt0jan. It looks like the total will need to be moved out of the results query so we don't lose it when it's empty. I'll try to get to this soon, but I've reopened this issue for now

@crypt0jan
Copy link

Thanks @crypt0jan. It looks like the total will need to be moved out of the results query so we don't lose it when it's empty. I'll try to get to this soon, but I've reopened this issue for now

Just know that changing it to always showing the total will be a breaking change in apps that now rely on "until total === 0".

Current pseudo code:
If savedTransactions is not total, keep fetching until total is 0.

Future pseudo code:
If savedTransactions is not total, keep fetching

@smcclellan
Copy link
Contributor

Thanks @crypt0jan. We won't consider this a breaking change that requires a major version bump as it's fixing a bug, rather than changing previously intended behavior. Put another way, no documentation update is needed for this as the prior behavior wasn't previously specified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants
@rafaelcr @tippenein @crypt0jan @smcclellan @blockstack-devops and others