UNPKG

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

Version:

## Peer dependencies

61 lines (60 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TxValidator = void 0; const tslib_1 = require("tslib"); const z = tslib_1.__importStar(require("zod")); const helpers_1 = require("../helpers"); const input_message_1 = require("../input-message"); const node_1 = require("../node"); const types_1 = require("../types"); const InputMessage = input_message_1.InputMessages.UnitsChanged({ ledger: z.array(types_1.LedgerEntry), }); const CORRECT_OUTPUT = 0; const WRONG_OUTPUT = 1; const TxValidator = (api) => class TxValidator extends node_1.Node { constructor(config) { super(api, config, InputMessage); } onInput(message) { var _a, _b; const rootUnitId = message.payload.txLog.rootUnitId; const changes = message.payload.txLog.changes; const volumeMap = (0, helpers_1.groupBy)(message.payload.ledger, i => `${i.accountId}.${i.rootUnitId}`, i => i.volume); for (const change of changes) { // Ensure entries to be modified exist if (change.prevOwner) { volumeMap[_a = `${change.prevOwner}.${rootUnitId}`] || (volumeMap[_a] = 0); } volumeMap[_b = `${change.owner}.${rootUnitId}`] || (volumeMap[_b] = 0); if (change.prevOwner) { const ownedVolume = volumeMap[`${change.prevOwner}.${rootUnitId}`] ?? 0; if (ownedVolume - change.volume < 0) { this.sendBuilder(message) .addPayload({ txFailedReason: `${change.prevOwner} has ${ownedVolume} volume in unit ${rootUnitId}, but ${change.volume} is required`, voting: message.payload.txLog.changes.map(v => ({ vote: types_1.VOTE_REJECT, votingId: v.unitId })) }) .sendToOutput(WRONG_OUTPUT); return; } } if (change.prevOwner) { volumeMap[`${change.prevOwner}.${rootUnitId}`] -= change.volume; } volumeMap[`${change.owner}.${rootUnitId}`] += change.volume; } this.sendBuilder(message) .addPayload({ voting: message.payload.txLog.changes.map(v => ({ vote: types_1.VOTE_APPROVE, votingId: v.unitId })) }) .sendToOutput(CORRECT_OUTPUT); } }; exports.TxValidator = TxValidator;