UNPKG

avail-js-sdk

Version:

Avail library of functions to interact with blockchain and manipulate transactions

133 lines (132 loc) 5.2 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.Watcher = void 0; const _1 = require("."); class Watcher { constructor(client, txHash, waitFor) { if (txHash instanceof Uint8Array) { txHash = new _1.H256(txHash); } else if (typeof txHash == "string") { txHash = _1.H256.fromString(txHash); } this.client = client; this.txHash = txHash; this.blockCountTimeout = null; this.blockHeightTimeout = null; this.waitFor = waitFor; } withBlockCountTimeout(value) { this.blockCountTimeout = value; } withTxHash(value) { if (value instanceof Uint8Array) { value = new _1.H256(value); } else if (typeof value == "string") { value = _1.H256.fromString(value); } this.txHash = value; } withBlockHeightTimeout(value) { this.blockHeightTimeout = value; } run() { return __awaiter(this, void 0, void 0, function* () { const timeout = yield this.determineTimeout(); if (this.waitFor == _1.WaitFor.BlockInclusion) { return yield this.runIncluded(timeout); } else { return yield this.runFinalized(timeout); } }); } determineTimeout() { return __awaiter(this, void 0, void 0, function* () { if (this.blockHeightTimeout != null) { return this.blockHeightTimeout; } const bestBlock = yield this.client.bestBlockNumber(); if (this.blockCountTimeout != null) { return bestBlock + this.blockCountTimeout; } return bestBlock + 5; }); } runFinalized(timeout) { return __awaiter(this, void 0, void 0, function* () { const result = yield new Promise((res, _) => { const unsub = this.client.api.rpc.chain.subscribeFinalizedHeads((header) => __awaiter(this, void 0, void 0, function* () { const details = yield this.checkBlock(header); if (details != null) { ; (yield unsub)(); res(details); } if (header.number.toNumber() >= timeout) { ; (yield unsub)(); res(null); } })); }); return result; }); } runIncluded(timeout) { return __awaiter(this, void 0, void 0, function* () { const result = yield new Promise((res, _) => { const unsub = this.client.api.rpc.chain.subscribeNewHeads((header) => __awaiter(this, void 0, void 0, function* () { const details = yield this.checkBlock(header); if (details != null) { ; (yield unsub)(); res(details); } if (header.number.toNumber() >= timeout) { ; (yield unsub)(); res(null); } })); }); return result; }); } checkBlock(header) { return __awaiter(this, void 0, void 0, function* () { const blockNumber = header.number.toNumber(); const blockHash = yield this.client.blockHash(header.number.toNumber()); const block = yield this.client.rpcBlockAt(blockHash); // console.log(`Block Number: ${header.number.toNumber()}, Block Hash: ${blockHash}`) let txIndex = 0; for (const ext of block.block.extrinsics) { const txHash = ext.hash; if (txHash.toHex() != this.txHash.toString()) { txIndex += 1; continue; } let events = undefined; try { events = yield _1.Events.EventRecords.fetch(this.client, blockHash, txIndex); } catch (err) { // Don't do anything } return new _1.TransactionDetails(this.client, events, new _1.H256(txHash), txIndex, blockHash, blockNumber); } return null; }); } } exports.Watcher = Watcher;