UNPKG

@taquito/taquito

Version:

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

118 lines (117 loc) 6.57 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.OperationFactory = exports.createNewPollingBasedHeadObservable = void 0; exports.timeoutAfter = timeoutAfter; const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const create_observable_from_subscription_1 = require("../subscribe/create-observable-from-subscription"); const batch_operation_1 = require("./batch-operation"); const delegation_operation_1 = require("./delegation-operation"); const increase_paid_storage_operation_1 = require("./increase-paid-storage-operation"); const operation_1 = require("./operation"); const origination_operation_1 = require("./origination-operation"); const transaction_operation_1 = require("./transaction-operation"); const transfer_ticket_operation_1 = require("./transfer-ticket-operation"); const errors_1 = require("../errors"); const register_global_constant_operation_1 = require("./register-global-constant-operation"); function timeoutAfter(timeoutMillisec) { return function inner(source) { return new rxjs_1.BehaviorSubject(null).pipe((0, operators_1.timeout)({ each: timeoutMillisec, with: () => (0, rxjs_1.throwError)(() => new errors_1.ConfirmationTimeoutError(`Confirmation polling timed out`)), }), (0, operators_1.mergeMap)(() => source)); }; } const createNewPollingBasedHeadObservable = (sharedHeadOb, context, _scheduler) => { return sharedHeadOb.pipe(timeoutAfter(context.config.confirmationPollingTimeoutSecond * 1000), (0, operators_1.share)({ connector: () => new rxjs_1.ReplaySubject(1), resetOnError: false, resetOnComplete: false, resetOnRefCountZero: true, })); }; exports.createNewPollingBasedHeadObservable = createNewPollingBasedHeadObservable; class OperationFactory { constructor(context) { this.context = context; // Cache the last block for one second across all operations this.sharedHeadObs = (0, rxjs_1.defer)(() => { return (0, create_observable_from_subscription_1.createObservableFromSubscription)(this.context.stream.subscribeBlock('head')); }); } createNewHeadObservable() { return __awaiter(this, void 0, void 0, function* () { return (0, exports.createNewPollingBasedHeadObservable)(this.sharedHeadObs, this.context); }); } createPastBlockWalker(startBlock, count = 1) { return (0, rxjs_1.from)(this.context.readProvider.getBlock(startBlock)).pipe((0, operators_1.switchMap)((block) => { if (count === 1) { return (0, rxjs_1.of)(block); } return (0, rxjs_1.range)(block.header.level, count - 1).pipe((0, operators_1.startWith)(block), (0, operators_1.concatMap)((level) => __awaiter(this, void 0, void 0, function* () { return this.context.readProvider.getBlock(typeof level === 'number' ? level : level.header.level); }))); })); } createHeadObservableFromConfig(_a) { return __awaiter(this, arguments, void 0, function* ({ blockIdentifier }) { const observableSequence = []; if (blockIdentifier) { observableSequence.push(this.createPastBlockWalker(blockIdentifier)); } observableSequence.push(yield this.createNewHeadObservable()); return (0, rxjs_1.concat)(...observableSequence); }); } createOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new operation_1.WalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } createBatchOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new batch_operation_1.BatchWalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } createTransactionOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new transaction_operation_1.TransactionWalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } createTransferTicketOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new transfer_ticket_operation_1.TransferTicketWalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } createDelegationOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new delegation_operation_1.DelegationWalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } createOriginationOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new origination_operation_1.OriginationWalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } createIncreasePaidStorageOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new increase_paid_storage_operation_1.IncreasePaidStorageWalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } createRegisterGlobalConstantOperation(hash_1) { return __awaiter(this, arguments, void 0, function* (hash, config = {}) { return new register_global_constant_operation_1.RegisterGlobalConstantWalletOperation(hash, this.context.clone(), yield this.createHeadObservableFromConfig(config)); }); } } exports.OperationFactory = OperationFactory;