@streamflow/stream 3.0.16 → 4.0.0-Alpha.0

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 (45) hide show
  1. package/dist/StreamClient.d.ts +3 -4
  2. package/dist/StreamClient.js +35 -21
  3. package/dist/__tests__/aptos/streamClient.spec.d.ts +1 -0
  4. package/dist/__tests__/aptos/streamClient.spec.js +160 -0
  5. package/dist/__tests__/test_dependencies/airdrop/airdrop-request.d.ts +0 -1
  6. package/dist/aptos/StreamClient.d.ts +19 -0
  7. package/dist/aptos/StreamClient.js +296 -0
  8. package/dist/aptos/constants.d.ts +2 -0
  9. package/dist/aptos/constants.js +12 -0
  10. package/dist/aptos/index.d.ts +2 -0
  11. package/dist/aptos/index.js +23 -0
  12. package/dist/aptos/types.d.ts +10 -0
  13. package/dist/aptos/types.js +2 -0
  14. package/dist/common/types.d.ts +70 -0
  15. package/dist/common/types.js +29 -0
  16. package/dist/common/utils.d.ts +15 -0
  17. package/dist/common/utils.js +31 -0
  18. package/dist/index.d.ts +4 -6
  19. package/dist/index.js +17 -13
  20. package/dist/instructionTypes.d.ts +81 -0
  21. package/dist/instructionTypes.js +2 -0
  22. package/dist/instructions.d.ts +0 -2
  23. package/dist/instructions.js +18 -29
  24. package/dist/layout.d.ts +7 -7
  25. package/dist/layout.js +78 -75
  26. package/dist/solana/StreamClient.d.ts +145 -0
  27. package/dist/solana/StreamClient.js +952 -0
  28. package/dist/solana/constants.d.ts +18 -0
  29. package/dist/solana/constants.js +23 -0
  30. package/dist/solana/index.d.ts +5 -0
  31. package/dist/solana/index.js +27 -0
  32. package/dist/solana/instructionTypes.d.ts +81 -0
  33. package/dist/solana/instructionTypes.js +2 -0
  34. package/dist/solana/instructions.d.ts +131 -0
  35. package/dist/solana/instructions.js +375 -0
  36. package/dist/solana/layout.d.ts +22 -0
  37. package/dist/solana/layout.js +96 -0
  38. package/dist/solana/timelockIDL.d.ts +22 -0
  39. package/dist/solana/timelockIDL.js +422 -0
  40. package/dist/solana/types.d.ts +321 -0
  41. package/dist/solana/types.js +74 -0
  42. package/dist/solana/utils.d.ts +42 -0
  43. package/dist/solana/utils.js +269 -0
  44. package/dist/types.d.ts +12 -13
  45. package/package.json +23 -10
