UNPKG

@energyweb/node-red-contrib-green-proof-worker

Version:
77 lines (76 loc) 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.partitionToVote = void 0; const zod_1 = require("zod"); const helpers_1 = require("./helpers"); const CheckVotingResponse = zod_1.z.object({ data: zod_1.z.object({ votes: zod_1.z.array(zod_1.z.object({ rawVotingRoundId: zod_1.z.string(), })), votingRounds: zod_1.z.array(zod_1.z.object({ votingRoundId: zod_1.z.string(), expired: zod_1.z.boolean(), settled: zod_1.z.boolean(), })) }) }); const partitionToVote = async (votings, config) => { const { indexerUrl, workerAddress } = config; // GraphQL query to get voting result for our worker, namespace and all votingIds. // Voting can fail if for example it is DoubleVote or something. const query = ` query CheckVoting($votingRoundId: [String!], $workerAddress: String!, $offset: Int!, $limit: Int!) { votes(limit: $limit, offset: $offset, where: {rawVotingRoundId_in: $votingRoundId, worker: { account: { id_eq: $workerAddress } }}) { rawVotingRoundId } votingRounds(limit: $limit, offset: $offset, where: {votingRoundId_in: $votingRoundId}) { votingRoundId expired settled } } `; const queryVariables = { workerAddress, votingRoundId: votings.map(v => v.votingId), }; const indexerBaseUrl = indexerUrl.endsWith('/') ? indexerUrl : `${indexerUrl}/`; const indexerQueryUrl = new URL('graphql', indexerBaseUrl); const { votes, votingRounds } = await (0, helpers_1.downloadAllGraphqlPages)({ query, variables: queryVariables, url: indexerQueryUrl, mapResponse: (res) => { const data = CheckVotingResponse.parse(res).data; try { if (data.votes.length === 0 && data.votingRounds.length === 0) { return []; } return [{ votes: data.votes, votingRounds: data.votingRounds, }]; } catch (error) { console.error(`Failed when parsing indexer response: ${JSON.stringify(res)}`); throw error; } } }).then((results) => results.reduce((acc, result) => { acc.votes.push(...result.votes); acc.votingRounds.push(...result.votingRounds); return acc; }, { votes: [], votingRounds: [] })); const successfullyVotedIds = new Set(votes.map(item => item.rawVotingRoundId)); const expiredOrSettledIds = new Set(votingRounds.filter(r => r.expired || r.settled).map(r => r.votingRoundId)); const [toNotVote, toVote] = (0, helpers_1.partition)(votings, voting => { return successfullyVotedIds.has(voting.votingId) === true || expiredOrSettledIds.has(voting.votingId) === true; }); return { toNotVote, toVote }; }; exports.partitionToVote = partitionToVote;