UNPKG

evm-watcher

Version:

Fault tolerant event watcher for topics on the ethereum virtual machine.

118 lines (117 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDiskClient = exports.getS3Client = void 0; const tslib_1 = require("tslib"); const s3_1 = require("../data-access/s3"); const disk_1 = require("../data-access/disk"); /** * */ let STATE; /** * * @param {string} address * @param {string} newHistory * @return {whocares} */ function appendHistory(address, newHistory) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const newLines = newHistory .map((h) => `${h.mode};${h.tx};${h.amount};${h.address}`) .join("\n"); yield STATE.appendObject(`history/${address}`, newLines + "\n"); }); } /** * * @param {whocares} address * @param {whocares} history * @return {whocares} */ function writeHistory(address, history) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return yield STATE.putObject(`history/${address}`, JSON.stringify(history, null, 2)); }); } /** * * @param {string} key * @return {HistoryObject} */ function getHistory(key) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const str = yield STATE.getObject(`history/${key}`); return JSON.parse(str); } catch (e) { // TODO, json parse err should fail diff. console.log("NO history yet, defaulting to empty array."); return []; } }); } /** * */ function getLatestBlock() { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const str = yield STATE.getObject("LAST_BLOCK"); return Number(str); } catch (e) { return 0; } }); } /** * * @param {string} block * @return {void} */ function setLastBlock(block) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield STATE.putObject("LAST_BLOCK", block); } catch (e) { console.error("There was a problem setting last block"); throw new Error("Unable to set block."); } }); } /** * * @param {string} bucket * @return {string} */ function getS3Client({ bucket } = {}) { if (!bucket) throw Error("PARAM ERROR. Missing bucket name."); STATE = new s3_1.S3({ Bucket: bucket }); return { appendHistory, writeHistory, getHistory, getLatestBlock, setLastBlock, }; } exports.getS3Client = getS3Client; /** * * @param {string} basePath * @return {string} */ function getDiskClient({ basePath }) { STATE = new disk_1.Disk({ basePath }); return { appendHistory, writeHistory, getHistory, getLatestBlock, setLastBlock, }; } exports.getDiskClient = getDiskClient;