@streamflow/stream 3.0.0 → 3.0.1-3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,31 +83,43 @@ var __values = (this && this.__values) || function(o) {
83
83
  };
84
84
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
85
85
  };
86
+ var __importDefault = (this && this.__importDefault) || function (mod) {
87
+ return (mod && mod.__esModule) ? mod : { "default": mod };
88
+ };
86
89
  Object.defineProperty(exports, "__esModule", { value: true });
87
- var spl_token_1 = require("@solana/spl-token");
90
+ var bn_js_1 = __importDefault(require("bn.js"));
88
91
  var buffer_1 = require("buffer");
89
92
  var anchor_1 = require("@project-serum/anchor");
90
- var spl_token_2 = require("@solana/spl-token");
93
+ var spl_token_1 = require("@solana/spl-token");
91
94
  var web3_js_1 = require("@solana/web3.js");
92
95
  var types_1 = require("./types");
93
96
  var utils_1 = require("./utils");
94
97
  var constants_1 = require("./constants");
95
98
  var instructions_1 = require("./instructions");
99
+ var bs58_1 = require("@project-serum/anchor/dist/cjs/utils/bytes/bs58");
100
+ var METADATA_ACC_SIZE = 1104;
96
101
  var StreamClient = /** @class */ (function () {
97
102
  /**
98
103
  * Create Stream instance
99
104
  */
100
- function StreamClient(clusterUrl, cluster, commitment) {
105
+ function StreamClient(clusterUrl, cluster, commitment, programId) {
101
106
  if (cluster === void 0) { cluster = types_1.Cluster.Mainnet; }
102
107
  if (commitment === void 0) { commitment = "confirmed"; }
108
+ if (programId === void 0) { programId = ""; }
103
109
  this.commitment = commitment;
104
110
  this.cluster = cluster;
105
111
  this.connection = new web3_js_1.Connection(clusterUrl, this.commitment);
106
112
  this.programId = new web3_js_1.PublicKey(constants_1.PROGRAM_ID[cluster]);
113
+ if (programId != "") {
114
+ this.programId = new web3_js_1.PublicKey(programId);
115
+ }
107
116
  }
108
117
  StreamClient.prototype.getConnection = function () {
109
118
  return this.connection;
110
119
  };
120
+ StreamClient.prototype.getProgramId = function () {
121
+ return this.programId.toBase58();
122
+ };
111
123
  /**
112
124
  * Creates a new stream/vesting contract.
113
125
  * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
@@ -116,11 +128,11 @@ var StreamClient = /** @class */ (function () {
116
128
  * @param {string} data.recipient - Solana address of the recipient. Associated token account will be derived using this address and token mint address.
117
129
  * @param {string} data.mint - SPL Token mint.
118
130
  * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
119
- * @param {u64} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
131
+ * @param {BN} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
120
132
  * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
121
133
  * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
122
- * @param {u64} data.cliffAmount - Amount unlocked at the "cliff".
123
- * @param {u64} data.amountPerPeriod - Amount unlocked per each period.
134
+ * @param {BN} data.cliffAmount - Amount unlocked at the "cliff".
135
+ * @param {BN} data.amountPerPeriod - Amount unlocked per each period.
124
136
  * @param {string} data.name - Stream name/subject.
125
137
  * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
126
138
  * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
@@ -129,44 +141,55 @@ var StreamClient = /** @class */ (function () {
129
141
  * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
130
142
  * @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
131
143
  * @param {number} [data.withdrawalFrequency = 0] - Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
144
+ * @param {boolean} [data.isNative = false] - When true Automatically wraps SOLs to WrappedSOL and creates wSOL stream
132
145
  * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
133
146
  */
134
147
  StreamClient.prototype.create = function (_a) {
135
- var sender = _a.sender, recipient = _a.recipient, mint = _a.mint, start = _a.start, depositedAmount = _a.depositedAmount, period = _a.period, cliff = _a.cliff, cliffAmount = _a.cliffAmount, amountPerPeriod = _a.amountPerPeriod, name = _a.name, canTopup = _a.canTopup, cancelableBySender = _a.cancelableBySender, cancelableByRecipient = _a.cancelableByRecipient, transferableBySender = _a.transferableBySender, transferableByRecipient = _a.transferableByRecipient, _b = _a.automaticWithdrawal, automaticWithdrawal = _b === void 0 ? false : _b, _c = _a.withdrawalFrequency, withdrawalFrequency = _c === void 0 ? 0 : _c, _d = _a.partner, partner = _d === void 0 ? null : _d;
148
+ var sender = _a.sender, recipient = _a.recipient, mint = _a.mint, start = _a.start, depositedAmount = _a.depositedAmount, period = _a.period, cliff = _a.cliff, cliffAmount = _a.cliffAmount, amountPerPeriod = _a.amountPerPeriod, name = _a.name, canTopup = _a.canTopup, cancelableBySender = _a.cancelableBySender, cancelableByRecipient = _a.cancelableByRecipient, transferableBySender = _a.transferableBySender, transferableByRecipient = _a.transferableByRecipient, _b = _a.automaticWithdrawal, automaticWithdrawal = _b === void 0 ? false : _b, _c = _a.withdrawalFrequency, withdrawalFrequency = _c === void 0 ? 0 : _c, _d = _a.partner, partner = _d === void 0 ? null : _d, _e = _a.isNative, isNative = _e === void 0 ? false : _e;
136
149
  return __awaiter(this, void 0, void 0, function () {
137
- var ixs, mintPublicKey, recipientPublicKey, metadata, _e, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, commitment, hash, tx, signature;
138
- var _f;
139
- return __generator(this, function (_g) {
140
- switch (_g.label) {
150
+ var ixs, mintPublicKey, recipientPublicKey, metadata, _f, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, nativeInstructions, _g, commitment, hash, tx, signature;
151
+ var _h;
152
+ return __generator(this, function (_j) {
153
+ switch (_j.label) {
141
154
  case 0:
142
155
  ixs = [];
143
- mintPublicKey = new web3_js_1.PublicKey(mint);
156
+ mintPublicKey = isNative ? new web3_js_1.PublicKey(mint) : spl_token_1.NATIVE_MINT;
144
157
  recipientPublicKey = new web3_js_1.PublicKey(recipient);
145
158
  metadata = web3_js_1.Keypair.generate();
146
159
  return [4 /*yield*/, web3_js_1.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], this.programId)];
147
160
  case 1:
148
- _e = __read.apply(void 0, [_g.sent(), 1]), escrowTokens = _e[0];
161
+ _f = __read.apply(void 0, [_j.sent(), 1]), escrowTokens = _f[0];
149
162
  return [4 /*yield*/, ata(mintPublicKey, sender.publicKey)];
150
163
  case 2:
151
- senderTokens = _g.sent();
164
+ senderTokens = _j.sent();
152
165
  return [4 /*yield*/, ata(mintPublicKey, recipientPublicKey)];
153
166
  case 3:
154
- recipientTokens = _g.sent();
167
+ recipientTokens = _j.sent();
155
168
  return [4 /*yield*/, ata(mintPublicKey, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
156
169
  case 4:
157
- streamflowTreasuryTokens = _g.sent();
170
+ streamflowTreasuryTokens = _j.sent();
158
171
  partnerPublicKey = partner
159
172
  ? new web3_js_1.PublicKey(partner)
160
173
  : constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY;
161
174
  return [4 /*yield*/, ata(mintPublicKey, partnerPublicKey)];
162
175
  case 5:
163
- partnerTokens = _g.sent();
176
+ partnerTokens = _j.sent();
177
+ if (!isNative) return [3 /*break*/, 7];
178
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, sender.publicKey, depositedAmount)];
179
+ case 6:
180
+ _g = _j.sent();
181
+ return [3 /*break*/, 8];
182
+ case 7:
183
+ _g = [];
184
+ _j.label = 8;
185
+ case 8:
186
+ nativeInstructions = _g;
164
187
  ixs.push((0, instructions_1.createStreamInstruction)({
165
- start: new spl_token_1.u64(start),
188
+ start: new bn_js_1.default(start),
166
189
  depositedAmount: depositedAmount,
167
- period: new spl_token_1.u64(period),
190
+ period: new bn_js_1.default(period),
168
191
  amountPerPeriod: amountPerPeriod,
169
- cliff: new spl_token_1.u64(cliff),
192
+ cliff: new bn_js_1.default(cliff),
170
193
  cliffAmount: cliffAmount,
171
194
  cancelableBySender: cancelableBySender,
172
195
  cancelableByRecipient: cancelableByRecipient,
@@ -175,7 +198,7 @@ var StreamClient = /** @class */ (function () {
175
198
  transferableByRecipient: transferableByRecipient,
176
199
  canTopup: canTopup,
177
200
  name: name,
178
- withdrawFrequency: new spl_token_1.u64(automaticWithdrawal ? withdrawalFrequency : period),
201
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
179
202
  }, this.programId, {
180
203
  sender: sender.publicKey,
181
204
  senderTokens: senderTokens,
@@ -191,42 +214,52 @@ var StreamClient = /** @class */ (function () {
191
214
  feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
192
215
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
193
216
  timelockProgram: this.programId,
194
- tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
195
- associatedTokenProgram: spl_token_2.ASSOCIATED_TOKEN_PROGRAM_ID,
217
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
218
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
196
219
  withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
197
220
  systemProgram: web3_js_1.SystemProgram.programId,
198
221
  }));
199
222
  commitment = typeof this.commitment == "string"
200
223
  ? this.commitment
201
224
  : this.commitment.commitment;
202
- return [4 /*yield*/, this.connection.getRecentBlockhash(commitment)];
203
- case 6:
204
- hash = _g.sent();
205
- tx = (_f = new web3_js_1.Transaction({
225
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
226
+ case 9:
227
+ hash = _j.sent();
228
+ tx = (_h = new web3_js_1.Transaction({
206
229
  feePayer: sender.publicKey,
207
- recentBlockhash: hash.blockhash,
208
- })).add.apply(_f, __spreadArray([], __read(ixs), false));
230
+ blockhash: hash.blockhash,
231
+ lastValidBlockHeight: hash.lastValidBlockHeight,
232
+ })).add.apply(_h, __spreadArray(__spreadArray([], __read(nativeInstructions), false), __read(ixs), false));
209
233
  tx.partialSign(metadata);
210
- return [4 /*yield*/, this.sign(sender, tx)];
211
- case 7:
212
- signature = _g.sent();
234
+ return [4 /*yield*/, this.sign(sender, tx, hash)];
235
+ case 10:
236
+ signature = _j.sent();
213
237
  return [2 /*return*/, { ixs: ixs, tx: signature, metadata: metadata }];
214
238
  }
215
239
  });
216
240
  });
217
241
  };
218
242
  /**
219
- * Creates a new stream/vesting contract.
220
- * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
221
- * @param {CreateMultiParams} data
243
+ * Creates a new stream/vesting contract using unchecked instruction.
244
+ *
245
+ * Unchecked instruction differes from the regular in:
246
+ * - does not check for initialized associated token account (wallets with no control over their ATA should not be used
247
+ * as sender/recipient/partner or there are risks of funds being locked in the contract)
248
+ * - initialized contract PDA off chain
249
+ *
250
+ * If you are not sure if you should use create or create_unchecked, go for create to be safer.
251
+ *
252
+ * @param {CreateParams} data
222
253
  * @param {Wallet | Keypair} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
223
- * @param {MultiRecipient[]} data.recipientsData
254
+ * @param {string} data.recipient - Solana address of the recipient. Associated token account will be derived using this address and token mint address.
224
255
  * @param {string} data.mint - SPL Token mint.
225
256
  * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
257
+ * @param {BN} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
226
258
  * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
227
259
  * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
228
- * @param {u64} data.cliffAmount - Amount unlocked at the "cliff".
229
- * @param {u64} data.amountPerPeriod - Amount unlocked per each period.
260
+ * @param {BN} data.cliffAmount - Amount unlocked at the "cliff".
261
+ * @param {BN} data.amountPerPeriod - Amount unlocked per each period.
262
+ * @param {string} data.name - Stream name/subject.
230
263
  * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
231
264
  * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
232
265
  * @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
@@ -234,56 +267,55 @@ var StreamClient = /** @class */ (function () {
234
267
  * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
235
268
  * @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
236
269
  * @param {number} [data.withdrawalFrequency = 0] - Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
237
- * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
270
+ * @param {boolean} [data.isNative = false] - When true Automatically wraps SOLs to WrappedSOL and creates wSOL stream
271
+ * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
238
272
  */
239
- StreamClient.prototype.createMultiple = function (_a) {
240
- var sender = _a.sender, recipientsData = _a.recipientsData, mint = _a.mint, start = _a.start, period = _a.period, cliff = _a.cliff, cliffAmount = _a.cliffAmount, amountPerPeriod = _a.amountPerPeriod, canTopup = _a.canTopup, cancelableBySender = _a.cancelableBySender, cancelableByRecipient = _a.cancelableByRecipient, transferableBySender = _a.transferableBySender, transferableByRecipient = _a.transferableByRecipient, _b = _a.automaticWithdrawal, automaticWithdrawal = _b === void 0 ? false : _b, _c = _a.withdrawalFrequency, withdrawalFrequency = _c === void 0 ? 0 : _c, _d = _a.partner, partner = _d === void 0 ? null : _d;
273
+ StreamClient.prototype.createUnchecked = function (_a) {
274
+ var sender = _a.sender, recipient = _a.recipient, mint = _a.mint, start = _a.start, depositedAmount = _a.depositedAmount, period = _a.period, cliff = _a.cliff, cliffAmount = _a.cliffAmount, amountPerPeriod = _a.amountPerPeriod, name = _a.name, canTopup = _a.canTopup, cancelableBySender = _a.cancelableBySender, cancelableByRecipient = _a.cancelableByRecipient, transferableBySender = _a.transferableBySender, transferableByRecipient = _a.transferableByRecipient, _b = _a.automaticWithdrawal, automaticWithdrawal = _b === void 0 ? false : _b, _c = _a.withdrawalFrequency, withdrawalFrequency = _c === void 0 ? 0 : _c, _d = _a.partner, partner = _d === void 0 ? null : _d, _e = _a.isNative, isNative = _e === void 0 ? false : _e;
241
275
  return __awaiter(this, void 0, void 0, function () {
242
- var mintPublicKey, batch, commitment, recipientsData_1, recipientsData_1_1, recipient, ixs, recipientPublicKey, metadata, _e, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, hash, tx, e_1_1, signed_batch, sigs, i, buf, signature;
243
- var e_1, _f, _g;
244
- return __generator(this, function (_h) {
245
- switch (_h.label) {
276
+ var mintPublicKey, recipientPublicKey, metadata, rentToExempt, createMetadataInstruction, _f, escrowTokens, senderTokens, partnerPublicKey, nativeInstructions, _g, createInstruction, commitment, ixs, hash, tx, signature;
277
+ var _h;
278
+ return __generator(this, function (_j) {
279
+ switch (_j.label) {
246
280
  case 0:
247
281
  mintPublicKey = new web3_js_1.PublicKey(mint);
248
- batch = [];
249
- commitment = typeof this.commitment == "string"
250
- ? this.commitment
251
- : this.commitment.commitment;
252
- _h.label = 1;
282
+ recipientPublicKey = new web3_js_1.PublicKey(recipient);
283
+ metadata = web3_js_1.Keypair.generate();
284
+ return [4 /*yield*/, this.connection.getMinimumBalanceForRentExemption(METADATA_ACC_SIZE)];
253
285
  case 1:
254
- _h.trys.push([1, 11, 12, 13]);
255
- recipientsData_1 = __values(recipientsData), recipientsData_1_1 = recipientsData_1.next();
256
- _h.label = 2;
286
+ rentToExempt = _j.sent();
287
+ createMetadataInstruction = web3_js_1.SystemProgram.createAccount({
288
+ programId: this.programId,
289
+ space: METADATA_ACC_SIZE,
290
+ lamports: rentToExempt,
291
+ fromPubkey: sender === null || sender === void 0 ? void 0 : sender.publicKey,
292
+ newAccountPubkey: metadata.publicKey,
293
+ });
294
+ return [4 /*yield*/, web3_js_1.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], this.programId)];
257
295
  case 2:
258
- if (!!recipientsData_1_1.done) return [3 /*break*/, 10];
259
- recipient = recipientsData_1_1.value;
260
- ixs = [];
261
- recipientPublicKey = new web3_js_1.PublicKey(recipient.recipient);
262
- metadata = web3_js_1.Keypair.generate();
263
- return [4 /*yield*/, anchor_1.web3.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], this.programId)];
264
- case 3:
265
- _e = __read.apply(void 0, [_h.sent(), 1]), escrowTokens = _e[0];
296
+ _f = __read.apply(void 0, [_j.sent(), 1]), escrowTokens = _f[0];
266
297
  return [4 /*yield*/, ata(mintPublicKey, sender.publicKey)];
267
- case 4:
268
- senderTokens = _h.sent();
269
- return [4 /*yield*/, ata(mintPublicKey, recipientPublicKey)];
270
- case 5:
271
- recipientTokens = _h.sent();
272
- return [4 /*yield*/, ata(mintPublicKey, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
273
- case 6:
274
- streamflowTreasuryTokens = _h.sent();
298
+ case 3:
299
+ senderTokens = _j.sent();
275
300
  partnerPublicKey = partner
276
301
  ? new web3_js_1.PublicKey(partner)
277
302
  : constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY;
278
- return [4 /*yield*/, ata(mintPublicKey, partnerPublicKey)];
279
- case 7:
280
- partnerTokens = _h.sent();
281
- ixs.push((0, instructions_1.createStreamInstruction)({
282
- start: new spl_token_1.u64(start),
283
- depositedAmount: recipient.depositedAmount,
284
- period: new spl_token_1.u64(period),
303
+ if (!isNative) return [3 /*break*/, 5];
304
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, sender.publicKey, depositedAmount)];
305
+ case 4:
306
+ _g = _j.sent();
307
+ return [3 /*break*/, 6];
308
+ case 5:
309
+ _g = [];
310
+ _j.label = 6;
311
+ case 6:
312
+ nativeInstructions = _g;
313
+ createInstruction = (0, instructions_1.createUncheckedStreamInstruction)({
314
+ start: new bn_js_1.default(start),
315
+ depositedAmount: depositedAmount,
316
+ period: new bn_js_1.default(period),
285
317
  amountPerPeriod: amountPerPeriod,
286
- cliff: new spl_token_1.u64(cliff),
318
+ cliff: new bn_js_1.default(cliff),
287
319
  cliffAmount: cliffAmount,
288
320
  cancelableBySender: cancelableBySender,
289
321
  cancelableByRecipient: cancelableByRecipient,
@@ -291,84 +323,157 @@ var StreamClient = /** @class */ (function () {
291
323
  transferableBySender: transferableBySender,
292
324
  transferableByRecipient: transferableByRecipient,
293
325
  canTopup: canTopup,
294
- name: recipient.name,
295
- withdrawFrequency: new spl_token_1.u64(automaticWithdrawal ? withdrawalFrequency : period),
326
+ name: name,
327
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
328
+ recipient: recipientPublicKey,
329
+ partner: partnerPublicKey,
296
330
  }, this.programId, {
297
331
  sender: sender.publicKey,
298
332
  senderTokens: senderTokens,
299
- recipient: new web3_js_1.PublicKey(recipient.recipient),
300
333
  metadata: metadata.publicKey,
301
334
  escrowTokens: escrowTokens,
302
- recipientTokens: recipientTokens,
303
- streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
304
- streamflowTreasuryTokens: streamflowTreasuryTokens,
305
- partner: partnerPublicKey,
306
- partnerTokens: partnerTokens,
307
335
  mint: new web3_js_1.PublicKey(mint),
308
336
  feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
309
337
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
310
338
  timelockProgram: this.programId,
311
- tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
312
- associatedTokenProgram: spl_token_2.ASSOCIATED_TOKEN_PROGRAM_ID,
339
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
313
340
  withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
314
341
  systemProgram: web3_js_1.SystemProgram.programId,
315
- }));
316
- return [4 /*yield*/, this.connection.getRecentBlockhash(commitment)];
317
- case 8:
318
- hash = _h.sent();
319
- tx = (_g = new web3_js_1.Transaction({
342
+ });
343
+ commitment = typeof this.commitment == "string"
344
+ ? this.commitment
345
+ : this.commitment.commitment;
346
+ ixs = [
347
+ createMetadataInstruction,
348
+ createInstruction,
349
+ ];
350
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
351
+ case 7:
352
+ hash = _j.sent();
353
+ tx = (_h = new web3_js_1.Transaction({
320
354
  feePayer: sender.publicKey,
321
- recentBlockhash: hash.blockhash,
322
- })).add.apply(_g, __spreadArray([], __read(ixs), false));
355
+ blockhash: hash.blockhash,
356
+ lastValidBlockHeight: hash.lastValidBlockHeight,
357
+ })).add.apply(_h, __spreadArray(__spreadArray([], __read(nativeInstructions), false), __read(ixs), false));
323
358
  tx.partialSign(metadata);
324
- batch.push(tx);
325
- _h.label = 9;
326
- case 9:
359
+ return [4 /*yield*/, this.sign(sender, tx, hash)];
360
+ case 8:
361
+ signature = _j.sent();
362
+ return [2 /*return*/, { ixs: ixs, tx: signature, metadata: metadata }];
363
+ }
364
+ });
365
+ });
366
+ };
367
+ /**
368
+ * Creates a new stream/vesting contract.
369
+ * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
370
+ * @param {CreateMultiParams} data
371
+ * @param {Wallet | Keypair} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
372
+ * @param {MultiRecipient[]} data.recipientsData
373
+ * @param {string} data.mint - SPL Token mint.
374
+ * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
375
+ * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
376
+ * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
377
+ * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
378
+ * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
379
+ * @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
380
+ * @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
381
+ * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
382
+ * @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
383
+ * @param {number} [data.withdrawalFrequency = 0] - Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
384
+ * @param {boolean} [data.isNative = false] - When true Automatically wraps SOLs to WrappedSOL and creates wSOL stream
385
+ * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
386
+ */
387
+ StreamClient.prototype.createMultiple = function (data) {
388
+ return __awaiter(this, void 0, void 0, function () {
389
+ var sender, recipientsData, metadatas, metadataToRecipient, errors, signatures, batch, recipientsData_1, recipientsData_1_1, recipientData, _a, tx, metadata, metadataPubKey, e_1_1, totalDepositedAmount, nativeInstructions, commitment, hash, prepareTransaction, signed_batch, prepareTx, batchTransactionsCalls, responses, successes, failures;
390
+ var e_1, _b, _c;
391
+ var _this = this;
392
+ return __generator(this, function (_d) {
393
+ switch (_d.label) {
394
+ case 0:
395
+ sender = data.sender, recipientsData = data.recipientsData;
396
+ metadatas = [];
397
+ metadataToRecipient = {};
398
+ errors = [];
399
+ signatures = [];
400
+ batch = [];
401
+ _d.label = 1;
402
+ case 1:
403
+ _d.trys.push([1, 6, 7, 8]);
404
+ recipientsData_1 = __values(recipientsData), recipientsData_1_1 = recipientsData_1.next();
405
+ _d.label = 2;
406
+ case 2:
407
+ if (!!recipientsData_1_1.done) return [3 /*break*/, 5];
408
+ recipientData = recipientsData_1_1.value;
409
+ return [4 /*yield*/, this.prepareStreamTransaction(recipientData, data)];
410
+ case 3:
411
+ _a = _d.sent(), tx = _a.tx, metadata = _a.metadata;
412
+ metadataPubKey = metadata.publicKey.toBase58();
413
+ metadataToRecipient[metadataPubKey] = recipientData;
414
+ metadatas.push(metadata);
415
+ batch.push({ tx: tx, recipient: recipientData.recipient });
416
+ _d.label = 4;
417
+ case 4:
327
418
  recipientsData_1_1 = recipientsData_1.next();
328
419
  return [3 /*break*/, 2];
329
- case 10: return [3 /*break*/, 13];
330
- case 11:
331
- e_1_1 = _h.sent();
420
+ case 5: return [3 /*break*/, 8];
421
+ case 6:
422
+ e_1_1 = _d.sent();
332
423
  e_1 = { error: e_1_1 };
333
- return [3 /*break*/, 13];
334
- case 12:
424
+ return [3 /*break*/, 8];
425
+ case 7:
335
426
  try {
336
- if (recipientsData_1_1 && !recipientsData_1_1.done && (_f = recipientsData_1.return)) _f.call(recipientsData_1);
427
+ if (recipientsData_1_1 && !recipientsData_1_1.done && (_b = recipientsData_1.return)) _b.call(recipientsData_1);
337
428
  }
338
429
  finally { if (e_1) throw e_1.error; }
339
430
  return [7 /*endfinally*/];
431
+ case 8:
432
+ if (!data.isNative) return [3 /*break*/, 11];
433
+ totalDepositedAmount = recipientsData.reduce(function (acc, recipient) { return recipient.depositedAmount.add(acc); }, new bn_js_1.default(0));
434
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalDepositedAmount)];
435
+ case 9:
436
+ nativeInstructions = _d.sent();
437
+ commitment = typeof this.commitment == "string"
438
+ ? this.commitment
439
+ : this.commitment.commitment;
440
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
441
+ case 10:
442
+ hash = _d.sent();
443
+ prepareTransaction = (_c = new web3_js_1.Transaction({
444
+ feePayer: sender.publicKey,
445
+ blockhash: hash.blockhash,
446
+ lastValidBlockHeight: hash.lastValidBlockHeight,
447
+ })).add.apply(_c, __spreadArray([], __read(nativeInstructions), false));
448
+ batch.push({ tx: prepareTransaction, recipient: sender.publicKey.toBase58() });
449
+ _d.label = 11;
450
+ case 11: return [4 /*yield*/, signAllTransactionWithRecipients(sender, batch)];
451
+ case 12:
452
+ signed_batch = _d.sent();
453
+ if (!data.isNative) return [3 /*break*/, 14];
454
+ prepareTx = signed_batch.pop();
455
+ return [4 /*yield*/, sendAndConfirmStreamRawTransaction(this.connection, prepareTx)];
340
456
  case 13:
341
- if (!(sender instanceof web3_js_1.Keypair)) return [3 /*break*/, 14];
342
- signed_batch = batch.map(function (t) {
343
- t.partialSign(sender);
344
- return t;
345
- });
346
- return [3 /*break*/, 17];
457
+ _d.sent();
458
+ _d.label = 14;
347
459
  case 14:
348
- if (!(sender === null || sender === void 0 ? void 0 : sender.signAllTransactions)) return [3 /*break*/, 16];
349
- return [4 /*yield*/, sender.signAllTransactions(batch)];
460
+ batchTransactionsCalls = signed_batch.map(function (el) {
461
+ return sendAndConfirmStreamRawTransaction(_this.connection, el);
462
+ });
463
+ return [4 /*yield*/, Promise.allSettled(batchTransactionsCalls)];
350
464
  case 15:
351
- signed_batch = _h.sent();
352
- return [3 /*break*/, 17];
353
- case 16:
354
- signed_batch = [];
355
- _h.label = 17;
356
- case 17:
357
- sigs = [];
358
- i = 0;
359
- _h.label = 18;
360
- case 18:
361
- if (!(i < signed_batch.length)) return [3 /*break*/, 21];
362
- buf = signed_batch[i].serialize();
363
- return [4 /*yield*/, (0, web3_js_1.sendAndConfirmRawTransaction)(this.connection, buf)];
364
- case 19:
365
- signature = _h.sent();
366
- sigs.push(signature);
367
- _h.label = 20;
368
- case 20:
369
- i++;
370
- return [3 /*break*/, 18];
371
- case 21: return [2 /*return*/, { txs: sigs }];
465
+ responses = _d.sent();
466
+ successes = responses
467
+ .filter(function (el) {
468
+ return el.status === "fulfilled";
469
+ })
470
+ .map(function (el) { return el.value; });
471
+ signatures.push.apply(signatures, __spreadArray([], __read(successes.map(function (el) { return el.signature; })), false));
472
+ failures = responses
473
+ .filter(function (el) { return el.status === "rejected"; })
474
+ .map(function (el) { return el.reason; });
475
+ errors.push.apply(errors, __spreadArray([], __read(failures), false));
476
+ return [2 /*return*/, { txs: signatures, metadatas: metadatas, metadataToRecipient: metadataToRecipient, errors: errors }];
372
477
  }
373
478
  });
374
479
  });
@@ -378,7 +483,7 @@ var StreamClient = /** @class */ (function () {
378
483
  * @param {WithdrawParams} data
379
484
  * @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (recipient) or transaction will fail.
380
485
  * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be withdrawn from.
381
- * @param {u64} data.amount - Requested amount (in the smallest units) to withdraw (while streaming). If stream is completed, the whole amount will be withdrawn.
486
+ * @param {BN} data.amount - Requested amount (in the smallest units) to withdraw (while streaming). If stream is completed, the whole amount will be withdrawn.
382
487
  */
383
488
  StreamClient.prototype.withdraw = function (_a) {
384
489
  var invoker = _a.invoker, id = _a.id, amount = _a.amount;
@@ -414,19 +519,20 @@ var StreamClient = /** @class */ (function () {
414
519
  partner: data.partner,
415
520
  partnerTokens: partnerTokens,
416
521
  mint: data.mint,
417
- tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
522
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
418
523
  }));
419
524
  commitment = typeof this.commitment == "string"
420
525
  ? this.commitment
421
526
  : this.commitment.commitment;
422
- return [4 /*yield*/, this.connection.getRecentBlockhash(commitment)];
527
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
423
528
  case 4:
424
529
  hash = _c.sent();
425
530
  tx = (_b = new web3_js_1.Transaction({
426
531
  feePayer: invoker.publicKey,
427
- recentBlockhash: hash.blockhash,
532
+ blockhash: hash.blockhash,
533
+ lastValidBlockHeight: hash.lastValidBlockHeight,
428
534
  })).add.apply(_b, __spreadArray([], __read(ixs), false));
429
- return [4 /*yield*/, this.sign(invoker, tx)];
535
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
430
536
  case 5:
431
537
  signature = _c.sent();
432
538
  return [2 /*return*/, { ixs: ixs, tx: signature }];
@@ -476,19 +582,20 @@ var StreamClient = /** @class */ (function () {
476
582
  partner: data.partner,
477
583
  partnerTokens: partnerTokens,
478
584
  mint: data.mint,
479
- tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
585
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
480
586
  }));
481
587
  commitment = typeof this.commitment == "string"
482
588
  ? this.commitment
483
589
  : this.commitment.commitment;
484
- return [4 /*yield*/, this.connection.getRecentBlockhash(commitment)];
590
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
485
591
  case 4:
486
592
  hash = _c.sent();
487
593
  tx = (_b = new web3_js_1.Transaction({
488
594
  feePayer: invoker.publicKey,
489
- recentBlockhash: hash.blockhash,
595
+ blockhash: hash.blockhash,
596
+ lastValidBlockHeight: hash.lastValidBlockHeight,
490
597
  })).add.apply(_b, __spreadArray([], __read(ixs), false));
491
- return [4 /*yield*/, this.sign(invoker, tx)];
598
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
492
599
  case 5:
493
600
  signature = _c.sent();
494
601
  return [2 /*return*/, { ixs: ixs, tx: signature }];
@@ -532,21 +639,22 @@ var StreamClient = /** @class */ (function () {
532
639
  metadata: stream,
533
640
  mint: mint,
534
641
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
535
- tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
536
- associatedTokenProgram: spl_token_2.ASSOCIATED_TOKEN_PROGRAM_ID,
642
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
643
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
537
644
  systemProgram: web3_js_1.SystemProgram.programId,
538
645
  }));
539
646
  commitment = typeof this.commitment == "string"
540
647
  ? this.commitment
541
648
  : this.commitment.commitment;
542
- return [4 /*yield*/, this.connection.getRecentBlockhash(commitment)];
649
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
543
650
  case 3:
544
651
  hash = _c.sent();
545
652
  tx = (_b = new web3_js_1.Transaction({
546
653
  feePayer: invoker.publicKey,
547
- recentBlockhash: hash.blockhash,
654
+ blockhash: hash.blockhash,
655
+ lastValidBlockHeight: hash.lastValidBlockHeight,
548
656
  })).add.apply(_b, __spreadArray([], __read(ixs), false));
549
- return [4 /*yield*/, this.sign(invoker, tx)];
657
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
550
658
  case 4:
551
659
  signature = _c.sent();
552
660
  return [2 /*return*/, { ixs: ixs, tx: signature }];
@@ -559,31 +667,41 @@ var StreamClient = /** @class */ (function () {
559
667
  * @param {TopupParams} data
560
668
  * @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match current stream sender or transaction will fail.
561
669
  * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be topped up.
562
- * @param {u64} data.amount - Specified amount (in the smallest units) to topup (increases deposited amount).
670
+ * @param {BN} data.amount - Specified amount (in the smallest units) to topup (increases deposited amount).
563
671
  */
564
672
  StreamClient.prototype.topup = function (_a) {
565
- var invoker = _a.invoker, id = _a.id, amount = _a.amount;
673
+ var invoker = _a.invoker, id = _a.id, amount = _a.amount, isNative = _a.isNative;
566
674
  return __awaiter(this, void 0, void 0, function () {
567
- var ixs, streamPublicKey, escrow, _b, mint, partner, senderTokens, escrowTokens, streamflowTreasuryTokens, partnerTokens, commitment, hash, tx, signature;
568
- var _c;
569
- return __generator(this, function (_d) {
570
- switch (_d.label) {
675
+ var ixs, streamPublicKey, escrow, _b, mint, partner, senderTokens, escrowTokens, streamflowTreasuryTokens, partnerTokens, nativeInstructions, _c, commitment, hash, tx, signature;
676
+ var _d;
677
+ return __generator(this, function (_e) {
678
+ switch (_e.label) {
571
679
  case 0:
572
680
  ixs = [];
573
681
  streamPublicKey = new web3_js_1.PublicKey(id);
574
682
  return [4 /*yield*/, this.connection.getAccountInfo(streamPublicKey)];
575
683
  case 1:
576
- escrow = _d.sent();
684
+ escrow = _e.sent();
577
685
  if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
578
686
  throw new Error("Couldn't get account info");
579
687
  }
580
688
  _b = (0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data), mint = _b.mint, partner = _b.partner, senderTokens = _b.senderTokens, escrowTokens = _b.escrowTokens;
581
689
  return [4 /*yield*/, ata(mint, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
582
690
  case 2:
583
- streamflowTreasuryTokens = _d.sent();
691
+ streamflowTreasuryTokens = _e.sent();
584
692
  return [4 /*yield*/, ata(mint, partner)];
585
693
  case 3:
586
- partnerTokens = _d.sent();
694
+ partnerTokens = _e.sent();
695
+ if (!isNative) return [3 /*break*/, 5];
696
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, invoker.publicKey, amount)];
697
+ case 4:
698
+ _c = _e.sent();
699
+ return [3 /*break*/, 6];
700
+ case 5:
701
+ _c = [];
702
+ _e.label = 6;
703
+ case 6:
704
+ nativeInstructions = _c;
587
705
  ixs.push((0, instructions_1.topupStreamInstruction)(amount, this.programId, {
588
706
  sender: invoker.publicKey,
589
707
  senderTokens: senderTokens,
@@ -594,23 +712,24 @@ var StreamClient = /** @class */ (function () {
594
712
  partner: partner,
595
713
  partnerTokens: partnerTokens,
596
714
  mint: mint,
597
- tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
715
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
598
716
  withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
599
717
  systemProgram: web3_js_1.SystemProgram.programId,
600
718
  }));
601
719
  commitment = typeof this.commitment == "string"
602
720
  ? this.commitment
603
721
  : this.commitment.commitment;
604
- return [4 /*yield*/, this.connection.getRecentBlockhash(commitment)];
605
- case 4:
606
- hash = _d.sent();
607
- tx = (_c = new web3_js_1.Transaction({
722
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
723
+ case 7:
724
+ hash = _e.sent();
725
+ tx = (_d = new web3_js_1.Transaction({
608
726
  feePayer: invoker.publicKey,
609
- recentBlockhash: hash.blockhash,
610
- })).add.apply(_c, __spreadArray([], __read(ixs), false));
611
- return [4 /*yield*/, this.sign(invoker, tx)];
612
- case 5:
613
- signature = _d.sent();
727
+ blockhash: hash.blockhash,
728
+ lastValidBlockHeight: hash.lastValidBlockHeight,
729
+ })).add.apply(_d, __spreadArray(__spreadArray([], __read(nativeInstructions), false), __read(ixs), false));
730
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
731
+ case 8:
732
+ signature = _e.sent();
614
733
  return [2 /*return*/, { ixs: ixs, tx: signature }];
615
734
  }
616
735
  });
@@ -631,7 +750,7 @@ var StreamClient = /** @class */ (function () {
631
750
  if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
632
751
  throw new Error("Couldn't get account info.");
633
752
  }
634
- return [2 /*return*/, (0, utils_1.formatDecodedStream)((0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data))];
753
+ return [2 /*return*/, new types_1.Contract((0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data))];
635
754
  }
636
755
  });
637
756
  });
@@ -653,10 +772,10 @@ var StreamClient = /** @class */ (function () {
653
772
  case 0:
654
773
  accounts = [];
655
774
  if (!(direction === "all")) return [3 /*break*/, 3];
656
- return [4 /*yield*/, getProgramAccounts(this.connection, wallet, constants_1.STREAM_STRUCT_OFFSET_SENDER, this.cluster)];
775
+ return [4 /*yield*/, getProgramAccounts(this.connection, wallet, constants_1.STREAM_STRUCT_OFFSET_SENDER, this.programId)];
657
776
  case 1:
658
777
  outgoingAccounts = _d.sent();
659
- return [4 /*yield*/, getProgramAccounts(this.connection, wallet, constants_1.STREAM_STRUCT_OFFSET_RECIPIENT, this.cluster)];
778
+ return [4 /*yield*/, getProgramAccounts(this.connection, wallet, constants_1.STREAM_STRUCT_OFFSET_RECIPIENT, this.programId)];
660
779
  case 2:
661
780
  incomingAccounts = _d.sent();
662
781
  accounts = __spreadArray(__spreadArray([], __read(outgoingAccounts), false), __read(incomingAccounts), false);
@@ -665,7 +784,7 @@ var StreamClient = /** @class */ (function () {
665
784
  offset = direction === "outgoing"
666
785
  ? constants_1.STREAM_STRUCT_OFFSET_SENDER
667
786
  : constants_1.STREAM_STRUCT_OFFSET_RECIPIENT;
668
- return [4 /*yield*/, getProgramAccounts(this.connection, wallet, offset, this.cluster)];
787
+ return [4 /*yield*/, getProgramAccounts(this.connection, wallet, offset, this.programId)];
669
788
  case 4:
670
789
  accounts = _d.sent();
671
790
  _d.label = 5;
@@ -673,7 +792,7 @@ var StreamClient = /** @class */ (function () {
673
792
  streams = {};
674
793
  accounts.forEach(function (account) {
675
794
  var _a;
676
- var decoded = (0, utils_1.formatDecodedStream)((0, utils_1.decodeStream)(account.account.data));
795
+ var decoded = new types_1.Contract((0, utils_1.decodeStream)(account.account.data));
677
796
  streams = __assign(__assign({}, streams), (_a = {}, _a[account.pubkey.toBase58()] = decoded, _a));
678
797
  });
679
798
  sortedStreams = Object.entries(streams).sort(function (_a, _b) {
@@ -690,24 +809,30 @@ var StreamClient = /** @class */ (function () {
690
809
  });
691
810
  });
692
811
  };
693
- StreamClient.prototype.sign = function (invoker, tx) {
812
+ StreamClient.prototype.sign = function (invoker, tx, hash) {
694
813
  return __awaiter(this, void 0, void 0, function () {
695
- var rawTx, signature;
814
+ var rawTx, confirmationStrategy, signature;
696
815
  return __generator(this, function (_a) {
697
816
  switch (_a.label) {
698
817
  case 0:
699
- if (!(invoker instanceof web3_js_1.Keypair)) return [3 /*break*/, 1];
700
- tx.partialSign(invoker);
701
- return [3 /*break*/, 3];
702
- case 1:
703
- if (!(invoker === null || invoker === void 0 ? void 0 : invoker.signTransaction)) return [3 /*break*/, 3];
818
+ if (!isAnchorWallet(invoker)) return [3 /*break*/, 2];
704
819
  return [4 /*yield*/, invoker.signTransaction(tx)];
705
- case 2:
820
+ case 1:
706
821
  _a.sent();
822
+ return [3 /*break*/, 3];
823
+ case 2:
824
+ tx.partialSign(invoker);
707
825
  _a.label = 3;
708
826
  case 3:
709
827
  rawTx = tx.serialize();
710
- return [4 /*yield*/, (0, web3_js_1.sendAndConfirmRawTransaction)(this.connection, rawTx)];
828
+ if (!hash.lastValidBlockHeight || !tx.signature || !hash.blockhash)
829
+ throw Error("Error with transaction parameters.");
830
+ confirmationStrategy = {
831
+ lastValidBlockHeight: hash.lastValidBlockHeight,
832
+ signature: (0, bs58_1.encode)(tx.signature),
833
+ blockhash: hash.blockhash,
834
+ };
835
+ return [4 /*yield*/, (0, web3_js_1.sendAndConfirmRawTransaction)(this.connection, rawTx, confirmationStrategy)];
711
836
  case 4:
712
837
  signature = _a.sent();
713
838
  return [2 /*return*/, signature];
@@ -715,14 +840,100 @@ var StreamClient = /** @class */ (function () {
715
840
  });
716
841
  });
717
842
  };
843
+ /**
844
+ * Forms instructions from params, creates a raw transaction and fetch recent blockhash.
845
+ * @param {MultiRecipient} recipient - Wallet sending stream to
846
+ * @param {CreateMultiParams} streamParams - Parameters of stream user wants to create.
847
+ */
848
+ StreamClient.prototype.prepareStreamTransaction = function (recipient, streamParams) {
849
+ return __awaiter(this, void 0, void 0, function () {
850
+ var sender, mint, start, period, cliff, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, _a, automaticWithdrawal, _b, withdrawalFrequency, _c, partner, ixs, commitment, recipientPublicKey, mintPublicKey, metadata, _d, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, hash, tx;
851
+ var _e;
852
+ return __generator(this, function (_f) {
853
+ switch (_f.label) {
854
+ case 0:
855
+ sender = streamParams.sender, mint = streamParams.mint, start = streamParams.start, period = streamParams.period, cliff = streamParams.cliff, canTopup = streamParams.canTopup, cancelableBySender = streamParams.cancelableBySender, cancelableByRecipient = streamParams.cancelableByRecipient, transferableBySender = streamParams.transferableBySender, transferableByRecipient = streamParams.transferableByRecipient, _a = streamParams.automaticWithdrawal, automaticWithdrawal = _a === void 0 ? false : _a, _b = streamParams.withdrawalFrequency, withdrawalFrequency = _b === void 0 ? 0 : _b, _c = streamParams.partner, partner = _c === void 0 ? null : _c;
856
+ ixs = [];
857
+ commitment = typeof this.commitment == "string"
858
+ ? this.commitment
859
+ : this.commitment.commitment;
860
+ recipientPublicKey = new web3_js_1.PublicKey(recipient.recipient);
861
+ mintPublicKey = new web3_js_1.PublicKey(mint);
862
+ metadata = web3_js_1.Keypair.generate();
863
+ return [4 /*yield*/, anchor_1.web3.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], this.programId)];
864
+ case 1:
865
+ _d = __read.apply(void 0, [_f.sent(), 1]), escrowTokens = _d[0];
866
+ return [4 /*yield*/, ata(mintPublicKey, sender.publicKey)];
867
+ case 2:
868
+ senderTokens = _f.sent();
869
+ return [4 /*yield*/, ata(mintPublicKey, recipientPublicKey)];
870
+ case 3:
871
+ recipientTokens = _f.sent();
872
+ return [4 /*yield*/, ata(mintPublicKey, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
873
+ case 4:
874
+ streamflowTreasuryTokens = _f.sent();
875
+ partnerPublicKey = partner
876
+ ? new web3_js_1.PublicKey(partner)
877
+ : constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY;
878
+ return [4 /*yield*/, ata(mintPublicKey, partnerPublicKey)];
879
+ case 5:
880
+ partnerTokens = _f.sent();
881
+ ixs.push((0, instructions_1.createStreamInstruction)({
882
+ start: new bn_js_1.default(start),
883
+ depositedAmount: recipient.depositedAmount,
884
+ period: new bn_js_1.default(period),
885
+ amountPerPeriod: recipient.amountPerPeriod,
886
+ cliff: new bn_js_1.default(cliff),
887
+ cliffAmount: recipient.cliffAmount,
888
+ cancelableBySender: cancelableBySender,
889
+ cancelableByRecipient: cancelableByRecipient,
890
+ automaticWithdrawal: automaticWithdrawal,
891
+ transferableBySender: transferableBySender,
892
+ transferableByRecipient: transferableByRecipient,
893
+ canTopup: canTopup,
894
+ name: recipient.name,
895
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
896
+ }, this.programId, {
897
+ sender: sender.publicKey,
898
+ senderTokens: senderTokens,
899
+ recipient: new web3_js_1.PublicKey(recipient.recipient),
900
+ metadata: metadata.publicKey,
901
+ escrowTokens: escrowTokens,
902
+ recipientTokens: recipientTokens,
903
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
904
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
905
+ partner: partnerPublicKey,
906
+ partnerTokens: partnerTokens,
907
+ mint: new web3_js_1.PublicKey(mint),
908
+ feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
909
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
910
+ timelockProgram: this.programId,
911
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
912
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
913
+ withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
914
+ systemProgram: web3_js_1.SystemProgram.programId,
915
+ }));
916
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
917
+ case 6:
918
+ hash = _f.sent();
919
+ tx = (_e = new web3_js_1.Transaction({
920
+ feePayer: sender.publicKey,
921
+ blockhash: hash.blockhash,
922
+ lastValidBlockHeight: hash.lastValidBlockHeight,
923
+ })).add.apply(_e, __spreadArray([], __read(ixs), false));
924
+ tx.partialSign(metadata);
925
+ return [2 /*return*/, { tx: tx, metadata: metadata }];
926
+ }
927
+ });
928
+ });
929
+ };
718
930
  return StreamClient;
719
931
  }());
720
932
  exports.default = StreamClient;
721
- function getProgramAccounts(connection, wallet, offset, cluster) {
722
- if (cluster === void 0) { cluster = types_1.Cluster.Mainnet; }
933
+ function getProgramAccounts(connection, wallet, offset, programId) {
723
934
  return __awaiter(this, void 0, void 0, function () {
724
935
  return __generator(this, function (_a) {
725
- return [2 /*return*/, connection === null || connection === void 0 ? void 0 : connection.getProgramAccounts(new web3_js_1.PublicKey(constants_1.PROGRAM_ID[cluster]), {
936
+ return [2 /*return*/, connection === null || connection === void 0 ? void 0 : connection.getProgramAccounts(programId, {
726
937
  filters: [
727
938
  {
728
939
  memcmp: {
@@ -739,9 +950,68 @@ function ata(mint, account) {
739
950
  return __awaiter(this, void 0, void 0, function () {
740
951
  return __generator(this, function (_a) {
741
952
  switch (_a.label) {
742
- case 0: return [4 /*yield*/, spl_token_2.Token.getAssociatedTokenAddress(spl_token_2.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_2.TOKEN_PROGRAM_ID, mint, account)];
953
+ case 0: return [4 /*yield*/, spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, mint, account, true)];
743
954
  case 1: return [2 /*return*/, _a.sent()];
744
955
  }
745
956
  });
746
957
  });
747
958
  }
959
+ function isAnchorWallet(wallet) {
960
+ return wallet.signTransaction !== undefined;
961
+ }
962
+ function signAllTransactionWithRecipients(sender, batch) {
963
+ return __awaiter(this, void 0, void 0, function () {
964
+ var signed_batch, isKeypair, isWallet;
965
+ return __generator(this, function (_a) {
966
+ switch (_a.label) {
967
+ case 0:
968
+ signed_batch = [];
969
+ isKeypair = sender instanceof web3_js_1.Keypair;
970
+ isWallet = isAnchorWallet(sender);
971
+ if (!isKeypair && !isWallet)
972
+ return [2 /*return*/, signed_batch];
973
+ if (!isKeypair) return [3 /*break*/, 1];
974
+ batch.map(function (t) {
975
+ t.tx.partialSign(sender);
976
+ return { tx: t.tx, recipient: t.recipient };
977
+ });
978
+ return [3 /*break*/, 3];
979
+ case 1:
980
+ if (!isWallet) return [3 /*break*/, 3];
981
+ return [4 /*yield*/, sender.signAllTransactions(batch.map(function (t) { return t.tx; }))];
982
+ case 2:
983
+ _a.sent();
984
+ _a.label = 3;
985
+ case 3: return [2 /*return*/, batch];
986
+ }
987
+ });
988
+ });
989
+ }
990
+ function sendAndConfirmStreamRawTransaction(connection, batchItem) {
991
+ return __awaiter(this, void 0, void 0, function () {
992
+ var rawTx, _a, lastValidBlockHeight, signature, recentBlockhash, confirmationStrategy, completedTxSignature, error_1;
993
+ return __generator(this, function (_b) {
994
+ switch (_b.label) {
995
+ case 0:
996
+ _b.trys.push([0, 2, , 3]);
997
+ rawTx = batchItem.tx.serialize();
998
+ _a = batchItem.tx, lastValidBlockHeight = _a.lastValidBlockHeight, signature = _a.signature, recentBlockhash = _a.recentBlockhash;
999
+ if (!lastValidBlockHeight || !signature || !recentBlockhash)
1000
+ throw { recipient: batchItem.recipient, error: "no recent blockhash" };
1001
+ confirmationStrategy = {
1002
+ lastValidBlockHeight: lastValidBlockHeight,
1003
+ signature: (0, bs58_1.encode)(signature),
1004
+ blockhash: recentBlockhash,
1005
+ };
1006
+ return [4 /*yield*/, (0, web3_js_1.sendAndConfirmRawTransaction)(connection, rawTx, confirmationStrategy)];
1007
+ case 1:
1008
+ completedTxSignature = _b.sent();
1009
+ return [2 /*return*/, __assign(__assign({}, batchItem), { signature: completedTxSignature })];
1010
+ case 2:
1011
+ error_1 = _b.sent();
1012
+ throw { recipient: batchItem.recipient, error: error_1 };
1013
+ case 3: return [2 /*return*/];
1014
+ }
1015
+ });
1016
+ });
1017
+ }