UNPKG

burst-whale-watch

Version:

An account watcher for Burstcoin

66 lines 3.65 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 }); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const core_1 = require("@burstjs/core"); const util_1 = require("@burstjs/util"); const collector_1 = require("./collector"); const constants_1 = require("../constants"); const fetchBalances = (api, accounts) => __awaiter(void 0, void 0, void 0, function* () { return (Promise.all(accounts.map(api.account.getAccountBalance))); }); const mapBalancesToAccounts = (accounts) => (balances) => (accounts.reduce((prev, accountId, index) => (Object.assign(Object.assign({}, prev), { [accountId]: balances[index].balanceNQT })), {})); const addTotalSum = (accountBalances) => { const total = Object.keys(accountBalances).reduce((totalBalance, accountId) => totalBalance + util_1.convertNQTStringToNumber(accountBalances[accountId]), 0); return { accounts: Object.assign({}, accountBalances), total, }; }; const fetchTransactions = (api, accounts) => __awaiter(void 0, void 0, void 0, function* () { return (Promise.all(accounts.map(accountId => api.account.getAccountTransactions(accountId, 0, 25)))); }); const mapTransactionsToAccounts = (accounts) => (transactionLists) => (accounts.reduce((prev, accountId, index) => (Object.assign(Object.assign({}, prev), { [accountId]: transactionLists[index].transactions })), {})); class BrsCollector extends collector_1.Collector { constructor(store, config) { super(store); this.config = config; this.api = core_1.composeApi(new core_1.ApiSettings(config.peer, '/burst')); } pollBalances() { const { accounts } = this.config; this.balancesSubscription = rxjs_1.interval(constants_1.BRS_POLLING_SEC * 1000).pipe(operators_1.startWith(0), operators_1.mergeMap(() => fetchBalances(this.api, accounts)), operators_1.map(mapBalancesToAccounts(accounts)), operators_1.map(addTotalSum)).subscribe((data) => { this.update('balances', Object.assign(Object.assign({}, data), { isLoading: false })); }); } pollTransactions() { const { accounts } = this.config; this.transactionsSubscription = rxjs_1.interval(constants_1.BRS_POLLING_SEC * 1000).pipe(operators_1.startWith(0), operators_1.mergeMap(() => fetchTransactions(this.api, accounts)), operators_1.map(mapTransactionsToAccounts(accounts))).subscribe((data) => { this.update('transactions', { accounts: Object.assign({}, data), isLoading: false }); }); } onStart() { this.pollBalances(); this.pollTransactions(); } onStop() { if (this.balancesSubscription) this.balancesSubscription.unsubscribe(); if (this.transactionsSubscription) this.transactionsSubscription.unsubscribe(); } } exports.BrsCollector = BrsCollector; //# sourceMappingURL=brs.collector.js.map