@energyweb/node-red-contrib-green-proof-worker
Version:
## Peer dependencies
50 lines (49 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LedgerUpdate = void 0;
const tslib_1 = require("tslib");
const z = tslib_1.__importStar(require("zod"));
const errors_1 = require("../errors");
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 LedgerUpdate = (api) => class LedgerUpdate extends node_1.Node {
constructor(config) {
super(api, config, InputMessage);
}
onInput(message) {
var _a;
const rootUnitId = message.payload.txLog.rootUnitId;
const volumeMap = (0, helpers_1.groupBy)(message.payload.ledger, i => `${i.accountId}.${i.rootUnitId}`, i => i.volume);
for (const change of message.payload.txLog.changes) {
if (change.prevOwner) {
const prevOwnerVolume = volumeMap[`${change.prevOwner}.${rootUnitId}`];
if (prevOwnerVolume === undefined) {
throw new errors_1.GGPError(errors_1.ErrorCode.PrevOwnerNotFound, { prevOwnerId: change.prevOwner, rootUnitId });
}
volumeMap[`${change.prevOwner}.${rootUnitId}`] -= change.volume;
}
volumeMap[_a = `${change.owner}.${rootUnitId}`] || (volumeMap[_a] = 0);
volumeMap[`${change.owner}.${rootUnitId}`] += change.volume;
}
const ledgerUpdate = Object.entries(volumeMap).map(([key, volume]) => {
const [accountId, rootUnitId] = key.split('.');
return {
accountId,
rootUnitId,
volume,
};
});
return this.sendBuilder(message)
.setTopic('update_ledger')
.addPayload({
ledgerUpdate
})
.sendToOutput(0);
}
};
exports.LedgerUpdate = LedgerUpdate;