-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathBitcoinEsploraBatchBaseProvider.ts
50 lines (42 loc) · 1.83 KB
/
BitcoinEsploraBatchBaseProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { HttpClient } from '@liquality/client';
import { AddressType, BigNumber } from '@liquality/types';
import { flatten, uniq } from 'lodash';
import { UTXO } from '../../types';
import { BitcoinEsploraBaseProvider } from './BitcoinEsploraBaseProvider';
import * as EsploraTypes from './types';
interface EsploraBatchApiProviderOptions extends EsploraTypes.EsploraApiProviderOptions {
batchUrl: string;
}
export class BitcoinEsploraBatchBaseProvider extends BitcoinEsploraBaseProvider {
private _batchHttpClient: HttpClient;
constructor(options: EsploraBatchApiProviderOptions) {
super(options);
this._batchHttpClient = new HttpClient({ baseURL: options.batchUrl });
}
async getUnspentTransactions(_addresses: AddressType[]): Promise<UTXO[]> {
const addresses = _addresses.map((a) => a.toString());
const data: EsploraTypes.BatchUTXOs = await this._batchHttpClient.nodePost('/addresses/utxo', {
addresses: uniq(addresses),
});
const utxos = data.map(({ address, utxo }) => {
return utxo.map((obj) => ({
...obj,
address,
satoshis: obj.value,
amount: new BigNumber(obj.value).dividedBy(1e8).toNumber(),
blockHeight: obj.status.block_height,
}));
});
return flatten(utxos);
}
async getAddressTransactionCounts(_addresses: AddressType[]) {
const addresses = _addresses.map((a) => a.toString());
const data: EsploraTypes.Address[] = await this._batchHttpClient.nodePost('/addresses', {
addresses: uniq(addresses),
});
return data.reduce((acc: { [index: string]: number }, obj) => {
acc[obj.address] = obj.chain_stats.tx_count + obj.mempool_stats.tx_count;
return acc;
}, {});
}
}