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

bulk request txs for array of addresses #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion dashsight.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
/** @typedef {import('./').GetTxs} GetTxs */
/** @typedef {import('./').GetUtxos} GetUtxos */
/** @typedef {import('./').GetAllUtxos} GetAllUtxos */
/** @typedef {import('./').GetAllTxs} GetAllTxs */
/** @typedef {import('./').GetPageTxs} GetPageTxs */
/** @typedef {import('./').ToCoreUtxo} ToCoreUtxo */
/** @typedef {import('./').ToCoreUtxos} ToCoreUtxos */

Expand Down Expand Up @@ -166,6 +168,58 @@
return addrs
};

/** @type {GetPageTxs} */
insight.getPageTxs = async function (
addresses, from = 0, to = 50, txs = []
) {
let addrs = insight._joinAddrs(addresses);

let txsUrl = `${insightBaseUrl}/addrs/txs`;
let txsResp = await Dashsight.fetch(txsUrl, {
method: 'POST',
body: {
// @ts-ignore
addrs,
from,
to,
},
});

let txrj = await txsResp.json();

return txrj;
};

/** @type {GetAllTxs} */
insight.getAllTxs = async function (
addresses, from = 0, to = 50, txs = []
) {
let txrj = await insight.getPageTxs(
addresses,
from,
to,
txs,
);

/** @type Array<InsightTx> */
txs = [
...txs,
...txrj.items,
];

if (txrj.totalItems > txrj.to) {
// @ts-ignore
return await insight.getAllTxs(
addresses,
txrj.to,
txrj.to + 50,
txs,
);
}

return txs;
};

/** @type {GetAllUtxos} */
insight.getAllUtxos = async function (addresses) {
let addrs = insight._joinAddrs(addresses)
Expand Down Expand Up @@ -373,11 +427,17 @@
return resp;
}

let body = await resp.text()

if (body.charAt(0) === '{') {
body = JSON.parse(body)
}

let err = new Error(
`http request was ${resp.status}, not ok. See err.response for details.`,
);
// @ts-ignore
err.response = resp.json();
err.response = body;
throw err;
};

Expand Down
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports = require("./dashsight.js");
* @prop {GetTxs} getTxs
* @prop {GetUtxos} getUtxos
* @prop {GetAllUtxos} getAllUtxos
* @prop {GetAllTxs} getAllTxs
* @prop {GetPageTxs} getPageTxs
* @prop {InstantSend} instantSend
* @prop {ToCoreUtxos} toCoreUtxos
*/
Expand Down Expand Up @@ -44,6 +46,24 @@ module.exports = require("./dashsight.js");
* @returns {Promise<Array<InsightUtxo>>}
*/

/**
* @callback GetPageTxs
* @param {String | Array<String>} addresses
* @param {Number} [from=0]
* @param {Number} [to=100]
* @param {Array<InsightTx>} [txs=[]]
* @returns {Promise<InsightTxsResponse>}
*/

/**
* @callback GetAllTxs
* @param {String | Array<String>} addresses
* @param {Number} [from=0]
* @param {Number} [to=100]
* @param {Array<InsightTx>} [txs=[]]
* @returns {Promise<Array<InsightTx>>}
*/

/**
* @callback GetAllUtxos
* @param {String | Array<String>} addresses
Expand Down Expand Up @@ -175,6 +195,14 @@ module.exports = require("./dashsight.js");
* @property {Array<InsightTx>} txs
*/

/**
* @typedef {Object} InsightTxsResponse
* @property {Number} from
* @property {Number} to
* @property {Number} totalItems
* @property {Array<InsightTx>} items
*/

/**
* @typedef {Object} InsightTx
* @property {String} txid
Expand All @@ -185,6 +213,13 @@ module.exports = require("./dashsight.js");
* @property {Array<InsightTxVin>} vin
* @property {Array<InsightTxVout>} vout
* @property {Number} fees
* @property {Number} [locktime]
* @property {String} [blockhash]
* @property {Number} [blockheight]
* @property {Number} [blocktime]
* @property {Number} [valueOut] - DASH
* @property {Number} [valueIn] - DASH
* @property {Number} [size]
*/

/**
Expand Down