UNPKG

@mavrykdynamics/taquito

Version:

High level functionality that builds upon the other packages in the Mavryk Typescript Library Suite.

131 lines (130 loc) 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InvalidEstimateValueError = exports.OriginationOperationError = exports.flattenErrors = exports.flattenOperationResult = exports.MavrykPreapplyFailureError = exports.MavrykOperationError = void 0; const taquito_core_1 = require("@mavrykdynamics/taquito-core"); const types_1 = require("./types"); const isErrorWithMessage = (error) => { return 'with' in error; }; /** * @category Error * @description Generic mavryk error that will be thrown when a mistake occurs when doing an operation; more details here https://protocol.mavryk.org/api/errors.html */ class MavrykOperationError extends taquito_core_1.RpcError { constructor(errors, errorDetails, operationsWithResults) { super(); this.errors = errors; this.errorDetails = errorDetails; this.operationsWithResults = operationsWithResults; this.name = 'MavrykOperationError'; // Last error is 'often' the one with more detail this.lastError = errors[errors.length - 1]; this.message = `(${this.kind}) ${this.id}`; if (isErrorWithMessage(this.lastError)) { if (this.lastError.with.string) { this.message = this.lastError.with.string; } else if (this.lastError.with.int) { this.message = this.lastError.with.int; } else { this.message = JSON.stringify(this.lastError.with); } } } get id() { return this.lastError.id; } get kind() { return this.lastError.kind; } } exports.MavrykOperationError = MavrykOperationError; /** * @category Error * @description Mavryk error that will be thrown when a mistake happens during the preapply stage */ class MavrykPreapplyFailureError extends Error { constructor(result) { super(); this.result = result; this.name = 'MavrykPreapplyFailureError'; this.message = 'Preapply returned an unexpected result'; } } exports.MavrykPreapplyFailureError = MavrykPreapplyFailureError; // Flatten all operation content results and internal operation results into a single array // Some cases where we can have multiple operation results or internal operation results are: // - When an operation includes a reveal operation // - When an operation is made using the batch API // - Smart contract call can contains internal operation results when they call other smart contract internally or originate contracts const flattenOperationResult = (response) => { const results = Array.isArray(response) ? response : [response]; const returnedResults = []; for (let i = 0; i < results.length; i++) { for (let j = 0; j < results[i].contents.length; j++) { const content = results[i].contents[j]; if ((0, types_1.hasMetadataWithResult)(content) && 'fee' in content) { returnedResults.push(Object.assign({ fee: content.fee }, content.metadata.operation_result)); if (Array.isArray(content.metadata.internal_operation_results)) { content.metadata.internal_operation_results.forEach((x) => returnedResults.push(x.result)); } } } } return returnedResults; }; exports.flattenOperationResult = flattenOperationResult; /*** * @description Flatten all error from preapply response (including internal error) */ const flattenErrors = (response, status = 'failed') => { const results = Array.isArray(response) ? response : [response]; let errors = []; // Transaction that do not fail will be backtracked in case one failure occur for (let i = 0; i < results.length; i++) { for (let j = 0; j < results[i].contents.length; j++) { const content = results[i].contents[j]; if ((0, types_1.hasMetadata)(content)) { if ((0, types_1.hasMetadataWithResult)(content) && content.metadata.operation_result.status === status) { errors = errors.concat(content.metadata.operation_result.errors || []); } if ((0, types_1.hasMetadataWithInternalOperationResult)(content) && Array.isArray(content.metadata.internal_operation_results)) { for (const internalResult of content.metadata.internal_operation_results) { if ('result' in internalResult && internalResult.result.status === status) { errors = errors.concat(internalResult.result.errors || []); } } } } } } return errors; }; exports.flattenErrors = flattenErrors; /** * @category Error * @description Error that indicates a general failure happening during an origination operation. */ class OriginationOperationError extends taquito_core_1.TaquitoError { constructor(message) { super(); this.message = message; this.name = 'OriginationOperationError'; } } exports.OriginationOperationError = OriginationOperationError; /** * @category Error * @description Error that indicates an invalid estimate value being passed */ class InvalidEstimateValueError extends taquito_core_1.ParameterValidationError { constructor(message) { super(); this.message = message; this.name = 'InvalidEstimateValueError'; } } exports.InvalidEstimateValueError = InvalidEstimateValueError;