UNPKG

machinomy

Version:

Micropayments powered by Ethereum

49 lines 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const namespaced_1 = require("../util/namespaced"); const logger_1 = require("@machinomy/logger"); const LOG = new logger_1.default('abstract-channels-database'); class AbstractChannelsDatabase { constructor(engine, inflator, namespace) { this.kind = namespaced_1.namespaced(namespace, 'channel'); this.engine = engine; this.inflator = inflator; } inflatePaymentChannels(channels) { if (!channels.length) { return Promise.resolve([]); } // There shouldn't be any nulls here. return Promise.all(channels.map((chan) => this.inflatePaymentChannel(chan))); } async inflatePaymentChannel(json) { if (!json) { return null; } else { return this.inflator.inflate(json); } } filterByState(state, channels) { return channels.filter((chan) => chan ? chan.state === state : false); } saveOrUpdate(paymentChannel) { LOG.info(`Saving or updating channel with ID ${paymentChannel.channelId.toString()}`); return this.firstById(paymentChannel.channelId).then((found) => { if (found) { LOG.info(`Spending channel with ID ${paymentChannel.channelId.toString()}: ${JSON.stringify(paymentChannel)}`); return this.spend(paymentChannel.channelId, paymentChannel.spent); } else { LOG.info(`Saving channel with ID ${paymentChannel.channelId.toString()}: ${JSON.stringify(paymentChannel)}`); return this.save(paymentChannel); } }); } async allSettling() { const all = await this.all(); return this.filterByState(1, all); } } exports.default = AbstractChannelsDatabase; //# sourceMappingURL=AbstractChannelsDatabase.js.map