UNPKG

@test-org122/hypernet-core

Version:

Hypernet Core. Represents the SDK for running the Hypernet Protocol.

85 lines 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VectorLinkRepository = void 0; const utils_1 = require("@test-org122/utils"); const objects_1 = require("@interfaces/objects"); const neverthrow_1 = require("neverthrow"); /** * Provides methods for retrieving Hypernet Links. */ class VectorLinkRepository { /** * Get an instance of the VectorLinkRepository */ constructor(browserNodeProvider, configProvider, contextProvider, vectorUtils, paymentUtils, linkUtils, timeUtils) { this.browserNodeProvider = browserNodeProvider; this.configProvider = configProvider; this.contextProvider = contextProvider; this.vectorUtils = vectorUtils; this.paymentUtils = paymentUtils; this.linkUtils = linkUtils; this.timeUtils = timeUtils; } /** * Get all Hypernet Links for this client */ getHypernetLinks() { let browserNode; return utils_1.ResultUtils.combine([this.browserNodeProvider.getBrowserNode(), this.vectorUtils.getRouterChannelAddress()]) .andThen((vals) => { let channelAddress; [browserNode, channelAddress] = vals; return browserNode.getActiveTransfers(channelAddress); }) .andThen((activeTransfers) => { // We also need to look for potentially resolved transfers const earliestDate = this.paymentUtils.getEarliestDateFromTransfers(activeTransfers); return browserNode.getTransfers(earliestDate, this.timeUtils.getUnixNow()); }) .andThen((transfers) => { if (transfers.length === 0) { return neverthrow_1.okAsync([]); } return this.paymentUtils.transfersToPayments(transfers); }) .andThen((payments) => { return this.linkUtils.paymentsToHypernetLinks(payments); }); } /** * Given the ID of the link, return it. * @param counterpartyId The ID of the link to retrieve */ getHypernetLink(counterpartyId) { let browserNode; return utils_1.ResultUtils.combine([this.browserNodeProvider.getBrowserNode(), this.vectorUtils.getRouterChannelAddress()]) .andThen((vals) => { let channelAddress; [browserNode, channelAddress] = vals; return browserNode.getActiveTransfers(channelAddress); }) .andThen((activeTransfers) => { // We also need to look for potentially resolved transfers const earliestDate = this.paymentUtils.getEarliestDateFromTransfers(activeTransfers); return browserNode.getTransfers(earliestDate, this.timeUtils.getUnixNow()); }) .andThen((transfers) => { const filteredActiveTransfers = transfers.filter((val) => { return val.responder === counterpartyId || val.initiator === counterpartyId; }); // Because of the filter above, this should only produce a single link return this.paymentUtils.transfersToPayments(filteredActiveTransfers); }) .andThen((payments) => { return this.linkUtils.paymentsToHypernetLinks(payments); }) .map((links) => { if (links.length === 0) { return new objects_1.HypernetLink(counterpartyId, [], [], [], [], []); } return links[0]; }); } } exports.VectorLinkRepository = VectorLinkRepository; //# sourceMappingURL=VectorLinkRepository.js.map