UNPKG

@requestnetwork/data-access

Version:
158 lines • 7.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataAccessRead = void 0; const tslib_1 = require("tslib"); const types_1 = require("@requestnetwork/types"); class DataAccessRead { constructor(storage, { network, pendingStore }) { this.storage = storage; this.network = network; this.pendingStore = pendingStore; } initialize() { return tslib_1.__awaiter(this, void 0, void 0, function* () { yield this.storage.initialize(); }); } close() { return Promise.resolve(); } getTransactionsByChannelId(channelId, updatedBetween) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const pending = yield this.getPending(channelId); const result = yield this.storage.getTransactionsByChannelId(channelId, updatedBetween); return { meta: { transactionsStorageLocation: result.transactions .map((x) => x.hash) .concat(pending.meta.transactionsStorageLocation), storageMeta: result.transactions.map((tx) => this.toStorageMeta(tx, result.blockNumber, this.network)), }, result: { transactions: result.transactions .map(this.toTimestampedTransaction) .concat(pending.result.transactions), }, }; }); } getChannelsByTopic(topic, updatedBetween) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.getChannelsByMultipleTopics([topic], updatedBetween); }); } getChannelsByMultipleTopics(topics, updatedBetween) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const result = yield this.storage.getTransactionsByTopics(topics); const pending = ((_a = this.pendingStore) === null || _a === void 0 ? void 0 : _a.findByTopics(topics)) || []; const pendingItems = pending.map((item) => { var _a, _b, _c, _d, _e; return (Object.assign(Object.assign({ hash: item.storageResult.id, channelId: item.channelId }, item.transaction), { blockNumber: ((_a = item.storageResult.meta.ethereum) === null || _a === void 0 ? void 0 : _a.blockNumber) || -1, blockTimestamp: ((_b = item.storageResult.meta.ethereum) === null || _b === void 0 ? void 0 : _b.blockTimestamp) || -1, transactionHash: ((_c = item.storageResult.meta.ethereum) === null || _c === void 0 ? void 0 : _c.transactionHash) || '', smartContractAddress: ((_d = item.storageResult.meta.ethereum) === null || _d === void 0 ? void 0 : _d.smartContractAddress) || '', size: String(((_e = item.storageResult.meta.ipfs) === null || _e === void 0 ? void 0 : _e.size) || 0), topics: item.topics || [] })); }); const transactions = result.transactions.concat(...pendingItems); // list of channels having at least one tx updated during the updatedBetween boundaries const channels = (updatedBetween ? transactions.filter((tx) => tx.blockTimestamp >= (updatedBetween.from || 0) && tx.blockTimestamp <= (updatedBetween.to || Number.MAX_SAFE_INTEGER)) : transactions).map((x) => x.channelId); const filteredTxs = transactions.filter((tx) => channels.includes(tx.channelId)); return { meta: { storageMeta: filteredTxs.reduce((acc, tx) => { acc[tx.channelId] = [this.toStorageMeta(tx, result.blockNumber, this.network)]; return acc; }, {}), transactionsStorageLocation: filteredTxs.reduce((prev, curr) => { if (!prev[curr.channelId]) { prev[curr.channelId] = []; } prev[curr.channelId].push(curr.hash); return prev; }, {}), }, result: { transactions: filteredTxs.reduce((prev, curr) => { if (!prev[curr.channelId]) { prev[curr.channelId] = []; } prev[curr.channelId].push(this.toTimestampedTransaction(curr)); return prev; }, {}), }, }; }); } getPending(channelId) { var _a, _b; return tslib_1.__awaiter(this, void 0, void 0, function* () { const emptyResult = { meta: { transactionsStorageLocation: [], storageMeta: [], }, result: { transactions: [], }, }; const pending = (_a = this.pendingStore) === null || _a === void 0 ? void 0 : _a.get(channelId); if (!pending) { return emptyResult; } const { storageResult, transaction } = pending; const { transactions } = yield this.storage.getTransactionsByStorageLocation(storageResult.id); // if the pending tx is found, remove its state and fetch the real data if (transactions.length > 0) { (_b = this.pendingStore) === null || _b === void 0 ? void 0 : _b.remove(channelId); return emptyResult; } return { meta: { transactionsStorageLocation: [storageResult.id], storageMeta: [storageResult.meta], }, result: { transactions: [ { state: types_1.DataAccessTypes.TransactionState.PENDING, timestamp: storageResult.meta.timestamp, transaction, }, ], }, }; }); } toStorageMeta(result, lastBlockNumber, network) { return { ethereum: { blockConfirmation: lastBlockNumber - result.blockNumber, blockNumber: result.blockNumber, blockTimestamp: result.blockTimestamp, networkName: network, smartContractAddress: result.smartContractAddress, transactionHash: result.transactionHash, }, ipfs: { size: parseInt(result.size), }, state: types_1.StorageTypes.ContentState.CONFIRMED, storageType: types_1.StorageTypes.StorageSystemType.ETHEREUM_IPFS, timestamp: result.blockTimestamp, }; } toTimestampedTransaction(transaction) { return { state: types_1.DataAccessTypes.TransactionState.CONFIRMED, timestamp: transaction.blockTimestamp, transaction: { data: transaction.data || undefined, encryptedData: transaction.encryptedData || undefined, encryptionMethod: transaction.encryptionMethod || undefined, keys: transaction.keys || undefined, }, }; } } exports.DataAccessRead = DataAccessRead; //# sourceMappingURL=data-read.js.map