UNPKG

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

Version:
65 lines (64 loc) 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VotingMarketplace = void 0; const tslib_1 = require("tslib"); const z = tslib_1.__importStar(require("zod")); const node_1 = require("../node"); const nodes_config_env_1 = require("../nodes-config-env"); const types_1 = require("../types"); const voting_1 = require("../voting"); const host_api_1 = require("../host-api"); const InputMessage = z.looseObject({ payload: z.looseObject({ voting: z.array(types_1.Vote), }) }); const Config = z.object({ indexerUrl: z.url(), workerAddress: z.string(), noHashing: z.boolean(), // noHashing instead of "should hash" because we need to keep it backward compatible }); /** Based on https://github.com/energywebfoundation/node-red-contrib-energywebx/blob/master/submit-solution.js */ const VotingMarketplace = (api) => class VotingMarketplace extends node_1.Node { constructor(rawConfig) { super(api, rawConfig, InputMessage); const envConfig = this.getNodeEnvConfig(); this.config = (async () => { return Config.parse({ noHashing: rawConfig.noHashing ?? false, indexerUrl: rawConfig.indexerUrl || nodes_config_env_1.nodesGlobalEnvConfig.VOTING_SERVICE_CONFIG_INDEXER_URL || await this.getBaseUrls() .then(c => new URL('/votes', c.base_indexer_url).href), workerAddress: rawConfig.workerAddress || envConfig.EWX_WORKER_ADDRESS || nodes_config_env_1.nodesGlobalEnvConfig.VOTING_SERVICE_CONFIG_WORKER_ADDRESS, }); })(); } async onInput(message) { const config = await this.config; this.api.log(`Checking already voted votings as worker ${config.workerAddress} at indexer ${config.indexerUrl}`); const { toNotVote, toVote } = await (0, voting_1.partitionToVote)(message.payload.voting, config); for (const vote of toNotVote) { this.api.log(`Skipping voting on votingId = ${vote.votingId}, vote = ${vote.vote}, because it was already voted on or voting is expired/settled`); } for (const vote of toVote) { this.api.log(`Voting on votingId = ${vote.votingId}, vote = ${vote.vote}...`); this.api.status({ fill: 'yellow', shape: 'dot', text: 'Voting on correct transaction...' }); /** @TODO missing transaction for multiple votes for unit */ await this.sendResult(vote); this.api.status({}); this.api.log(`Voting on votingId = ${vote.votingId}, vote = ${vote.vote} done`); } this.sendBuilder(message).sendToOutput(0); } async sendResult(payload) { const config = await this.config; await host_api_1.hostApi.vote({ noderedId: this.nodeConfig.z, /** @NOTE I don't know why original node is sending that */ root: payload.vote, id: payload.votingId, hashVote: config.noHashing === false, }); } }; exports.VotingMarketplace = VotingMarketplace;