UNPKG

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

Version:

## Peer dependencies

68 lines (67 loc) 3.11 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 InputMessage = z.object({ payload: z.object({ voting: z.array(types_1.Vote), }).passthrough() }).passthrough(); const Config = z.object({ indexerUrl: z.string().url(), solutionNamespace: z.string(), workerAddress: z.string() }); /** 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({ indexerUrl: rawConfig.indexerUrl || nodes_config_env_1.nodesGlobalEnvConfig.VOTING_SERVICE_CONFIG_INDEXER_URL || await this.getBaseUrls() .then(c => c.indexer_url), workerAddress: rawConfig.workerAddress || envConfig.EWX_WORKER_ADDRESS || nodes_config_env_1.nodesGlobalEnvConfig.VOTING_SERVICE_CONFIG_WORKER_ADDRESS, solutionNamespace: rawConfig.solutionNamespace || envConfig.EWX_SOLUTION_ID || nodes_config_env_1.nodesGlobalEnvConfig.VOTING_SERVICE_CONFIG_SOLUTION_NAMESPACE, }); })(); } async onInput(message) { const { alreadyVoted, notVoted } = await (0, voting_1.partitionAlreadyVoted)(message.payload.voting, await this.config); for (const vote of alreadyVoted) { this.api.log(`Skipping voting on votingId = ${vote.votingId}, vote = ${vote.vote}, because it was already voted on`); } for (const vote of notVoted) { 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 requestPayload = { noderedId: this.nodeConfig.z, /** @NOTE I don't know why original node is sending that */ root: payload.vote, id: payload.votingId, }; await fetch('http://localhost:3002/sse/1', { method: 'POST', body: JSON.stringify(requestPayload), headers: { 'User-Agent': 'ewx-marketplace', 'Content-Type': 'application/json', } }); } }; exports.VotingMarketplace = VotingMarketplace;