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

Enable support for multiple unspent lookups with one call #1

Open
msmalley opened this issue Jun 3, 2023 · 1 comment
Open

Enable support for multiple unspent lookups with one call #1

msmalley opened this issue Jun 3, 2023 · 1 comment

Comments

@msmalley
Copy link

msmalley commented Jun 3, 2023

Is it feasible to enable endpoints such as unspents to accept an array or object of addresses and then return the results of all those within the object / array ?

@AFwcxx
Copy link
Contributor

AFwcxx commented Jun 7, 2023

Perhaps this can be achieved with non-blocking concurrency from the browser side?

Have a look at the following working example:

const url = "https://regtest.ordit.io/utxo/unspents";
const addresses = ["bcrt1plsnd7g7f5pgz4fu54wu75vtt7e88lpfmh5evfr96cra7zmdcx7vq8yyajy", "bcrt1p0y99hhswdade65t3hpgezjcls45q9nqual60fzpa2fv90cx66adse4ckla", "bcrt1p9pqvjdvdsmhwp7a2ydvph4k962n7ng9v2e3w0yfdc03wft3jf68qtfxpwy"];
const options = {
  txhex: true
};

async function fetch_unspents(url, address, options) {
  const response = await fetch(url, {
    headers: {
      "Content-Type": "application/json"
    },
    method: "POST",
    body: JSON.stringify({ address, options })
  });
  let result = await response.json();

  if (result.success) {
    return result.rdata;
  }

  return [];
}

let promises = [];

addresses.forEach(v => {
  promises.push(fetch_unspents(url, v, options));
});

// Result
Promise.all(promises).then(console.log).catch(console.error);

The result will be in the order of the addresses you have in the array.

Let me know if it works for your goal.

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

No branches or pull requests

2 participants