3 let Insight = module.exports;
5 let request = require("./request.js");
9 * @param {String} opts.baseUrl
11 Insight.create = function ({ baseUrl }) {
15 * Don't use this with instantSend
16 * @param {String} address
17 * @returns {Promise<InsightBalance>}
19 insight.getBalance = async function (address) {
20 console.warn(`warn: getBalance(pubkey) doesn't account for instantSend,`);
21 console.warn(` consider (await getUtxos()).reduce(countSatoshis)`);
22 let txUrl = `${baseUrl}/insight-api/addr/${address}/?noTxList=1`;
23 let txResp = await request({ url: txUrl, json: true });
25 /** @type {InsightBalance} */
26 let data = txResp.body;
31 * @param {String} address
32 * @returns {Promise<Array<InsightUtxo>>}
34 insight.getUtxos = async function (address) {
35 let utxoUrl = `${baseUrl}/insight-api/addr/${address}/utxo`;
36 let utxoResp = await request({ url: utxoUrl, json: true });
38 /** @type Array<InsightUtxo> */
39 let utxos = utxoResp.body;
44 * @param {String} txid
45 * @returns {Promise<InsightTx>}
47 insight.getTx = async function (txid) {
48 let txUrl = `${baseUrl}/insight-api/tx/${txid}`;
49 let txResp = await request({ url: txUrl, json: true });
51 /** @type InsightTx */
52 let data = txResp.body;
57 * @param {String} addr
58 * @param {Number} maxPages
59 * @returns {Promise<InsightTxResponse>}
61 insight.getTxs = async function (addr, maxPages) {
62 let txUrl = `${baseUrl}/insight-api/txs?address=${addr}&pageNum=0`;
63 let txResp = await request({ url: txUrl, json: true });
65 /** @type {InsightTxResponse} */
66 let body = txResp.body;
68 let data = await getAllPages(body, addr, maxPages);
73 * @param {InsightTxResponse} body
74 * @param {String} addr
75 * @param {Number} maxPages
77 async function getAllPages(body, addr, maxPages) {
78 let pagesTotal = Math.min(body.pagesTotal, maxPages);
79 for (let cursor = 1; cursor < pagesTotal; cursor += 1) {
80 let nextResp = await request({
81 url: `${baseUrl}/insight-api/txs?address=${addr}&pageNum=${cursor}`,
84 // Note: this could still be wrong, but I don't think we have
85 // a better way to page so... whatever
86 body.txs = body.txs.concat(nextResp.body.txs);
92 * @param {String} hexTx
94 insight.instantSend = async function (hexTx) {
95 let instUrl = `${baseUrl}/insight-api-dash/tx/sendix`;
103 let txResp = await request(reqObj);
105 // TODO better error check
106 throw new Error(JSON.stringify(txResp.body, null, 2));
108 return txResp.toJSON();