diff --git a/packages/near-api-js/src/account.ts b/packages/near-api-js/src/account.ts index b2b5bced0e..afffd2913c 100644 --- a/packages/near-api-js/src/account.ts +++ b/packages/near-api-js/src/account.ts @@ -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'; @@ -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; @@ -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 @@ -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) { @@ -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)); diff --git a/packages/near-api-js/src/utils/logging.ts b/packages/near-api-js/src/utils/logging.ts new file mode 100644 index 0000000000..8e1e6c1a86 --- /dev/null +++ b/packages/near-api-js/src/utils/logging.ts @@ -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}`); + } +}