-
Notifications
You must be signed in to change notification settings - Fork 39
/
TezosNodeReader.ts
294 lines (262 loc) · 13.7 KB
/
TezosNodeReader.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { JSONPath } from 'jsonpath-plus';
import * as TezosRPCTypes from '../../types/tezos/TezosRPCResponseTypes'
import { TezosRequestError } from '../../types/tezos/TezosErrorTypes';
import { TezosConstants } from '../../types/tezos/TezosConstants';
import FetchSelector from '../../utils/FetchSelector'
import LogSelector from '../../utils/LoggerSelector';
const log = LogSelector.log;
const fetch = FetchSelector.fetch;
/**
* Utility functions for interacting with a Tezos node.
*/
export namespace TezosNodeReader {
/**
* Send a GET request to a Tezos node.
*
* @param {string} server Tezos node to query
* @param {string} command RPC route to invoke
* @returns {Promise<object>} JSON-encoded response
*/
function performGetRequest(server: string, command: string): Promise<object> {
const url = `${server}/${command}`;
return fetch(url, { method: 'get' })
.then(response => {
if (!response.ok) {
log.error(`TezosNodeReader.performGetRequest error: ${response.status} for ${command} on ${server}`);
throw new TezosRequestError(response.status, response.statusText, url, null);
}
return response;
})
.then(async response => {
const json: any = await response.json();
log.debug(`TezosNodeReader.performGetRequest response: ${json} for ${command} on ${server}`);
return json;
});
}
/**
* Gets the delegate for a smart contract or an implicit account.
*
* @param {string} server Tezos node to query
* @param {string} accountHash The smart contract address or implicit account to query.
* @returns The address of the delegate, or undefined if there was no delegate set.
*/
export async function getDelegate(server: string, accountHash: string): Promise<string | undefined> {
const contractData = await getAccountForBlock(server, 'head', accountHash)
return contractData.delegate;
}
/**
* Gets a block for a given hash.
*
* @param {string} server Tezos node to query
* @param {string} hash Hash of the given block, defaults to 'head'
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main
* @returns {Promise<TezosRPCTypes.TezosBlock>} Block
*/
export function getBlock(server: string, hash: string = 'head', chainid: string = 'main'): Promise<TezosRPCTypes.TezosBlock> {
return performGetRequest(server, `chains/${chainid}/blocks/${hash}`).then(json => { return <TezosRPCTypes.TezosBlock>json });
}
/**
* Gets the top block.
*
* @param {string} server Tezos node to query
* @returns {Promise<TezosRPCTypes.TezosBlock>} Block head
*/
export function getBlockHead(server: string): Promise<TezosRPCTypes.TezosBlock> {
return getBlock(server);
}
/**
* Returns a block at a given offset below head.
*
* @param {string} server Tezos node to query
* @param {number} offset Number of blocks below head, must be positive, 0 = head
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main
*/
export async function getBlockAtOffset(server: string, offset: number, chainid: string = 'main'): Promise<TezosRPCTypes.TezosBlock> {
if (offset <= 0) { return getBlock(server); }
const head = await getBlock(server);
return performGetRequest(server, `chains/${chainid}/blocks/${Number(head['header']['level']) - offset}`).then(json => { return <TezosRPCTypes.TezosBlock>json });
}
/**
* Returns a block at a given offset below head.
*
* @param {string} server Tezos node to query
* @param {number} level Block level to get
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main
*/
export async function getBlockAtLevel(server: string, level: number | string, chainid: string = 'main'): Promise<TezosRPCTypes.TezosBlock> {
return performGetRequest(server, `chains/${chainid}/blocks/${level}`).then(json => { return <TezosRPCTypes.TezosBlock>json });
}
/**
* Fetches a specific account for a given block.
*
* @param {string} server Tezos node to query
* @param {string} blockHash Hash of given block
* @param {string} accountHash Account address
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main
* @returns {Promise<Account>} The account
*/
export function getAccountForBlock(server: string, blockHash: string, accountHash: string, chainid: string = 'main'): Promise<TezosRPCTypes.Contract> {
return performGetRequest(server, `chains/${chainid}/blocks/${blockHash}/context/contracts/${accountHash}`)
.then(json => <TezosRPCTypes.Contract>json);
}
/**
* Fetches the current counter for an account.
*
* @param {string} server Tezos node to query
* @param {string} accountHash Account address
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main
* @returns {Promise<number>} Current account counter
*/
export async function getCounterForAccount(server: string, accountHash: string, chainid: string = 'main'): Promise<number> {
const counter = await performGetRequest(server, `chains/${chainid}/blocks/head/context/contracts/${accountHash}/counter`)
.then(r => r.toString());
return parseInt(counter.toString(), 10);
}
/**
* Returns account balance as of the Head. Will return current balance or 0 if the account is marked as `spendable: false`.
* Use `getAccountForBlock()` to get balance as of a specific block. Balance value is in utez.
*
* @param {string} server Tezos node to query
* @param {string} accountHash Account address
* @param chainid Chain id, expected to be 'main' or 'test', defaults to main
*/
export async function getSpendableBalanceForAccount(server: string, accountHash: string, chainid: string = 'main'): Promise<number> {
const account = await performGetRequest(server, `chains/${chainid}/blocks/head/context/contracts/${accountHash}`) // TODO: get /balance
.then(json => <TezosRPCTypes.Contract>json);
return parseInt(account.balance.toString(), 10);
}
/**
* Fetches the manager public key of a specific account for a given block.
*
* @param {string} server Tezos node to query
* @param {string} block Hash of given block, will also accept block level and 'head'
* @param {string} accountHash Account address
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main.
* @returns {Promise<string>} Manager public key
*/
export async function getAccountManagerForBlock(server: string, block: string, accountHash: string, chainid: string = 'main'): Promise<string> {
const key = await performGetRequest(server, `chains/${chainid}/blocks/${block}/context/contracts/${accountHash}/manager_key`);
return key ? key.toString() : '';
}
/**
* Indicates whether an account is implicit and empty. If true, transaction will burn 0.06425tz.
*
* @param {string} server Tezos node to connect to
* @param {string} accountHash Account address
* @returns {Promise<boolean>} Result
*/
export async function isImplicitAndEmpty(server: string, accountHash: string): Promise<boolean> {
const account = await getAccountForBlock(server, 'head', accountHash);
const isImplicit = accountHash.toLowerCase().startsWith('tz');
const isEmpty = Number(account.balance) === 0;
return (isImplicit && isEmpty);
}
/**
* Indicates whether a reveal operation has already been done for a given account.
*
* @param {string} server Tezos node to connect to
* @param {string} accountHash Account address to delegate from
* @returns {Promise<boolean>} Result
*/
export async function isManagerKeyRevealedForAccount(server: string, accountHash: string): Promise<boolean> {
const managerKey = await getAccountManagerForBlock(server, 'head', accountHash);
return managerKey.length > 0;
}
export function getContractStorage(server: string, accountHash: string, block: string = 'head', chainid: string = 'main'): Promise<any> {
return performGetRequest(server, `chains/${chainid}/blocks/${block}/context/contracts/${accountHash}/storage`);
}
/**
* Queries the node for a key of a big_map
*
* @param {string} server Tezos node to connect to
* @param index big_map index to query
* @param key encoded big_map key to get, see `TezosMessageUtils.encodeBigMapKey`, `TezosMessageUtils.writePackedData`
* @param {string} block Block reference, level or hash, 'head' is default
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main.
*/
export function getValueForBigMapKey(server: string, index: number, key: string, block: string = 'head', chainid: string = 'main'): Promise<any> {
return performGetRequest(server, `chains/${chainid}/blocks/${block}/context/big_maps/${index}/${key}`);
}
/**
* Queries the /mempool/pending_operations RPC and parses it looking for the provided operation group id.
*
* @param {string} server Tezos node to connect to
* @param {string} operationGroupId
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main.
*/
export async function getMempoolOperation(server: string, operationGroupId: string, chainid: string = 'main') {
const mempoolContent: any = await performGetRequest(server, `chains/${chainid}/mempool/pending_operations`).catch(() => undefined);
const jsonresult = JSONPath({ path: `$.applied[?(@.hash=='${operationGroupId}')]`, json: mempoolContent });
return jsonresult[0];
}
/**
* This method is meant to be used with the output of `getMempoolOperationsForAccount()` to estimate TTL of pending transactions.
*
* @param {string} server Tezos node to connect to.
* @param {string} branch Branch field of the operation in question.
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main.
* @returns {Promise<number>} Number of blocks until the operation expires.
*/
export async function estimateBranchTimeout(server: string, branch: string, chainid: string = 'main'): Promise<number> {
const refBlock = getBlock(server, branch, chainid);
const headBlock = getBlock(server, 'head', chainid);
var result = await Promise.all([refBlock, headBlock]).then(blocks => Number(blocks[1]['header']['level']) - Number(blocks[0]['header']['level']));
return TezosConstants.MaxBranchOffset - result;
}
/**
* Queries the /mempool/pending_operations RPC and parses it looking for applied operations submitted by the provided account
*
* @param {string} server Tezos node to connect to.
* @param accountHash
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main.
*/
export async function getMempoolOperationsForAccount(server: string, accountHash: string, chainid: string = 'main') {
const mempoolContent: any = await performGetRequest(server, `chains/${chainid}/mempool/pending_operations`).catch(() => undefined);
const a = mempoolContent.applied.filter(g => g.contents.some(s => (s.source === accountHash || s.destination === accountHash)));
const o = a.map(g => { g.contents = g.contents.filter(s => (s.source === accountHash || s.destination === accountHash)); return g; });
return o;
}
/**
* Returns the current chain_id hash, for example, "NetXm8tYqnMWky1". This is not to be confused with the other chain id which is either "test" or "main".
*
* @param {string} server Tezos node to connect to.
* @param {string} chainid Chain id, expected to be 'main' or 'test', defaults to main.
*/
export async function getChainId(server: string, chainid: string = 'main'): Promise<string> {
return performGetRequest(server, `chains/${chainid}/chain_id`).then(r => r.toString());
}
/**
*
* @param tezosNode
* @param block
* @param operationHash
* @param limit
* @returns
*/
export async function awaitOperationConfirmation(tezosNode: string, block: number, operationHash: string, limit: number = 10): Promise<any> {
let lastSeenBlock = block;
if (lastSeenBlock < 0) {
const head = await TezosNodeReader.getBlockHead(tezosNode);
lastSeenBlock = head.header.level;
}
const transactionOperationIndex = 3;
const activationOperationIndex = 2;
while (lastSeenBlock < block + limit) {
try {
const blockData = await TezosNodeReader.getBlock(tezosNode, `${lastSeenBlock + 1}`);
lastSeenBlock = blockData.header.level;
const relevantOperation = blockData.operations[transactionOperationIndex]
.concat(blockData.operations[activationOperationIndex])
.filter(o => o.hash === operationHash);
if (relevantOperation.length === 1) {
return relevantOperation[0];
} else {
await new Promise(resolve => setTimeout(resolve, TezosConstants.DefaultBlockTime));
}
} catch (e) {
await new Promise(resolve => setTimeout(resolve, 10_000));
}
}
throw new Error(`Did not observe operation ${operationHash} between ${block} and ${lastSeenBlock}`)
}
}