@streamflow/stream 7.0.0-alpha.9 → 7.0.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.
Files changed (61) hide show
  1. package/README.md +22 -17
  2. package/dist/cjs/__tests__/aptos/streamClient.spec.js +4 -4
  3. package/dist/cjs/aptos/types.js +12 -12
  4. package/dist/cjs/aptos/utils.js +2 -3
  5. package/dist/cjs/common/constants.js +4 -3
  6. package/dist/cjs/common/contractUtils.js +23 -7
  7. package/dist/cjs/common/types.js +30 -1
  8. package/dist/cjs/common/utils.js +23 -20
  9. package/dist/cjs/evm/StreamClient.js +7 -7
  10. package/dist/cjs/evm/types.js +12 -12
  11. package/dist/cjs/evm/utils.js +1 -2
  12. package/dist/cjs/solana/StreamClient.js +399 -152
  13. package/dist/cjs/solana/constants.js +29 -1
  14. package/dist/cjs/solana/descriptor/idl/streamflow_aligned_unlocks.json +1471 -0
  15. package/dist/cjs/solana/descriptor/streamflow_aligned_unlocks.js +2 -0
  16. package/dist/cjs/solana/index.js +2 -1
  17. package/dist/cjs/solana/instructions.js +18 -19
  18. package/dist/cjs/solana/lib/derive-accounts.js +17 -0
  19. package/dist/cjs/solana/{utils.js → lib/utils.js} +32 -39
  20. package/dist/cjs/solana/types.js +26 -3
  21. package/dist/cjs/sui/StreamClient.js +1 -1
  22. package/dist/cjs/sui/types.js +12 -12
  23. package/dist/cjs/sui/utils.js +1 -2
  24. package/dist/cjs/sui/wallet.js +2 -2
  25. package/dist/esm/__tests__/aptos/streamClient.spec.js +4 -4
  26. package/dist/esm/aptos/types.d.ts +14 -14
  27. package/dist/esm/aptos/types.js +13 -13
  28. package/dist/esm/common/constants.d.ts +3 -2
  29. package/dist/esm/common/constants.js +3 -2
  30. package/dist/esm/common/contractUtils.d.ts +18 -15
  31. package/dist/esm/common/contractUtils.js +19 -6
  32. package/dist/esm/common/types.d.ts +112 -57
  33. package/dist/esm/common/types.js +29 -0
  34. package/dist/esm/common/utils.d.ts +11 -11
  35. package/dist/esm/common/utils.js +19 -16
  36. package/dist/esm/evm/StreamClient.d.ts +2 -2
  37. package/dist/esm/evm/StreamClient.js +7 -7
  38. package/dist/esm/evm/types.d.ts +14 -14
  39. package/dist/esm/evm/types.js +13 -13
  40. package/dist/esm/solana/StreamClient.d.ts +27 -16
  41. package/dist/esm/solana/StreamClient.js +402 -154
  42. package/dist/esm/solana/constants.d.ts +14 -1
  43. package/dist/esm/solana/constants.js +29 -1
  44. package/dist/esm/solana/descriptor/idl/streamflow_aligned_unlocks.json +1471 -0
  45. package/dist/esm/solana/descriptor/streamflow_aligned_unlocks.d.ts +1272 -0
  46. package/dist/esm/solana/descriptor/streamflow_aligned_unlocks.js +1 -0
  47. package/dist/esm/solana/index.d.ts +2 -1
  48. package/dist/esm/solana/index.js +2 -1
  49. package/dist/esm/solana/instructions.d.ts +17 -17
  50. package/dist/esm/solana/instructions.js +18 -19
  51. package/dist/esm/solana/layout.d.ts +0 -1
  52. package/dist/esm/solana/lib/derive-accounts.d.ts +4 -0
  53. package/dist/esm/solana/lib/derive-accounts.js +11 -0
  54. package/dist/esm/solana/{utils.d.ts → lib/utils.d.ts} +1 -4
  55. package/dist/esm/solana/{utils.js → lib/utils.js} +28 -34
  56. package/dist/esm/solana/types.d.ts +67 -40
  57. package/dist/esm/solana/types.js +32 -4
  58. package/dist/esm/sui/StreamClient.js +1 -1
  59. package/dist/esm/sui/types.d.ts +14 -14
  60. package/dist/esm/sui/types.js +13 -13
  61. package/package.json +11 -9
@@ -28,20 +28,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.SolanaStreamClient = void 0;
31
- const bignumber_js_1 = __importDefault(require("bignumber.js"));
31
+ const bn_js_1 = __importDefault(require("bn.js"));
32
32
  const buffer_1 = require("buffer");
33
33
  const spl_token_1 = require("@solana/spl-token");
34
34
  const web3_js_1 = require("@solana/web3.js");
35
35
  const solana_1 = require("@streamflow/common/solana");
36
36
  const borsh = __importStar(require("borsh"));
37
+ const anchor_1 = require("@coral-xyz/anchor");
38
+ const common_1 = require("@streamflow/common");
37
39
  const types_js_1 = require("./types.js");
38
- const utils_js_1 = require("./utils.js");
40
+ const utils_js_1 = require("./lib/utils.js");
39
41
  const constants_js_1 = require("./constants.js");
40
42
  const instructions_js_1 = require("./instructions.js");
41
43
  const types_js_2 = require("../common/types.js");
42
44
  const BaseStreamClient_js_1 = require("../common/BaseStreamClient.js");
43
45
  const utils_js_2 = require("../common/utils.js");
44
46
  const constants_js_2 = require("../common/constants.js");
47
+ const streamflow_aligned_unlocks_json_1 = __importDefault(require("./descriptor/idl/streamflow_aligned_unlocks.json"));
48
+ const derive_accounts_js_1 = require("./lib/derive-accounts.js");
49
+ const contractUtils_js_1 = require("../common/contractUtils.js");
45
50
  const METADATA_ACC_SIZE = 1104;
