This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitcoin.ts
131 lines (112 loc) · 2.88 KB
/
bitcoin.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {ApiConnection} from '../common/connection.js';
import {joinUrl} from '../common/utils.js';
import {
ChainInfo,
MempoolInfo,
MiningInfo,
NetworkInfo,
BitcoinTransaction,
} from './bitcoin-types';
export type SyncStatus = {
chain: string;
percent: number;
currentBlock: number;
headerCount: number;
};
export type StatsDump = {
blockchainInfo: ChainInfo;
networkInfo: NetworkInfo;
mempoolInfo: MempoolInfo;
miningInfo: MiningInfo;
};
export type Stats = {
difficulty: number;
size: number;
mempool: number;
connections: number;
networkhashps: number;
};
export type Block = {
block: string;
confirmations: number;
size: number;
height: number;
blocktime: number;
prevblock: string;
nextblock: string | undefined;
transactions: string | BitcoinTransaction[];
};
export type BasicBlock = {
hash: string;
height: number;
numTransactions: number;
confirmations: number;
time: number;
size: number;
};
export type Transaction = {
txid: string;
timestamp: number;
confirmations: number;
blockhash: string;
size: number;
input: string;
utxo: unknown;
rawtx: string;
};
export class MiddlewareBitcoin extends ApiConnection {
constructor(baseUrl: string) {
super(joinUrl(baseUrl, `v1/bitcoin/info`));
}
async mempool(): Promise<MempoolInfo> {
return await this.get<MempoolInfo>('/mempool');
}
async isInstalled(): Promise<boolean> {
return (await this.get<{installed: boolean}>('/isInstalled')).installed;
}
async blockcount(): Promise<number> {
return (await this.get<{count: number}>('/blockcount')).count;
}
async connections(): Promise<{
total: number;
inbound: number;
outbound: number;
}> {
return (
await this.get<{
count: {
total: number;
inbound: number;
outbound: number;
};
}>('/connections')
).count;
}
async isOperational(): Promise<boolean> {
return (await this.get<{operational: boolean}>('/status')).operational;
}
async syncStatus(): Promise<SyncStatus> {
return await this.get<SyncStatus>('/sync');
}
async version(): Promise<string> {
return (await this.get<{version: string}>('/version')).version;
}
async statsDump(): Promise<StatsDump> {
return await this.get<StatsDump>('/statsDump');
}
async stats(): Promise<Stats> {
return await this.get<Stats>('/stats');
}
async blockHash(height: number): Promise<string> {
return (await this.get<{hash: string}>(`/block?height=${height}`)).hash;
}
async block(hash: string): Promise<Block> {
return await this.get<Block>(`/block?hash=${hash}`);
}
async blocks(from: number, to: number): Promise<BasicBlock[]> {
return await this.get<BasicBlock[]>(`/blocks?from=${from}&to=${to}`);
}
async getTransaction(txid: string): Promise<Transaction> {
return await this.get<Transaction>(`/txid/${txid}`);
}
}