Skip to content

Commit

Permalink
Merge pull request #1017 from near/refactor/account-logging
Browse files Browse the repository at this point in the history
refactor: remove log/debug methods from `Account` class
  • Loading branch information
andy-haynes authored Oct 20, 2022
2 parents 624e4de + 560cfb8 commit f786164
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 42 deletions.
46 changes: 4 additions & 42 deletions packages/near-api-js/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import { Connection } from './connection';
import { baseDecode, baseEncode } from 'borsh';
import { PublicKey } from './utils/key_pair';
import { logWarning, PositionalArgsError } from './utils/errors';
import { parseRpcError, parseResultError } from './utils/rpc_errors';
import { ServerError } from './utils/rpc_errors';
import { printTxOutcomeLogs, printTxOutcomeLogsAndFailures } from './utils/logging';
import { parseResultError } from './utils/rpc_errors';
import { DEFAULT_FUNCTION_CALL_GAS } from './constants';

import exponentialBackoff from './utils/exponential-backoff';
Expand Down Expand Up @@ -123,12 +123,6 @@ export interface ViewFunctionCallOptions extends FunctionCallOptions {
blockQuery?: BlockReference;
}

interface ReceiptLogWithFailure {
receiptIds: [string];
logs: [string];
failure: ServerError;
}

interface StakedBalance {
validatorId: string;
amount?: string;
Expand Down Expand Up @@ -177,28 +171,6 @@ export class Account {
});
}

/** @hidden */
private printLogsAndFailures(contractId: string, results: [ReceiptLogWithFailure]) {
if (!process.env['NEAR_NO_LOGS']) {
for (const result of results) {
console.log(`Receipt${result.receiptIds.length > 1 ? 's' : ''}: ${result.receiptIds.join(', ')}`);
this.printLogs(contractId, result.logs, '\t');
if (result.failure) {
console.warn(`\tFailure [${contractId}]: ${result.failure}`);
}
}
}
}

/** @hidden */
private printLogs(contractId: string, logs: string[], prefix = '') {
if (!process.env['NEAR_NO_LOGS']) {
for (const log of logs) {
console.log(`${prefix}Log [${contractId}]: ${log}`);
}
}
}

/**
* Create a signed transaction which can be broadcast to the network
* @param receiverId NEAR account receiving the transaction
Expand Down Expand Up @@ -254,17 +226,7 @@ export class Account {
throw new TypedError('nonce retries exceeded for transaction. This usually means there are too many parallel requests with the same access key.', 'RetriesExceeded');
}

const flatLogs = [result.transaction_outcome, ...result.receipts_outcome].reduce((acc, it) => {
if (it.outcome.logs.length ||
(typeof it.outcome.status === 'object' && typeof it.outcome.status.Failure === 'object')) {
return acc.concat({
'receiptIds': it.outcome.receipt_ids,
'logs': it.outcome.logs,
'failure': typeof it.outcome.status.Failure != 'undefined' ? parseRpcError(it.outcome.status.Failure) : null
});
} else return acc;
}, []);
this.printLogsAndFailures(signedTx.transaction.receiverId, flatLogs);
printTxOutcomeLogsAndFailures({ contractId: signedTx.transaction.receiverId, outcome: result });

// Should be falsy if result.status.Failure is null
if (!returnError && typeof result.status === 'object' && typeof result.status.Failure === 'object' && result.status.Failure !== null) {
Expand Down Expand Up @@ -537,7 +499,7 @@ export class Account {
});

if (result.logs) {
this.printLogs(contractId, result.logs);
printTxOutcomeLogs({ contractId, logs: result.logs });
}

return result.result && result.result.length > 0 && parse(Buffer.from(result.result));
Expand Down
69 changes: 69 additions & 0 deletions packages/near-api-js/src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { FinalExecutionOutcome } from '../providers';
import { parseRpcError } from './rpc_errors';

const SUPPRESS_LOGGING = !!process.env.NEAR_NO_LOGS;

/**
* 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 }) {
if (SUPPRESS_LOGGING) {
return;
}

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) {
console.log(`Receipt${result.receiptIds.length > 1 ? 's' : ''}: ${result.receiptIds.join(', ')}`);
printTxOutcomeLogs({
contractId,
logs: result.logs,
prefix: '\t',
});

if (result.failure) {
console.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 }) {
if (SUPPRESS_LOGGING) {
return;
}

for (const log of logs) {
console.log(`${prefix}Log [${contractId}]: ${log}`);
}
}

0 comments on commit f786164

Please sign in to comment.