46
51
  class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
47
52
  /**
@@ -53,6 +58,11 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
53
58
  this.connection = new web3_js_1.Connection(clusterUrl, this.commitment);
54
59
  this.programId = programId !== "" ? new web3_js_1.PublicKey(programId) : new web3_js_1.PublicKey(constants_js_1.PROGRAM_ID[cluster]);
55
60
  this.sendThrottler = sendThrottler ?? (0, solana_1.buildSendThrottler)(sendRate);
61
+ const alignedUnlocksProgram = {
62
+ ...streamflow_aligned_unlocks_json_1.default,
63
+ address: streamflow_aligned_unlocks_json_1.default.address,
64
+ };
65
+ this.alignedProxyProgram = new anchor_1.Program(alignedUnlocksProgram, { connection: this.connection });
56
66
  }
57
67
  getConnection() {
58
68
  return this.connection;
@@ -68,7 +78,23 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
68
78
  * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
69
79
  */
70
80
  async create(data, extParams) {
71
- const { ixs, metadata, metadataPubKey } = await this.prepareCreateInstructions(data, extParams);
81
+ const { partner, amount } = data;
82
+ const { isNative, sender } = extParams;
83
+ const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
84
+ const mintPublicKey = new web3_js_1.PublicKey(data.tokenId);
85
+ if (!sender.publicKey) {
86
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
87
+ }
88
+ const ixs = await this.getCreateATAInstructions([constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, partnerPublicKey], mintPublicKey, sender, true);
89
+ const { ixs: createIxs, metadata, metadataPubKey } = await this.prepareCreateInstructions(data, extParams);
90
+ ixs.push(...createIxs);
91
+ if (isNative) {
92
+ const totalFee = await this.getTotalFee({
93
+ address: partnerPublicKey.toString(),
94
+ });
95
+ const totalAmount = (0, utils_js_2.calculateTotalAmountToDeposit)(amount, totalFee);
96
+ ixs.push(...(await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalAmount)));
97
+ }
72
98
  const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, extParams.sender.publicKey, undefined, metadata);
73
99
  const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.sender, tx, {
74
100
  hash,
@@ -77,11 +103,81 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
77
103
  }, { sendThrottler: this.sendThrottler });
78
104
  return { ixs, txId: signature, metadataId: metadataPubKey.toBase58() };
79
105
  }
