UNPKG

@mavrykdynamics/taquito

Version:

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

124 lines (123 loc) 5.95 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Operation = void 0; const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const types_1 = require("./types"); const taquito_utils_1 = require("@mavrykdynamics/taquito-utils"); const create_observable_from_subscription_1 = require("../subscribe/create-observable-from-subscription"); const errors_1 = require("../errors"); const taquito_core_1 = require("@mavrykdynamics/taquito-core"); /** * @description Utility class to interact with Mavryk operations */ class Operation { get includedInBlock() { return this._foundAt; } /** * * @param hash Operation hash * @param raw Raw operation that was injected * @param context Taquito context allowing access to rpc and signer * @throws {@link InvalidOperationHashError} */ constructor(hash, raw, results, context) { this.hash = hash; this.raw = raw; this.results = results; this.context = context; this._pollingConfig$ = new rxjs_1.ReplaySubject(1); this.currentHead$ = this._pollingConfig$.pipe((0, operators_1.switchMap)((config) => { return new rxjs_1.BehaviorSubject(config).pipe((0, operators_1.timeout)({ each: config.timeout * 1000, with: () => (0, rxjs_1.throwError)(() => new errors_1.ConfirmationTimeoutError(`Confirmation polling timed out`)), })); }), (0, operators_1.switchMap)(() => { return (0, rxjs_1.defer)(() => (0, create_observable_from_subscription_1.createObservableFromSubscription)(this.context.stream.subscribeBlock('head'))).pipe((0, operators_1.switchMap)((newHead) => { var _a, _b; const prevHead = (_b = (_a = this.lastHead) === null || _a === void 0 ? void 0 : _a.header.level) !== null && _b !== void 0 ? _b : newHead.header.level - 1; return (0, rxjs_1.range)(prevHead + 1, newHead.header.level - prevHead - 1).pipe((0, operators_1.concatMap)((level) => this.context.readProvider.getBlock(level)), (0, operators_1.endWith)(newHead)); }), (0, operators_1.tap)((newHead) => (this.lastHead = newHead))); }), (0, operators_1.shareReplay)({ refCount: true })); // Observable that emit once operation is seen in a block this.confirmed$ = this.currentHead$.pipe((0, operators_1.map)((head) => { for (let i = 3; i >= 0; i--) { head.operations[i].forEach((op) => { if (op.hash === this.hash) { this._foundAt = head.header.level; } }); } if (head.header.level - this._foundAt >= 0) { return this._foundAt; } }), (0, operators_1.filter)((x) => x !== undefined), (0, operators_1.first)(), (0, operators_1.shareReplay)()); this._foundAt = Number.POSITIVE_INFINITY; if ((0, taquito_utils_1.validateOperation)(this.hash) !== taquito_utils_1.ValidationResult.VALID) { throw new taquito_core_1.InvalidOperationHashError(this.hash); } this.confirmed$ .pipe((0, operators_1.first)(), (0, operators_1.catchError)(() => { return (0, rxjs_1.of)(rxjs_1.EMPTY); })) .subscribe(); } get revealOperation() { return (Array.isArray(this.results) && this.results.find((op) => op.kind === 'reveal')); } get revealStatus() { if (this.revealOperation) { return this.revealOperation.metadata.operation_result.status; } else { return 'unknown'; } } get status() { return (this.results.map((result) => { if ((0, types_1.hasMetadataWithResult)(result)) { return result.metadata.operation_result.status; } else { return 'unknown'; } })[0] || 'unknown'); } /** * * @param confirmations [0] Number of confirmation to wait for * @param timeout [180] Timeout */ confirmation(confirmations, timeout) { return __awaiter(this, void 0, void 0, function* () { if (typeof confirmations !== 'undefined' && confirmations < 1) { throw new errors_1.InvalidConfirmationCountError(confirmations); } const { defaultConfirmationCount, confirmationPollingTimeoutSecond } = this.context.config; this._pollingConfig$.next({ timeout: timeout || confirmationPollingTimeoutSecond, }); const conf = confirmations !== undefined ? confirmations : defaultConfirmationCount; return new Promise((resolve, reject) => { this.confirmed$ .pipe((0, operators_1.switchMap)(() => this.currentHead$), (0, operators_1.filter)((head) => head.header.level - this._foundAt >= conf - 1), (0, operators_1.first)()) .subscribe({ error: (e) => reject(e), complete: () => resolve(this._foundAt + (conf - 1)), }); }); }); } } exports.Operation = Operation;