UNPKG

machinomy

Version:

Micropayments powered by Ethereum

148 lines 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const AbstractChannelsDatabase_1 = require("../AbstractChannelsDatabase"); /** * Database layer for {PaymentChannel} */ class NedbChannelsDatabase extends AbstractChannelsDatabase_1.default { async save(paymentChannel) { const document = { kind: this.kind, sender: paymentChannel.sender, receiver: paymentChannel.receiver, value: paymentChannel.value.toString(), spent: paymentChannel.spent.toString(), channelId: paymentChannel.channelId, state: paymentChannel.state, tokenContract: paymentChannel.tokenContract, settlementPeriod: paymentChannel.settlementPeriod, settlingUntil: paymentChannel.settlingUntil.toString() }; await this.engine.exec(client => { return client.insert(document); }); } async firstById(channelId) { const query = { kind: this.kind, channelId: channelId.toString() }; return this.engine.exec(async (client) => { let doc = await client.find(query); return this.inflatePaymentChannel(doc[0]); }); } /** * Set amount of money spent on the channel. */ async spend(channelId, spent) { const query = { kind: this.kind, channelId: channelId.toString() }; const update = { $set: { spent: spent.toString() } }; await this.engine.exec(client => { return client.update(query, update, {}); }); } async deposit(channelId, value) { const channel = await this.firstById(channelId); if (!channel) { throw new Error('Channel not found.'); } const query = { kind: this.kind, channelId: channelId.toString() }; const newValue = channel.value.add(value); const update = { $set: { value: newValue.toString() } }; await this.engine.exec(client => { return client.update(query, update, {}); }); } /** * Retrieve all the payment channels stored. * * @return {Promise<PaymentChannel>} */ async all() { let raw = await this.engine.exec(client => { return client.find({ kind: this.kind }); }); return this.inflatePaymentChannels(raw); } async allOpen() { let raw = await this.engine.exec(client => { return client.find({ kind: this.kind, state: 0 }); }); let channels = await this.inflatePaymentChannels(raw); return this.filterByState(0, channels); } async findUsable(sender, receiver, amount) { const query = { kind: this.kind, state: 0, sender, receiver }; let raw = await this.engine.exec(client => { return client.find(query); }); let channels = await this.inflatePaymentChannels(raw); let filtered = this.filterByState(0, channels); return filtered.find((chan) => chan.value.greaterThanOrEqualTo(chan.spent.add(amount))) || null; } async findBySenderReceiver(sender, receiver) { let raw = await this.engine.exec(client => { return client.find({ sender, receiver, kind: this.kind }); }); return this.inflatePaymentChannels(raw); } async findBySenderReceiverChannelId(sender, receiver, channelId) { let query = { sender, receiver, channelId: channelId.toString(), kind: this.kind }; let res = await this.engine.exec(client => { return client.find(query); }); return this.inflatePaymentChannel(res[0]); } async updateState(channelId, state) { const query = { kind: this.kind, channelId: channelId.toString() }; const update = { $set: { state } }; await this.engine.exec(client => { return client.update(query, update, {}); }); } async updateSettlingUntil(channelId, settlingUntil) { const query = { kind: this.kind, channelId: channelId.toString() }; const update = { $set: { settlingUntil: settlingUntil.toString() } }; await this.engine.exec(client => { return client.update(query, update, {}); }); } } exports.default = NedbChannelsDatabase; //# sourceMappingURL=NedbChannelsDatabase.js.map