@@ -0,0 +1,952 @@
1
+ "use strict";
2
+ // Latest version of the SDK that does not use Anchor. It supports raw instructions.
3
+ var __extends = (this && this.__extends) || (function () {
4
+ var extendStatics = function (d, b) {
5
+ extendStatics = Object.setPrototypeOf ||
6
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
8
+ return extendStatics(d, b);
9
+ };
10
+ return function (d, b) {
11
+ if (typeof b !== "function" && b !== null)
12
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
13
+ extendStatics(d, b);
14
+ function __() { this.constructor = d; }
15
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
16
+ };
17
+ })();
18
+ var __assign = (this && this.__assign) || function () {
19
+ __assign = Object.assign || function(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
23
+ t[p] = s[p];
24
+ }
25
+ return t;
26
+ };
27
+ return __assign.apply(this, arguments);
28
+ };
29
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ };
38
+ var __generator = (this && this.__generator) || function (thisArg, body) {
39
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
40
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
41
+ function verb(n) { return function (v) { return step([n, v]); }; }
42
+ function step(op) {
43
+ if (f) throw new TypeError("Generator is already executing.");
44
+ while (_) try {
45
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
46
+ if (y = 0, t) op = [op[0] & 2, t.value];
47
+ switch (op[0]) {
48
+ case 0: case 1: t = op; break;
49
+ case 4: _.label++; return { value: op[1], done: false };
50
+ case 5: _.label++; y = op[1]; op = [0]; continue;
51
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
52
+ default:
53
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
54
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
55
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
56
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
57
+ if (t[2]) _.ops.pop();
58
+ _.trys.pop(); continue;
59
+ }
60
+ op = body.call(thisArg, _);
61
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
62
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
63
+ }
64
+ };
65
+ var __read = (this && this.__read) || function (o, n) {
66
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
67
+ if (!m) return o;
68
+ var i = m.call(o), r, ar = [], e;
69
+ try {
70
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
71
+ }
72
+ catch (error) { e = { error: error }; }
73
+ finally {
74
+ try {
75
+ if (r && !r.done && (m = i["return"])) m.call(i);
76
+ }
77
+ finally { if (e) throw e.error; }
78
+ }
79
+ return ar;
80
+ };
81
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
82
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
83
+ if (ar || !(i in from)) {
84
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
85
+ ar[i] = from[i];
86
+ }
87
+ }
88
+ return to.concat(ar || Array.prototype.slice.call(from));
89
+ };
90
+ var __values = (this && this.__values) || function(o) {
91
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
92
+ if (m) return m.call(o);
93
+ if (o && typeof o.length === "number") return {
94
+ next: function () {
95
+ if (o && i >= o.length) o = void 0;
96
+ return { value: o && o[i++], done: !o };
97
+ }
98
+ };
99
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
100
+ };
101
+ var __importDefault = (this && this.__importDefault) || function (mod) {
102
+ return (mod && mod.__esModule) ? mod : { "default": mod };
103
+ };
104
+ Object.defineProperty(exports, "__esModule", { value: true });
105
+ var bn_js_1 = __importDefault(require("bn.js"));
106
+ var buffer_1 = require("buffer");
107
+ var bs58_1 = __importDefault(require("bs58"));
108
+ var spl_token_1 = require("@solana/spl-token");
109
+ var web3_js_1 = require("@solana/web3.js");
110
+ var types_1 = require("./types");
111
+ var utils_1 = require("./utils");
112
+ var constants_1 = require("./constants");
113
+ var instructions_1 = require("./instructions");
114
+ var types_2 = require("../common/types");
115
+ var METADATA_ACC_SIZE = 1104;
116
+ var SolanaStreamClient = /** @class */ (function (_super) {
117
+ __extends(SolanaStreamClient, _super);
118
+ /**
119
+ * Create Stream instance
120
+ */
121
+ function SolanaStreamClient(clusterUrl, cluster, commitment, programId) {
122
+ if (cluster === void 0) { cluster = types_2.ICluster.Mainnet; }
123
+ if (commitment === void 0) { commitment = "confirmed"; }
124
+ if (programId === void 0) { programId = ""; }
125
+ var _this = _super.call(this) || this;
126
+ _this.commitment = commitment;
127
+ _this.connection = new web3_js_1.Connection(clusterUrl, _this.commitment);
128
+ _this.programId =
129
+ programId !== "" ? new web3_js_1.PublicKey(programId) : new web3_js_1.PublicKey(constants_1.PROGRAM_ID[cluster]);
130
+ return _this;
131
+ }
132
+ SolanaStreamClient.prototype.getConnection = function () {
133
+ return this.connection;
134
+ };
135
+ SolanaStreamClient.prototype.getProgramId = function () {
136
+ return this.programId.toBase58();
137
+ };
138
+ /**
139
+ * Creates a new stream/vesting contract.
140
+ * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
141
+ * @param {CreateParams} data
142
+ * @param {WalletAdapter | Keypair} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
143
+ * @param {string} data.recipient - Solana address of the recipient. Associated token account will be derived using this address and token mint address.
144
+ * @param {string} data.mint - SPL Token mint.
145
+ * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
146
+ * @param {BN} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
147
+ * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
148
+ * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
149
+ * @param {BN} data.cliffAmount - Amount unlocked at the "cliff".
150
+ * @param {BN} data.amountPerPeriod - Amount unlocked per each period.
151
+ * @param {string} data.name - Stream name/subject.
152
+ * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
153
+ * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
154
+ * @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
155
+ * @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
156
+ * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
157
+ * @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
158
+ * @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.
159
+ * @param {boolean} [data.isNative = false] - When true Automatically wraps SOLs to WrappedSOL and creates wSOL stream
160
+ * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
161
+ */
162
+ SolanaStreamClient.prototype.create = function (_a, _b) {
163
+ var recipient = _a.recipient, mint = _a.tokenId, start = _a.start, depositedAmount = _a.amount, 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, _c = _a.automaticWithdrawal, automaticWithdrawal = _c === void 0 ? false : _c, _d = _a.withdrawalFrequency, withdrawalFrequency = _d === void 0 ? 0 : _d;
164
+ var sender = _b.sender, _e = _b.partner, partner = _e === void 0 ? null : _e, _f = _b.isNative, isNative = _f === void 0 ? false : _f;
165
+ return __awaiter(this, void 0, void 0, function () {
166
+ var ixs, mintPublicKey, recipientPublicKey, metadata, _g, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, nativeInstructions, _h, commitment, hash, tx, signature;
167
+ var _j;
168
+ return __generator(this, function (_k) {
169
+ switch (_k.label) {
170
+ case 0:
171
+ if (!sender.publicKey) {
172
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
173
+ }
174
+ ixs = [];
175
+ mintPublicKey = isNative ? new web3_js_1.PublicKey(mint) : spl_token_1.NATIVE_MINT;
176
+ recipientPublicKey = new web3_js_1.PublicKey(recipient);
177
+ metadata = web3_js_1.Keypair.generate();
178
+ return [4 /*yield*/, web3_js_1.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], this.programId)];
179
+ case 1:
180
+ _g = __read.apply(void 0, [_k.sent(), 1]), escrowTokens = _g[0];
181
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, sender.publicKey)];
182
+ case 2:
183
+ senderTokens = _k.sent();
184
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, recipientPublicKey)];
185
+ case 3:
186
+ recipientTokens = _k.sent();
187
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
188
+ case 4:
189
+ streamflowTreasuryTokens = _k.sent();
190
+ partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY;
191
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, partnerPublicKey)];
192
+ case 5:
193
+ partnerTokens = _k.sent();
194
+ if (!isNative) return [3 /*break*/, 7];
195
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, sender.publicKey, depositedAmount)];
196
+ case 6:
197
+ _h = _k.sent();
198
+ return [3 /*break*/, 8];
199
+ case 7:
200
+ _h = [];
201
+ _k.label = 8;
202
+ case 8:
203
+ nativeInstructions = _h;
204
+ ixs.push((0, instructions_1.createStreamInstruction)({
205
+ start: new bn_js_1.default(start),
206
+ depositedAmount: depositedAmount,
207
+ period: new bn_js_1.default(period),
208
+ amountPerPeriod: amountPerPeriod,
209
+ cliff: new bn_js_1.default(cliff),
210
+ cliffAmount: cliffAmount,
211
+ cancelableBySender: cancelableBySender,
212
+ cancelableByRecipient: cancelableByRecipient,
213
+ automaticWithdrawal: automaticWithdrawal,
214
+ transferableBySender: transferableBySender,
215
+ transferableByRecipient: transferableByRecipient,
216
+ canTopup: canTopup,
217
+ name: name,
218
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
219
+ }, this.programId, {
220
+ sender: sender.publicKey,
221
+ senderTokens: senderTokens,
222
+ recipient: new web3_js_1.PublicKey(recipient),
223
+ metadata: metadata.publicKey,
224
+ escrowTokens: escrowTokens,
225
+ recipientTokens: recipientTokens,
226
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
227
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
228
+ partner: partnerPublicKey,
229
+ partnerTokens: partnerTokens,
230
+ mint: new web3_js_1.PublicKey(mint),
231
+ feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
232
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
233
+ timelockProgram: this.programId,
234
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
235
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
236
+ withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
237
+ systemProgram: web3_js_1.SystemProgram.programId,
238
+ }));
239
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
240
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
241
+ case 9:
242
+ hash = _k.sent();
243
+ tx = (_j = new web3_js_1.Transaction({
244
+ feePayer: sender.publicKey,
245
+ blockhash: hash.blockhash,
246
+ lastValidBlockHeight: hash.lastValidBlockHeight,
247
+ })).add.apply(_j, __spreadArray(__spreadArray([], __read(nativeInstructions), false), __read(ixs), false));
248
+ tx.partialSign(metadata);
249
+ return [4 /*yield*/, this.sign(sender, tx, hash)];
250
+ case 10:
251
+ signature = _k.sent();
252
+ return [2 /*return*/, { ixs: ixs, tx: signature, metadata: metadata }];
253
+ }
254
+ });
255
+ });
256
+ };
257
+ /**
258
+ * Creates a new stream/vesting contract using unchecked instruction.
259
+ *
260
+ * Unchecked instruction differes from the regular in:
261
+ * - does not check for initialized associated token account (wallets with no control over their ATA should not be used
262
+ * as sender/recipient/partner or there are risks of funds being locked in the contract)
263
+ * - initialized contract PDA off chain
264
+ *
265
+ * If you are not sure if you should use create or create_unchecked, go for create to be safer.
266
+ *
267
+ * @param {CreateParams} data
268
+ * @param {Wallet | Keypair} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
269
+ * @param {string} data.recipient - Solana address of the recipient. Associated token account will be derived using this address and token mint address.
270
+ * @param {string} data.mint - SPL Token mint.
271
+ * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
272
+ * @param {BN} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
273
+ * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
274
+ * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
275
+ * @param {BN} data.cliffAmount - Amount unlocked at the "cliff".
276
+ * @param {BN} data.amountPerPeriod - Amount unlocked per each period.
277
+ * @param {string} data.name - Stream name/subject.
278
+ * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
279
+ * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
280
+ * @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
281
+ * @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
282
+ * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
283
+ * @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
284
+ * @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.
285
+ * @param {boolean} [data.isNative = false] - When true Automatically wraps SOLs to WrappedSOL and creates wSOL stream
286
+ * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
287
+ */
288
+ SolanaStreamClient.prototype.createUnchecked = function (_a, _b) {
289
+ var recipient = _a.recipient, mint = _a.tokenId, start = _a.start, depositedAmount = _a.amount, 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, _c = _a.automaticWithdrawal, automaticWithdrawal = _c === void 0 ? false : _c, _d = _a.withdrawalFrequency, withdrawalFrequency = _d === void 0 ? 0 : _d;
290
+ var sender = _b.sender, _e = _b.partner, partner = _e === void 0 ? null : _e, _f = _b.isNative, isNative = _f === void 0 ? false : _f;
291
+ return __awaiter(this, void 0, void 0, function () {
292
+ var mintPublicKey, recipientPublicKey, metadata, rentToExempt, createMetadataInstruction, _g, escrowTokens, senderTokens, partnerPublicKey, nativeInstructions, _h, createInstruction, commitment, ixs, hash, tx, signature;
293
+ var _j;
294
+ return __generator(this, function (_k) {
295
+ switch (_k.label) {
296
+ case 0:
297
+ if (!sender.publicKey) {
298
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
299
+ }
300
+ mintPublicKey = new web3_js_1.PublicKey(mint);
301
+ recipientPublicKey = new web3_js_1.PublicKey(recipient);
302
+ metadata = web3_js_1.Keypair.generate();
303
+ return [4 /*yield*/, this.connection.getMinimumBalanceForRentExemption(METADATA_ACC_SIZE)];
304
+ case 1:
305
+ rentToExempt = _k.sent();
306
+ createMetadataInstruction = web3_js_1.SystemProgram.createAccount({
307
+ programId: this.programId,
308
+ space: METADATA_ACC_SIZE,
309
+ lamports: rentToExempt,
310
+ fromPubkey: sender === null || sender === void 0 ? void 0 : sender.publicKey,
311
+ newAccountPubkey: metadata.publicKey,
312
+ });
313
+ return [4 /*yield*/, web3_js_1.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], this.programId)];
314
+ case 2:
315
+ _g = __read.apply(void 0, [_k.sent(), 1]), escrowTokens = _g[0];
316
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, sender.publicKey)];
317
+ case 3:
318
+ senderTokens = _k.sent();
319
+ partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY;
320
+ if (!isNative) return [3 /*break*/, 5];
321
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, sender.publicKey, depositedAmount)];
322
+ case 4:
323
+ _h = _k.sent();
324
+ return [3 /*break*/, 6];
325
+ case 5:
326
+ _h = [];
327
+ _k.label = 6;
328
+ case 6:
329
+ nativeInstructions = _h;
330
+ createInstruction = (0, instructions_1.createUncheckedStreamInstruction)({
331
+ start: new bn_js_1.default(start),
332
+ depositedAmount: depositedAmount,
333
+ period: new bn_js_1.default(period),
334
+ amountPerPeriod: amountPerPeriod,
335
+ cliff: new bn_js_1.default(cliff),
336
+ cliffAmount: cliffAmount,
337
+ cancelableBySender: cancelableBySender,
338
+ cancelableByRecipient: cancelableByRecipient,
339
+ automaticWithdrawal: automaticWithdrawal,
340
+ transferableBySender: transferableBySender,
341
+ transferableByRecipient: transferableByRecipient,
342
+ canTopup: canTopup,
343
+ name: name,
344
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
345
+ recipient: recipientPublicKey,
346
+ partner: partnerPublicKey,
347
+ }, this.programId, {
348
+ sender: sender.publicKey,
349
+ senderTokens: senderTokens,
350
+ metadata: metadata.publicKey,
351
+ escrowTokens: escrowTokens,
352
+ mint: new web3_js_1.PublicKey(mint),
353
+ feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
354
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
355
+ timelockProgram: this.programId,
356
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
357
+ withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
358
+ systemProgram: web3_js_1.SystemProgram.programId,
359
+ });
360
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
361
+ ixs = [createMetadataInstruction, createInstruction];
362
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
363
+ case 7:
364
+ hash = _k.sent();
365
+ tx = (_j = new web3_js_1.Transaction({
366
+ feePayer: sender.publicKey,
367
+ blockhash: hash.blockhash,
368
+ lastValidBlockHeight: hash.lastValidBlockHeight,
369
+ })).add.apply(_j, __spreadArray(__spreadArray([], __read(nativeInstructions), false), __read(ixs), false));
370
+ tx.partialSign(metadata);
371
+ return [4 /*yield*/, this.sign(sender, tx, hash)];
372
+ case 8:
373
+ signature = _k.sent();
374
+ return [2 /*return*/, { ixs: ixs, tx: signature, metadata: metadata }];
375
+ }
376
+ });
377
+ });
378
+ };
379
+ /**
380
+ * Creates a new stream/vesting contract.
381
+ * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
382
+ * @param {CreateMultiParams} data
383
+ * @param {Wallet | Keypair} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
384
+ * @param {MultiRecipient[]} data.recipientsData
385
+ * @param {string} data.mint - SPL Token mint.
386
+ * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
387
+ * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
388
+ * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
389
+ * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
390
+ * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
391
+ * @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
392
+ * @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
393
+ * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
394
+ * @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
395
+ * @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.
396
+ * @param {boolean} [data.isNative = false] - When true Automatically wraps SOLs to WrappedSOL and creates wSOL stream
397
+ * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
398
+ */
399
+ SolanaStreamClient.prototype.createMultiple = function (data, _a) {
400
+ var sender = _a.sender, _b = _a.partner, partner = _b === void 0 ? null : _b, _c = _a.isNative, isNative = _c === void 0 ? false : _c;
401
+ return __awaiter(this, void 0, void 0, function () {
402
+ var recipients, metadatas, metadataToRecipient, errors, signatures, batch, recipients_1, recipients_1_1, recipientData, _d, tx, metadata, metadataPubKey, e_1_1, totalDepositedAmount, nativeInstructions, commitment, hash, prepareTransaction, signed_batch, prepareTx, batchTransactionsCalls, responses, successes, failures;
403
+ var e_1, _e, _f;
404
+ var _this = this;
405
+ return __generator(this, function (_g) {
406
+ switch (_g.label) {
407
+ case 0:
408
+ recipients = data.recipients;
409
+ if (!sender.publicKey) {
410
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
411
+ }
412
+ metadatas = [];
413
+ metadataToRecipient = {};
414
+ errors = [];
415
+ signatures = [];
416
+ batch = [];
417
+ _g.label = 1;
418
+ case 1:
419
+ _g.trys.push([1, 6, 7, 8]);
420
+ recipients_1 = __values(recipients), recipients_1_1 = recipients_1.next();
421
+ _g.label = 2;
422
+ case 2:
423
+ if (!!recipients_1_1.done) return [3 /*break*/, 5];
424
+ recipientData = recipients_1_1.value;
425
+ return [4 /*yield*/, this.prepareStreamTransaction(recipientData, data, {
426
+ sender: sender,
427
+ partner: partner,
428
+ })];
429
+ case 3:
430
+ _d = _g.sent(), tx = _d.tx, metadata = _d.metadata;
431
+ metadataPubKey = metadata.publicKey.toBase58();
432
+ metadataToRecipient[metadataPubKey] = recipientData;
433
+ metadatas.push(metadata);
434
+ batch.push({ tx: tx, recipient: recipientData.recipient });
435
+ _g.label = 4;
436
+ case 4:
437
+ recipients_1_1 = recipients_1.next();
438
+ return [3 /*break*/, 2];
439
+ case 5: return [3 /*break*/, 8];
440
+ case 6:
441
+ e_1_1 = _g.sent();
442
+ e_1 = { error: e_1_1 };
443
+ return [3 /*break*/, 8];
444
+ case 7:
445
+ try {
446
+ if (recipients_1_1 && !recipients_1_1.done && (_e = recipients_1.return)) _e.call(recipients_1);
447
+ }
448
+ finally { if (e_1) throw e_1.error; }
449
+ return [7 /*endfinally*/];
450
+ case 8:
451
+ if (!isNative) return [3 /*break*/, 11];
452
+ totalDepositedAmount = recipients.reduce(function (acc, recipient) { return recipient.amount.add(acc); }, new bn_js_1.default(0));
453
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalDepositedAmount)];
454
+ case 9:
455
+ nativeInstructions = _g.sent();
456
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
457
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
458
+ case 10:
459
+ hash = _g.sent();
460
+ prepareTransaction = (_f = new web3_js_1.Transaction({
461
+ feePayer: sender.publicKey,
462
+ blockhash: hash.blockhash,
463
+ lastValidBlockHeight: hash.lastValidBlockHeight,
464
+ })).add.apply(_f, __spreadArray([], __read(nativeInstructions), false));
465
+ batch.push({ tx: prepareTransaction, recipient: sender.publicKey.toBase58() });
466
+ _g.label = 11;
467
+ case 11: return [4 /*yield*/, (0, utils_1.signAllTransactionWithRecipients)(sender, batch)];
468
+ case 12:
469
+ signed_batch = _g.sent();
470
+ if (!isNative) return [3 /*break*/, 14];
471
+ prepareTx = signed_batch.pop();
472
+ return [4 /*yield*/, (0, utils_1.sendAndConfirmStreamRawTransaction)(this.connection, prepareTx)];
473
+ case 13:
474
+ _g.sent();
475
+ _g.label = 14;
476
+ case 14:
477
+ batchTransactionsCalls = signed_batch.map(function (el) {
478
+ return (0, utils_1.sendAndConfirmStreamRawTransaction)(_this.connection, el);
479
+ });
480
+ return [4 /*yield*/, Promise.allSettled(batchTransactionsCalls)];
481
+ case 15:
482
+ responses = _g.sent();
483
+ successes = responses
484
+ .filter(function (el) { return el.status === "fulfilled"; })
485
+ .map(function (el) { return el.value; });
486
+ signatures.push.apply(signatures, __spreadArray([], __read(successes.map(function (el) { return el.signature; })), false));
487
+ failures = responses
488
+ .filter(function (el) { return el.status === "rejected"; })
489
+ .map(function (el) { return el.reason; });
490
+ errors.push.apply(errors, __spreadArray([], __read(failures), false));
491
+ return [2 /*return*/, { txs: signatures, metadatas: metadatas, metadataToRecipient: metadataToRecipient, errors: errors }];
492
+ }
493
+ });
494
+ });
495
+ };
496
+ /**
497
+ * Attempts withdrawal from the specified stream.
498
+ * @param {WithdrawParams} data
499
+ * @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (recipient) or transaction will fail.
500
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be withdrawn from.
501
+ * @param {BN} data.amount - Requested amount (in the smallest units) to withdraw (while streaming). If stream is completed, the whole amount will be withdrawn.
502
+ */
503
+ SolanaStreamClient.prototype.withdraw = function (_a, _b) {
504
+ var id = _a.id, amount = _a.amount;
505
+ var invoker = _b.invoker;
506
+ return __awaiter(this, void 0, void 0, function () {
507
+ var ixs, streamPublicKey, escrow, data, streamflowTreasuryTokens, partnerTokens, commitment, hash, tx, signature;
508
+ var _c;
509
+ return __generator(this, function (_d) {
510
+ switch (_d.label) {
511
+ case 0:
512
+ if (!invoker.publicKey) {
513
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
514
+ }
515
+ ixs = [];
516
+ streamPublicKey = new web3_js_1.PublicKey(id);
517
+ return [4 /*yield*/, this.connection.getAccountInfo(streamPublicKey)];
518
+ case 1:
519
+ escrow = _d.sent();
520
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
521
+ throw new Error("Couldn't get account info");
522
+ }
523
+ data = (0, utils_1.decodeStream)(escrow.data);
524
+ return [4 /*yield*/, (0, utils_1.ata)(data.mint, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
525
+ case 2:
526
+ streamflowTreasuryTokens = _d.sent();
527
+ return [4 /*yield*/, (0, utils_1.ata)(data.mint, data.partner)];
528
+ case 3:
529
+ partnerTokens = _d.sent();
530
+ ixs.push((0, instructions_1.withdrawStreamInstruction)(amount, this.programId, {
531
+ authority: invoker.publicKey,
532
+ recipient: invoker.publicKey,
533
+ recipientTokens: data.recipientTokens,
534
+ metadata: streamPublicKey,
535
+ escrowTokens: data.escrowTokens,
536
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
537
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
538
+ partner: data.partner,
539
+ partnerTokens: partnerTokens,
540
+ mint: data.mint,
541
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
542
+ }));
543
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
544
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
545
+ case 4:
546
+ hash = _d.sent();
547
+ tx = (_c = new web3_js_1.Transaction({
548
+ feePayer: invoker.publicKey,
549
+ blockhash: hash.blockhash,
550
+ lastValidBlockHeight: hash.lastValidBlockHeight,
551
+ })).add.apply(_c, __spreadArray([], __read(ixs), false));
552
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
553
+ case 5:
554
+ signature = _d.sent();
555
+ return [2 /*return*/, { ixs: ixs, tx: signature }];
556
+ }
557
+ });
558
+ });
559
+ };
560
+ /**
561
+ * Attempts canceling the specified stream.
562
+ * @param {CancelParams} data
563
+ * @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
564
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be canceled.
565
+ */
566
+ SolanaStreamClient.prototype.cancel = function (_a, _b) {
567
+ var id = _a.id;
568
+ var invoker = _b.invoker;
569
+ return __awaiter(this, void 0, void 0, function () {
570
+ var streamPublicKey, escrow_acc, data, streamflowTreasuryTokens, partnerTokens, ixs, commitment, hash, tx, signature;
571
+ var _c;
572
+ return __generator(this, function (_d) {
573
+ switch (_d.label) {
574
+ case 0:
575
+ if (!invoker.publicKey) {
576
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
577
+ }
578
+ streamPublicKey = new web3_js_1.PublicKey(id);
579
+ return [4 /*yield*/, this.connection.getAccountInfo(streamPublicKey)];
580
+ case 1:
581
+ escrow_acc = _d.sent();
582
+ if (!(escrow_acc === null || escrow_acc === void 0 ? void 0 : escrow_acc.data)) {
583
+ throw new Error("Couldn't get account info");
584
+ }
585
+ data = (0, utils_1.decodeStream)(escrow_acc === null || escrow_acc === void 0 ? void 0 : escrow_acc.data);
586
+ return [4 /*yield*/, (0, utils_1.ata)(data.mint, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
587
+ case 2:
588
+ streamflowTreasuryTokens = _d.sent();
589
+ return [4 /*yield*/, (0, utils_1.ata)(data.mint, data.partner)];
590
+ case 3:
591
+ partnerTokens = _d.sent();
592
+ ixs = [];
593
+ ixs.push((0, instructions_1.cancelStreamInstruction)(this.programId, {
594
+ authority: invoker.publicKey,
595
+ sender: data.sender,
596
+ senderTokens: data.senderTokens,
597
+ recipient: data.recipient,
598
+ recipientTokens: data.recipientTokens,
599
+ metadata: streamPublicKey,
600
+ escrowTokens: data.escrowTokens,
601
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
602
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
603
+ partner: data.partner,
604
+ partnerTokens: partnerTokens,
605
+ mint: data.mint,
606
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
607
+ }));
608
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
609
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
610
+ case 4:
611
+ hash = _d.sent();
612
+ tx = (_c = new web3_js_1.Transaction({
613
+ feePayer: invoker.publicKey,
614
+ blockhash: hash.blockhash,
615
+ lastValidBlockHeight: hash.lastValidBlockHeight,
616
+ })).add.apply(_c, __spreadArray([], __read(ixs), false));
617
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
618
+ case 5:
619
+ signature = _d.sent();
620
+ return [2 /*return*/, { ixs: ixs, tx: signature }];
621
+ }
622
+ });
623
+ });
624
+ };
625
+ /**
626
+ * Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
627
+ * Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
628
+ * @param {TransferParams} data
629
+ * @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
630
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be transferred.
631
+ * @param {string} data.recipientId - Address of a new recipient.
632
+ */
633
+ SolanaStreamClient.prototype.transfer = function (_a, _b) {
634
+ var id = _a.id, newRecipientString = _a.newRecipient;
635
+ var invoker = _b.invoker;
636
+ return __awaiter(this, void 0, void 0, function () {
637
+ var ixs, stream, newRecipient, escrow, mint, newRecipientTokens, commitment, hash, tx, signature;
638
+ var _c;
639
+ return __generator(this, function (_d) {
640
+ switch (_d.label) {
641
+ case 0:
642
+ if (!invoker.publicKey) {
643
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
644
+ }
645
+ ixs = [];
646
+ stream = new web3_js_1.PublicKey(id);
647
+ newRecipient = new web3_js_1.PublicKey(newRecipientString);
648
+ return [4 /*yield*/, this.connection.getAccountInfo(stream)];
649
+ case 1:
650
+ escrow = _d.sent();
651
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
652
+ throw new Error("Couldn't get account info");
653
+ }
654
+ mint = (0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data).mint;
655
+ return [4 /*yield*/, (0, utils_1.ata)(mint, newRecipient)];
656
+ case 2:
657
+ newRecipientTokens = _d.sent();
658
+ ixs.push((0, instructions_1.transferStreamInstruction)(this.programId, {
659
+ authority: invoker.publicKey,
660
+ newRecipient: newRecipient,
661
+ newRecipientTokens: newRecipientTokens,
662
+ metadata: stream,
663
+ mint: mint,
664
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
665
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
666
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
667
+ systemProgram: web3_js_1.SystemProgram.programId,
668
+ }));
669
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
670
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
671
+ case 3:
672
+ hash = _d.sent();
673
+ tx = (_c = new web3_js_1.Transaction({
674
+ feePayer: invoker.publicKey,
675
+ blockhash: hash.blockhash,
676
+ lastValidBlockHeight: hash.lastValidBlockHeight,
677
+ })).add.apply(_c, __spreadArray([], __read(ixs), false));
678
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
679
+ case 4:
680
+ signature = _d.sent();
681
+ return [2 /*return*/, { ixs: ixs, tx: signature }];
682
+ }
683
+ });
684
+ });
685
+ };
686
+ /**
687
+ * Tops up stream account deposited amount.
688
+ * @param {TopupParams} data
689
+ * @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match current stream sender or transaction will fail.
690
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be topped up.
691
+ * @param {BN} data.amount - Specified amount (in the smallest units) to topup (increases deposited amount).
692
+ */
693
+ SolanaStreamClient.prototype.topup = function (_a, _b) {
694
+ var id = _a.id, amount = _a.amount;
695
+ var invoker = _b.invoker, isNative = _b.isNative;
696
+ return __awaiter(this, void 0, void 0, function () {
697
+ var ixs, streamPublicKey, escrow, _c, mint, partner, senderTokens, escrowTokens, streamflowTreasuryTokens, partnerTokens, nativeInstructions, _d, commitment, hash, tx, signature;
698
+ var _e;
699
+ return __generator(this, function (_f) {
700
+ switch (_f.label) {
701
+ case 0:
702
+ if (!invoker.publicKey) {
703
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
704
+ }
705
+ ixs = [];
706
+ streamPublicKey = new web3_js_1.PublicKey(id);
707
+ return [4 /*yield*/, this.connection.getAccountInfo(streamPublicKey)];
708
+ case 1:
709
+ escrow = _f.sent();
710
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
711
+ throw new Error("Couldn't get account info");
712
+ }
713
+ _c = (0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data), mint = _c.mint, partner = _c.partner, senderTokens = _c.senderTokens, escrowTokens = _c.escrowTokens;
714
+ return [4 /*yield*/, (0, utils_1.ata)(mint, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
715
+ case 2:
716
+ streamflowTreasuryTokens = _f.sent();
717
+ return [4 /*yield*/, (0, utils_1.ata)(mint, partner)];
718
+ case 3:
719
+ partnerTokens = _f.sent();
720
+ if (!isNative) return [3 /*break*/, 5];
721
+ return [4 /*yield*/, (0, instructions_1.prepareWrappedAccount)(this.connection, invoker.publicKey, amount)];
722
+ case 4:
723
+ _d = _f.sent();
724
+ return [3 /*break*/, 6];
725
+ case 5:
726
+ _d = [];
727
+ _f.label = 6;
728
+ case 6:
729
+ nativeInstructions = _d;
730
+ ixs.push((0, instructions_1.topupStreamInstruction)(amount, this.programId, {
731
+ sender: invoker.publicKey,
732
+ senderTokens: senderTokens,
733
+ metadata: streamPublicKey,
734
+ escrowTokens: escrowTokens,
735
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
736
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
737
+ partner: partner,
738
+ partnerTokens: partnerTokens,
739
+ mint: mint,
740
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
741
+ withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
742
+ systemProgram: web3_js_1.SystemProgram.programId,
743
+ }));
744
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
745
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
746
+ case 7:
747
+ hash = _f.sent();
748
+ tx = (_e = new web3_js_1.Transaction({
749
+ feePayer: invoker.publicKey,
750
+ blockhash: hash.blockhash,
751
+ lastValidBlockHeight: hash.lastValidBlockHeight,
752
+ })).add.apply(_e, __spreadArray(__spreadArray([], __read(nativeInstructions), false), __read(ixs), false));
753
+ return [4 /*yield*/, this.sign(invoker, tx, hash)];
754
+ case 8:
755
+ signature = _f.sent();
756
+ return [2 /*return*/, { ixs: ixs, tx: signature }];
757
+ }
758
+ });
759
+ });
760
+ };
761
+ /**
762
+ * Fetch stream data by its id (address).
763
+ * @param {string} id - Identifier of a stream that is fetched.
764
+ */
765
+ SolanaStreamClient.prototype.getOne = function (_a) {
766
+ var id = _a.id;
767
+ return __awaiter(this, void 0, void 0, function () {
768
+ var escrow;
769
+ return __generator(this, function (_b) {
770
+ switch (_b.label) {
771
+ case 0: return [4 /*yield*/, this.connection.getAccountInfo(new web3_js_1.PublicKey(id), constants_1.TX_FINALITY_CONFIRMED)];
772
+ case 1:
773
+ escrow = _b.sent();
774
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
775
+ throw new Error("Couldn't get account info.");
776
+ }
777
+ return [2 /*return*/, new types_1.Contract((0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data))];
778
+ }
779
+ });
780
+ });
781
+ };
782
+ /**
783
+ * Fetch streams/contracts by providing direction.
784
+ * Streams are sorted by start time in ascending order.
785
+ * @param {GetAllParams} data
786
+ * @param {PublicKey} data.wallet - PublicKey of the wallet for which the streams/contracts are fetched.
787
+ * @param {StreamType} [data.type = StreamType.All] - It can be one of: stream, vesting, all.
788
+ * @param {StreamDirection} [data.direction = StreamDirection.All] - It can be one of: incoming, outgoing, all.
789
+ */
790
+ SolanaStreamClient.prototype.get = function (_a) {
791
+ var wallet = _a.wallet, _b = _a.type, type = _b === void 0 ? types_2.StreamType.All : _b, _c = _a.direction, direction = _c === void 0 ? types_2.StreamDirection.All : _c;
792
+ return __awaiter(this, void 0, void 0, function () {
793
+ var accounts, outgoingAccounts, incomingAccounts, offset, streams, sortedStreams;
794
+ return __generator(this, function (_d) {
795
+ switch (_d.label) {
796
+ case 0:
797
+ accounts = [];
798
+ if (!(direction === "all")) return [3 /*break*/, 3];
799
+ return [4 /*yield*/, (0, utils_1.getProgramAccounts)(this.connection, wallet, constants_1.STREAM_STRUCT_OFFSET_SENDER, this.programId)];
800
+ case 1:
801
+ outgoingAccounts = _d.sent();
802
+ return [4 /*yield*/, (0, utils_1.getProgramAccounts)(this.connection, wallet, constants_1.STREAM_STRUCT_OFFSET_RECIPIENT, this.programId)];
803
+ case 2:
804
+ incomingAccounts = _d.sent();
805
+ accounts = __spreadArray(__spreadArray([], __read(outgoingAccounts), false), __read(incomingAccounts), false);
806
+ return [3 /*break*/, 5];
807
+ case 3:
808
+ offset = direction === "outgoing" ? constants_1.STREAM_STRUCT_OFFSET_SENDER : constants_1.STREAM_STRUCT_OFFSET_RECIPIENT;
809
+ return [4 /*yield*/, (0, utils_1.getProgramAccounts)(this.connection, wallet, offset, this.programId)];
810
+ case 4:
811
+ accounts = _d.sent();
812
+ _d.label = 5;
813
+ case 5:
814
+ streams = {};
815
+ accounts.forEach(function (account) {
816
+ var _a;
817
+ var decoded = new types_1.Contract((0, utils_1.decodeStream)(account.account.data));
818
+ streams = __assign(__assign({}, streams), (_a = {}, _a[account.pubkey.toBase58()] = decoded, _a));
819
+ });
820
+ sortedStreams = Object.entries(streams).sort(function (_a, _b) {
821
+ var _c = __read(_a, 2), stream1 = _c[1];
822
+ var _d = __read(_b, 2), stream2 = _d[1];
823
+ return stream2.startTime - stream1.startTime;
824
+ });
825
+ if (type === "all")
826
+ return [2 /*return*/, sortedStreams];
827
+ return [2 /*return*/, type === "stream"
828
+ ? sortedStreams.filter(function (stream) { return stream[1].canTopup; })
829
+ : sortedStreams.filter(function (stream) { return !stream[1].canTopup; })];
830
+ }
831
+ });
832
+ });
833
+ };
834
+ SolanaStreamClient.prototype.sign = function (invoker, tx, hash) {
835
+ return __awaiter(this, void 0, void 0, function () {
836
+ var rawTx, confirmationStrategy, signature;
837
+ return __generator(this, function (_a) {
838
+ switch (_a.label) {
839
+ case 0:
840
+ if (!(0, utils_1.isSignerWallet)(invoker)) return [3 /*break*/, 2];
841
+ return [4 /*yield*/, invoker.signTransaction(tx)];
842
+ case 1:
843
+ _a.sent();
844
+ return [3 /*break*/, 3];
845
+ case 2:
846
+ tx.partialSign(invoker);
847
+ _a.label = 3;
848
+ case 3:
849
+ rawTx = tx.serialize();
850
+ if (!hash.lastValidBlockHeight || !tx.signature || !hash.blockhash)
851
+ throw Error("Error with transaction parameters.");
852
+ confirmationStrategy = {
853
+ lastValidBlockHeight: hash.lastValidBlockHeight,
854
+ signature: bs58_1.default.encode(tx.signature),
855
+ blockhash: hash.blockhash,
856
+ };
857
+ return [4 /*yield*/, (0, web3_js_1.sendAndConfirmRawTransaction)(this.connection, rawTx, confirmationStrategy)];
858
+ case 4:
859
+ signature = _a.sent();
860
+ return [2 /*return*/, signature];
861
+ }
862
+ });
863
+ });
864
+ };
865
+ /**
866
+ * Forms instructions from params, creates a raw transaction and fetch recent blockhash.
867
+ * @param {MultiRecipient} recipient - Wallet sending stream to
868
+ * @param {CreateMultiParams} streamParams - Parameters of stream user wants to create.
869
+ */
870
+ SolanaStreamClient.prototype.prepareStreamTransaction = function (recipient, streamParams, solanaExtendedConfig) {
871
+ return __awaiter(this, void 0, void 0, function () {
872
+ var mint, start, period, cliff, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, _a, automaticWithdrawal, _b, withdrawalFrequency, sender, _c, partner, ixs, commitment, recipientPublicKey, mintPublicKey, metadata, _d, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, hash, tx;
873
+ var _e;
874
+ return __generator(this, function (_f) {
875
+ switch (_f.label) {
876
+ case 0:
877
+ mint = streamParams.tokenId, 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;
878
+ sender = solanaExtendedConfig.sender, _c = solanaExtendedConfig.partner, partner = _c === void 0 ? null : _c;
879
+ if (!sender.publicKey) {
880
+ throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
881
+ }
882
+ ixs = [];
883
+ commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
884
+ recipientPublicKey = new web3_js_1.PublicKey(recipient.recipient);
885
+ mintPublicKey = new web3_js_1.PublicKey(mint);
886
+ metadata = web3_js_1.Keypair.generate();
887
+ _d = __read(web3_js_1.PublicKey.findProgramAddressSync([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], this.programId), 1), escrowTokens = _d[0];
888
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, sender.publicKey)];
889
+ case 1:
890
+ senderTokens = _f.sent();
891
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, recipientPublicKey)];
892
+ case 2:
893
+ recipientTokens = _f.sent();
894
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
895
+ case 3:
896
+ streamflowTreasuryTokens = _f.sent();
897
+ partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY;
898
+ return [4 /*yield*/, (0, utils_1.ata)(mintPublicKey, partnerPublicKey)];
899
+ case 4:
900
+ partnerTokens = _f.sent();
901
+ ixs.push((0, instructions_1.createStreamInstruction)({
902
+ start: new bn_js_1.default(start),
903
+ depositedAmount: recipient.amount,
904
+ period: new bn_js_1.default(period),
905
+ amountPerPeriod: recipient.amountPerPeriod,
906
+ cliff: new bn_js_1.default(cliff),
907
+ cliffAmount: recipient.cliffAmount,
908
+ cancelableBySender: cancelableBySender,
909
+ cancelableByRecipient: cancelableByRecipient,
910
+ automaticWithdrawal: automaticWithdrawal,
911
+ transferableBySender: transferableBySender,
912
+ transferableByRecipient: transferableByRecipient,
913
+ canTopup: canTopup,
914
+ name: recipient.name,
915
+ withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
916
+ }, this.programId, {
917
+ sender: sender.publicKey,
918
+ senderTokens: senderTokens,
919
+ recipient: new web3_js_1.PublicKey(recipient.recipient),
920
+ metadata: metadata.publicKey,
921
+ escrowTokens: escrowTokens,
922
+ recipientTokens: recipientTokens,
923
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
924
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
925
+ partner: partnerPublicKey,
926
+ partnerTokens: partnerTokens,
927
+ mint: new web3_js_1.PublicKey(mint),
928
+ feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
929
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
930
+ timelockProgram: this.programId,
931
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
932
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
933
+ withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
934
+ systemProgram: web3_js_1.SystemProgram.programId,
935
+ }));
936
+ return [4 /*yield*/, this.connection.getLatestBlockhash(commitment)];
937
+ case 5:
938
+ hash = _f.sent();
939
+ tx = (_e = new web3_js_1.Transaction({
940
+ feePayer: sender.publicKey,
941
+ blockhash: hash.blockhash,
942
+ lastValidBlockHeight: hash.lastValidBlockHeight,
943
+ })).add.apply(_e, __spreadArray([], __read(ixs), false));
944
+ tx.partialSign(metadata);
945
+ return [2 /*return*/, { tx: tx, metadata: metadata }];
946
+ }
947
+ });
948
+ });
949
+ };
950
+ return SolanaStreamClient;
951
+ }(types_2.GenericStreamClient));
952
+ exports.default = SolanaStreamClient;