106
+ async prepareCreateInstructions(streamParams, extParams) {
107
+ const { ixs, metadata, metadataPubKey } = (0, contractUtils_js_1.isCreateAlignedStreamData)(streamParams)
108
+ ? await this.prepareCreateAlignedUnlockInstructions(streamParams, extParams)
109
+ : await this.prepareCreateStreamInstructions(streamParams, extParams);
110
+ return { ixs, metadata, metadataPubKey };
111
+ }
112
+ async prepareCreateAlignedUnlockInstructions(streamParams, extParams) {
113
+ const { tokenId: mint, start, period, cliff, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, partner, recipient, cliffAmount, amountPerPeriod, amount: depositedAmount, name: streamName, minPrice, maxPercentage, minPercentage, maxPrice, skipInitial, tickSize, priceOracle, oracleType, } = streamParams;
114
+ const { isNative, sender, computeLimit, computePrice, metadataPubKeys } = extParams;
115
+ if (!sender.publicKey) {
116
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
117
+ }
118
+ if (!priceOracle && oracleType && oracleType !== "none") {
119
+ throw new Error("Price oracle is required for the specified oracle type");
120
+ }
121
+ const recipientPublicKey = new web3_js_1.PublicKey(recipient);
122
+ const mintPublicKey = isNative ? spl_token_1.NATIVE_MINT : new web3_js_1.PublicKey(mint);
123
+ const metadata = !metadataPubKeys ? web3_js_1.Keypair.generate() : undefined;
124
+ const metadataPubKey = metadata ? metadata.publicKey : metadataPubKeys[0];
125
+ const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mintPublicKey);
126
+ const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
127
+ const streamflowProgramPublicKey = new web3_js_1.PublicKey(this.programId);
128
+ const escrowPDA = (0, derive_accounts_js_1.deriveEscrowPDA)(streamflowProgramPublicKey, metadataPubKey);
129
+ const oracle = priceOracle ?? (0, derive_accounts_js_1.deriveTestOraclePDA)(this.alignedProxyProgram.programId, mintPublicKey, sender.publicKey);
130
+ const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
131
+ computePrice,
132
+ computeLimit: computeLimit ?? constants_js_1.ALIGNED_COMPUTE_LIMIT,
133
+ });
134
+ ixs.push(...(await this.getCreateATAInstructions([recipientPublicKey], mintPublicKey, sender, true, tokenProgramId)));
135
+ const encodedUIntArray = new TextEncoder().encode(streamName);
136
+ const streamNameArray = Array.from(encodedUIntArray);
137
+ const createIx = await this.alignedProxyProgram.methods
138
+ .create({
139
+ startTime: new bn_js_1.default(start),
140
+ netAmountDeposited: depositedAmount,
141
+ period: new bn_js_1.default(period),
142
+ amountPerPeriod: amountPerPeriod,
143
+ cliff: new bn_js_1.default(cliff),
144
+ cliffAmount: cliffAmount,
145
+ transferableBySender,
146
+ transferableByRecipient,
147
+ cancelableByRecipient,
148
+ cancelableBySender,
149
+ canTopup,
150
+ oracleType: (!!oracleType ? { [oracleType]: {} } : { none: {} }),
151
+ streamName: streamNameArray,
152
+ minPrice: (0, common_1.getBN)(minPrice, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
153
+ maxPrice: (0, common_1.getBN)(maxPrice, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
154
+ minPercentage: (0, common_1.getBN)(minPercentage, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
155
+ maxPercentage: (0, common_1.getBN)(maxPercentage, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
156
+ tickSize: new bn_js_1.default(tickSize || 1),
157
+ skipInitial: skipInitial ?? false,
158
+ })
159
+ .accountsPartial({
160
+ sender: sender.publicKey,
161
+ streamMetadata: metadataPubKey,
162
+ escrowTokens: escrowPDA,
163
+ mint: mintPublicKey,
164
+ partner: partnerPublicKey,
165
+ recipient: recipientPublicKey,
166
+ withdrawor: constants_js_1.WITHDRAWOR_PUBLIC_KEY,
167
+ feeOracle: constants_js_1.FEE_ORACLE_PUBLIC_KEY,
168
+ priceOracle: oracle,
169
+ tokenProgram: tokenProgramId,
170
+ streamflowProgram: this.programId,
171
+ })
172
+ .instruction();
173
+ ixs.push(createIx);
174
+ return { ixs, metadata, metadataPubKey };
175
+ }
80
176
  /**
81
177
  * Creates a new stream/vesting contract.
82
178
  * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
83
179
  */
84
- async prepareCreateInstructions({ recipient, tokenId: mint, start, amount: depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canPause, canUpdateRate, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal = false, withdrawalFrequency = 0, partner, }, { sender, metadataPubKeys, isNative = false, computePrice, computeLimit }) {
180
+ async prepareCreateStreamInstructions({ recipient, tokenId: mint, start, amount: depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canPause, canUpdateRate, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal = false, withdrawalFrequency = 0, partner, }, { sender, metadataPubKeys, isNative = false, computePrice, computeLimit }) {
85
181
  if (!sender.publicKey) {
86
182
  throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
87
183
  }
@@ -99,20 +195,13 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
99
195
  const streamflowTreasuryTokens = await (0, solana_1.ata)(mintPublicKey, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
100
196
  const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
101
197
  const partnerTokens = await (0, solana_1.ata)(mintPublicKey, partnerPublicKey, tokenProgramId);
102
- if (isNative) {
103
- const totalFee = await this.getTotalFee({
104
- address: partner ?? sender.publicKey.toBase58(),
105
- });
106
- const totalAmount = (0, utils_js_2.calculateTotalAmountToDeposit)(depositedAmount, totalFee);
107
- ixs.push(...(await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalAmount)));
108
- }
109
198
  ixs.push((0, instructions_js_1.createStreamInstruction)({
110
- start: (0, bignumber_js_1.default)(start),
199
+ start: new bn_js_1.default(start),
111
200
  depositedAmount,
112
- period: (0, bignumber_js_1.default)(period),
201
+ period: new bn_js_1.default(period),
113
202
  amountPerPeriod,
114
- cliff: (0, bignumber_js_1.default)(cliff),
115
- cliffAmount: (0, bignumber_js_1.default)(cliffAmount),
203
+ cliff: new bn_js_1.default(cliff),
204
+ cliffAmount: new bn_js_1.default(cliffAmount),
116
205
  cancelableBySender,
117
206
  cancelableByRecipient,
118
207
  automaticWithdrawal,
@@ -122,7 +211,7 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
122
211
  canUpdateRate: !!canUpdateRate,
123
212
  canPause: !!canPause,
124
213
  name,
125
- withdrawFrequency: (0, bignumber_js_1.default)(automaticWithdrawal ? withdrawalFrequency : period),
214
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
126
215
  }, this.programId, {
127
216
  sender: sender.publicKey,
128
217
  senderTokens,
@@ -192,17 +281,17 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
192
281
  computeLimit,
193
282
  });
194
283
  if (isNative) {
195
- const totalFee = await this.getTotalFee({ address: partner ?? sender.publicKey.toBase58() });
284
+ const totalFee = await this.getTotalFee({ address: partnerPublicKey.toString() });
196
285
  const totalAmount = (0, utils_js_2.calculateTotalAmountToDeposit)(depositedAmount, totalFee);
197
286
  ixs.push(...(await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalAmount)));
198
287
  }
199
288
  const createInstruction = (0, instructions_js_1.createUncheckedStreamInstruction)({
200
- start: (0, bignumber_js_1.default)(start),
289
+ start: new bn_js_1.default(start),
201
290
  depositedAmount,
202
- period: (0, bignumber_js_1.default)(period),
291
+ period: new bn_js_1.default(period),
203
292
  amountPerPeriod,
204
- cliff: (0, bignumber_js_1.default)(cliff),
205
- cliffAmount: (0, bignumber_js_1.default)(cliffAmount),
293
+ cliff: new bn_js_1.default(cliff),
294
+ cliffAmount: new bn_js_1.default(cliffAmount),
206
295
  cancelableBySender,
207
296
  cancelableByRecipient,
208
297
  automaticWithdrawal,
@@ -212,7 +301,7 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
212
301
  canUpdateRate: !!canUpdateRate,
213
302
  canPause: !!canPause,
214
303
  name,
215
- withdrawFrequency: (0, bignumber_js_1.default)(automaticWithdrawal ? withdrawalFrequency : period),
304
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
216
305
  recipient: recipientPublicKey,
217
306
  partner: partnerPublicKey,
218
307
  }, this.programId, {
@@ -235,26 +324,34 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
235
324
  * Creates multiple stream/vesting contracts.
236
325
  * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
237
326
  */
238
- async createMultiple(data, { sender, metadataPubKeys, isNative = false, computePrice, computeLimit }) {
239
- const { recipients } = data;
240
- if (!sender.publicKey) {
241
- throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
242
- }
327
+ async createMultiple(data, extParams) {
328
+ const { recipients, ...streamParams } = data;
329
+ const { sender, metadataPubKeys: metadataPubKeysExt, isNative, computePrice, computeLimit } = extParams;
243
330
  const metadatas = [];
244
331
  const metadataToRecipient = {};
245
332
  const errors = [];
246
333
  const signatures = [];
247
334
  const batch = [];
248
335
  const instructionsBatch = [];
249
- metadataPubKeys = metadataPubKeys || [];
336
+ const metadataPubKeys = metadataPubKeysExt || [];
337
+ const partnerPublicKey = data.partner ? new web3_js_1.PublicKey(data.partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
338
+ const mintPublicKey = new web3_js_1.PublicKey(data.tokenId);
339
+ if (recipients.length === 0) {
340
+ throw new Error("Recipients array is empty!");
341
+ }
342
+ if (!sender.publicKey) {
343
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
344
+ }
250
345
  for (let i = 0; i < recipients.length; i++) {
251
346
  const recipientData = recipients[i];
252
- const { ixs, metadata, metadataPubKey } = await this.prepareStreamInstructions(recipientData, data, {
347
+ const createStreamData = { ...streamParams, ...recipientData };
348
+ const createStreamExtParams = {
253
349
  sender,
254
350
  metadataPubKeys: metadataPubKeys[i] ? [metadataPubKeys[i]] : undefined,
255
351
  computePrice,
256
352
  computeLimit,
257
- });
353
+ };
354
+ const { ixs, metadata, metadataPubKey } = await this.prepareCreateInstructions(createStreamData, createStreamExtParams);
258
355
  metadataToRecipient[metadataPubKey.toBase58()] = recipientData;
259
356
  metadatas.push(metadataPubKey.toBase58());
260
357
  instructionsBatch.push({
@@ -276,13 +373,17 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
276
373
  }
277
374
  batch.push({ tx, recipient });
278
375
  }
376
+ const prepareInstructions = await this.getCreateATAInstructions([constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, partnerPublicKey], mintPublicKey, sender, true);
279
377
  if (isNative) {
280
- const totalDepositedAmount = recipients.reduce((acc, recipient) => recipient.amount.plus(acc), (0, bignumber_js_1.default)(0));
378
+ const totalDepositedAmount = recipients.reduce((acc, recipient) => recipient.amount.add(acc), new bn_js_1.default(0));
281
379
  const nativeInstructions = await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalDepositedAmount);
380
+ prepareInstructions.push(...nativeInstructions);
381
+ }
382
+ if (prepareInstructions.length > 0) {
282
383
  const messageV0 = new web3_js_1.TransactionMessage({
283
384
  payerKey: sender.publicKey,
284
385
  recentBlockhash: hash.blockhash,
285
- instructions: nativeInstructions,
386
+ instructions: prepareInstructions,
286
387
  }).compileToV0Message();
287
388
  const tx = new web3_js_1.VersionedTransaction(messageV0);
288
389
  batch.push({
@@ -291,7 +392,7 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
291
392
  });
292
393
  }
293
394
  const signedBatch = await (0, utils_js_1.signAllTransactionWithRecipients)(sender, batch);
294
- if (isNative) {
395
+ if (prepareInstructions.length > 0) {
295
396
  const prepareTx = signedBatch.pop();
296
397
  await (0, utils_js_1.sendAndConfirmStreamRawTransaction)(this.connection, prepareTx, { hash, context }, { sendThrottler: this.sendThrottler });
297
398
  }
@@ -325,6 +426,102 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
325
426
  });
326
427
  return { txs: signatures, metadatas, metadataToRecipient, errors };
327
428
  }
429
+ /**
430
+ * Creates multiple stream/vesting contracts, and send all transactions sequentially.
431
+ * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
432
+ */
433
+ async createMultipleSequential(data, extParams) {
434
+ const { recipients, ...streamParams } = data;
435
+ const { sender, metadataPubKeys: metadataPubKeysExt, isNative, computePrice, computeLimit } = extParams;
436
+ const metadatas = [];
437
+ const metadataToRecipient = {};
438
+ const errors = [];
439
+ const signatures = [];
440
+ const batch = [];
441
+ const instructionsBatch = [];
442
+ const metadataPubKeys = metadataPubKeysExt || [];
443
+ const partnerPublicKey = data.partner ? new web3_js_1.PublicKey(data.partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
444
+ const mintPublicKey = new web3_js_1.PublicKey(data.tokenId);
445
+ if (recipients.length === 0) {
446
+ throw new Error("Recipients array is empty!");
447
+ }
448
+ if (!sender.publicKey) {
449
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
450
+ }
451
+ for (let i = 0; i < recipients.length; i++) {
452
+ const recipientData = recipients[i];
453
+ const createStreamData = { ...streamParams, ...recipientData };
454
+ const createStreamExtParams = {
455
+ sender,
456
+ metadataPubKeys: metadataPubKeys[i] ? [metadataPubKeys[i]] : undefined,
457
+ computePrice,
458
+ computeLimit,
459
+ };
460
+ const { ixs, metadata, metadataPubKey } = await this.prepareCreateInstructions(createStreamData, createStreamExtParams);
461
+ metadataToRecipient[metadataPubKey.toBase58()] = recipientData;
462
+ metadatas.push(metadataPubKey.toBase58());
463
+ instructionsBatch.push({
464
+ ixs,
465
+ metadata,
466
+ recipient: recipientData.recipient,
467
+ });
468
+ }
469
+ const { value: hash, context } = await this.connection.getLatestBlockhashAndContext();
470
+ for (const { ixs, metadata, recipient } of instructionsBatch) {
471
+ const messageV0 = new web3_js_1.TransactionMessage({
472
+ payerKey: sender.publicKey,
473
+ recentBlockhash: hash.blockhash,
474
+ instructions: ixs,
475
+ }).compileToV0Message();
476
+ const tx = new web3_js_1.VersionedTransaction(messageV0);
477
+ if (metadata) {
478
+ tx.sign([metadata]);
479
+ }
480
+ batch.push({ tx, recipient });
481
+ }
482
+ const prepareInstructions = await this.getCreateATAInstructions([constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, partnerPublicKey], mintPublicKey, sender, true);
483
+ if (isNative) {
484
+ const totalDepositedAmount = recipients.reduce((acc, recipient) => recipient.amount.add(acc), new bn_js_1.default(0));
485
+ const nativeInstructions = await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalDepositedAmount);
486
+ prepareInstructions.push(...nativeInstructions);
487
+ }
488
+ if (prepareInstructions.length > 0) {
489
+ const messageV0 = new web3_js_1.TransactionMessage({
490
+ payerKey: sender.publicKey,
491
+ recentBlockhash: hash.blockhash,
492
+ instructions: prepareInstructions,
493
+ }).compileToV0Message();
494
+ const tx = new web3_js_1.VersionedTransaction(messageV0);
495
+ batch.push({
496
+ tx,
497
+ recipient: sender.publicKey.toBase58(),
498
+ });
499
+ }
500
+ const signedBatch = await (0, utils_js_1.signAllTransactionWithRecipients)(sender, batch);
501
+ if (prepareInstructions.length > 0) {
502
+ const prepareTx = signedBatch.pop();
503
+ await (0, utils_js_1.sendAndConfirmStreamRawTransaction)(this.connection, prepareTx, { hash, context }, { sendThrottler: this.sendThrottler });
504
+ }
505
+ const responses = [];
506
+ for (const batchTx of signedBatch) {
507
+ responses.push(...(await Promise.allSettled([
508
+ (0, solana_1.executeTransaction)(this.connection, batchTx.tx, { hash, context }, { sendThrottler: this.sendThrottler }),
509
+ ])));
510
+ }
511
+ responses.forEach((item, index) => {
512
+ if (item.status === "fulfilled") {
513
+ signatures.push(item.value);
514
+ }
515
+ else {
516
+ errors.push({
517
+ recipient: signedBatch[index].recipient,
518
+ error: item.reason,
519
+ contractErrorCode: this.extractErrorCode(item.reason) || undefined,
520
+ });
521
+ }
522
+ });
523
+ return { txs: signatures, metadatas, metadataToRecipient, errors };
524
+ }
328
525
  /**
329
526
  * Attempts withdrawing from the specified stream.
330
527
  */
@@ -355,21 +552,22 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
355
552
  throw new Error("Couldn't get account info");
356
553
  }
357
554
  const data = (0, utils_js_1.decodeStream)(escrow.data);
358
- const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, data.mint);
359
- const streamflowTreasuryTokens = await (0, solana_1.ata)(data.mint, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
360
- const partnerTokens = await (0, solana_1.ata)(data.mint, data.partner, tokenProgramId);
361
- await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, tokenProgramId);
362
- ixs.push((0, instructions_js_1.withdrawStreamInstruction)(amount, this.programId, {
555
+ const { sender, recipient, mint, streamflowTreasury, partner, recipientTokens, escrowTokens } = data;
556
+ const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
557
+ const streamflowTreasuryTokens = await (0, solana_1.ata)(mint, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
558
+ const partnerTokens = await (0, solana_1.ata)(mint, partner, tokenProgramId);
559
+ const ataIx = await this.getCreateATAInstructions([sender, recipient, streamflowTreasury, partner], mint, invoker, checkTokenAccounts, tokenProgramId);
560
+ ixs.push(...ataIx, (0, instructions_js_1.withdrawStreamInstruction)(amount, this.programId, {
561
+ partner,
562
+ partnerTokens,
563
+ mint,
564
+ streamflowTreasuryTokens,
565
+ recipientTokens,
566
+ escrowTokens,
363
567
  authority: invoker.publicKey,
364
568
  recipient: invoker.publicKey,
365
- recipientTokens: data.recipientTokens,
366
569
  metadata: streamPublicKey,
367
- escrowTokens: data.escrowTokens,
368
570
  streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
369
- streamflowTreasuryTokens,
370
- partner: data.partner,
371
- partnerTokens,
372
- mint: data.mint,
373
571
  tokenProgram: tokenProgramId,
374
572
  }));
375
573
  return ixs;
@@ -377,8 +575,8 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
377
575
  /**
378
576
  * Attempts canceling the specified stream.
379
577
  */
380
- async cancel({ id }, extParams) {
381
- const ixs = await this.prepareCancelInstructions({ id }, extParams);
578
+ async cancel(cancelData, extParams) {
579
+ const ixs = await this.prepareCancelInstructions(cancelData, extParams);
382
580
  const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, extParams.invoker.publicKey);
383
581
  const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.invoker, tx, {
384
582
  hash,
@@ -387,10 +585,57 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
387
585
  }, { sendThrottler: this.sendThrottler });
388
586
  return { ixs, txId: signature };
389
587
  }
588
+ async prepareCancelInstructions(cancelData, extParams) {
589
+ const streamPublicKey = new web3_js_1.PublicKey(cancelData.id);
590
+ const account = await this.connection.getAccountInfo(streamPublicKey);
591
+ if (!account) {
592
+ throw new Error("Impossible to cancel a stream contract that does not exist");
593
+ }
594
+ const { sender: senderPublicKey } = (0, utils_js_1.decodeStream)(account.data);
595
+ const isAlignedUnlock = this.isAlignedUnlock(streamPublicKey, senderPublicKey);
596
+ const ixs = isAlignedUnlock
597
+ ? await this.prepareCancelAlignedUnlockInstructions(cancelData, extParams)
598
+ : await this.prepareCancelStreamInstructions(cancelData, extParams);
599
+ return ixs;
600
+ }
601
+ async prepareCancelAlignedUnlockInstructions({ id }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
602
+ if (!invoker.publicKey) {
603
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
604
+ }
605
+ const streamPublicKey = new web3_js_1.PublicKey(id);
606
+ const escrowAcc = await this.connection.getAccountInfo(streamPublicKey);
607
+ if (!escrowAcc?.data) {
608
+ throw new Error("Couldn't get account info");
609
+ }
610
+ const streamData = (0, utils_js_1.decodeStream)(escrowAcc.data);
611
+ const { sender, recipient, mint, streamflowTreasury, partner, escrowTokens } = streamData;
612
+ const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
613
+ const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
614
+ computePrice,
615
+ computeLimit: computeLimit ?? constants_js_1.ALIGNED_COMPUTE_LIMIT,
616
+ });
617
+ const ataIx = await this.getCreateATAInstructions([sender, recipient, partner, streamflowTreasury], mint, invoker, checkTokenAccounts, tokenProgramId);
618
+ const cancelIx = await this.alignedProxyProgram.methods
619
+ .cancel()
620
+ .accountsPartial({
621
+ mint,
622
+ partner,
623
+ recipient,
624
+ escrowTokens,
625
+ sender: invoker.publicKey,
626
+ streamMetadata: streamPublicKey,
627
+ streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
628
+ tokenProgram: tokenProgramId,
629
+ streamflowProgram: this.programId,
630
+ })
631
+ .instruction();
632
+ ixs.push(...ataIx, cancelIx);
633
+ return ixs;
634
+ }
390
635
  /**
391
636
  * Creates Transaction Instructions for cancel
392
637
  */
393
- async prepareCancelInstructions({ id }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
638
+ async prepareCancelStreamInstructions({ id }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
394
639
  if (!invoker.publicKey) {
395
640
  throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
396
641
  }
@@ -399,28 +644,29 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
399
644
  if (!escrowAcc?.data) {
400
645
  throw new Error("Couldn't get account info");
401
646
  }
402
- const data = (0, utils_js_1.decodeStream)(escrowAcc?.data);
403
- const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, data.mint);
404
- const streamflowTreasuryTokens = await (0, solana_1.ata)(data.mint, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
405
- const partnerTokens = await (0, solana_1.ata)(data.mint, data.partner, tokenProgramId);
647
+ const data = (0, utils_js_1.decodeStream)(escrowAcc.data);
648
+ const { sender, recipient, partner, streamflowTreasury, mint, senderTokens, recipientTokens, escrowTokens } = data;
649
+ const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
650
+ const streamflowTreasuryTokens = await (0, solana_1.ata)(mint, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
651
+ const partnerTokens = await (0, solana_1.ata)(mint, partner, tokenProgramId);
406
652
  const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
407
653
  computePrice,
408
654
  computeLimit,
409
655
  });
410
- await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, tokenProgramId);
411
- ixs.push((0, instructions_js_1.cancelStreamInstruction)(this.programId, {
656
+ const ixsAta = await this.getCreateATAInstructions([sender, recipient, partner, streamflowTreasury], mint, invoker, checkTokenAccounts, tokenProgramId);
657
+ ixs.push(...ixsAta, (0, instructions_js_1.cancelStreamInstruction)(this.programId, {
658
+ sender,
659
+ senderTokens,
660
+ recipient,
661
+ recipientTokens,
662
+ streamflowTreasuryTokens,
663
+ partner,
664
+ partnerTokens,
665
+ mint,
666
+ escrowTokens,
412
667
  authority: invoker.publicKey,
413
- sender: data.sender,
414
- senderTokens: data.senderTokens,
415
- recipient: data.recipient,
416
- recipientTokens: data.recipientTokens,
417
668
  metadata: streamPublicKey,
418
- escrowTokens: data.escrowTokens,
419
669
  streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
420
- streamflowTreasuryTokens: streamflowTreasuryTokens,
421
- partner: data.partner,
422
- partnerTokens,
423
- mint: data.mint,
424
670
  tokenProgram: tokenProgramId,
425
671
  }));
426
672
  return ixs;
@@ -529,11 +775,56 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
529
775
  * Fetch stream data by its id (address).
530
776
  */
531
777
  async getOne({ id }) {
532
- const escrow = await this.connection.getAccountInfo(new web3_js_1.PublicKey(id), constants_js_1.TX_FINALITY_CONFIRMED);
778
+ const streamPublicKey = new web3_js_1.PublicKey(id);
779
+ const escrow = await this.connection.getAccountInfo(streamPublicKey, constants_js_1.TX_FINALITY_CONFIRMED);
533
780
  if (!escrow?.data) {
534
781
  throw new Error("Couldn't get account info.");
535
782
  }
536
- return new types_js_1.Contract((0, utils_js_1.decodeStream)(escrow?.data));
783
+ const stream = (0, utils_js_1.decodeStream)(escrow.data);
784
+ if (this.isAlignedUnlock(streamPublicKey, stream.sender)) {
785
+ const alignedProxy = await this.alignedProxyProgram.account.contract.fetch((0, derive_accounts_js_1.deriveContractPDA)(this.alignedProxyProgram.programId, streamPublicKey));
786
+ if (!alignedProxy) {
787
+ throw new Error("Couldn't get proxy account info.");
788
+ }
789
+ return new types_js_1.AlignedContract(stream, alignedProxy);
790
+ }
791
+ return new types_js_1.Contract(stream);
792
+ }
793
+ /**
794
+ * Fetch all aligned outgoing streams/contracts by the provided public key.
795
+ */
796
+ async getOutgoingAlignedStreams(sender) {
797
+ const streams = {};
798
+ const alignedOutgoingProgramAccounts = await this.alignedProxyProgram.account.contract.all([
799
+ {
800
+ memcmp: {
801
+ offset: constants_js_1.ORIGINAL_CONTRACT_SENDER_OFFSET,
802
+ bytes: sender,
803
+ },
804
+ },
805
+ ]);
806
+ const streamPubKeys = alignedOutgoingProgramAccounts.map((account) => account.account.stream);
807
+ const streamAccounts = await this.connection.getMultipleAccountsInfo(streamPubKeys, constants_js_1.TX_FINALITY_CONFIRMED);
808
+ streamAccounts.forEach((account, index) => {
809
+ if (account) {
810
+ const alignedData = alignedOutgoingProgramAccounts[index].account;
811
+ streams[streamPubKeys[index].toBase58()] = new types_js_1.AlignedContract((0, utils_js_1.decodeStream)(account.data), alignedData);
812
+ }
813
+ });
814
+ return streams;
815
+ }
816
+ async getIncomingAlignedStreams(streamRecord) {
817
+ const streams = {};
818
+ const alignedStreamsPubKeys = Object.keys(streamRecord);
819
+ const alignedProxyPDAs = alignedStreamsPubKeys.map((streamPubKey) => (0, derive_accounts_js_1.deriveContractPDA)(this.alignedProxyProgram.programId, new web3_js_1.PublicKey(streamPubKey)));
820
+ const alignedProxyAccounts = await this.alignedProxyProgram.account.contract.fetchMultiple(alignedProxyPDAs);
821
+ alignedProxyAccounts.forEach((account, index) => {
822
+ if (account) {
823
+ const alignedData = streamRecord[alignedStreamsPubKeys[index]];
824
+ streams[alignedStreamsPubKeys[index]] = new types_js_1.AlignedContract(alignedData, account);
825
+ }
826
+ });
827
+ return streams;
537
828
  }
538
829
  /**
539
830
  * Fetch streams/contracts by providing direction.
@@ -541,24 +832,39 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
541
832
  */
542
833
  async get({ address, type = types_js_2.StreamType.All, direction = types_js_2.StreamDirection.All, }) {
543
834
  const publicKey = new web3_js_1.PublicKey(address);
544
- let accounts = [];
545
- //todo: we need to be smart with our layout so we minimize rpc calls to the chain
546
- if (direction === "all") {
547
- const outgoingAccounts = await (0, solana_1.getProgramAccounts)(this.connection, publicKey, constants_js_1.STREAM_STRUCT_OFFSET_SENDER, this.programId);
548
- const incomingAccounts = await (0, solana_1.getProgramAccounts)(this.connection, publicKey, constants_js_1.STREAM_STRUCT_OFFSET_RECIPIENT, this.programId);
549
- accounts = [...outgoingAccounts, ...incomingAccounts];
835
+ let streams = {};
836
+ // don't do unnecessary rpc calls if we are not querying for vesting streams
837
+ const shouldFetchAligned = type === types_js_2.StreamType.All || type === types_js_2.StreamType.Vesting;
838
+ if (direction !== types_js_2.StreamDirection.Incoming) {
839
+ const outgoingStreamAccounts = await (0, solana_1.getProgramAccounts)(this.connection, publicKey, constants_js_1.STREAM_STRUCT_OFFSET_SENDER, this.programId);
840
+ outgoingStreamAccounts.forEach((account) => {
841
+ streams[account.pubkey.toBase58()] = new types_js_1.Contract((0, utils_js_1.decodeStream)(account.account.data));
842
+ });
843
+ if (shouldFetchAligned) {
844
+ const alignedStreams = await this.getOutgoingAlignedStreams(address);
845
+ streams = { ...streams, ...alignedStreams };
846
+ }
550
847
  }
551
- else {
552
- const offset = direction === "outgoing" ? constants_js_1.STREAM_STRUCT_OFFSET_SENDER : constants_js_1.STREAM_STRUCT_OFFSET_RECIPIENT;
553
- accounts = await (0, solana_1.getProgramAccounts)(this.connection, publicKey, offset, this.programId);
848
+ if (direction !== types_js_2.StreamDirection.Outgoing) {
849
+ const allIncomingAccounts = await (0, solana_1.getProgramAccounts)(this.connection, publicKey, constants_js_1.STREAM_STRUCT_OFFSET_RECIPIENT, this.programId);
850
+ const allIncomingStreams = allIncomingAccounts.map((account) => (0, utils_js_1.decodeStream)(account.account.data));
851
+ const alignedDecoded = {};
852
+ // filter out aligned unlocks and store them in a separate object
853
+ allIncomingAccounts.forEach((account, index) => {
854
+ if (this.isAlignedUnlock(account.pubkey, allIncomingStreams[index].sender)) {
855
+ alignedDecoded[account.pubkey.toBase58()] = allIncomingStreams[index];
856
+ }
857
+ else {
858
+ streams[account.pubkey.toBase58()] = new types_js_1.Contract(allIncomingStreams[index]);
859
+ }
860
+ });
861
+ if (shouldFetchAligned) {
862
+ const incomingAlignedStreams = await this.getIncomingAlignedStreams(alignedDecoded);
863
+ streams = { ...streams, ...incomingAlignedStreams };
864
+ }
554
865
  }
555
- let streams = {};
556
- accounts.forEach((account) => {
557
- const decoded = new types_js_1.Contract((0, utils_js_1.decodeStream)(account.account.data));
558
- streams = { ...streams, [account.pubkey.toBase58()]: decoded };
559
- });
560
866
  const sortedStreams = Object.entries(streams).sort(([, stream1], [, stream2]) => stream2.start - stream1.start);
561
- if (type === "all")
867
+ if (type === types_js_2.StreamType.All)
562
868
  return sortedStreams;
563
869
  return sortedStreams.filter((stream) => stream[1].type === type);
564
870
  }
@@ -641,68 +947,6 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
641
947
  const logs = "logs" in err && Array.isArray(err.logs) ? err.logs : undefined;
642
948
  return (0, utils_js_1.extractSolanaErrorCode)(err.toString() ?? "Unknown error!", logs);
643
949
  }
644
- /**
645
- * Forms instructions from params, creates a raw transaction and fetch recent blockhash.
646
- */
647
- async prepareStreamInstructions(recipient, streamParams, extParams) {
648
- const { tokenId: mint, start, period, cliff, canTopup, canUpdateRate, canPause, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal = false, withdrawalFrequency = 0, partner, } = streamParams;
649
- const { sender, metadataPubKeys, computeLimit, computePrice } = extParams;
650
- if (!sender.publicKey) {
651
- throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
652
- }
653
- const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
654
- computePrice,
655
- computeLimit,
656
- });
657
- const recipientPublicKey = new web3_js_1.PublicKey(recipient.recipient);
658
- const mintPublicKey = new web3_js_1.PublicKey(mint);
659
- const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
660
- const [escrowTokens] = web3_js_1.PublicKey.findProgramAddressSync([buffer_1.Buffer.from("strm"), metadataPubKey.toBuffer()], this.programId);
661
- const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mintPublicKey);
662
- const senderTokens = await (0, solana_1.ata)(mintPublicKey, sender.publicKey, tokenProgramId);
663
- const recipientTokens = await (0, solana_1.ata)(mintPublicKey, recipientPublicKey, tokenProgramId);
664
- const streamflowTreasuryTokens = await (0, solana_1.ata)(mintPublicKey, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
665
- const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
666
- const partnerTokens = await (0, solana_1.ata)(mintPublicKey, partnerPublicKey, tokenProgramId);
667
- ixs.push((0, instructions_js_1.createStreamInstruction)({
668
- start: (0, bignumber_js_1.default)(start),
669
- depositedAmount: recipient.amount,
670
- period: (0, bignumber_js_1.default)(period),
671
- amountPerPeriod: recipient.amountPerPeriod,
672
- cliff: (0, bignumber_js_1.default)(cliff),
673
- cliffAmount: (0, bignumber_js_1.default)(recipient.cliffAmount),
674
- cancelableBySender,
675
- cancelableByRecipient,
676
- automaticWithdrawal,
677
- transferableBySender,
678
- transferableByRecipient,
679
- canTopup,
680
- canUpdateRate: !!canUpdateRate,
681
- canPause: !!canPause,
682
- name: recipient.name,
683
- withdrawFrequency: (0, bignumber_js_1.default)(automaticWithdrawal ? withdrawalFrequency : period),
684
- }, this.programId, {
685
- sender: sender.publicKey,
686
- senderTokens,
687
- recipient: new web3_js_1.PublicKey(recipient.recipient),
688
- metadata: metadataPubKey,
689
- escrowTokens,
690
- recipientTokens,
691
- streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
692
- streamflowTreasuryTokens: streamflowTreasuryTokens,
693
- partner: partnerPublicKey,
694
- partnerTokens: partnerTokens,
695
- mint: new web3_js_1.PublicKey(mint),
696
- feeOracle: constants_js_1.FEE_ORACLE_PUBLIC_KEY,
697
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
698
- timelockProgram: this.programId,
699
- tokenProgram: tokenProgramId,
700
- associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
701
- withdrawor: constants_js_1.WITHDRAWOR_PUBLIC_KEY,
702
- systemProgram: web3_js_1.SystemProgram.programId,
703
- }));
704
- return { ixs, metadata, metadataPubKey };
705
- }
706
950
  /**
707
951
  * Utility function to generate metadata for a Contract or return existing Pubkey
708
952
  */
@@ -719,19 +963,22 @@ class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
719
963
  return { metadata, metadataPubKey };
720
964
  }
721
965
  /**
722
- * Utility function that checks whether associated token accounts still exist and adds instructions to add them if not
966
+ * Utility function that checks whether the associated stream address is an aligned unlock contract, indicated by whether the sender/creator is a PDA
723
967
  */
724
- async checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, programId) {
968
+ isAlignedUnlock(streamPublicKey, senderPublicKey) {
969
+ const pda = (0, derive_accounts_js_1.deriveContractPDA)(this.alignedProxyProgram.programId, streamPublicKey);
970
+ return senderPublicKey.equals(pda);
971
+ }
972
+ /**
973
+ * Returns insrtuctions for creating associated token accounts for the provided owners
974
+ */
975
+ async getCreateATAInstructions(owners, mint, invoker, checkTokenAccounts, programId) {
725
976
  if (!checkTokenAccounts) {
726
- return;
727
- }
728
- const owners = Array.from(new Set([
729
- data.sender.toBase58(),
730
- data.recipient.toBase58(),
731
- data.partner.toBase58(),
732
- data.streamflowTreasury.toBase58(),
733
- ]), (address) => new web3_js_1.PublicKey(address));
734
- ixs.push(...(await (0, solana_1.checkOrCreateAtaBatch)(this.connection, owners, data.mint, invoker, programId)));
977
+ return [];
978
+ }
979
+ // filter out duplicate PublicKeys, otherwise transaction will fail
980
+ const uniqueOwners = Array.from(new Set(owners.map((owner) => owner.toBase58()))).map((pkString) => new web3_js_1.PublicKey(pkString));
981
+ return (0, solana_1.checkOrCreateAtaBatch)(this.connection, uniqueOwners, mint, invoker, programId);
735
982
  }
736
983
  }
737
984
  exports.SolanaStreamClient = SolanaStreamClient;