@energyweb/node-red-contrib-green-proof-worker
Version:
## Peer dependencies
51 lines (50 loc) • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VotingService = void 0;
const tslib_1 = require("tslib");
const z = tslib_1.__importStar(require("zod"));
const errors_1 = require("../errors");
const node_1 = require("../node");
const types_1 = require("../types");
const voting_1 = require("../voting");
const voting_polkadot_1 = require("./voting-polkadot");
const InputMessage = z.object({
payload: z.object({
voting: z.array(types_1.Vote),
}).passthrough()
}).passthrough();
const Config = z.object({
votingConfig: z.string(),
});
const VotingService = (api) => class VotingService extends node_1.Node {
constructor(rawConfig) {
super(api, rawConfig, InputMessage);
const config = Config.parse(rawConfig);
const configNode = this.api.getNode(config.votingConfig);
if (!configNode) {
throw new errors_1.GGPError(errors_1.ErrorCode.VotingConfigNotFound, {});
}
this.votingConfig = configNode.config;
}
async onInput(message) {
const votingConfig = await this.votingConfig;
const { alreadyVoted, notVoted } = await (0, voting_1.partitionAlreadyVoted)(message.payload.voting, votingConfig);
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 */
const { votingTxHash } = await (0, voting_polkadot_1.voteOnPolkadot)({
connection: votingConfig,
vote: vote.vote,
votingId: vote.votingId,
});
this.api.status({});
this.api.log(`Voting on votingId = ${vote.votingId}, vote = ${vote.vote} done (voting tx hashes: ${votingTxHash})`);
}
this.sendBuilder(message).sendToOutput(0);
}
};
exports.VotingService = VotingService;