sadoprotocol / utxo-api

Ordinal aware API wrapper ...
6 stars 2 forks source link

Enable support for multiple unspent lookups with one call #1

Open msmalley opened 1 year ago

msmalley commented 1 year ago

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 commented 1 year ago

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.