@streamflow/stream 3.0.18 → 3.0.19

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 (52) hide show
  1. package/dist/Stream.d.ts +87 -0
  2. package/dist/Stream.js +503 -0
  3. package/dist/StreamClient.d.ts +146 -0
  4. package/dist/{solana/StreamClient.js → StreamClient.js} +397 -301
  5. package/dist/__tests__/test_dependencies/airdrop/airdrop-request.d.ts +3 -0
  6. package/dist/__tests__/test_dependencies/airdrop/airdrop-request.js +67 -0
  7. package/dist/__tests__/test_dependencies/airdrop/airdrop.d.ts +6 -0
  8. package/dist/__tests__/test_dependencies/airdrop/airdrop.js +215 -0
  9. package/dist/__tests__/test_dependencies/constants/index.d.ts +122 -0
  10. package/dist/__tests__/test_dependencies/constants/index.js +158 -0
  11. package/dist/__tests__/test_dependencies/idl/airdrop.d.ts +27 -0
  12. package/dist/__tests__/test_dependencies/idl/airdrop.js +171 -0
  13. package/dist/{solana/constants.d.ts → constants.d.ts} +1 -1
  14. package/dist/{solana/constants.js → constants.js} +5 -5
  15. package/dist/{solana/timelockIDL.d.ts → idl/timelock.d.ts} +0 -0
  16. package/dist/{solana/timelockIDL.js → idl/timelock.js} +0 -0
  17. package/dist/index.d.ts +6 -5
  18. package/dist/index.js +10 -19
  19. package/dist/{solana/instructions.d.ts → instructions.d.ts} +2 -8
  20. package/dist/{solana/instructions.js → instructions.js} +38 -55
  21. package/dist/layout.d.ts +22 -0
  22. package/dist/{solana/layout.js → layout.js} +3 -65
  23. package/dist/{solana/types.d.ts → types.d.ts} +34 -37
  24. package/dist/{solana/types.js → types.js} +20 -3
  25. package/dist/{common/utils.d.ts → utils.d.ts} +4 -0
  26. package/dist/utils.js +126 -0
  27. package/package.json +1 -1
  28. package/dist/__tests__/aptos/streamClient.spec.d.ts +0 -1
  29. package/dist/__tests__/aptos/streamClient.spec.js +0 -160
  30. package/dist/aptos/StreamClient.d.ts +0 -49
  31. package/dist/aptos/StreamClient.js +0 -362
  32. package/dist/aptos/constants.d.ts +0 -2
  33. package/dist/aptos/constants.js +0 -12
  34. package/dist/aptos/index.d.ts +0 -2
  35. package/dist/aptos/index.js +0 -24
  36. package/dist/aptos/types.d.ts +0 -145
  37. package/dist/aptos/types.js +0 -65
  38. package/dist/common/BaseStreamClient.d.ts +0 -11
  39. package/dist/common/BaseStreamClient.js +0 -9
  40. package/dist/common/GenericStreamClient.d.ts +0 -60
  41. package/dist/common/GenericStreamClient.js +0 -137
  42. package/dist/common/types.d.ts +0 -71
  43. package/dist/common/types.js +0 -28
  44. package/dist/common/utils.js +0 -31
  45. package/dist/solana/StreamClient.d.ts +0 -71
  46. package/dist/solana/index.d.ts +0 -7
  47. package/dist/solana/index.js +0 -43
  48. package/dist/solana/instructionTypes.d.ts +0 -86
  49. package/dist/solana/instructionTypes.js +0 -2
  50. package/dist/solana/layout.d.ts +0 -27
  51. package/dist/solana/utils.d.ts +0 -42
  52. package/dist/solana/utils.js +0 -271
