UNPKG

stellar-plus

Version:

beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain

83 lines (82 loc) 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChannelAccounts = void 0; const tslib_1 = require("tslib"); const stellar_sdk_1 = require("@stellar/stellar-sdk"); const default_1 = require("../../stellar-plus/account/account-handler/default"); const errors_1 = require("../../stellar-plus/channel-accounts/errors"); const classic_transaction_1 = require("../../stellar-plus/core/pipelines/classic-transaction"); class ChannelAccounts { /** * @args {} The arguments for opening channels. * @param {number} numberOfChannels The number of channels to open. * @param {AccountHandler} sponsor The account that will sponsor the channels. * @param {NetworkConfig} networkConfig The network to use. * @param {TransactionInvocation} txInvocation: The transaction invocation settings to use when building the transaction envelope. * * @description - Opens the given number of channels and returns the list of channel accounts. The accounts will be funded with 0 balance and sponsored by the sponsor account. * * @returns {DefaultAccountHandler[]} Array of channel accounts. */ static openChannels(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { numberOfChannels, sponsor, transactionPipelineOptions, txInvocation, networkConfig } = args; const classicTransactionPipeline = new classic_transaction_1.ClassicTransactionPipeline(networkConfig, transactionPipelineOptions); if (numberOfChannels <= 0 || numberOfChannels > 15) { throw errors_1.CHAError.invalidNumberOfChannelsToCreate(0, 15); } const channels = []; const operations = []; for (let i = 0; i < numberOfChannels; i++) { const channel = new default_1.DefaultAccountHandlerClient({ networkConfig: networkConfig }); channels.push(channel); operations.push(stellar_sdk_1.Operation.beginSponsoringFutureReserves({ sponsoredId: channel.getPublicKey(), }), stellar_sdk_1.Operation.createAccount({ source: sponsor.getPublicKey(), destination: channel.getPublicKey(), startingBalance: '0', }), stellar_sdk_1.Operation.endSponsoringFutureReserves({ source: channel.getPublicKey(), })); } const updatedTxInvocation = Object.assign(Object.assign({}, txInvocation), { signers: [...txInvocation.signers, ...channels, sponsor] }); yield classicTransactionPipeline.execute({ txInvocation: updatedTxInvocation, operations, }); return channels; }); } /** * @args {} The arguments for closing channels. * @param {DefaultAccountHandler[]} channels The list of channels to close. * @param {DefaultAccountHandler} sponsor The account that was the sponsor for the channels. * @param {NetworkConfig} networkConfig The network to use. * @param {TransactionInvocation }xInvocation The transaction invocation settings to use when building the transaction envelope. * * @description - Closes the given channels and merges the balances into the sponsor account. * * @returns {void} */ static closeChannels(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { channels, sponsor, networkConfig, txInvocation, transactionPipelineOptions } = args; const classicTransactionPipeline = new classic_transaction_1.ClassicTransactionPipeline(networkConfig, transactionPipelineOptions); const operations = []; for (let i = 0; i < channels.length; i++) { const channel = channels[i]; operations.push(stellar_sdk_1.Operation.accountMerge({ source: channel.getPublicKey(), destination: sponsor.getPublicKey(), })); } const updatedTxInvocation = Object.assign(Object.assign({}, txInvocation), { signers: [...txInvocation.signers, ...channels, sponsor] }); yield classicTransactionPipeline.execute({ txInvocation: updatedTxInvocation, operations, }); }); } } exports.ChannelAccounts = ChannelAccounts;