@xchainjs/xchain-utxo-providers 0.1.1 → 0.2.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 +1036 -0
- package/lib/index.js +1056 -13
- package/lib/providers/blockcypher/blockcypher-api-types.d.ts +12 -12
- package/lib/providers/blockcypher/blockcypher-api.d.ts +4 -4
- package/lib/providers/blockcypher/blockcypher-data-provider.d.ts +1 -1
- package/lib/providers/haskoin/haskoin-api-types.d.ts +15 -15
- package/lib/providers/haskoin/haskoin-api.d.ts +1 -1
- package/lib/providers/haskoin/haskoin-data-provider.d.ts +1 -1
- package/lib/providers/sochainv3/sochain-api-types.d.ts +13 -13
- package/lib/providers/sochainv3/sochain-data-provider.d.ts +1 -1
- package/package.json +11 -12
- package/lib/index.js.map +0 -1
- package/lib/providers/blockcypher/blockcypher-api-types.js +0 -11
- package/lib/providers/blockcypher/blockcypher-api-types.js.map +0 -1
- package/lib/providers/blockcypher/blockcypher-api.js +0 -231
- package/lib/providers/blockcypher/blockcypher-api.js.map +0 -1
- package/lib/providers/blockcypher/blockcypher-data-provider.js +0 -366
- package/lib/providers/blockcypher/blockcypher-data-provider.js.map +0 -1
- package/lib/providers/haskoin/haskoin-api-types.js +0 -14
- package/lib/providers/haskoin/haskoin-api-types.js.map +0 -1
- package/lib/providers/haskoin/haskoin-api.js +0 -485
- package/lib/providers/haskoin/haskoin-api.js.map +0 -1
- package/lib/providers/haskoin/haskoin-data-provider.js +0 -269
- package/lib/providers/haskoin/haskoin-data-provider.js.map +0 -1
- package/lib/providers/index.js +0 -34
- package/lib/providers/index.js.map +0 -1
- package/lib/providers/sochainv3/sochain-api-types.js +0 -13
- package/lib/providers/sochainv3/sochain-api-types.js.map +0 -1
- package/lib/providers/sochainv3/sochain-api.js +0 -354
- package/lib/providers/sochainv3/sochain-api.js.map +0 -1
- package/lib/providers/sochainv3/sochain-data-provider.js +0 -312
- package/lib/providers/sochainv3/sochain-data-provider.js.map +0 -1
package/lib/index.esm.js
ADDED
|
@@ -0,0 +1,1036 @@
|
|
|
1
|
+
import { assetAmount, assetToBase, baseAmount, delay } from '@xchainjs/xchain-util';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import { TxType } from '@xchainjs/xchain-client';
|
|
4
|
+
import PromisePool from '@supercharge/promise-pool';
|
|
5
|
+
|
|
6
|
+
var SochainNetwork;
|
|
7
|
+
(function (SochainNetwork) {
|
|
8
|
+
SochainNetwork["BTC"] = "BTC";
|
|
9
|
+
SochainNetwork["BTCTEST"] = "BTCTEST";
|
|
10
|
+
SochainNetwork["LTC"] = "LTC";
|
|
11
|
+
SochainNetwork["LTCTEST"] = "LTCTEST";
|
|
12
|
+
SochainNetwork["DOGE"] = "DOGE";
|
|
13
|
+
SochainNetwork["DOGETEST"] = "DOGETEST";
|
|
14
|
+
})(SochainNetwork || (SochainNetwork = {}));
|
|
15
|
+
|
|
16
|
+
/******************************************************************************
|
|
17
|
+
Copyright (c) Microsoft Corporation.
|
|
18
|
+
|
|
19
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
20
|
+
purpose with or without fee is hereby granted.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
23
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
24
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
25
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
26
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
27
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
28
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
29
|
+
***************************************************************************** */
|
|
30
|
+
|
|
31
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
32
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
33
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
34
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
35
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
36
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
37
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get transaction by hash.
|
|
43
|
+
*
|
|
44
|
+
* @see https://sochain.com/api#get-tx
|
|
45
|
+
*
|
|
46
|
+
* @param {string} sochainUrl The sochain node url.
|
|
47
|
+
* @param {string} network network id
|
|
48
|
+
* @param {string} hash The transaction hash.
|
|
49
|
+
* @returns {Transactions}
|
|
50
|
+
*/
|
|
51
|
+
const getTx$2 = ({ apiKey, sochainUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
|
+
const url = `${sochainUrl}/transaction/${network}/${hash}`;
|
|
53
|
+
const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
|
|
54
|
+
const tx = response.data;
|
|
55
|
+
return tx.data;
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Get transactions
|
|
59
|
+
*
|
|
60
|
+
* @see https://sochain.com/api#get-tx
|
|
61
|
+
*
|
|
62
|
+
* @param {string} sochainUrl The sochain node url.
|
|
63
|
+
* @param {string} network network id
|
|
64
|
+
* @param {string} hash The transaction hash.
|
|
65
|
+
* @returns {Transactions}
|
|
66
|
+
*/
|
|
67
|
+
const getTxs$2 = ({ apiKey, address, sochainUrl, network, page, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
|
+
const url = `${sochainUrl}/transactions/${network}/${address}/${page}`; //TODO support paging
|
|
69
|
+
const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
|
|
70
|
+
const txs = response.data;
|
|
71
|
+
return txs.data;
|
|
72
|
+
});
|
|
73
|
+
/**
|
|
74
|
+
* Get address balance.
|
|
75
|
+
*
|
|
76
|
+
* @see https://sochain.com/api#get-balance
|
|
77
|
+
*
|
|
78
|
+
* @param {string} sochainUrl The sochain node url.
|
|
79
|
+
* @param {string} network Network
|
|
80
|
+
* @param {string} address Address
|
|
81
|
+
* @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
|
|
82
|
+
* @returns {number}
|
|
83
|
+
*/
|
|
84
|
+
const getBalance$2 = ({ apiKey, sochainUrl, network, address, confirmedOnly, assetDecimals, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85
|
+
const url = `${sochainUrl}/balance/${network}/${address}`;
|
|
86
|
+
const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
|
|
87
|
+
const balanceResponse = response.data;
|
|
88
|
+
const confirmed = assetAmount(balanceResponse.data.confirmed, assetDecimals);
|
|
89
|
+
const unconfirmed = assetAmount(balanceResponse.data.unconfirmed, assetDecimals);
|
|
90
|
+
const netAmt = confirmedOnly ? confirmed : confirmed.plus(unconfirmed);
|
|
91
|
+
const result = assetToBase(netAmt);
|
|
92
|
+
return result;
|
|
93
|
+
});
|
|
94
|
+
/**
|
|
95
|
+
* Get unspent txs
|
|
96
|
+
*
|
|
97
|
+
* @see https://sochain.com/api#get-unspent-tx
|
|
98
|
+
*
|
|
99
|
+
* @param {string} sochainUrl The sochain node url.
|
|
100
|
+
* @param {string} network
|
|
101
|
+
* @param {string} address
|
|
102
|
+
* @returns {AddressUTXO[]}
|
|
103
|
+
*/
|
|
104
|
+
const getUnspentTxs$1 = ({ apiKey, sochainUrl, network, address, page, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
105
|
+
const url = [sochainUrl, 'unspent_outputs', network, address, page].filter((v) => !!v).join('/');
|
|
106
|
+
const resp = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
|
|
107
|
+
const response = resp.data;
|
|
108
|
+
const txs = response.data.outputs;
|
|
109
|
+
if (txs.length === 10) {
|
|
110
|
+
//fetch the next batch
|
|
111
|
+
const nextBatch = yield getUnspentTxs$1({
|
|
112
|
+
apiKey,
|
|
113
|
+
sochainUrl,
|
|
114
|
+
network,
|
|
115
|
+
address,
|
|
116
|
+
page: page + 1,
|
|
117
|
+
});
|
|
118
|
+
return txs.concat(nextBatch);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
return txs;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
/**
|
|
125
|
+
* Get address balance.
|
|
126
|
+
*
|
|
127
|
+
* @see https://sochain.com/api#get-balance
|
|
128
|
+
*
|
|
129
|
+
* @param {string} sochainUrl The sochain node url.
|
|
130
|
+
* @param {string} network Network
|
|
131
|
+
* @param {string} address Address
|
|
132
|
+
* @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
|
|
133
|
+
* @returns {number}
|
|
134
|
+
*/
|
|
135
|
+
const broadcastTx$2 = ({ apiKey, sochainUrl, network, txHex, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
136
|
+
const url = `${sochainUrl}/broadcast_transaction/${network}`;
|
|
137
|
+
const response = yield axios.post(url, { tx_hex: txHex }, { headers: { 'API-KEY': apiKey } });
|
|
138
|
+
const broadcastResponse = response.data;
|
|
139
|
+
return broadcastResponse.tx_hex;
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
class SochainProvider {
|
|
143
|
+
constructor(baseUrl = 'https://sochain.com/api/v3', apiKey, chain, asset, assetDecimals, sochainNetwork) {
|
|
144
|
+
this.baseUrl = baseUrl;
|
|
145
|
+
this._apiKey = apiKey;
|
|
146
|
+
this.chain = chain;
|
|
147
|
+
this.asset = asset;
|
|
148
|
+
this.assetDecimals = assetDecimals;
|
|
149
|
+
this.sochainNetwork = sochainNetwork;
|
|
150
|
+
this.asset;
|
|
151
|
+
this.chain;
|
|
152
|
+
}
|
|
153
|
+
get apiKey() {
|
|
154
|
+
return this._apiKey;
|
|
155
|
+
}
|
|
156
|
+
set apiKey(value) {
|
|
157
|
+
this._apiKey = value;
|
|
158
|
+
}
|
|
159
|
+
broadcastTx(txHex) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
return yield broadcastTx$2({
|
|
162
|
+
apiKey: this._apiKey,
|
|
163
|
+
sochainUrl: this.baseUrl,
|
|
164
|
+
network: this.sochainNetwork,
|
|
165
|
+
txHex,
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
getConfirmedUnspentTxs(address) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
+
const allUnspent = yield getUnspentTxs$1({
|
|
172
|
+
apiKey: this._apiKey,
|
|
173
|
+
sochainUrl: this.baseUrl,
|
|
174
|
+
network: this.sochainNetwork,
|
|
175
|
+
address,
|
|
176
|
+
page: 1,
|
|
177
|
+
});
|
|
178
|
+
const confirmedUnspent = allUnspent.filter((i) => i.block); //if it has a block noumber it's been confirmed
|
|
179
|
+
return this.mapUTXOs(confirmedUnspent);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
getUnspentTxs(address) {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
const allUnspent = yield getUnspentTxs$1({
|
|
185
|
+
apiKey: this._apiKey,
|
|
186
|
+
sochainUrl: this.baseUrl,
|
|
187
|
+
network: this.sochainNetwork,
|
|
188
|
+
address,
|
|
189
|
+
page: 1,
|
|
190
|
+
});
|
|
191
|
+
return this.mapUTXOs(allUnspent);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
getBalance(address, assets /*ignored*/, confirmedOnly) {
|
|
195
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
196
|
+
try {
|
|
197
|
+
const amount = yield getBalance$2({
|
|
198
|
+
apiKey: this._apiKey,
|
|
199
|
+
sochainUrl: this.baseUrl,
|
|
200
|
+
network: this.sochainNetwork,
|
|
201
|
+
address,
|
|
202
|
+
confirmedOnly: !!confirmedOnly,
|
|
203
|
+
assetDecimals: this.assetDecimals,
|
|
204
|
+
});
|
|
205
|
+
return [{ amount, asset: this.asset }];
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
throw new Error(`Could not get balances for address ${address}`);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Get transaction history of a given address with pagination options.
|
|
214
|
+
* By default it will return the transaction history of the current wallet.
|
|
215
|
+
*
|
|
216
|
+
* @param {TxHistoryParams} params The options to get transaction history. (optional)
|
|
217
|
+
* @returns {TxsPage} The transaction history.
|
|
218
|
+
*/
|
|
219
|
+
getTransactions(params) {
|
|
220
|
+
var _a;
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
const offset = (_a = params === null || params === void 0 ? void 0 : params.offset) !== null && _a !== void 0 ? _a : 0;
|
|
223
|
+
const limit = (params === null || params === void 0 ? void 0 : params.limit) || 10;
|
|
224
|
+
if (offset < 0 || limit < 0)
|
|
225
|
+
throw Error('ofset and limit must be equal or greater than 0');
|
|
226
|
+
const firstPage = Math.floor(offset / 10) + 1;
|
|
227
|
+
const lastPage = limit > 10 ? firstPage + Math.floor(limit / 10) : firstPage;
|
|
228
|
+
const offsetOnFirstPage = offset % 10;
|
|
229
|
+
const txHashesToFetch = [];
|
|
230
|
+
let page = firstPage;
|
|
231
|
+
try {
|
|
232
|
+
while (page <= lastPage) {
|
|
233
|
+
const response = yield getTxs$2({
|
|
234
|
+
apiKey: this._apiKey,
|
|
235
|
+
sochainUrl: this.baseUrl,
|
|
236
|
+
network: this.sochainNetwork,
|
|
237
|
+
address: `${params === null || params === void 0 ? void 0 : params.address}`,
|
|
238
|
+
page,
|
|
239
|
+
});
|
|
240
|
+
if (response.transactions.length === 0)
|
|
241
|
+
break;
|
|
242
|
+
if (page === firstPage && response.transactions.length > offsetOnFirstPage) {
|
|
243
|
+
//start from offset
|
|
244
|
+
const txsToGet = response.transactions.slice(offsetOnFirstPage);
|
|
245
|
+
this.addArrayUpToLimit(txHashesToFetch, txsToGet.map((i) => i.hash), limit);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
this.addArrayUpToLimit(txHashesToFetch, response.transactions.map((i) => i.hash), limit);
|
|
249
|
+
}
|
|
250
|
+
page++;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
console.error(error);
|
|
255
|
+
//an errors means no more results
|
|
256
|
+
}
|
|
257
|
+
const total = txHashesToFetch.length;
|
|
258
|
+
const transactions = yield Promise.all(txHashesToFetch.map((hash) => this.getTransactionData(hash)));
|
|
259
|
+
const result = {
|
|
260
|
+
total,
|
|
261
|
+
txs: transactions,
|
|
262
|
+
};
|
|
263
|
+
return result;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get the transaction details of a given transaction id.
|
|
268
|
+
*
|
|
269
|
+
* @param {string} txId The transaction id.
|
|
270
|
+
* @returns {Tx} The transaction details of the given transaction id.
|
|
271
|
+
*/
|
|
272
|
+
getTransactionData(txId) {
|
|
273
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
274
|
+
try {
|
|
275
|
+
const rawTx = yield getTx$2({
|
|
276
|
+
apiKey: this._apiKey,
|
|
277
|
+
sochainUrl: this.baseUrl,
|
|
278
|
+
network: this.sochainNetwork,
|
|
279
|
+
hash: txId,
|
|
280
|
+
});
|
|
281
|
+
return {
|
|
282
|
+
asset: this.asset,
|
|
283
|
+
from: rawTx.inputs.map((i) => ({
|
|
284
|
+
from: i.address,
|
|
285
|
+
amount: assetToBase(assetAmount(i.value, this.assetDecimals)),
|
|
286
|
+
})),
|
|
287
|
+
to: rawTx.outputs
|
|
288
|
+
.filter((i) => i.type !== 'nulldata') //filter out op_return outputs
|
|
289
|
+
.map((i) => ({ to: i.address, amount: assetToBase(assetAmount(i.value, this.assetDecimals)) })),
|
|
290
|
+
date: new Date(rawTx.time * 1000),
|
|
291
|
+
type: TxType.Transfer,
|
|
292
|
+
hash: rawTx.hash,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
console.error(error);
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
mapUTXOs(utxos) {
|
|
302
|
+
return utxos.map((utxo) => ({
|
|
303
|
+
hash: utxo.hash,
|
|
304
|
+
index: utxo.index,
|
|
305
|
+
value: assetToBase(assetAmount(utxo.value, this.assetDecimals)).amount().toNumber(),
|
|
306
|
+
witnessUtxo: {
|
|
307
|
+
value: assetToBase(assetAmount(utxo.value, this.assetDecimals)).amount().toNumber(),
|
|
308
|
+
script: Buffer.from(utxo.script, 'hex'),
|
|
309
|
+
},
|
|
310
|
+
txHex: utxo.tx_hex,
|
|
311
|
+
}));
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* helper function tto limit adding to an array
|
|
315
|
+
*
|
|
316
|
+
* @param arr array to be added to
|
|
317
|
+
* @param toAdd elements to add
|
|
318
|
+
* @param limit do not add more than this limit
|
|
319
|
+
*/
|
|
320
|
+
addArrayUpToLimit(arr, toAdd, limit) {
|
|
321
|
+
for (let index = 0; index < toAdd.length; index++) {
|
|
322
|
+
const element = toAdd[index];
|
|
323
|
+
if (arr.length < limit) {
|
|
324
|
+
arr.push(element);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Haskoin API types
|
|
332
|
+
*/
|
|
333
|
+
var HaskoinNetwork;
|
|
334
|
+
(function (HaskoinNetwork) {
|
|
335
|
+
HaskoinNetwork["BTC"] = "btc";
|
|
336
|
+
HaskoinNetwork["BTCTEST"] = "btctest";
|
|
337
|
+
HaskoinNetwork["BCH"] = "bch";
|
|
338
|
+
HaskoinNetwork["BCHTEST"] = "bchtest";
|
|
339
|
+
})(HaskoinNetwork || (HaskoinNetwork = {}));
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Module to interact with Haskoin API
|
|
343
|
+
*
|
|
344
|
+
* Doc (SwaggerHub) https://app.swaggerhub.com/apis/eligecode/blockchain-api/0.0.1-oas3
|
|
345
|
+
*
|
|
346
|
+
*/
|
|
347
|
+
/**
|
|
348
|
+
* Check error response.
|
|
349
|
+
*
|
|
350
|
+
* @param {any} response The api response.
|
|
351
|
+
* @returns {boolean}
|
|
352
|
+
*/
|
|
353
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
354
|
+
const isErrorResponse = (response) => {
|
|
355
|
+
return !!response.error;
|
|
356
|
+
};
|
|
357
|
+
/**
|
|
358
|
+
* Get account from address.
|
|
359
|
+
*
|
|
360
|
+
* @param {string} haskoinUrl The haskoin API url.
|
|
361
|
+
* @param {string} address The BCH address.
|
|
362
|
+
* @returns {AddressBalance}
|
|
363
|
+
*
|
|
364
|
+
* @throws {"failed to query account by a given address"} thrown if failed to query account by a given address
|
|
365
|
+
*/
|
|
366
|
+
const getAccount = ({ haskoinUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
367
|
+
const url = `${haskoinUrl}/${network}/address/${address}/balance`;
|
|
368
|
+
const result = (yield axios.get(url)).data;
|
|
369
|
+
if (!result || isErrorResponse(result))
|
|
370
|
+
throw new Error(`failed to query account by given address ${address}`);
|
|
371
|
+
return result;
|
|
372
|
+
});
|
|
373
|
+
/**
|
|
374
|
+
* Get address information.
|
|
375
|
+
*
|
|
376
|
+
* @param {string} haskoinUrl The haskoin node url.
|
|
377
|
+
* @param {string} network
|
|
378
|
+
* @param {string} address
|
|
379
|
+
* @returns {AddressDTO}
|
|
380
|
+
*/
|
|
381
|
+
const getAddress = ({ haskoinUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
382
|
+
const url = `${haskoinUrl}/${network}/address/${address}/balance`;
|
|
383
|
+
const response = yield axios.get(url);
|
|
384
|
+
const addressResponse = response.data;
|
|
385
|
+
return addressResponse.data;
|
|
386
|
+
});
|
|
387
|
+
/**
|
|
388
|
+
* Get transaction by hash.
|
|
389
|
+
*
|
|
390
|
+
* @param {string} haskoinUrl The haskoin API url.
|
|
391
|
+
* @param {string} txId The transaction id.
|
|
392
|
+
* @returns {Transaction}
|
|
393
|
+
*
|
|
394
|
+
* @throws {"failed to query transaction by a given hash"} thrown if failed to query transaction by a given hash
|
|
395
|
+
*/
|
|
396
|
+
const getTx$1 = ({ haskoinUrl, txId, network }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
397
|
+
const result = (yield axios.get(`${haskoinUrl}/${network}/transaction/${txId}`)).data;
|
|
398
|
+
if (!result || isErrorResponse(result))
|
|
399
|
+
throw new Error(`failed to query transaction by a given hash ${txId}`);
|
|
400
|
+
return result;
|
|
401
|
+
});
|
|
402
|
+
/**
|
|
403
|
+
* Get raw transaction by hash.
|
|
404
|
+
*
|
|
405
|
+
* @param {string} haskoinUrl The haskoin API url.
|
|
406
|
+
* @param {string} txId The transaction id.
|
|
407
|
+
* @returns {Transaction}
|
|
408
|
+
*
|
|
409
|
+
* @throws {"failed to query transaction by a given hash"} thrown if failed to query raw transaction by a given hash
|
|
410
|
+
*/
|
|
411
|
+
const getRawTransaction = ({ haskoinUrl, network, txId }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
412
|
+
const result = (yield axios.get(`${haskoinUrl}/${network}/transaction/${txId}/raw`))
|
|
413
|
+
.data;
|
|
414
|
+
if (!result || isErrorResponse(result))
|
|
415
|
+
throw new Error(`failed to query transaction by a given hash ${txId}`);
|
|
416
|
+
return result.result;
|
|
417
|
+
});
|
|
418
|
+
/**
|
|
419
|
+
* Get transactions
|
|
420
|
+
*
|
|
421
|
+
* @see https://haskoin.com/api#get-tx
|
|
422
|
+
*
|
|
423
|
+
* @param {string} haskoinUrl The haskoin node url.
|
|
424
|
+
* @param {string} network network id
|
|
425
|
+
* @param {string} hash The transaction hash.
|
|
426
|
+
* @returns {Transactions}
|
|
427
|
+
*/
|
|
428
|
+
const getTxs$1 = ({ address, haskoinUrl, network, limit, offset, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
429
|
+
const params = { limit: `${limit}`, offset: `${offset}` };
|
|
430
|
+
const url = `${haskoinUrl}/${network}/address/${address}/transactions/full`;
|
|
431
|
+
const result = (yield axios.get(url, { params })).data;
|
|
432
|
+
if (!result || isErrorResponse(result))
|
|
433
|
+
throw new Error('failed to query transactions');
|
|
434
|
+
return result;
|
|
435
|
+
});
|
|
436
|
+
/**
|
|
437
|
+
*
|
|
438
|
+
* @param param
|
|
439
|
+
* @returns Returns BaseAmount
|
|
440
|
+
*/
|
|
441
|
+
const getBalance$1 = ({ haskoinUrl, haskoinNetwork, address, confirmedOnly, assetDecimals, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
442
|
+
const { data: { confirmed, unconfirmed }, } = yield axios.get(`${haskoinUrl}/${haskoinNetwork}/address/${address}/balance`);
|
|
443
|
+
const confirmedAmount = baseAmount(confirmed, assetDecimals);
|
|
444
|
+
const unconfirmedAmount = baseAmount(unconfirmed, assetDecimals);
|
|
445
|
+
return confirmedOnly ? confirmedAmount : confirmedAmount.plus(unconfirmedAmount);
|
|
446
|
+
});
|
|
447
|
+
/**
|
|
448
|
+
* Get unspent transactions.
|
|
449
|
+
*
|
|
450
|
+
* @param {string} haskoinUrl The haskoin API url.
|
|
451
|
+
* @param {string} address The BCH address.
|
|
452
|
+
* @returns {TxUnspent[]}
|
|
453
|
+
*
|
|
454
|
+
* @throws {"failed to query unspent transactions"} thrown if failed to query unspent transactions
|
|
455
|
+
*/
|
|
456
|
+
const getUnspentTxs = ({ haskoinUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
457
|
+
// Get transaction count for a given address.
|
|
458
|
+
const account = yield getAccount({ haskoinUrl, network, address });
|
|
459
|
+
// Set limit to the transaction count to be all the utxos.
|
|
460
|
+
const result = (yield axios.get(`${haskoinUrl}/${network}/address/${address}/unspent?limit${account === null || account === void 0 ? void 0 : account.utxo}`)).data;
|
|
461
|
+
if (!result || isErrorResponse(result))
|
|
462
|
+
throw new Error('failed to query unspent transactions');
|
|
463
|
+
return result;
|
|
464
|
+
});
|
|
465
|
+
/**
|
|
466
|
+
* Get Tx Confirmation status
|
|
467
|
+
*
|
|
468
|
+
* @param {string} haskoinUrl The haskoin node url.
|
|
469
|
+
* @param {Network} network
|
|
470
|
+
* @param {string} hash tx id
|
|
471
|
+
* @returns {TxConfirmedStatus}
|
|
472
|
+
*/
|
|
473
|
+
const getIsTxConfirmed = ({ haskoinUrl, network, txId }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
474
|
+
const tx = yield getTx$1({ haskoinUrl, network, txId });
|
|
475
|
+
return {
|
|
476
|
+
network: network,
|
|
477
|
+
txid: txId,
|
|
478
|
+
confirmations: tx.confirmations,
|
|
479
|
+
is_confirmed: tx.confirmations >= 1,
|
|
480
|
+
};
|
|
481
|
+
});
|
|
482
|
+
/**
|
|
483
|
+
* List of confirmed txs
|
|
484
|
+
*
|
|
485
|
+
* Stores a list of confirmed txs (hashes) in memory to avoid requesting same data
|
|
486
|
+
*/
|
|
487
|
+
const confirmedTxs = [];
|
|
488
|
+
/**
|
|
489
|
+
* Helper to get `confirmed` status of a tx.
|
|
490
|
+
*
|
|
491
|
+
* It will get it from cache or try to get it from haskoin (if not cached before)
|
|
492
|
+
*/
|
|
493
|
+
const getConfirmedTxStatus = ({ txHash, haskoinUrl, network, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
494
|
+
// try to get it from cache
|
|
495
|
+
if (confirmedTxs.includes(txHash))
|
|
496
|
+
return true;
|
|
497
|
+
// or get status from haskoin
|
|
498
|
+
const { is_confirmed } = yield getIsTxConfirmed({
|
|
499
|
+
haskoinUrl,
|
|
500
|
+
network,
|
|
501
|
+
txId: txHash,
|
|
502
|
+
});
|
|
503
|
+
// cache status
|
|
504
|
+
confirmedTxs.push(txHash);
|
|
505
|
+
return is_confirmed;
|
|
506
|
+
});
|
|
507
|
+
/**
|
|
508
|
+
* Get unspent txs and filter out pending UTXOs
|
|
509
|
+
*
|
|
510
|
+
* @see https://haskoin.com/api#get-unspent-tx
|
|
511
|
+
*
|
|
512
|
+
* @param {string} haskoinUrl The haskoin node url.
|
|
513
|
+
* @param {Network} network
|
|
514
|
+
* @param {string} address
|
|
515
|
+
* @returns {AddressUTXO[]}
|
|
516
|
+
*/
|
|
517
|
+
const getConfirmedUnspentTxs = ({ haskoinUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
518
|
+
const txs = yield getUnspentTxs({
|
|
519
|
+
haskoinUrl,
|
|
520
|
+
network,
|
|
521
|
+
address,
|
|
522
|
+
});
|
|
523
|
+
const confirmedUTXOs = [];
|
|
524
|
+
yield Promise.all(txs.map((tx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
525
|
+
const confirmed = yield getConfirmedTxStatus({
|
|
526
|
+
haskoinUrl,
|
|
527
|
+
network,
|
|
528
|
+
txHash: tx.txid,
|
|
529
|
+
});
|
|
530
|
+
if (confirmed) {
|
|
531
|
+
confirmedUTXOs.push(tx);
|
|
532
|
+
}
|
|
533
|
+
})));
|
|
534
|
+
return confirmedUTXOs;
|
|
535
|
+
});
|
|
536
|
+
// Stores list of txHex in memory to avoid requesting same data
|
|
537
|
+
const txHexMap = {};
|
|
538
|
+
/**
|
|
539
|
+
* Helper to get `hex` of `Tx`
|
|
540
|
+
*
|
|
541
|
+
* It will try to get it from cache before requesting it from Sochain
|
|
542
|
+
*/
|
|
543
|
+
const getTxHex = ({ haskoinUrl, network, txId }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
544
|
+
// try to get hex from cache
|
|
545
|
+
let txHex = txHexMap[txId];
|
|
546
|
+
if (!!txHex)
|
|
547
|
+
return txHex;
|
|
548
|
+
// or get it from Haskoin
|
|
549
|
+
txHex = yield getRawTransaction({ haskoinUrl, txId: txId, network: network });
|
|
550
|
+
// cache it
|
|
551
|
+
txHexMap[txId] = txHex;
|
|
552
|
+
return txHex;
|
|
553
|
+
});
|
|
554
|
+
/**
|
|
555
|
+
* Get unspent transactions.
|
|
556
|
+
*
|
|
557
|
+
* @param {string} haskoinUrl The haskoin API url.
|
|
558
|
+
* @param {string} address The BCH address.
|
|
559
|
+
* @returns {TxUnspent[]}
|
|
560
|
+
*
|
|
561
|
+
* @throws {"failed to query unspent transactions"} thrown if failed to query unspent transactions
|
|
562
|
+
*/
|
|
563
|
+
const getUnspentTransactions = ({ haskoinUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
564
|
+
// Get transaction count for a given address.
|
|
565
|
+
const account = yield getAccount({ haskoinUrl, network, address });
|
|
566
|
+
// Set limit to the transaction count to be all the utxos.
|
|
567
|
+
const result = (yield axios.get(`${haskoinUrl}/${network}/address/${address}/unspent?limit${account === null || account === void 0 ? void 0 : account.utxo}`)).data;
|
|
568
|
+
if (!result || isErrorResponse(result))
|
|
569
|
+
throw new Error('failed to query unspent transactions');
|
|
570
|
+
return result;
|
|
571
|
+
});
|
|
572
|
+
/**
|
|
573
|
+
* Broadcast transaction.
|
|
574
|
+
*
|
|
575
|
+
* @see https://app.swaggerhub.com/apis/eligecode/blockchain-api/0.0.1-oas3#/blockchain/sendTransaction
|
|
576
|
+
*
|
|
577
|
+
* Note: Because of an Haskoin issue (@see https://github.com/haskoin/haskoin-store/issues/25),
|
|
578
|
+
* we need to broadcast same tx several times in case of `500` errors
|
|
579
|
+
* @see https://github.com/xchainjs/xchainjs-lib/issues/492
|
|
580
|
+
*
|
|
581
|
+
* @param {BroadcastTxParams} params
|
|
582
|
+
* @returns {TxHash} Transaction hash.
|
|
583
|
+
*/
|
|
584
|
+
const broadcastTx$1 = ({ txHex, haskoinUrl, haskoinNetwork, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
585
|
+
var _a;
|
|
586
|
+
const MAX_RETRIES = 5;
|
|
587
|
+
let retries = 0;
|
|
588
|
+
const axiosInstance = axios.create();
|
|
589
|
+
const url = `${haskoinUrl}/${haskoinNetwork}/transactions`;
|
|
590
|
+
while (retries < MAX_RETRIES) {
|
|
591
|
+
try {
|
|
592
|
+
const response = yield axiosInstance.post(url, txHex);
|
|
593
|
+
const { txid } = response.data;
|
|
594
|
+
return txid;
|
|
595
|
+
}
|
|
596
|
+
catch (error) {
|
|
597
|
+
if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 500) {
|
|
598
|
+
retries++;
|
|
599
|
+
yield delay(200 * retries);
|
|
600
|
+
}
|
|
601
|
+
else {
|
|
602
|
+
return Promise.reject(error);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return Promise.reject(new Error('Max retries exceeded'));
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
class HaskoinProvider {
|
|
610
|
+
constructor(baseUrl = 'https://api.haskoin.com/', chain, asset, assetDecimals, haskoinNetwork) {
|
|
611
|
+
this.baseUrl = baseUrl;
|
|
612
|
+
this.chain = chain;
|
|
613
|
+
this.asset = asset;
|
|
614
|
+
this.assetDecimals = assetDecimals;
|
|
615
|
+
this.haskoinNetwork = haskoinNetwork;
|
|
616
|
+
this.asset;
|
|
617
|
+
this.chain;
|
|
618
|
+
}
|
|
619
|
+
broadcastTx(txHex) {
|
|
620
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
621
|
+
return yield broadcastTx$1({
|
|
622
|
+
haskoinUrl: this.baseUrl,
|
|
623
|
+
haskoinNetwork: this.haskoinNetwork,
|
|
624
|
+
txHex,
|
|
625
|
+
});
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
getConfirmedUnspentTxs(address) {
|
|
629
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
630
|
+
const allUnspent = yield getUnspentTransactions({
|
|
631
|
+
haskoinUrl: this.baseUrl,
|
|
632
|
+
network: this.haskoinNetwork,
|
|
633
|
+
address,
|
|
634
|
+
});
|
|
635
|
+
const confirmedUnspent = allUnspent.filter((i) => i.block); //if it has a block noumber it's been confirmed
|
|
636
|
+
return this.mapUTXOs(confirmedUnspent);
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
getUnspentTxs(address) {
|
|
640
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
641
|
+
const allUnspent = yield getUnspentTxs({
|
|
642
|
+
haskoinUrl: this.baseUrl,
|
|
643
|
+
network: this.haskoinNetwork,
|
|
644
|
+
address,
|
|
645
|
+
});
|
|
646
|
+
return yield this.mapUTXOs(allUnspent);
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
getBalance(address, assets /*ignored*/, confirmedOnly) {
|
|
650
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
651
|
+
const amount = yield getBalance$1({
|
|
652
|
+
haskoinUrl: this.baseUrl,
|
|
653
|
+
haskoinNetwork: this.haskoinNetwork,
|
|
654
|
+
address,
|
|
655
|
+
confirmedOnly: !!confirmedOnly,
|
|
656
|
+
assetDecimals: this.assetDecimals,
|
|
657
|
+
});
|
|
658
|
+
return [{ amount, asset: this.asset }];
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Get transaction history of a given address with pagination options.
|
|
663
|
+
* By default it will return the transaction history of the current wallet.
|
|
664
|
+
*
|
|
665
|
+
* @param {TxHistoryParams} params The options to get transaction history. (optional)
|
|
666
|
+
* @returns {TxsPage} The transaction history.
|
|
667
|
+
*/
|
|
668
|
+
getTransactions(params) {
|
|
669
|
+
var _a;
|
|
670
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
671
|
+
const offset = (_a = params === null || params === void 0 ? void 0 : params.offset) !== null && _a !== void 0 ? _a : 0;
|
|
672
|
+
const limit = (params === null || params === void 0 ? void 0 : params.limit) || 10;
|
|
673
|
+
if (offset + limit > 2000)
|
|
674
|
+
throw Error('cannot fetch more than last 2000 txs');
|
|
675
|
+
if (offset < 0 || limit < 0)
|
|
676
|
+
throw Error('ofset and limit must be equal or greater than 0');
|
|
677
|
+
const response = yield getTxs$1({
|
|
678
|
+
address: `${params === null || params === void 0 ? void 0 : params.address}`,
|
|
679
|
+
haskoinUrl: this.baseUrl,
|
|
680
|
+
network: this.haskoinNetwork,
|
|
681
|
+
limit: limit,
|
|
682
|
+
offset,
|
|
683
|
+
});
|
|
684
|
+
const txs = response.map((i) => this.mapTransactionToTx(i));
|
|
685
|
+
const result = {
|
|
686
|
+
total: txs.length,
|
|
687
|
+
txs: txs,
|
|
688
|
+
};
|
|
689
|
+
return result;
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
mapTransactionToTx(rawTx) {
|
|
693
|
+
return {
|
|
694
|
+
asset: this.asset,
|
|
695
|
+
from: rawTx.inputs.map((i) => ({
|
|
696
|
+
from: i.address,
|
|
697
|
+
amount: baseAmount(i.value, this.assetDecimals),
|
|
698
|
+
})),
|
|
699
|
+
to: rawTx.outputs
|
|
700
|
+
.filter((i) => i.script !== 'null-data') //filter out op_return outputs
|
|
701
|
+
.map((i) => ({ to: i.address, amount: baseAmount(i.value, this.assetDecimals) })),
|
|
702
|
+
date: new Date(rawTx.time),
|
|
703
|
+
type: TxType.Transfer,
|
|
704
|
+
hash: rawTx.txid,
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Get the transaction details of a given transaction id.
|
|
709
|
+
*
|
|
710
|
+
* @param {string} txId The transaction id.
|
|
711
|
+
* @returns {Tx} The transaction details of the given transaction id.
|
|
712
|
+
*/
|
|
713
|
+
getTransactionData(txId) {
|
|
714
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
715
|
+
try {
|
|
716
|
+
const rawTx = yield getTx$1({
|
|
717
|
+
haskoinUrl: this.baseUrl,
|
|
718
|
+
network: this.haskoinNetwork,
|
|
719
|
+
txId: txId,
|
|
720
|
+
});
|
|
721
|
+
return this.mapTransactionToTx(rawTx);
|
|
722
|
+
}
|
|
723
|
+
catch (error) {
|
|
724
|
+
console.error(error);
|
|
725
|
+
throw error;
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
*
|
|
731
|
+
* @param utxos
|
|
732
|
+
* @returns utxo array
|
|
733
|
+
*/
|
|
734
|
+
mapUTXOs(utxos) {
|
|
735
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
736
|
+
return yield Promise.all(utxos.map((utxo) => __awaiter(this, void 0, void 0, function* () {
|
|
737
|
+
return ({
|
|
738
|
+
hash: utxo.txid,
|
|
739
|
+
index: utxo.index,
|
|
740
|
+
value: baseAmount(utxo.value, this.assetDecimals).amount().toNumber(),
|
|
741
|
+
witnessUtxo: {
|
|
742
|
+
value: baseAmount(utxo.value, this.assetDecimals).amount().toNumber(),
|
|
743
|
+
script: Buffer.from(utxo.pkscript, 'hex'),
|
|
744
|
+
},
|
|
745
|
+
txHex: yield getTxHex({ haskoinUrl: this.baseUrl, txId: utxo.txid, network: this.haskoinNetwork }),
|
|
746
|
+
});
|
|
747
|
+
})));
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Get transaction by hash.
|
|
754
|
+
*
|
|
755
|
+
*
|
|
756
|
+
* @param {string} baseUrl The sochain node url.
|
|
757
|
+
* @param {string} network network id
|
|
758
|
+
* @param {string} hash The transaction hash.
|
|
759
|
+
* @returns {Transactions}
|
|
760
|
+
*/
|
|
761
|
+
const getTx = ({ apiKey, baseUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
762
|
+
const params = { includeHex: 'true' };
|
|
763
|
+
if (apiKey)
|
|
764
|
+
params['token'] = apiKey;
|
|
765
|
+
const url = `${baseUrl}/${network}/txs/${hash}`;
|
|
766
|
+
const response = yield axios.get(url, { params });
|
|
767
|
+
const tx = response.data;
|
|
768
|
+
return tx;
|
|
769
|
+
});
|
|
770
|
+
/**
|
|
771
|
+
* Get transactions
|
|
772
|
+
*
|
|
773
|
+
*
|
|
774
|
+
* @param {string} baseUrl The sochain node url.
|
|
775
|
+
* @param {string} network network id
|
|
776
|
+
* @param {string} hash The transaction hash.
|
|
777
|
+
* @returns {Transactions}
|
|
778
|
+
*/
|
|
779
|
+
const getTxs = ({ apiKey, address, baseUrl, network, beforeBlock, limit, unspentOnly, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
780
|
+
const params = { limit: `${limit}`, unspentOnly: `${unspentOnly}` };
|
|
781
|
+
const url = `${baseUrl}/${network}/addrs/${address}`;
|
|
782
|
+
if (apiKey)
|
|
783
|
+
params['token'] = apiKey;
|
|
784
|
+
if (beforeBlock)
|
|
785
|
+
params['before'] = `${beforeBlock}`;
|
|
786
|
+
const response = yield axios.get(url, { params });
|
|
787
|
+
const txs = response.data;
|
|
788
|
+
return txs;
|
|
789
|
+
});
|
|
790
|
+
/**
|
|
791
|
+
* Get address balance.
|
|
792
|
+
*
|
|
793
|
+
*
|
|
794
|
+
* @param {string} baseUrl The sochain node url.
|
|
795
|
+
* @param {string} network Network
|
|
796
|
+
* @param {string} address Address
|
|
797
|
+
* @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
|
|
798
|
+
* @returns {number}
|
|
799
|
+
*/
|
|
800
|
+
const getBalance = ({ apiKey, baseUrl, network, address, confirmedOnly, assetDecimals, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
801
|
+
const params = {};
|
|
802
|
+
const url = `${baseUrl}/${network}/addrs/${address}/balance`;
|
|
803
|
+
if (apiKey)
|
|
804
|
+
params['token'] = apiKey;
|
|
805
|
+
const response = yield axios.get(url, { params });
|
|
806
|
+
const balanceResponse = response.data;
|
|
807
|
+
const confirmedAmount = baseAmount(balanceResponse.balance, assetDecimals);
|
|
808
|
+
const finalBalance = baseAmount(balanceResponse.final_balance, assetDecimals);
|
|
809
|
+
return confirmedOnly ? confirmedAmount : finalBalance;
|
|
810
|
+
});
|
|
811
|
+
const broadcastTx = ({ apiKey, baseUrl, network, txHex, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
812
|
+
const params = {};
|
|
813
|
+
const url = `${baseUrl}/${network}/txs/push`;
|
|
814
|
+
if (apiKey)
|
|
815
|
+
params['token'] = apiKey;
|
|
816
|
+
const response = yield axios.post(url, { tx: txHex }, { params });
|
|
817
|
+
const broadcastResponse = response.data;
|
|
818
|
+
return broadcastResponse.tx.hash;
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
class BlockcypherProvider {
|
|
822
|
+
constructor(baseUrl = 'https://api.blockcypher.com/v1/', chain, asset, assetDecimals, blockcypherNetwork, apiKey) {
|
|
823
|
+
this.baseUrl = baseUrl;
|
|
824
|
+
this._apiKey = apiKey;
|
|
825
|
+
this.chain = chain;
|
|
826
|
+
this.asset = asset;
|
|
827
|
+
this.assetDecimals = assetDecimals;
|
|
828
|
+
this.blockcypherNetwork = blockcypherNetwork;
|
|
829
|
+
this.asset;
|
|
830
|
+
this.chain;
|
|
831
|
+
}
|
|
832
|
+
get apiKey() {
|
|
833
|
+
return this._apiKey;
|
|
834
|
+
}
|
|
835
|
+
set apiKey(value) {
|
|
836
|
+
this._apiKey = value;
|
|
837
|
+
}
|
|
838
|
+
broadcastTx(txHex) {
|
|
839
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
840
|
+
return yield broadcastTx({
|
|
841
|
+
apiKey: this._apiKey,
|
|
842
|
+
baseUrl: this.baseUrl,
|
|
843
|
+
network: this.blockcypherNetwork,
|
|
844
|
+
txHex,
|
|
845
|
+
});
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
getConfirmedUnspentTxs(address) {
|
|
849
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
850
|
+
const allUnspent = yield this.getRawTransactions({ address, offset: 0, limit: 2000 }, true);
|
|
851
|
+
return this.mapUTXOs(address, allUnspent.filter((i) => i.confirmed));
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
getUnspentTxs(address) {
|
|
855
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
856
|
+
const allUnspent = yield this.getRawTransactions({ address, offset: 0, limit: 2000 }, true);
|
|
857
|
+
return this.mapUTXOs(address, allUnspent);
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
getBalance(address, assets /*ignored*/, confirmedOnly) {
|
|
861
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
862
|
+
const amount = yield getBalance({
|
|
863
|
+
apiKey: this._apiKey,
|
|
864
|
+
baseUrl: this.baseUrl,
|
|
865
|
+
network: this.blockcypherNetwork,
|
|
866
|
+
address,
|
|
867
|
+
confirmedOnly: !!confirmedOnly,
|
|
868
|
+
assetDecimals: this.assetDecimals,
|
|
869
|
+
});
|
|
870
|
+
return [{ amount, asset: this.asset }];
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Get transaction history of a given address with pagination options.
|
|
875
|
+
* By default it will return the transaction history of the current wallet.
|
|
876
|
+
*
|
|
877
|
+
* @param {TxHistoryParams} params The options to get transaction history. (optional)
|
|
878
|
+
* @returns {TxsPage} The transaction history.
|
|
879
|
+
*/
|
|
880
|
+
getTransactions(params, unspentOnly = false) {
|
|
881
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
882
|
+
const rawTxs = yield this.getRawTransactions(params, unspentOnly);
|
|
883
|
+
const txs = rawTxs.map((i) => this.mapTransactionToTx(i));
|
|
884
|
+
const result = {
|
|
885
|
+
total: txs.length,
|
|
886
|
+
txs,
|
|
887
|
+
};
|
|
888
|
+
return result;
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Get the transaction details of a given transaction id.
|
|
893
|
+
*
|
|
894
|
+
* @param {string} txId The transaction id.
|
|
895
|
+
* @returns {Tx} The transaction details of the given transaction id.
|
|
896
|
+
*/
|
|
897
|
+
getTransactionData(txId) {
|
|
898
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
899
|
+
try {
|
|
900
|
+
const rawTx = yield getTx({
|
|
901
|
+
apiKey: this._apiKey,
|
|
902
|
+
baseUrl: this.baseUrl,
|
|
903
|
+
network: this.blockcypherNetwork,
|
|
904
|
+
hash: txId,
|
|
905
|
+
});
|
|
906
|
+
return this.mapTransactionToTx(rawTx);
|
|
907
|
+
}
|
|
908
|
+
catch (error) {
|
|
909
|
+
console.error(error);
|
|
910
|
+
throw error;
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
mapTransactionToTx(rawTx) {
|
|
915
|
+
return {
|
|
916
|
+
asset: this.asset,
|
|
917
|
+
from: rawTx.inputs.map((i) => ({
|
|
918
|
+
from: i.addresses[0],
|
|
919
|
+
amount: baseAmount(i.output_value, this.assetDecimals),
|
|
920
|
+
})),
|
|
921
|
+
to: rawTx.outputs
|
|
922
|
+
.filter((i) => i.script_type !== 'null-data') //filter out op_return outputs
|
|
923
|
+
.map((i) => ({ to: i.addresses[0], amount: baseAmount(i.value, this.assetDecimals) })),
|
|
924
|
+
date: new Date(rawTx.confirmed),
|
|
925
|
+
type: TxType.Transfer,
|
|
926
|
+
hash: rawTx.hash,
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
mapUTXOs(address, utxos) {
|
|
930
|
+
const utxosOut = [];
|
|
931
|
+
for (let index = 0; index < utxos.length; index++) {
|
|
932
|
+
const utxo = utxos[index];
|
|
933
|
+
for (let index2 = 0; index2 < utxo.outputs.length; index2++) {
|
|
934
|
+
const output = utxo.outputs[index2];
|
|
935
|
+
if (output.addresses && output.addresses[0] === address) {
|
|
936
|
+
const utxoOut = {
|
|
937
|
+
hash: utxo.hash,
|
|
938
|
+
index: index2,
|
|
939
|
+
value: baseAmount(output.value, this.assetDecimals).amount().toNumber(),
|
|
940
|
+
witnessUtxo: {
|
|
941
|
+
value: baseAmount(output.value, this.assetDecimals).amount().toNumber(),
|
|
942
|
+
script: Buffer.from(output.script, 'hex'),
|
|
943
|
+
},
|
|
944
|
+
txHex: utxo.hex,
|
|
945
|
+
};
|
|
946
|
+
utxosOut.push(utxoOut);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return utxosOut;
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* helper function tto limit adding to an array
|
|
954
|
+
*
|
|
955
|
+
* @param arr array to be added to
|
|
956
|
+
* @param toAdd elements to add
|
|
957
|
+
* @param limit do not add more than this limit
|
|
958
|
+
*/
|
|
959
|
+
addArrayUpToLimit(arr, toAdd, limit) {
|
|
960
|
+
for (let index = 0; index < toAdd.length; index++) {
|
|
961
|
+
const element = toAdd[index];
|
|
962
|
+
if (arr.length < limit) {
|
|
963
|
+
arr.push(element);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
delay(ms) {
|
|
968
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Get transaction history of a given address with pagination options.
|
|
972
|
+
* By default it will return the transaction history of the current wallet.
|
|
973
|
+
*
|
|
974
|
+
* @param {TxHistoryParams} params The options to get transaction history. (optional)
|
|
975
|
+
* @returns {TxsPage} The transaction history.
|
|
976
|
+
*/
|
|
977
|
+
getRawTransactions(params, unspentOnly = false) {
|
|
978
|
+
var _a;
|
|
979
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
980
|
+
const offset = (_a = params === null || params === void 0 ? void 0 : params.offset) !== null && _a !== void 0 ? _a : 0;
|
|
981
|
+
const limit = (params === null || params === void 0 ? void 0 : params.limit) || 10;
|
|
982
|
+
if (offset + limit > 2000)
|
|
983
|
+
throw Error('cannot fetch more than last 2000 txs');
|
|
984
|
+
if (offset < 0 || limit < 0)
|
|
985
|
+
throw Error('ofset and limit must be equal or greater than 0');
|
|
986
|
+
const txHashesToFetch = [];
|
|
987
|
+
try {
|
|
988
|
+
const response = yield getTxs({
|
|
989
|
+
apiKey: this._apiKey,
|
|
990
|
+
baseUrl: this.baseUrl,
|
|
991
|
+
network: this.blockcypherNetwork,
|
|
992
|
+
address: `${params === null || params === void 0 ? void 0 : params.address}`,
|
|
993
|
+
limit: 2000,
|
|
994
|
+
unspentOnly,
|
|
995
|
+
});
|
|
996
|
+
//remove duplicates
|
|
997
|
+
const txs = response.txrefs.map((i) => i.tx_hash);
|
|
998
|
+
const uniqTxs = [...new Set(txs)];
|
|
999
|
+
const start = offset >= uniqTxs.length ? uniqTxs.length : offset;
|
|
1000
|
+
const end = offset + limit >= uniqTxs.length ? uniqTxs.length : offset + limit;
|
|
1001
|
+
const txsToFetch = uniqTxs.slice(start, end);
|
|
1002
|
+
// console.log(JSON.stringify(txsToFetch, null, 2))
|
|
1003
|
+
this.addArrayUpToLimit(txHashesToFetch, txsToFetch, limit);
|
|
1004
|
+
}
|
|
1005
|
+
catch (error) {
|
|
1006
|
+
console.error(error);
|
|
1007
|
+
//an errors means no more results
|
|
1008
|
+
}
|
|
1009
|
+
//note: blockcypher has rate of 3 req/sec --> https://www.blockcypher.com/dev/bitcoin/#rate-limits-and-tokens
|
|
1010
|
+
const batchResult = yield PromisePool.for(txHashesToFetch)
|
|
1011
|
+
.withConcurrency(3)
|
|
1012
|
+
.useCorrespondingResults()
|
|
1013
|
+
.process((hash) => __awaiter(this, void 0, void 0, function* () {
|
|
1014
|
+
yield this.delay(1000);
|
|
1015
|
+
const rawTx = yield getTx({
|
|
1016
|
+
apiKey: this._apiKey,
|
|
1017
|
+
baseUrl: this.baseUrl,
|
|
1018
|
+
network: this.blockcypherNetwork,
|
|
1019
|
+
hash,
|
|
1020
|
+
});
|
|
1021
|
+
return rawTx;
|
|
1022
|
+
}));
|
|
1023
|
+
return batchResult.results;
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
var BlockcypherNetwork;
|
|
1029
|
+
(function (BlockcypherNetwork) {
|
|
1030
|
+
BlockcypherNetwork["BTC"] = "btc/main";
|
|
1031
|
+
BlockcypherNetwork["BTCTEST"] = "btc/test3";
|
|
1032
|
+
BlockcypherNetwork["LTC"] = "ltc/main";
|
|
1033
|
+
BlockcypherNetwork["DOGE"] = "doge/main";
|
|
1034
|
+
})(BlockcypherNetwork || (BlockcypherNetwork = {}));
|
|
1035
|
+
|
|
1036
|
+
export { BlockcypherNetwork, BlockcypherProvider, HaskoinNetwork, HaskoinProvider, SochainNetwork, SochainProvider, broadcastTx$1 as broadcastTx, getAccount, getAddress, getBalance$1 as getBalance, getConfirmedTxStatus, getConfirmedUnspentTxs, getIsTxConfirmed, getTx$1 as getTx, getTxs$1 as getTxs, getUnspentTxs };
|