From c5f2b09cc95f7f05fd781cc632a3eebac472cb2b Mon Sep 17 00:00:00 2001 From: Bruno Leal Date: Mon, 25 Nov 2024 16:51:29 -0300 Subject: [PATCH] add voters collection and route handling in MongoRoutes --- src/indexer/helpers/mongo-routes.ts | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/indexer/helpers/mongo-routes.ts b/src/indexer/helpers/mongo-routes.ts index 678d97cd..e359ec60 100644 --- a/src/indexer/helpers/mongo-routes.ts +++ b/src/indexer/helpers/mongo-routes.ts @@ -4,6 +4,7 @@ import { Message } from "amqplib"; import { hLog } from "./common_functions.js"; import { IAccount } from "../../interfaces/table-account.js"; import { IProposal } from "../../interfaces/table-proposal.js"; +import { IVoter } from "../../interfaces/table-voter.js"; export class MongoRoutes { @@ -12,6 +13,7 @@ export class MongoRoutes { private db?: Db; private accountsCollection?: Collection; private proposalsCollection?: Collection; + private votersCollection?: Collection; constructor(connectionManager: ConnectionManager) { this.cm = connectionManager; @@ -20,6 +22,7 @@ export class MongoRoutes { this.db = this.cm.mongodbClient.db(`${this.cm.conn.mongodb.database_prefix}_${this.cm.chain}`); this.accountsCollection = this.db.collection('accounts'); this.proposalsCollection = this.db.collection('proposals'); + this.votersCollection = this.db.collection('voters'); this.addRoutes(); } } @@ -91,5 +94,36 @@ export class MongoRoutes { callback(payload.length); }); }; + + this.routes['table-voters'] = (payload: Message[], callback: (indexed_size?: number) => void) => { + const operations = payload.map((msg: Message) => { + const data = JSON.parse(msg.content.toString()) as IVoter; + return { + updateOne: { + filter: { + voter: data.voter + }, + update: { + $set: { + block_num: data.block_num, + is_proxy: data.is_proxy, + last_vote_weight: data.last_vote_weight, + producers: data.producers, + proxied_vote_weight: data.proxied_vote_weight, + proxy: data.proxy, + staked: data.staked, + } + }, + upsert: true + } + }; + }); + + this.votersCollection?.bulkWrite(operations, {ordered: false}).catch(reason => { + hLog('error', 'mongo-routes', 'table-voters', reason); + }).finally(() => { + callback(payload.length); + }); + }; } }