-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathlogging.ts
61 lines (56 loc) · 2.09 KB
/
logging.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
import { FinalExecutionOutcome } from '@near-js/types';
import { parseRpcError } from './errors';
import { Logger } from './logger';
/**
* Parse and print details from a query execution response
* @param params
* @param params.contractId ID of the account/contract which made the query
* @param params.outcome the query execution response
*/
export function printTxOutcomeLogsAndFailures({
contractId,
outcome,
}: { contractId: string, outcome: FinalExecutionOutcome }) {
const flatLogs = [outcome.transaction_outcome, ...outcome.receipts_outcome]
.reduce((acc, it) => {
const isFailure = typeof it.outcome.status === 'object' && typeof it.outcome.status.Failure === 'object';
if (it.outcome.logs.length || isFailure) {
return acc.concat({
receiptIds: it.outcome.receipt_ids,
logs: it.outcome.logs,
failure: typeof it.outcome.status === 'object' && it.outcome.status.Failure !== undefined
? parseRpcError(it.outcome.status.Failure)
: null
});
} else {
return acc;
}
}, []);
for (const result of flatLogs) {
Logger.log(`Receipt${result.receiptIds.length > 1 ? 's' : ''}: ${result.receiptIds.join(', ')}`);
printTxOutcomeLogs({
contractId,
logs: result.logs,
prefix: '\t',
});
if (result.failure) {
Logger.warn(`\tFailure [${contractId}]: ${result.failure}`);
}
}
}
/**
* Format and print log output from a query execution response
* @param params
* @param params.contractId ID of the account/contract which made the query
* @param params.logs log output from a query execution response
* @param params.prefix string to append to the beginning of each log
*/
export function printTxOutcomeLogs({
contractId,
logs,
prefix = '',
}: { contractId: string, logs: string[], prefix?: string }) {
for (const log of logs) {
Logger.log(`${prefix}Log [${contractId}]: ${log}`);
}
}