Skip to content

Commit

Permalink
add voters collection and route handling in MongoRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
lealbrunocalhau committed Nov 25, 2024
1 parent aea8472 commit c5f2b09
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/indexer/helpers/mongo-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -12,6 +13,7 @@ export class MongoRoutes {
private db?: Db;
private accountsCollection?: Collection<IAccount>;
private proposalsCollection?: Collection<IProposal>;
private votersCollection?: Collection<IVoter>;

constructor(connectionManager: ConnectionManager) {
this.cm = connectionManager;
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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);
});
};
}
}

0 comments on commit c5f2b09

Please sign in to comment.