@energyweb/node-red-contrib-green-proof-worker
Version:
## Peer dependencies
49 lines (48 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.partitionAlreadyVoted = void 0;
const zod_1 = require("zod");
const helpers_1 = require("./helpers");
const CheckVotingResponse = zod_1.z.object({
data: zod_1.z.object({
solutionResultSubmitteds: zod_1.z.array(zod_1.z.object({
successful: zod_1.z.boolean(),
votingRoundId: zod_1.z.string(),
}))
})
});
const partitionAlreadyVoted = async (votings, config) => {
const { indexerUrl, solutionNamespace, workerAddress } = config;
// GraphQL query to get voting result for our worker, namespace and all votingIds. Only successful once.
// Voting can fail if for example it is DoubleVote or something.
const query = `
query CheckVoting($namespace: String!, $votingRoundId: [String!], $workerAddress: String!, $offset: Int!, $limit: Int!) {
solutionResultSubmitteds(limit: $limit, offset: $offset, where: {votingRoundId_in: $votingRoundId, worker: {address_eq: $workerAddress}, solution: {namespace_eq: $namespace}, successful_eq: true}) {
successful
votingRoundId
}
}
`;
const queryVariables = {
namespace: solutionNamespace,
workerAddress,
votingRoundId: votings.map(v => v.votingId),
};
const indexerQueryUrl = new URL('/graphql', indexerUrl);
const votingResults = await (0, helpers_1.downloadAllGraphqlPages)({
query,
variables: queryVariables,
url: indexerQueryUrl,
mapResponse: (res) => {
return CheckVotingResponse.parse(res).data.solutionResultSubmitteds;
}
});
// Due to GraphQL filter all of them should be successful
const successfullyVotedIds = new Set(votingResults.map(item => item.votingRoundId));
const [alreadyVoted, notVoted] = (0, helpers_1.partition)(votings, voting => successfullyVotedIds.has(voting.votingId));
return {
alreadyVoted,
notVoted
};
};
exports.partitionAlreadyVoted = partitionAlreadyVoted;