Select Git revision
data_handler.ts
-
Hugo Trentesaux authored
see #37
Hugo Trentesaux authoredsee #37
data_handler.ts 26.49 KiB
import { strict as assert } from 'assert';
import { In } from "typeorm";
import {
Account,
Cert,
CertEvent,
ChangeOwnerKey,
Event,
EventType,
Identity,
IdentityStatus,
MembershipEvent,
SmithEvent,
SmithEventType,
Smith,
SmithCert,
SmithStatus,
PopulationHistory,
Transfer,
TxComment,
UdReeval,
UniversalDividend,
Validator,
} from "./model";
import { Address, BlockNumber, Ctx, Data, IdtyIndex, NewData } from "./types_custom";
import { hexToString } from "./utils";
import { events } from "./types";
import { getCommentType } from "./comment";
export class DataHandler {
private data: Data;
constructor() {
this.data = {
accounts: new Map(),
identities: new Map(),
smiths: new Map(),
populationHistories: [],
validators: new Map(),
membershipEvents: [],
smithEvents: [],
changeOwnerKey: [],
transfers: new Map(),
certification: new Map(),
certEvent: [],
smithCert: new Map(),
universalDividend: [],
udReeval: [],
comments: []
};
}
async processNewData(newData: NewData, ctx: Ctx) {
// Process population history by adding the value
// of the last point in database.
if (newData.populationHistories) {
const lastHistory = await ctx.store.findOneOrFail(PopulationHistory, {
where: {},
order: { blockNumber: 'DESC' }
});
this.data.populationHistories = newData.populationHistories.map(history => (new PopulationHistory({
activeAccountCount: history.activeAccountCount + lastHistory.activeAccountCount,
memberCount: history.memberCount + lastHistory.memberCount,
smithCount: history.smithCount + lastHistory.smithCount,
blockNumber: history.blockNumber,
id: `population-${history.blockNumber}`,
})));
}