-
Notifications
You must be signed in to change notification settings - Fork 1
/
esplora.ts
35 lines (31 loc) · 985 Bytes
/
esplora.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
import axios from "axios";
import { Transaction } from "./transaction";
import { BlockchainClient } from "./client";
export class EsploraClient implements BlockchainClient {
url: string;
constructor(url: string) {
this.url = url;
}
private async _getBlockHeight() {
// cache it
return (await axios.get(`${this.url}/blocks/tip/height`)).data;
}
async getAddressTransactions(address: string): Promise<Transaction[]> {
const height = await this._getBlockHeight();
const txs = (await axios.get(`${this.url}/address/${address}/txs`)).data;
return txs.map((tx: any) => {
const confirmations = tx.status.confirmed
? height - tx.status.block_height
: 0;
const output = tx.vout.find(
(vout: any) => vout.scriptpubkey_address === address
);
const transaction: Transaction = {
hash: tx.txid,
amount: output.value / 1e8,
confirmations,
};
return transaction;
});
}
}