diff --git a/dashsight.js b/dashsight.js index 8f04523..f6f2fa0 100644 --- a/dashsight.js +++ b/dashsight.js @@ -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 */ @@ -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 */ + 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) @@ -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; }; diff --git a/index.js b/index.js index 616a9e6..9258ff2 100644 --- a/index.js +++ b/index.js @@ -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 */ @@ -44,6 +46,24 @@ module.exports = require("./dashsight.js"); * @returns {Promise>} */ +/** + * @callback GetPageTxs + * @param {String | Array} addresses + * @param {Number} [from=0] + * @param {Number} [to=100] + * @param {Array} [txs=[]] + * @returns {Promise} + */ + +/** + * @callback GetAllTxs + * @param {String | Array} addresses + * @param {Number} [from=0] + * @param {Number} [to=100] + * @param {Array} [txs=[]] + * @returns {Promise>} + */ + /** * @callback GetAllUtxos * @param {String | Array} addresses @@ -175,6 +195,14 @@ module.exports = require("./dashsight.js"); * @property {Array} txs */ +/** + * @typedef {Object} InsightTxsResponse + * @property {Number} from + * @property {Number} to + * @property {Number} totalItems + * @property {Array} items + */ + /** * @typedef {Object} InsightTx * @property {String} txid @@ -185,6 +213,13 @@ module.exports = require("./dashsight.js"); * @property {Array} vin * @property {Array} 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] */ /**