@xchainjs/xchain-utxo-providers 2.0.17 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.esm.js CHANGED
@@ -55,7 +55,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
55
55
  * @param {string} hash The transaction hash.
56
56
  * @returns {Transactions}
57
57
  */
58
- const getTx$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, sochainUrl, network, hash }) {
58
+ const getTx$4 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, sochainUrl, network, hash }) {
59
59
  const url = `${sochainUrl}/transaction/${network}/${hash}`;
60
60
  const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
61
61
  const tx = response.data;
@@ -71,7 +71,7 @@ const getTx$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, soc
71
71
  * @param {string} hash The transaction hash.
72
72
  * @returns {Transactions}
73
73
  */
74
- const getTxs$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, sochainUrl, network, page, }) {
74
+ const getTxs$4 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, sochainUrl, network, page, }) {
75
75
  const url = `${sochainUrl}/transactions/${network}/${address}/${page}`; //TODO support paging
76
76
  const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
77
77
  const txs = response.data;
@@ -88,7 +88,7 @@ const getTxs$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, ad
88
88
  * @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
89
89
  * @returns {number}
90
90
  */
91
- const getBalance$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, sochainUrl, network, address, confirmedOnly, assetDecimals, }) {
91
+ const getBalance$4 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, sochainUrl, network, address, confirmedOnly, assetDecimals, }) {
92
92
  const url = `${sochainUrl}/balance/${network}/${address}`;
93
93
  const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
94
94
  const balanceResponse = response.data;
@@ -139,7 +139,7 @@ const getUnspentTxs$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ api
139
139
  * @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
140
140
  * @returns {number}
141
141
  */
142
- const broadcastTx$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, sochainUrl, network, txHex, }) {
142
+ const broadcastTx$4 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, sochainUrl, network, txHex, }) {
143
143
  const url = `${sochainUrl}/broadcast_transaction/${network}`;
144
144
  const response = yield axios.post(url, { tx_hex: txHex }, { headers: { 'API-KEY': apiKey } });
145
145
  const broadcastResponse = response.data;
@@ -163,7 +163,7 @@ class SochainProvider {
163
163
  }
164
164
  broadcastTx(txHex) {
165
165
  return __awaiter(this, void 0, void 0, function* () {
166
- return yield broadcastTx$3({
166
+ return yield broadcastTx$4({
167
167
  apiKey: this._apiKey,
168
168
  sochainUrl: this.baseUrl,
169
169
  network: this.sochainNetwork,
@@ -199,7 +199,7 @@ class SochainProvider {
199
199
  getBalance(address, confirmedOnly) {
200
200
  return __awaiter(this, void 0, void 0, function* () {
201
201
  try {
202
- const amount = yield getBalance$3({
202
+ const amount = yield getBalance$4({
203
203
  apiKey: this._apiKey,
204
204
  sochainUrl: this.baseUrl,
205
205
  network: this.sochainNetwork,
@@ -235,7 +235,7 @@ class SochainProvider {
235
235
  let page = firstPage;
236
236
  try {
237
237
  while (page <= lastPage) {
238
- const response = yield getTxs$3({
238
+ const response = yield getTxs$4({
239
239
  apiKey: this._apiKey,
240
240
  sochainUrl: this.baseUrl,
241
241
  network: this.sochainNetwork,
@@ -277,7 +277,7 @@ class SochainProvider {
277
277
  getTransactionData(txId) {
278
278
  return __awaiter(this, void 0, void 0, function* () {
279
279
  try {
280
- const rawTx = yield getTx$3({
280
+ const rawTx = yield getTx$4({
281
281
  apiKey: this._apiKey,
282
282
  sochainUrl: this.baseUrl,
283
283
  network: this.sochainNetwork,
@@ -404,7 +404,7 @@ const getAddress = (_a) => __awaiter(void 0, [_a], void 0, function* ({ haskoinU
404
404
  *
405
405
  * @throws {"failed to query transaction by a given hash"} thrown if failed to query transaction by a given hash
406
406
  */
407
- const getTx$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ haskoinUrl, txId, network }) {
407
+ const getTx$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ haskoinUrl, txId, network }) {
408
408
  const result = (yield axios.get(`${haskoinUrl}/${network}/transaction/${txId}`)).data;
409
409
  if (!result || isErrorResponse(result))
410
410
  throw new Error(`failed to query transaction by a given hash ${txId}`);
@@ -436,7 +436,7 @@ const getRawTransaction = (_a) => __awaiter(void 0, [_a], void 0, function* ({ h
436
436
  * @param {string} hash The transaction hash.
437
437
  * @returns {Transactions}
438
438
  */
439
- const getTxs$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ address, haskoinUrl, network, limit, offset, }) {
439
+ const getTxs$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ address, haskoinUrl, network, limit, offset, }) {
440
440
  const params = { limit: `${limit}`, offset: `${offset}` };
441
441
  const url = `${haskoinUrl}/${network}/address/${address}/transactions/full`;
442
442
  const result = (yield axios.get(url, { params })).data;
@@ -449,7 +449,7 @@ const getTxs$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ address, h
449
449
  * @param param
450
450
  * @returns Returns BaseAmount
451
451
  */
452
- const getBalance$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ haskoinUrl, haskoinNetwork, address, confirmedOnly, assetDecimals, }) {
452
+ const getBalance$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ haskoinUrl, haskoinNetwork, address, confirmedOnly, assetDecimals, }) {
453
453
  const { data: { confirmed, unconfirmed }, } = yield axios.get(`${haskoinUrl}/${haskoinNetwork}/address/${address}/balance`);
454
454
  const confirmedAmount = baseAmount(confirmed, assetDecimals);
455
455
  const unconfirmedAmount = baseAmount(unconfirmed, assetDecimals);
@@ -482,7 +482,7 @@ const getUnspentTxs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ hasko
482
482
  * @returns {TxConfirmedStatus}
483
483
  */
484
484
  const getIsTxConfirmed = (_a) => __awaiter(void 0, [_a], void 0, function* ({ haskoinUrl, network, txId }) {
485
- const tx = yield getTx$2({ haskoinUrl, network, txId });
485
+ const tx = yield getTx$3({ haskoinUrl, network, txId });
486
486
  return {
487
487
  network: network,
488
488
  txid: txId,
@@ -592,7 +592,7 @@ const getUnspentTransactions = (_a) => __awaiter(void 0, [_a], void 0, function*
592
592
  * @param {BroadcastTxParams} params
593
593
  * @returns {TxHash} Transaction hash.
594
594
  */
595
- const broadcastTx$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ txHex, haskoinUrl, haskoinNetwork, }) {
595
+ const broadcastTx$3 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ txHex, haskoinUrl, haskoinNetwork, }) {
596
596
  var _b;
597
597
  const MAX_RETRIES = 5;
598
598
  let retries = 0;
@@ -628,7 +628,7 @@ class HaskoinProvider {
628
628
  }
629
629
  broadcastTx(txHex) {
630
630
  return __awaiter(this, void 0, void 0, function* () {
631
- return yield broadcastTx$2({
631
+ return yield broadcastTx$3({
632
632
  haskoinUrl: this.baseUrl,
633
633
  haskoinNetwork: this.haskoinNetwork,
634
634
  txHex,
@@ -658,7 +658,7 @@ class HaskoinProvider {
658
658
  }
659
659
  getBalance(address, confirmedOnly) {
660
660
  return __awaiter(this, void 0, void 0, function* () {
661
- const amount = yield getBalance$2({
661
+ const amount = yield getBalance$3({
662
662
  haskoinUrl: this.baseUrl,
663
663
  haskoinNetwork: this.haskoinNetwork,
664
664
  address,
@@ -684,7 +684,7 @@ class HaskoinProvider {
684
684
  throw Error('cannot fetch more than last 2000 txs');
685
685
  if (offset < 0 || limit < 0)
686
686
  throw Error('ofset and limit must be equal or greater than 0');
687
- const response = yield getTxs$2({
687
+ const response = yield getTxs$3({
688
688
  address: `${params === null || params === void 0 ? void 0 : params.address}`,
689
689
  haskoinUrl: this.baseUrl,
690
690
  network: this.haskoinNetwork,
@@ -723,7 +723,7 @@ class HaskoinProvider {
723
723
  getTransactionData(txId) {
724
724
  return __awaiter(this, void 0, void 0, function* () {
725
725
  try {
726
- const rawTx = yield getTx$2({
726
+ const rawTx = yield getTx$3({
727
727
  haskoinUrl: this.baseUrl,
728
728
  network: this.haskoinNetwork,
729
729
  txId: txId,
@@ -774,7 +774,7 @@ class HaskoinProvider {
774
774
  * @param {string} hash The transaction hash.
775
775
  * @returns {Transactions}
776
776
  */
777
- const getTx$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, network, hash, limit = 20 }) {
777
+ const getTx$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, network, hash, limit = 20 }) {
778
778
  const params = { includeHex: 'true', limit: limit.toString() };
779
779
  if (apiKey)
780
780
  params['token'] = apiKey;
@@ -792,7 +792,7 @@ const getTx$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, bas
792
792
  * @param {string} hash The transaction hash.
793
793
  * @returns {Transactions}
794
794
  */
795
- const getTxs$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, network, beforeBlock, limit, unspentOnly, }) {
795
+ const getTxs$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, network, beforeBlock, limit, unspentOnly, }) {
796
796
  const params = { limit: `${limit}`, unspentOnly: `${unspentOnly}` };
797
797
  const url = `${baseUrl}/${network}/addrs/${address}`;
798
798
  if (apiKey)
@@ -813,7 +813,7 @@ const getTxs$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, ad
813
813
  * @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
814
814
  * @returns {number}
815
815
  */
816
- const getBalance$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, network, address, confirmedOnly, assetDecimals, }) {
816
+ const getBalance$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, network, address, confirmedOnly, assetDecimals, }) {
817
817
  const params = {};
818
818
  const url = `${baseUrl}/${network}/addrs/${address}/balance`;
819
819
  if (apiKey)
@@ -824,7 +824,7 @@ const getBalance$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey
824
824
  const finalBalance = baseAmount(balanceResponse.final_balance, assetDecimals);
825
825
  return confirmedOnly ? confirmedAmount : finalBalance;
826
826
  });
827
- const broadcastTx$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, network, txHex, }) {
827
+ const broadcastTx$2 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, network, txHex, }) {
828
828
  const params = {};
829
829
  const url = `${baseUrl}/${network}/txs/push`;
830
830
  if (apiKey)
@@ -864,7 +864,7 @@ class BlockcypherProvider {
864
864
  }
865
865
  broadcastTx(txHex) {
866
866
  return __awaiter(this, void 0, void 0, function* () {
867
- return yield broadcastTx$1({
867
+ return yield broadcastTx$2({
868
868
  apiKey: this._apiKey,
869
869
  baseUrl: this.baseUrl,
870
870
  network: this.blockcypherNetwork,
@@ -886,7 +886,7 @@ class BlockcypherProvider {
886
886
  }
887
887
  getBalance(address, confirmedOnly) {
888
888
  return __awaiter(this, void 0, void 0, function* () {
889
- const amount = yield getBalance$1({
889
+ const amount = yield getBalance$2({
890
890
  apiKey: this._apiKey,
891
891
  baseUrl: this.baseUrl,
892
892
  network: this.blockcypherNetwork,
@@ -924,7 +924,7 @@ class BlockcypherProvider {
924
924
  getTransactionData(txId) {
925
925
  return __awaiter(this, void 0, void 0, function* () {
926
926
  try {
927
- const rawTx = yield getTx$1({
927
+ const rawTx = yield getTx$2({
928
928
  apiKey: this._apiKey,
929
929
  baseUrl: this.baseUrl,
930
930
  network: this.blockcypherNetwork,
@@ -1029,7 +1029,7 @@ class BlockcypherProvider {
1029
1029
  throw Error('ofset and limit must be equal or greater than 0');
1030
1030
  const txHashesToFetch = [];
1031
1031
  try {
1032
- const response = yield getTxs$1({
1032
+ const response = yield getTxs$2({
1033
1033
  apiKey: this._apiKey,
1034
1034
  baseUrl: this.baseUrl,
1035
1035
  network: this.blockcypherNetwork,
@@ -1062,7 +1062,7 @@ class BlockcypherProvider {
1062
1062
  .useCorrespondingResults()
1063
1063
  .process((hash) => __awaiter(this, void 0, void 0, function* () {
1064
1064
  yield this.delay(1000);
1065
- const rawTx = yield getTx$1({
1065
+ const rawTx = yield getTx$2({
1066
1066
  apiKey: this._apiKey,
1067
1067
  baseUrl: this.baseUrl,
1068
1068
  network: this.blockcypherNetwork,
@@ -1091,7 +1091,7 @@ var BlockcypherNetwork;
1091
1091
  * @param {GetFeeEstimateRequest} request Get fee estimate params
1092
1092
  * @returns {GetFeeEstimateResponse} Get fee estimate response
1093
1093
  */
1094
- const getFeeEstimate = (baseUrl, request) => __awaiter(void 0, void 0, void 0, function* () {
1094
+ const getFeeEstimate$1 = (baseUrl, request) => __awaiter(void 0, void 0, void 0, function* () {
1095
1095
  const response = yield axios.get(`${baseUrl}/tx/fee`, { params: request });
1096
1096
  return response.data;
1097
1097
  });
@@ -1108,7 +1108,7 @@ class BitgoProvider {
1108
1108
  getFeeRates() {
1109
1109
  return __awaiter(this, void 0, void 0, function* () {
1110
1110
  var _a, _b, _c, _d;
1111
- const gasFeeEstimateResponse = yield getFeeEstimate(`${this.baseUrl}/api/v2/${this.blockchainId}`, {
1111
+ const gasFeeEstimateResponse = yield getFeeEstimate$1(`${this.baseUrl}/api/v2/${this.blockchainId}`, {
1112
1112
  numBlocks: 2,
1113
1113
  });
1114
1114
  const fastestRate = gasFeeEstimateResponse.feePerKb / 1000;
@@ -1169,7 +1169,7 @@ class BitgoProvider {
1169
1169
  * @param {string} hash The transaction hash.
1170
1170
  * @returns {Transactions}
1171
1171
  */
1172
- const getTx = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, hash }) {
1172
+ const getTx$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, hash }) {
1173
1173
  const url = `${baseUrl}/tx/${hash}`;
1174
1174
  const response = yield axios.get(url, {
1175
1175
  headers: {
@@ -1187,7 +1187,7 @@ const getTx = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseU
1187
1187
  * @param {string} hash The transaction hash.
1188
1188
  * @returns {Transactions}
1189
1189
  */
1190
- const getTxs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, limit, }) {
1190
+ const getTxs$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, limit, }) {
1191
1191
  const url = `${baseUrl}/address/${address}`;
1192
1192
  const response = yield axios.get(url, {
1193
1193
  params: {
@@ -1208,7 +1208,7 @@ const getTxs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, addr
1208
1208
  * @param {string} address address.
1209
1209
  * @returns {Transactions}
1210
1210
  */
1211
- const getUTXOs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, isConfirmed = true, }) {
1211
+ const getUTXOs$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, isConfirmed = true, }) {
1212
1212
  const url = `${baseUrl}/utxo/${address}`;
1213
1213
  const response = yield axios.get(url, {
1214
1214
  params: {
@@ -1230,7 +1230,7 @@ const getUTXOs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, ad
1230
1230
  * @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
1231
1231
  * @returns {number}
1232
1232
  */
1233
- const getBalance = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, address, confirmedOnly = true, assetDecimals, }) {
1233
+ const getBalance$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, address, confirmedOnly = true, assetDecimals, }) {
1234
1234
  const url = `${baseUrl}/address/${address}`;
1235
1235
  const response = yield axios.get(url, {
1236
1236
  headers: {
@@ -1268,7 +1268,7 @@ const getConsensusBranchId = (_a) => __awaiter(void 0, [_a], void 0, function* (
1268
1268
  return undefined;
1269
1269
  return parseInt(nextblock, 16);
1270
1270
  });
1271
- const broadcastTx = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, txHex, }) {
1271
+ const broadcastTx$1 = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, txHex, }) {
1272
1272
  const url = `${baseUrl}/sendtx/${txHex}`;
1273
1273
  const response = yield axios.get(url, {
1274
1274
  headers: {
@@ -1296,7 +1296,7 @@ class NownodesProvider {
1296
1296
  }
1297
1297
  broadcastTx(txHex) {
1298
1298
  return __awaiter(this, void 0, void 0, function* () {
1299
- return yield broadcastTx({
1299
+ return yield broadcastTx$1({
1300
1300
  apiKey: this._apiKey,
1301
1301
  baseUrl: this.baseUrl,
1302
1302
  txHex,
@@ -1305,7 +1305,7 @@ class NownodesProvider {
1305
1305
  }
1306
1306
  getConfirmedUnspentTxs(address) {
1307
1307
  return __awaiter(this, void 0, void 0, function* () {
1308
- const allUnspent = yield getUTXOs({
1308
+ const allUnspent = yield getUTXOs$1({
1309
1309
  apiKey: this._apiKey,
1310
1310
  baseUrl: this.baseUrl,
1311
1311
  address: address,
@@ -1316,7 +1316,7 @@ class NownodesProvider {
1316
1316
  }
1317
1317
  getUnspentTxs(address) {
1318
1318
  return __awaiter(this, void 0, void 0, function* () {
1319
- const allUnspent = yield getUTXOs({
1319
+ const allUnspent = yield getUTXOs$1({
1320
1320
  apiKey: this._apiKey,
1321
1321
  baseUrl: this.baseUrl,
1322
1322
  address: address,
@@ -1327,7 +1327,7 @@ class NownodesProvider {
1327
1327
  }
1328
1328
  getBalance(address, confirmedOnly) {
1329
1329
  return __awaiter(this, void 0, void 0, function* () {
1330
- const amount = yield getBalance({
1330
+ const amount = yield getBalance$1({
1331
1331
  apiKey: this._apiKey,
1332
1332
  baseUrl: this.baseUrl,
1333
1333
  address,
@@ -1351,7 +1351,7 @@ class NownodesProvider {
1351
1351
  getTransactionData(txId) {
1352
1352
  return __awaiter(this, void 0, void 0, function* () {
1353
1353
  try {
1354
- const rawTx = yield getTx({
1354
+ const rawTx = yield getTx$1({
1355
1355
  apiKey: this._apiKey,
1356
1356
  baseUrl: this.baseUrl,
1357
1357
  hash: txId,
@@ -1423,7 +1423,7 @@ class NownodesProvider {
1423
1423
  if (offset < 0 || limit < 0)
1424
1424
  throw Error('ofset and limit must be equal or greater than 0');
1425
1425
  try {
1426
- const transactions = yield getTxs({
1426
+ const transactions = yield getTxs$1({
1427
1427
  apiKey: this._apiKey,
1428
1428
  baseUrl: this.baseUrl,
1429
1429
  address: `${params === null || params === void 0 ? void 0 : params.address}`,
@@ -1438,4 +1438,252 @@ class NownodesProvider {
1438
1438
  }
1439
1439
  }
1440
1440
 
1441
- export { BitgoProvider, BlockcypherNetwork, BlockcypherProvider, HaskoinNetwork, HaskoinProvider, NownodesProvider, SochainNetwork, SochainProvider, broadcastTx$2 as broadcastTx, getAccount, getAddress, getBalance$2 as getBalance, getConfirmedTxStatus, getConfirmedUnspentTxs, getIsTxConfirmed, getTx$2 as getTx, getTxs$2 as getTxs, getUnspentTxs };
1441
+ const makeHeaders = (apiKey) => (apiKey ? { 'api-key': apiKey } : {});
1442
+ const encodeAddressPathSegment = (address) => encodeURIComponent(address);
1443
+ const getTx = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, hash }) {
1444
+ const url = `${baseUrl}/tx/${hash}`;
1445
+ const response = yield axios.get(url, { headers: makeHeaders(apiKey) });
1446
+ return response.data;
1447
+ });
1448
+ const getTxs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, limit, }) {
1449
+ var _b;
1450
+ const url = `${baseUrl}/address/${encodeAddressPathSegment(address)}`;
1451
+ const response = yield axios.get(url, {
1452
+ params: { details: 'txs', pageSize: limit },
1453
+ headers: makeHeaders(apiKey),
1454
+ });
1455
+ const info = response.data;
1456
+ return { transactions: (_b = info.transactions) !== null && _b !== void 0 ? _b : [], total: info.txs };
1457
+ });
1458
+ const getUTXOs = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, address, baseUrl, isConfirmed = true, }) {
1459
+ const url = `${baseUrl}/utxo/${encodeAddressPathSegment(address)}`;
1460
+ const response = yield axios.get(url, {
1461
+ params: { confirmed: isConfirmed },
1462
+ headers: makeHeaders(apiKey),
1463
+ });
1464
+ return response.data;
1465
+ });
1466
+ const getBalance = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, address, confirmedOnly = true, assetDecimals, }) {
1467
+ const url = `${baseUrl}/address/${encodeAddressPathSegment(address)}`;
1468
+ const response = yield axios.get(url, { params: { details: 'tokenBalances' }, headers: makeHeaders(apiKey) });
1469
+ const info = response.data;
1470
+ return confirmedOnly
1471
+ ? baseAmount(info.balance, assetDecimals)
1472
+ : baseAmount(info.balance, assetDecimals).plus(baseAmount(info.unconfirmedBalance, assetDecimals));
1473
+ });
1474
+ const broadcastTx = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, txHex, }) {
1475
+ const url = `${baseUrl}/sendtx/`;
1476
+ const response = yield axios.post(url, txHex, {
1477
+ headers: Object.assign(Object.assign({}, makeHeaders(apiKey)), { 'Content-Type': 'text/plain' }),
1478
+ });
1479
+ return response.data.result;
1480
+ });
1481
+ /**
1482
+ * Fetches fee estimate using Blockbook's V2 REST endpoint.
1483
+ *
1484
+ * Docs: https://github.com/trezor/blockbook/blob/master/docs/api.md
1485
+ *
1486
+ * @param numberOfBlocks Target block confirmation count
1487
+ * @returns Estimated fee rate in sat/byte
1488
+ */
1489
+ const getFeeEstimate = (_a) => __awaiter(void 0, [_a], void 0, function* ({ apiKey, baseUrl, numberOfBlocks, }) {
1490
+ const url = `${baseUrl}/estimatefee/${numberOfBlocks}`;
1491
+ const response = yield axios.get(url, { headers: makeHeaders(apiKey) });
1492
+ const dto = response.data;
1493
+ // result is in BTC/kB; convert to sat/byte
1494
+ const btcPerKb = parseFloat(dto.result);
1495
+ if (isNaN(btcPerKb) || btcPerKb <= 0) {
1496
+ throw new Error(`BlockbookProvider: invalid fee estimate: ${dto.result}`);
1497
+ }
1498
+ return (btcPerKb * 1e8) / 1000;
1499
+ });
1500
+
1501
+ class BlockbookProvider {
1502
+ constructor(baseUrl, asset, assetDecimals, options) {
1503
+ this.baseUrl = baseUrl;
1504
+ this._apiKey = options === null || options === void 0 ? void 0 : options.apiKey;
1505
+ this.asset = asset;
1506
+ this.assetDecimals = assetDecimals;
1507
+ this.normalizeAddressForApi = options === null || options === void 0 ? void 0 : options.normalizeAddressForApi;
1508
+ }
1509
+ toApiAddress(address) {
1510
+ return this.normalizeAddressForApi ? this.normalizeAddressForApi(address) : address;
1511
+ }
1512
+ get apiKey() {
1513
+ return this._apiKey;
1514
+ }
1515
+ set apiKey(value) {
1516
+ this._apiKey = value;
1517
+ }
1518
+ broadcastTx(txHex) {
1519
+ return __awaiter(this, void 0, void 0, function* () {
1520
+ return yield broadcastTx({
1521
+ apiKey: this._apiKey,
1522
+ baseUrl: this.baseUrl,
1523
+ txHex,
1524
+ });
1525
+ });
1526
+ }
1527
+ getConfirmedUnspentTxs(address) {
1528
+ return __awaiter(this, void 0, void 0, function* () {
1529
+ const addr = this.toApiAddress(address);
1530
+ const utxos = yield getUTXOs({
1531
+ apiKey: this._apiKey,
1532
+ baseUrl: this.baseUrl,
1533
+ address: addr,
1534
+ isConfirmed: true,
1535
+ });
1536
+ return yield this.mapUTXOs(utxos);
1537
+ });
1538
+ }
1539
+ getUnspentTxs(address) {
1540
+ return __awaiter(this, void 0, void 0, function* () {
1541
+ const addr = this.toApiAddress(address);
1542
+ const utxos = yield getUTXOs({
1543
+ apiKey: this._apiKey,
1544
+ baseUrl: this.baseUrl,
1545
+ address: addr,
1546
+ isConfirmed: false,
1547
+ });
1548
+ return yield this.mapUTXOs(utxos);
1549
+ });
1550
+ }
1551
+ getBalance(address, confirmedOnly) {
1552
+ return __awaiter(this, void 0, void 0, function* () {
1553
+ const addr = this.toApiAddress(address);
1554
+ const amount = yield getBalance({
1555
+ apiKey: this._apiKey,
1556
+ baseUrl: this.baseUrl,
1557
+ address: addr,
1558
+ confirmedOnly: confirmedOnly !== undefined ? confirmedOnly : true,
1559
+ assetDecimals: this.assetDecimals,
1560
+ });
1561
+ return [{ amount, asset: this.asset }];
1562
+ });
1563
+ }
1564
+ getTransactions(params) {
1565
+ return __awaiter(this, void 0, void 0, function* () {
1566
+ const { transactions: rawTxs, total } = yield this.getRawTransactions(params);
1567
+ const txs = rawTxs.map((tx) => this.mapTransactionToTx(tx));
1568
+ return { total, txs };
1569
+ });
1570
+ }
1571
+ getTransactionData(txId) {
1572
+ return __awaiter(this, void 0, void 0, function* () {
1573
+ const rawTx = yield getTx({
1574
+ apiKey: this._apiKey,
1575
+ baseUrl: this.baseUrl,
1576
+ hash: txId,
1577
+ });
1578
+ return this.mapTransactionToTx(rawTx);
1579
+ });
1580
+ }
1581
+ getFeeRate(feeOption) {
1582
+ return __awaiter(this, void 0, void 0, function* () {
1583
+ return getFeeEstimate({
1584
+ apiKey: this._apiKey,
1585
+ baseUrl: this.baseUrl,
1586
+ numberOfBlocks: BlockbookProvider.BLOCK_TARGETS[feeOption],
1587
+ });
1588
+ });
1589
+ }
1590
+ getFeeRates() {
1591
+ return __awaiter(this, void 0, void 0, function* () {
1592
+ const [average, fast, fastest] = yield Promise.all([
1593
+ this.getFeeRate(FeeOption.Average),
1594
+ this.getFeeRate(FeeOption.Fast),
1595
+ this.getFeeRate(FeeOption.Fastest),
1596
+ ]);
1597
+ return {
1598
+ [FeeOption.Average]: average,
1599
+ [FeeOption.Fast]: fast,
1600
+ [FeeOption.Fastest]: fastest,
1601
+ };
1602
+ });
1603
+ }
1604
+ mapTransactionToTx(rawTx) {
1605
+ return {
1606
+ asset: this.asset,
1607
+ from: rawTx.vin
1608
+ .filter((i) => { var _a; return i.isAddress && ((_a = i.addresses) === null || _a === void 0 ? void 0 : _a.length) > 0; })
1609
+ .map((i) => ({
1610
+ from: i.addresses[0],
1611
+ amount: baseAmount(i.value, this.assetDecimals),
1612
+ })),
1613
+ to: rawTx.vout
1614
+ .filter((i) => { var _a; return i.isAddress && ((_a = i.addresses) === null || _a === void 0 ? void 0 : _a.length) > 0; })
1615
+ .map((i) => ({ to: i.addresses[0], amount: baseAmount(i.value, this.assetDecimals) })),
1616
+ date: rawTx.blockTime ? new Date(rawTx.blockTime * 1000) : new Date(0),
1617
+ type: TxType.Transfer,
1618
+ hash: rawTx.txid,
1619
+ };
1620
+ }
1621
+ mapUTXOs(utxos) {
1622
+ return __awaiter(this, void 0, void 0, function* () {
1623
+ const result = [];
1624
+ const txCache = new Map();
1625
+ for (const utxo of utxos) {
1626
+ let tx = txCache.get(utxo.txid);
1627
+ if (!tx) {
1628
+ // Rate-limit: pause between consecutive fetches to avoid overwhelming the node
1629
+ if (txCache.size > 0)
1630
+ yield new Promise((resolve) => setTimeout(resolve, 50));
1631
+ tx = yield getTx({
1632
+ apiKey: this._apiKey,
1633
+ baseUrl: this.baseUrl,
1634
+ hash: utxo.txid,
1635
+ });
1636
+ txCache.set(utxo.txid, tx);
1637
+ }
1638
+ const output = tx.vout.find((vout) => vout.n === utxo.vout);
1639
+ if (!(output === null || output === void 0 ? void 0 : output.hex)) {
1640
+ throw Error(`BlockbookProvider: could not resolve scriptPubKey for UTXO ${utxo.txid}:${utxo.vout}`);
1641
+ }
1642
+ const value = Number(utxo.value);
1643
+ result.push({
1644
+ hash: utxo.txid,
1645
+ index: utxo.vout,
1646
+ value,
1647
+ witnessUtxo: {
1648
+ value,
1649
+ script: Buffer.from(output.hex, 'hex'),
1650
+ },
1651
+ txHex: tx.hex,
1652
+ });
1653
+ }
1654
+ return result;
1655
+ });
1656
+ }
1657
+ getRawTransactions(params) {
1658
+ return __awaiter(this, void 0, void 0, function* () {
1659
+ var _a;
1660
+ if (params === null || params === void 0 ? void 0 : params.startTime) {
1661
+ throw Error('BlockbookProvider: startTime is not supported');
1662
+ }
1663
+ const offset = (_a = params === null || params === void 0 ? void 0 : params.offset) !== null && _a !== void 0 ? _a : 0;
1664
+ const limit = (params === null || params === void 0 ? void 0 : params.limit) || 10;
1665
+ if (offset + limit > 1000)
1666
+ throw Error('BlockbookProvider: cannot fetch more than the last 1000 txs');
1667
+ if (offset < 0 || limit < 0)
1668
+ throw Error('BlockbookProvider: offset and limit must be >= 0');
1669
+ const address = params === null || params === void 0 ? void 0 : params.address;
1670
+ if (!address)
1671
+ throw Error('BlockbookProvider: address is required');
1672
+ const addr = this.toApiAddress(address);
1673
+ const { transactions, total } = yield getTxs({
1674
+ apiKey: this._apiKey,
1675
+ baseUrl: this.baseUrl,
1676
+ address: addr,
1677
+ limit: offset + limit,
1678
+ });
1679
+ return { transactions: transactions.slice(offset, offset + limit), total };
1680
+ });
1681
+ }
1682
+ }
1683
+ BlockbookProvider.BLOCK_TARGETS = {
1684
+ [FeeOption.Average]: 5,
1685
+ [FeeOption.Fast]: 3,
1686
+ [FeeOption.Fastest]: 1,
1687
+ };
1688
+
1689
+ export { BitgoProvider, BlockbookProvider, BlockcypherNetwork, BlockcypherProvider, HaskoinNetwork, HaskoinProvider, NownodesProvider, SochainNetwork, SochainProvider, broadcastTx$3 as broadcastTx, getAccount, getAddress, getBalance$3 as getBalance, getConfirmedTxStatus, getConfirmedUnspentTxs, getIsTxConfirmed, getTx$3 as getTx, getTxs$3 as getTxs, getUnspentTxs };