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