-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathwebsocket.ts
100 lines (77 loc) · 2.73 KB
/
websocket.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
import WebSocket from 'ws';
import type {
Send,
BlockContent,
BlockfrostTransaction,
} from '@trezor/blockchain-link-types/src/blockfrost';
import type {
AccountInfoParams,
EstimateFeeParams,
AccountBalanceHistoryParams,
} from '@trezor/blockchain-link-types/src/params';
import { getSuiteVersion } from '@trezor/env-utils';
import { BaseWebsocket } from '../baseWebsocket';
interface BlockfrostEvents {
block: BlockContent;
notification: BlockfrostTransaction;
}
export class BlockfrostAPI extends BaseWebsocket<BlockfrostEvents> {
protected createWebsocket() {
const { url } = this.options;
// options are not used in web builds (see ./src/utils/ws)
return new WebSocket(url, {
agent: this.options.agent,
headers: {
Origin: 'https://node.trezor.io',
'User-Agent': `Trezor Suite ${getSuiteVersion()}`,
...this.options.headers,
},
});
}
protected ping() {
return this.getBlockHash(1);
}
send: Send = (command, params = {}) => this.sendMessage({ command, params });
getServerInfo() {
return this.send('GET_SERVER_INFO');
}
getBlockHash(number: number) {
return this.send('GET_BLOCK', { hashOrNumber: number });
}
estimateFee(payload: EstimateFeeParams) {
return this.send('ESTIMATE_FEE', payload);
}
getAccountInfo(payload: AccountInfoParams) {
return this.send('GET_ACCOUNT_INFO', payload);
}
getAccountUtxo(descriptor: string) {
return this.send('GET_ACCOUNT_UTXO', { descriptor });
}
getAccountBalanceHistory(payload: AccountBalanceHistoryParams) {
return this.send('GET_BALANCE_HISTORY', payload);
}
getTransaction(txId: string) {
return this.send('GET_TRANSACTION', { txId });
}
pushTransaction(txData: string) {
return this.send('PUSH_TRANSACTION', { txData });
}
subscribeBlock() {
this.removeSubscription('block');
this.addSubscription('block', result => this.emit('block', result));
return this.send('SUBSCRIBE_BLOCK');
}
subscribeAddresses(addresses: string[]) {
this.removeSubscription('notification');
this.addSubscription('notification', result => this.emit('notification', result));
return this.send('SUBSCRIBE_ADDRESS', { addresses });
}
unsubscribeBlock() {
const index = this.removeSubscription('block');
return index >= 0 ? this.send('UNSUBSCRIBE_BLOCK') : { subscribed: false };
}
unsubscribeAddresses() {
const index = this.removeSubscription('notification');
return index >= 0 ? this.send('UNSUBSCRIBE_ADDRESS') : { subscribed: false };
}
}