@@ -0,0 +1,87 @@
1
+ import { Stream as StreamData, GetStreamsParams, CreateStreamParams, CreateStreamResponse, WithdrawStreamParams, CancelStreamParams, TransactionResponse, TopupStreamParams, TransferStreamParams, GetStreamParams } from "./types";
2
+ export default class Stream {
3
+ /**
4
+ * Creates a new stream/vesting contract.
5
+ * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
6
+ * @param {CreateStreamParams} data
7
+ * @param {Connection} data.connection - A connection to the cluster.
8
+ * @param {Wallet} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
9
+ * @param {string} data.recipient - Solana address of the recipient. Associated token account will be derived using this address and token mint address.
10
+ * @param {string} data.mint - SPL Token mint.
11
+ * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
12
+ * @param {BN} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
13
+ * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
14
+ * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
15
+ * @param {BN} data.cliffAmount - Amount unlocked at the "cliff".
16
+ * @param {BN} data.amountPerPeriod - Amount unlocked per each period.
17
+ * @param {string} data.name - Stream name/subject.
18
+ * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
19
+ * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
20
+ * @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
21
+ * @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
22
+ * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
23
+ * @param {boolean} [data.automaticWithdrawal = false] - Whether or not a 3rd party can initiate withdraw in the name of recipient.
24
+ * @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.
25
+ * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
26
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
27
+ */
28
+ static create({ connection, sender, recipient, mint, start, depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal, withdrawalFrequency, partner, cluster, }: CreateStreamParams): Promise<CreateStreamResponse>;
29
+ /**
30
+ * Attempts withdrawal from the specified stream.
31
+ * @param {WithdrawStreamParams} data
32
+ * @param {Connection} data.connection - A connection to the cluster.
33
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (recipient) or transaction will fail.
34
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be withdrawn from.
35
+ * @param {BN} data.amount - Requested amount (in the smallest units) to withdraw (while streaming). If stream is completed, the whole amount will be withdrawn.
36
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
37
+ */
38
+ static withdraw({ connection, invoker, id, amount, cluster, }: WithdrawStreamParams): Promise<TransactionResponse>;
39
+ /**
40
+ * Attempts canceling the specified stream.
41
+ * @param {CancelStreamParams} data
42
+ * @param {Connection} data.connection - A connection to the cluster.
43
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
44
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be canceled.
45
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
46
+ */
47
+ static cancel({ connection, invoker, id, cluster, }: CancelStreamParams): Promise<TransactionResponse>;
48
+ /**
49
+ * Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
50
+ * Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
51
+ * @param {TransferStreamParams} data
52
+ * @param {Connection} data.connection - A connection to the cluster.
53
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
54
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be transferred.
55
+ * @param {string} data.recipientId - Address of a new recipient.
56
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
57
+ */
58
+ static transfer({ connection, invoker, id, recipientId, cluster, }: TransferStreamParams): Promise<TransactionResponse>;
59
+ /**
60
+ * Tops up stream account deposited amount.
61
+ * @param {TopupStreamParams} data
62
+ * @param {Connection} data.connection - A connection to the cluster.
63
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match current stream sender or transaction will fail.
64
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be topped up.
65
+ * @param {BN} data.amount - Specified amount (in the smallest units) to topup (increases deposited amount).
66
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
67
+ */
68
+ static topup({ connection, invoker, id, amount, cluster, }: TopupStreamParams): Promise<TransactionResponse>;
69
+ /**
70
+ * Fetch stream data by its id (address).
71
+ * @param {GetStreamParams} data
72
+ * @param {Connection} data.connection - A connection to the cluster.
73
+ * @param {string} data.id - Identifier of a stream that is fetched.
74
+ */
75
+ static getOne({ connection, id, }: GetStreamParams): Promise<StreamData>;
76
+ /**
77
+ * Fetch streams/contracts by providing direction.
78
+ * Streams are sorted by start time in ascending order.
79
+ * @param {GetStreamsParams} data
80
+ * @param {Connection} data.connection - A connection to the cluster.
81
+ * @param {PublicKey} data.wallet - PublicKey of the wallet for which the streams/contracts are fetched.
82
+ * @param {StreamType} [data.type = StreamType.All] - It can be one of: stream, vesting, all.
83
+ * @param {StreamDirection} [data.direction = StreamDirection.All] - It can be one of: incoming, outgoing, all.
84
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
85
+ */
86
+ static get({ connection, wallet, type, direction, cluster, }: GetStreamsParams): Promise<[string, StreamData][]>;
87
+ }
package/dist/Stream.js ADDED
@@ -0,0 +1,503 @@
1
+ "use strict";
2
+ // Version 1 of the SDK that uses Anchor.
3
+ var __assign = (this && this.__assign) || function () {
4
+ __assign = Object.assign || function(t) {
5
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
6
+ s = arguments[i];
7
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8
+ t[p] = s[p];
9
+ }
10
+ return t;
11
+ };
12
+ return __assign.apply(this, arguments);
13
+ };
14
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
+ return new (P || (P = Promise))(function (resolve, reject) {
17
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
21
+ });
22
+ };
23
+ var __generator = (this && this.__generator) || function (thisArg, body) {
24
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
25
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
26
+ function verb(n) { return function (v) { return step([n, v]); }; }
27
+ function step(op) {
28
+ if (f) throw new TypeError("Generator is already executing.");
29
+ while (_) try {
30
+ 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;
31
+ if (y = 0, t) op = [op[0] & 2, t.value];
32
+ switch (op[0]) {
33
+ case 0: case 1: t = op; break;
34
+ case 4: _.label++; return { value: op[1], done: false };
35
+ case 5: _.label++; y = op[1]; op = [0]; continue;
36
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
37
+ default:
38
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
39
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
40
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
41
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
42
+ if (t[2]) _.ops.pop();
43
+ _.trys.pop(); continue;
44
+ }
45
+ op = body.call(thisArg, _);
46
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
47
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
48
+ }
49
+ };
50
+ var __read = (this && this.__read) || function (o, n) {
51
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
52
+ if (!m) return o;
53
+ var i = m.call(o), r, ar = [], e;
54
+ try {
55
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
56
+ }
57
+ catch (error) { e = { error: error }; }
58
+ finally {
59
+ try {
60
+ if (r && !r.done && (m = i["return"])) m.call(i);
61
+ }
62
+ finally { if (e) throw e.error; }
63
+ }
64
+ return ar;
65
+ };
66
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
67
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
68
+ if (ar || !(i in from)) {
69
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
70
+ ar[i] = from[i];
71
+ }
72
+ }
73
+ return to.concat(ar || Array.prototype.slice.call(from));
74
+ };
75
+ var __importDefault = (this && this.__importDefault) || function (mod) {
76
+ return (mod && mod.__esModule) ? mod : { "default": mod };
77
+ };
78
+ Object.defineProperty(exports, "__esModule", { value: true });
79
+ var bn_js_1 = __importDefault(require("bn.js"));
80
+ var buffer_1 = require("buffer");
81
+ var anchor_1 = require("@project-serum/anchor");
82
+ var spl_token_1 = require("@solana/spl-token");
83
+ var web3_js_1 = require("@solana/web3.js");
84
+ var types_1 = require("./types");
85
+ var utils_1 = require("./utils");
86
+ var constants_1 = require("./constants");
87
+ var timelock_1 = __importDefault(require("./idl/timelock"));
88
+ var encoder = new TextEncoder();
89
+ var initProgram = function (connection, wallet, cluster) {
90
+ var provider = new anchor_1.Provider(connection, wallet, {});
91
+ return new anchor_1.Program(timelock_1.default, constants_1.PROGRAM_ID[cluster], provider);
92
+ };
93
+ var Stream = /** @class */ (function () {
94
+ function Stream() {
95
+ }
96
+ /**
97
+ * Creates a new stream/vesting contract.
98
+ * All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
99
+ * @param {CreateStreamParams} data
100
+ * @param {Connection} data.connection - A connection to the cluster.
101
+ * @param {Wallet} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
102
+ * @param {string} data.recipient - Solana address of the recipient. Associated token account will be derived using this address and token mint address.
103
+ * @param {string} data.mint - SPL Token mint.
104
+ * @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
105
+ * @param {BN} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
106
+ * @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
107
+ * @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
108
+ * @param {BN} data.cliffAmount - Amount unlocked at the "cliff".
109
+ * @param {BN} data.amountPerPeriod - Amount unlocked per each period.
110
+ * @param {string} data.name - Stream name/subject.
111
+ * @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
112
+ * @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
113
+ * @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
114
+ * @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
115
+ * @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
116
+ * @param {boolean} [data.automaticWithdrawal = false] - Whether or not a 3rd party can initiate withdraw in the name of recipient.
117
+ * @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.
118
+ * @param {string | null} [data.partner = null] - Partner's wallet address (optional).
119
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
120
+ */
121
+ Stream.create = function (_a) {
122
+ var connection = _a.connection, sender = _a.sender, recipient = _a.recipient, mint = _a.mint, start = _a.start, depositedAmount = _a.depositedAmount, period = _a.period, cliff = _a.cliff, cliffAmount = _a.cliffAmount, amountPerPeriod = _a.amountPerPeriod, name = _a.name, canTopup = _a.canTopup, cancelableBySender = _a.cancelableBySender, cancelableByRecipient = _a.cancelableByRecipient, transferableBySender = _a.transferableBySender, transferableByRecipient = _a.transferableByRecipient, _b = _a.automaticWithdrawal, automaticWithdrawal = _b === void 0 ? false : _b, _c = _a.withdrawalFrequency, withdrawalFrequency = _c === void 0 ? 0 : _c, _d = _a.partner, partner = _d === void 0 ? "" : _d, _e = _a.cluster, cluster = _e === void 0 ? types_1.Cluster.Mainnet : _e;
123
+ return __awaiter(this, void 0, void 0, function () {
124
+ var program, mintPublicKey, recipientPublicKey, metadata, _f, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, signers, nameUtf8EncodedArr, tx;
125
+ return __generator(this, function (_g) {
126
+ switch (_g.label) {
127
+ case 0:
128
+ program = initProgram(connection, sender, cluster);
129
+ mintPublicKey = new web3_js_1.PublicKey(mint);
130
+ recipientPublicKey = new web3_js_1.PublicKey(recipient);
131
+ metadata = web3_js_1.Keypair.generate();
132
+ return [4 /*yield*/, anchor_1.web3.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], program.programId)];
133
+ case 1:
134
+ _f = __read.apply(void 0, [_g.sent(), 1]), escrowTokens = _f[0];
135
+ return [4 /*yield*/, ata(mintPublicKey, sender.publicKey)];
136
+ case 2:
137
+ senderTokens = _g.sent();
138
+ return [4 /*yield*/, ata(mintPublicKey, recipientPublicKey)];
139
+ case 3:
140
+ recipientTokens = _g.sent();
141
+ return [4 /*yield*/, ata(mintPublicKey, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
142
+ case 4:
143
+ streamflowTreasuryTokens = _g.sent();
144
+ partnerPublicKey = partner
145
+ ? new web3_js_1.PublicKey(partner)
146
+ : sender.publicKey;
147
+ return [4 /*yield*/, ata(mintPublicKey, partnerPublicKey)];
148
+ case 5:
149
+ partnerTokens = _g.sent();
150
+ signers = [metadata];
151
+ nameUtf8EncodedArr = encoder.encode(name);
152
+ return [4 /*yield*/, program.rpc.create(
153
+ // Order of the parameters must match the ones in the program
154
+ new bn_js_1.default(start), depositedAmount, new bn_js_1.default(period), amountPerPeriod, new bn_js_1.default(cliff), cliffAmount, cancelableBySender, cancelableByRecipient, automaticWithdrawal, transferableBySender, transferableByRecipient, canTopup, nameUtf8EncodedArr, new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period), {
155
+ accounts: {
156
+ sender: sender.publicKey,
157
+ senderTokens: senderTokens,
158
+ recipient: new web3_js_1.PublicKey(recipient),
159
+ metadata: metadata.publicKey,
160
+ escrowTokens: escrowTokens,
161
+ recipientTokens: recipientTokens,
162
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
163
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
164
+ partner: partnerPublicKey,
165
+ partnerTokens: partnerTokens,
166
+ mint: new web3_js_1.PublicKey(mint),
167
+ feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
168
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
169
+ timelockProgram: program.programId,
170
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
171
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
172
+ withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
173
+ systemProgram: web3_js_1.SystemProgram.programId,
174
+ },
175
+ signers: signers,
176
+ })];
177
+ case 6:
178
+ tx = _g.sent();
179
+ return [2 /*return*/, { tx: tx, id: metadata.publicKey.toBase58() }];
180
+ }
181
+ });
182
+ });
183
+ };
184
+ /**
185
+ * Attempts withdrawal from the specified stream.
186
+ * @param {WithdrawStreamParams} data
187
+ * @param {Connection} data.connection - A connection to the cluster.
188
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (recipient) or transaction will fail.
189
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be withdrawn from.
190
+ * @param {BN} data.amount - Requested amount (in the smallest units) to withdraw (while streaming). If stream is completed, the whole amount will be withdrawn.
191
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
192
+ */
193
+ Stream.withdraw = function (_a) {
194
+ var connection = _a.connection, invoker = _a.invoker, id = _a.id, amount = _a.amount, _b = _a.cluster, cluster = _b === void 0 ? types_1.Cluster.Mainnet : _b;
195
+ return __awaiter(this, void 0, void 0, function () {
196
+ var program, streamPublicKey, escrow, data, streamflowTreasuryTokens, partnerTokens, tx;
197
+ return __generator(this, function (_c) {
198
+ switch (_c.label) {
199
+ case 0:
200
+ program = initProgram(connection, invoker, cluster);
201
+ streamPublicKey = new web3_js_1.PublicKey(id);
202
+ return [4 /*yield*/, connection.getAccountInfo(streamPublicKey)];
203
+ case 1:
204
+ escrow = _c.sent();
205
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
206
+ throw new Error("Couldn't get account info");
207
+ }
208
+ data = (0, utils_1.decodeStream)(escrow.data);
209
+ return [4 /*yield*/, ata(data.mint, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
210
+ case 2:
211
+ streamflowTreasuryTokens = _c.sent();
212
+ return [4 /*yield*/, ata(data.mint, data.partner)];
213
+ case 3:
214
+ partnerTokens = _c.sent();
215
+ return [4 /*yield*/, program.rpc.withdraw(amount, {
216
+ accounts: {
217
+ authority: invoker.publicKey,
218
+ recipient: data.recipient,
219
+ recipientTokens: data.recipientTokens,
220
+ metadata: streamPublicKey,
221
+ escrowTokens: data.escrowTokens,
222
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
223
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
224
+ partner: data.partner,
225
+ partnerTokens: partnerTokens,
226
+ mint: data.mint,
227
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
228
+ },
229
+ })];
230
+ case 4:
231
+ tx = _c.sent();
232
+ return [2 /*return*/, { tx: tx }];
233
+ }
234
+ });
235
+ });
236
+ };
237
+ /**
238
+ * Attempts canceling the specified stream.
239
+ * @param {CancelStreamParams} data
240
+ * @param {Connection} data.connection - A connection to the cluster.
241
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
242
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be canceled.
243
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
244
+ */
245
+ Stream.cancel = function (_a) {
246
+ var connection = _a.connection, invoker = _a.invoker, id = _a.id, _b = _a.cluster, cluster = _b === void 0 ? types_1.Cluster.Mainnet : _b;
247
+ return __awaiter(this, void 0, void 0, function () {
248
+ var program, streamPublicKey, escrow_acc, data, streamflowTreasuryTokens, partnerTokens, tx;
249
+ return __generator(this, function (_c) {
250
+ switch (_c.label) {
251
+ case 0:
252
+ program = initProgram(connection, invoker, cluster);
253
+ streamPublicKey = new web3_js_1.PublicKey(id);
254
+ return [4 /*yield*/, connection.getAccountInfo(streamPublicKey)];
255
+ case 1:
256
+ escrow_acc = _c.sent();
257
+ if (!(escrow_acc === null || escrow_acc === void 0 ? void 0 : escrow_acc.data)) {
258
+ throw new Error("Couldn't get account info");
259
+ }
260
+ data = (0, utils_1.decodeStream)(escrow_acc === null || escrow_acc === void 0 ? void 0 : escrow_acc.data);
261
+ return [4 /*yield*/, ata(data.mint, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
262
+ case 2:
263
+ streamflowTreasuryTokens = _c.sent();
264
+ return [4 /*yield*/, ata(data.mint, data.partner)];
265
+ case 3:
266
+ partnerTokens = _c.sent();
267
+ return [4 /*yield*/, program.rpc.cancel({
268
+ accounts: {
269
+ authority: invoker.publicKey,
270
+ sender: data.sender,
271
+ senderTokens: data.senderTokens,
272
+ recipient: data.recipient,
273
+ recipientTokens: data.recipientTokens,
274
+ metadata: streamPublicKey,
275
+ escrowTokens: data.escrowTokens,
276
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
277
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
278
+ partner: data.partner,
279
+ partnerTokens: partnerTokens,
280
+ mint: data.mint,
281
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
282
+ },
283
+ })];
284
+ case 4:
285
+ tx = _c.sent();
286
+ return [2 /*return*/, { tx: tx }];
287
+ }
288
+ });
289
+ });
290
+ };
291
+ /**
292
+ * Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
293
+ * Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
294
+ * @param {TransferStreamParams} data
295
+ * @param {Connection} data.connection - A connection to the cluster.
296
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
297
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be transferred.
298
+ * @param {string} data.recipientId - Address of a new recipient.
299
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
300
+ */
301
+ Stream.transfer = function (_a) {
302
+ var connection = _a.connection, invoker = _a.invoker, id = _a.id, recipientId = _a.recipientId, _b = _a.cluster, cluster = _b === void 0 ? types_1.Cluster.Mainnet : _b;
303
+ return __awaiter(this, void 0, void 0, function () {
304
+ var program, stream, newRecipient, escrow, mint, newRecipientTokens, tx;
305
+ return __generator(this, function (_c) {
306
+ switch (_c.label) {
307
+ case 0:
308
+ program = initProgram(connection, invoker, cluster);
309
+ stream = new web3_js_1.PublicKey(id);
310
+ newRecipient = new web3_js_1.PublicKey(recipientId);
311
+ return [4 /*yield*/, connection.getAccountInfo(stream)];
312
+ case 1:
313
+ escrow = _c.sent();
314
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
315
+ throw new Error("Couldn't get account info");
316
+ }
317
+ mint = (0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data).mint;
318
+ return [4 /*yield*/, ata(mint, newRecipient)];
319
+ case 2:
320
+ newRecipientTokens = _c.sent();
321
+ return [4 /*yield*/, program.rpc.transferRecipient({
322
+ accounts: {
323
+ authority: invoker.publicKey,
324
+ newRecipient: newRecipient,
325
+ newRecipientTokens: newRecipientTokens,
326
+ metadata: stream,
327
+ mint: mint,
328
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
329
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
330
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
331
+ systemProgram: anchor_1.web3.SystemProgram.programId,
332
+ },
333
+ })];
334
+ case 3:
335
+ tx = _c.sent();
336
+ return [2 /*return*/, { tx: tx }];
337
+ }
338
+ });
339
+ });
340
+ };
341
+ /**
342
+ * Tops up stream account deposited amount.
343
+ * @param {TopupStreamParams} data
344
+ * @param {Connection} data.connection - A connection to the cluster.
345
+ * @param {Wallet} data.invoker - Wallet signing the transaction. It's address should match current stream sender or transaction will fail.
346
+ * @param {string} data.id - Identifier of a stream (escrow account with metadata) to be topped up.
347
+ * @param {BN} data.amount - Specified amount (in the smallest units) to topup (increases deposited amount).
348
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
349
+ */
350
+ Stream.topup = function (_a) {
351
+ var connection = _a.connection, invoker = _a.invoker, id = _a.id, amount = _a.amount, _b = _a.cluster, cluster = _b === void 0 ? types_1.Cluster.Mainnet : _b;
352
+ return __awaiter(this, void 0, void 0, function () {
353
+ var program, streamPublicKey, escrow, _c, mint, partner, senderTokens, escrowTokens, streamflowTreasuryTokens, partnerTokens, tx;
354
+ return __generator(this, function (_d) {
355
+ switch (_d.label) {
356
+ case 0:
357
+ program = initProgram(connection, invoker, cluster);
358
+ streamPublicKey = new web3_js_1.PublicKey(id);
359
+ return [4 /*yield*/, connection.getAccountInfo(streamPublicKey)];
360
+ case 1:
361
+ escrow = _d.sent();
362
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
363
+ throw new Error("Couldn't get account info");
364
+ }
365
+ _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;
366
+ return [4 /*yield*/, ata(mint, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
367
+ case 2:
368
+ streamflowTreasuryTokens = _d.sent();
369
+ return [4 /*yield*/, ata(mint, partner)];
370
+ case 3:
371
+ partnerTokens = _d.sent();
372
+ return [4 /*yield*/, program.rpc.topup(amount, {
373
+ accounts: {
374
+ sender: invoker.publicKey,
375
+ senderTokens: senderTokens,
376
+ metadata: streamPublicKey,
377
+ escrowTokens: escrowTokens,
378
+ streamflowTreasury: constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
379
+ streamflowTreasuryTokens: streamflowTreasuryTokens,
380
+ partner: partner,
381
+ partnerTokens: partnerTokens,
382
+ mint: mint,
383
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
384
+ withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
385
+ systemProgram: anchor_1.web3.SystemProgram.programId,
386
+ },
387
+ })];
388
+ case 4:
389
+ tx = _d.sent();
390
+ return [2 /*return*/, { tx: tx }];
391
+ }
392
+ });
393
+ });
394
+ };
395
+ /**
396
+ * Fetch stream data by its id (address).
397
+ * @param {GetStreamParams} data
398
+ * @param {Connection} data.connection - A connection to the cluster.
399
+ * @param {string} data.id - Identifier of a stream that is fetched.
400
+ */
401
+ Stream.getOne = function (_a) {
402
+ var connection = _a.connection, id = _a.id;
403
+ return __awaiter(this, void 0, void 0, function () {
404
+ var escrow;
405
+ return __generator(this, function (_b) {
406
+ switch (_b.label) {
407
+ case 0: return [4 /*yield*/, connection.getAccountInfo(new web3_js_1.PublicKey(id), constants_1.TX_FINALITY_CONFIRMED)];
408
+ case 1:
409
+ escrow = _b.sent();
410
+ if (!(escrow === null || escrow === void 0 ? void 0 : escrow.data)) {
411
+ throw new Error("Couldn't get account info.");
412
+ }
413
+ return [2 /*return*/, new types_1.Contract((0, utils_1.decodeStream)(escrow === null || escrow === void 0 ? void 0 : escrow.data))];
414
+ }
415
+ });
416
+ });
417
+ };
418
+ /**
419
+ * Fetch streams/contracts by providing direction.
420
+ * Streams are sorted by start time in ascending order.
421
+ * @param {GetStreamsParams} data
422
+ * @param {Connection} data.connection - A connection to the cluster.
423
+ * @param {PublicKey} data.wallet - PublicKey of the wallet for which the streams/contracts are fetched.
424
+ * @param {StreamType} [data.type = StreamType.All] - It can be one of: stream, vesting, all.
425
+ * @param {StreamDirection} [data.direction = StreamDirection.All] - It can be one of: incoming, outgoing, all.
426
+ * @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
427
+ */
428
+ Stream.get = function (_a) {
429
+ var connection = _a.connection, wallet = _a.wallet, _b = _a.type, type = _b === void 0 ? types_1.StreamType.All : _b, _c = _a.direction, direction = _c === void 0 ? types_1.StreamDirection.All : _c, _d = _a.cluster, cluster = _d === void 0 ? types_1.Cluster.Mainnet : _d;
430
+ return __awaiter(this, void 0, void 0, function () {
431
+ var accounts, outgoingAccounts, incomingAccounts, offset, streams, sortedStreams;
432
+ return __generator(this, function (_e) {
433
+ switch (_e.label) {
434
+ case 0:
435
+ accounts = [];
436
+ if (!(direction === "all")) return [3 /*break*/, 3];
437
+ return [4 /*yield*/, getProgramAccounts(connection, wallet, constants_1.STREAM_STRUCT_OFFSET_SENDER, cluster)];
438
+ case 1:
439
+ outgoingAccounts = _e.sent();
440
+ return [4 /*yield*/, getProgramAccounts(connection, wallet, constants_1.STREAM_STRUCT_OFFSET_RECIPIENT, cluster)];
441
+ case 2:
442
+ incomingAccounts = _e.sent();
443
+ accounts = __spreadArray(__spreadArray([], __read(outgoingAccounts), false), __read(incomingAccounts), false);
444
+ return [3 /*break*/, 5];
445
+ case 3:
446
+ offset = direction === "outgoing"
447
+ ? constants_1.STREAM_STRUCT_OFFSET_SENDER
448
+ : constants_1.STREAM_STRUCT_OFFSET_RECIPIENT;
449
+ return [4 /*yield*/, getProgramAccounts(connection, wallet, offset, cluster)];
450
+ case 4:
451
+ accounts = _e.sent();
452
+ _e.label = 5;
453
+ case 5:
454
+ streams = {};
455
+ accounts.forEach(function (account) {
456
+ var _a;
457
+ var decoded = new types_1.Contract((0, utils_1.decodeStream)(account.account.data));
458
+ streams = __assign(__assign({}, streams), (_a = {}, _a[account.pubkey.toBase58()] = decoded, _a));
459
+ });
460
+ sortedStreams = Object.entries(streams).sort(function (_a, _b) {
461
+ var _c = __read(_a, 2), stream1 = _c[1];
462
+ var _d = __read(_b, 2), stream2 = _d[1];
463
+ return stream2.startTime - stream1.startTime;
464
+ });
465
+ if (type === "all")
466
+ return [2 /*return*/, sortedStreams];
467
+ return [2 /*return*/, type === "stream"
468
+ ? sortedStreams.filter(function (stream) { return stream[1].canTopup; })
469
+ : sortedStreams.filter(function (stream) { return !stream[1].canTopup; })];
470
+ }
471
+ });
472
+ });
473
+ };
474
+ return Stream;
475
+ }());
476
+ exports.default = Stream;
477
+ function getProgramAccounts(connection, wallet, offset, cluster) {
478
+ if (cluster === void 0) { cluster = types_1.Cluster.Mainnet; }
479
+ return __awaiter(this, void 0, void 0, function () {
480
+ return __generator(this, function (_a) {
481
+ return [2 /*return*/, connection === null || connection === void 0 ? void 0 : connection.getProgramAccounts(new web3_js_1.PublicKey(constants_1.PROGRAM_ID[cluster]), {
482
+ filters: [
483
+ {
484
+ memcmp: {
485
+ offset: offset,
486
+ bytes: wallet.toBase58(),
487
+ },
488
+ },
489
+ ],
490
+ })];
491
+ });
492
+ });
493
+ }
494
+ function ata(mint, account) {
495
+ return __awaiter(this, void 0, void 0, function () {
496
+ return __generator(this, function (_a) {
497
+ switch (_a.label) {
498
+ case 0: return [4 /*yield*/, spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, mint, account, true)];
499
+ case 1: return [2 /*return*/, _a.sent()];
500
+ }
501
+ });
502
+ });
503
+ }