-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
162 lines (152 loc) · 6.22 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {BlockRecord} from "../../chia/consensus/block_record";
import {bool, float, int, None, str, uint128, uint32, uint64, uint8} from "../../chia/types/_python_types_";
import {TDaemon} from "../../../daemon/index";
import {
GetMessageType,
wallet_ui_service,
metrics_service,
unfinished_block_info_service,
TConnectionGeneral,
} from "../../types";
import {bytes32} from "../../chia/types/blockchain_format/sized_bytes";
import {NewSignagePoint} from "../../chia/protocols/farmer_protocol";
import {ReceiveBlockResult} from "../../chia/consensus/blockchain";
import {UnfinishedBlock} from "../../chia/types/unfinished_block";
export const chia_full_node_service = "chia_full_node";
export type chia_full_node_service = typeof chia_full_node_service;
export type TConnectionFullNode = ({
peak_height: uint32,
peak_weight: uint128,
peak_hash: bytes32,
} | {
peak_height: None,
peak_weight: None,
peak_hash: None,
}) & TConnectionGeneral;
export const get_connections_command = "get_connections";
export type get_connections_command = typeof get_connections_command;
export type TGetConnectionsBroadCast = {
connections: TConnectionFullNode[];
};
export type WsGetConnectionFullNodeMessage = GetMessageType<chia_full_node_service, get_connections_command, TGetConnectionsBroadCast>;
export async function on_get_connections(daemon: TDaemon, callback: (e: WsGetConnectionFullNodeMessage) => unknown){
await daemon.subscribe(wallet_ui_service);
const messageListener = (e: WsFullNodeMessage) => {
if(e.origin === chia_full_node_service && e.command === get_connections_command){
callback(e);
}
};
return daemon.addMessageListener(chia_full_node_service, messageListener);
}
export const get_blockchain_state_command = "get_blockchain_state";
export type get_blockchain_state_command = typeof get_blockchain_state_command;
export type TGetBlockchainStateBroadCast = {
blockchain_state: {
peak: BlockRecord;
genesis_challenge_initialized: bool;
sync: {
sync_mode: bool;
synced: bool;
sync_tip_height: uint32;
sync_progress_height: uint32;
};
difficulty: uint64;
sub_slot_iters: uint64;
space: uint128;
mempool_size: int;
mempool_cost: int;
"mempool_min_fees": {
"cost_5000000": float,
},
"mempool_max_total_cost": int,
"block_max_cost": int,
"node_id": str,
};
};
export type WsGetBlockchainStateMessage = GetMessageType<chia_full_node_service, get_blockchain_state_command, TGetBlockchainStateBroadCast>;
export async function on_get_blockchain_state(daemon: TDaemon, callback: (e: WsGetBlockchainStateMessage) => unknown){
await daemon.subscribe(wallet_ui_service);
const messageListener = (e: WsFullNodeMessage) => {
if(e.origin === chia_full_node_service && e.command === get_blockchain_state_command){
callback(e);
}
};
return daemon.addMessageListener(chia_full_node_service, messageListener);
}
export const block_command = "block";
export type block_command = typeof block_command;
export type TBlockBroadCast = Record<string, never> | {
transaction_block: bool;
k_size: uint8;
header_hash: bytes32;
height: uint32;
block_cost?: uint64;
block_fees?: uint64;
timestamp?: uint64;
transaction_generator_size_bytes?: int;
transaction_generator_ref_list: uint32[];
receive_block_result?: ReceiveBlockResult;
};
export type WsBlockMessage = GetMessageType<chia_full_node_service, block_command, TBlockBroadCast>;
export async function on_block(daemon: TDaemon, callback: (e: WsBlockMessage) => unknown){
await daemon.subscribe(metrics_service);
const messageListener = (e: WsFullNodeMessage) => {
if(e.origin === chia_full_node_service && e.command === block_command){
callback(e);
}
};
return daemon.addMessageListener(chia_full_node_service, messageListener);
}
export const signage_point_command = "signage_point";
export type signage_point_command = typeof signage_point_command;
export type TSignagePointBroadCast = {
broadcast_farmer: NewSignagePoint;
};
export type WsSignagePointMessage = GetMessageType<chia_full_node_service, signage_point_command, TSignagePointBroadCast>;
export async function on_signage_point(daemon: TDaemon, callback: (e: WsSignagePointMessage) => unknown){
await daemon.subscribe(metrics_service);
const messageListener = (e: WsFullNodeMessage) => {
if(e.origin === chia_full_node_service && e.command === signage_point_command){
callback(e);
}
};
return daemon.addMessageListener(chia_full_node_service, messageListener);
}
export const unfinished_block_command = "unfinished_block";
export type unfinished_block_command = typeof unfinished_block_command;
export type TUnfinishedBlockBroadCast = {
block_duration_in_seconds: float;
validation_time_in_seconds: float;
pre_validation_time_in_seconds: float | None;
unfinished_block: UnfinishedBlock;
};
export type WsUnfinishedBlockMessage = GetMessageType<chia_full_node_service, unfinished_block_command, TUnfinishedBlockBroadCast>;
export async function on_unfinished_block(daemon: TDaemon, callback: (e: WsUnfinishedBlockMessage) => unknown) {
await daemon.subscribe(unfinished_block_info_service);
const messageListener = (e: WsFullNodeMessage) => {
if (e.origin === chia_full_node_service && e.command === unfinished_block_command) {
callback(e);
}
};
return daemon.addMessageListener(chia_full_node_service, messageListener);
}
export type WsFullNodeMessage = WsGetConnectionFullNodeMessage
| WsGetBlockchainStateMessage
| WsBlockMessage
| WsSignagePointMessage
| WsUnfinishedBlockMessage
;
// Whole commands for the service
export type chia_full_node_commands = get_blockchain_state_command | get_connections_command | block_command
| signage_point_command | unfinished_block_command;
export type TChiaFullNodeBroadcast = TGetBlockchainStateBroadCast | TGetConnectionsBroadCast | TBlockBroadCast
| TSignagePointBroadCast | TUnfinishedBlockBroadCast;
export async function on_message_from_full_node(daemon: TDaemon, callback: (e: WsFullNodeMessage) => unknown){
await daemon.subscribe(wallet_ui_service);
const messageListener = (e: WsFullNodeMessage) => {
if(e.origin === chia_full_node_service){
callback(e);
}
};
return daemon.addMessageListener(chia_full_node_service, messageListener);
}