@streamflow/stream 7.0.0-alpha.2 → 7.0.0-alpha.5
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/dist/cjs/__tests__/aptos/streamClient.spec.js +83 -134
- package/dist/cjs/__tests__/common/GenericStreamClient.spec.js +9 -9
- package/dist/cjs/aptos/StreamClient.js +162 -367
- package/dist/cjs/aptos/constants.js +7 -8
- package/dist/cjs/aptos/index.js +3 -3
- package/dist/cjs/aptos/types.js +17 -26
- package/dist/cjs/aptos/utils.js +4 -4
- package/dist/cjs/aptos/wallet.js +20 -75
- package/dist/cjs/common/BaseStreamClient.js +11 -59
- package/dist/cjs/common/GenericStreamClient.js +46 -72
- package/dist/cjs/common/constants.js +1 -1
- package/dist/cjs/common/contractUtils.js +12 -13
- package/dist/cjs/common/utils.js +21 -66
- package/dist/cjs/evm/StreamClient.js +198 -456
- package/dist/cjs/evm/constants.js +19 -20
- package/dist/cjs/evm/types.js +14 -23
- package/dist/cjs/evm/utils.js +2 -2
- package/dist/cjs/index.js +10 -10
- package/dist/cjs/solana/StreamClient.js +592 -1090
- package/dist/cjs/solana/constants.js +9 -10
- package/dist/cjs/solana/instructions.js +60 -65
- package/dist/cjs/solana/layout.js +7 -8
- package/dist/cjs/solana/types.js +14 -24
- package/dist/cjs/solana/utils.js +48 -136
- package/dist/cjs/sui/StreamClient.js +325 -571
- package/dist/cjs/sui/constants.js +19 -20
- package/dist/cjs/sui/types.js +17 -51
- package/dist/cjs/sui/utils.js +7 -7
- package/dist/cjs/sui/wallet.js +13 -63
- package/dist/esm/__tests__/aptos/streamClient.spec.js +1 -1
- package/dist/esm/aptos/index.d.ts +3 -3
- package/dist/esm/aptos/index.js +3 -3
- package/dist/esm/common/GenericStreamClient.d.ts +4 -4
- package/dist/esm/common/GenericStreamClient.js +4 -4
- package/dist/esm/common/types.d.ts +1 -1
- package/dist/esm/index.d.ts +9 -9
- package/dist/esm/index.js +9 -9
- package/package.json +4 -4
|
@@ -1,482 +1,225 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
39
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
-
function step(op) {
|
|
42
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
|
-
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;
|
|
45
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
-
switch (op[0]) {
|
|
47
|
-
case 0: case 1: t = op; break;
|
|
48
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
-
default:
|
|
52
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
-
if (t[2]) _.ops.pop();
|
|
57
|
-
_.trys.pop(); continue;
|
|
58
|
-
}
|
|
59
|
-
op = body.call(thisArg, _);
|
|
60
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
65
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
66
|
-
if (!m) return o;
|
|
67
|
-
var i = m.call(o), r, ar = [], e;
|
|
68
|
-
try {
|
|
69
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
70
|
-
}
|
|
71
|
-
catch (error) { e = { error: error }; }
|
|
72
|
-
finally {
|
|
73
|
-
try {
|
|
74
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
75
|
-
}
|
|
76
|
-
finally { if (e) throw e.error; }
|
|
77
|
-
}
|
|
78
|
-
return ar;
|
|
79
|
-
};
|
|
80
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
81
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
82
|
-
if (ar || !(i in from)) {
|
|
83
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
84
|
-
ar[i] = from[i];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
88
|
-
};
|
|
89
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
90
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
91
4
|
};
|
|
92
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (cluster === void 0) { cluster = types_js_1.ICluster.Mainnet; }
|
|
108
|
-
var _this = _super.call(this) || this;
|
|
6
|
+
const ethers_1 = require("ethers");
|
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
+
const ethereum_checksum_address_1 = require("ethereum-checksum-address");
|
|
9
|
+
const BaseStreamClient_js_1 = require("../common/BaseStreamClient.js");
|
|
10
|
+
const types_js_1 = require("../common/types.js");
|
|
11
|
+
const constants_js_1 = require("./constants.js");
|
|
12
|
+
const abi_js_1 = __importDefault(require("./abi.js"));
|
|
13
|
+
const ercAbi_js_1 = __importDefault(require("./ercAbi.js"));
|
|
14
|
+
const constants_js_2 = require("../common/constants.js");
|
|
15
|
+
const types_js_2 = require("./types.js");
|
|
16
|
+
const utils_js_1 = require("./utils.js");
|
|
17
|
+
class EvmStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
|
|
18
|
+
constructor(clusterUrl, chain, signer, cluster = types_js_1.ICluster.Mainnet, programId) {
|
|
19
|
+
super();
|
|
109
20
|
if (chain !== types_js_1.IChain.Ethereum && chain !== types_js_1.IChain.BNB && chain !== types_js_1.IChain.Polygon) {
|
|
110
21
|
throw new Error("Wrong chain. Supported chains are Ethereum , BNB and Polygon!");
|
|
111
22
|
}
|
|
112
23
|
if (programId) {
|
|
113
|
-
|
|
24
|
+
this.programId = programId;
|
|
114
25
|
}
|
|
115
26
|
else {
|
|
116
27
|
switch (chain) {
|
|
117
28
|
case types_js_1.IChain.Ethereum:
|
|
118
|
-
|
|
29
|
+
this.programId = constants_js_1.ETHEREUM_PROGRAM_IDS[cluster];
|
|
119
30
|
break;
|
|
120
31
|
case types_js_1.IChain.BNB:
|
|
121
|
-
|
|
32
|
+
this.programId = constants_js_1.BNB_PROGRAM_IDS[cluster];
|
|
122
33
|
break;
|
|
123
34
|
case types_js_1.IChain.Polygon:
|
|
124
|
-
|
|
35
|
+
this.programId = constants_js_1.POLYGON_PROGRAM_IDS[cluster];
|
|
125
36
|
break;
|
|
126
37
|
}
|
|
127
38
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return _this;
|
|
39
|
+
this.provider = new ethers_1.ethers.providers.JsonRpcProvider(clusterUrl);
|
|
40
|
+
this.signer = signer;
|
|
41
|
+
const baseContract = new ethers_1.ethers.Contract(this.programId, abi_js_1.default);
|
|
42
|
+
this.readContract = baseContract.connect(this.provider);
|
|
43
|
+
this.writeContract = baseContract.connect(this.signer);
|
|
134
44
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return [4 /*yield*/, this.generateMultiPayloads(multiParams)];
|
|
147
|
-
case 2:
|
|
148
|
-
args = (_c.sent())[0];
|
|
149
|
-
sum = streamData.amount.times((0, bignumber_js_1.default)(constants_js_2.BASE_FEE)).div((0, bignumber_js_1.default)(1000000));
|
|
150
|
-
return [4 /*yield*/, this.approveTokens(streamData.tokenId, sum)];
|
|
151
|
-
case 3:
|
|
152
|
-
_c.sent();
|
|
153
|
-
return [4 /*yield*/, (_b = this.writeContract).create.apply(_b, __spreadArray(__spreadArray([], __read(args), false), [{
|
|
154
|
-
value: fees.value,
|
|
155
|
-
}], false))];
|
|
156
|
-
case 4:
|
|
157
|
-
result = _c.sent();
|
|
158
|
-
return [4 /*yield*/, result.wait()];
|
|
159
|
-
case 5:
|
|
160
|
-
confirmedTx = _c.sent();
|
|
161
|
-
metadataId = this.formatMetadataId(confirmedTx.events.find(function (item) { return item.event === "ContractCreated"; }).args[0]);
|
|
162
|
-
return [2 /*return*/, {
|
|
163
|
-
ixs: [],
|
|
164
|
-
txId: result.hash,
|
|
165
|
-
metadataId: metadataId,
|
|
166
|
-
}];
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
EvmStreamClient.prototype.createMultiple = function (multipleStreamData) {
|
|
172
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
173
|
-
var fees, args, sum, creationPromises, signatures, results, confirmations, successes, metadatas, metadataToRecipient, failures;
|
|
174
|
-
var _a;
|
|
175
|
-
var _this = this;
|
|
176
|
-
return __generator(this, function (_b) {
|
|
177
|
-
switch (_b.label) {
|
|
178
|
-
case 0: return [4 /*yield*/, (_a = this.readContract).getWithdrawalFees.apply(_a, __spreadArray([], __read(this.getFeeParams(multipleStreamData)), false))];
|
|
179
|
-
case 1:
|
|
180
|
-
fees = _b.sent();
|
|
181
|
-
return [4 /*yield*/, this.generateMultiPayloads(multipleStreamData)];
|
|
182
|
-
case 2:
|
|
183
|
-
args = _b.sent();
|
|
184
|
-
sum = multipleStreamData.recipients
|
|
185
|
-
.reduce(function (acc, v) { return acc.plus(v.amount); }, (0, bignumber_js_1.default)(0))
|
|
186
|
-
.times((0, bignumber_js_1.default)(constants_js_2.BASE_FEE))
|
|
187
|
-
.div((0, bignumber_js_1.default)(1000000));
|
|
188
|
-
return [4 /*yield*/, this.approveTokens(multipleStreamData.tokenId, sum)];
|
|
189
|
-
case 3:
|
|
190
|
-
_b.sent();
|
|
191
|
-
creationPromises = args.map(function (item) {
|
|
192
|
-
var _a;
|
|
193
|
-
return (_a = _this.writeContract).create.apply(_a, __spreadArray(__spreadArray([], __read(item), false), [{ value: fees.value }], false));
|
|
194
|
-
});
|
|
195
|
-
signatures = [];
|
|
196
|
-
return [4 /*yield*/, Promise.all(creationPromises)];
|
|
197
|
-
case 4:
|
|
198
|
-
results = _b.sent();
|
|
199
|
-
return [4 /*yield*/, Promise.allSettled(results.map(function (result) { return result.wait(); }))];
|
|
200
|
-
case 5:
|
|
201
|
-
confirmations = _b.sent();
|
|
202
|
-
successes = confirmations
|
|
203
|
-
.filter(function (el) { return el.status === "fulfilled"; })
|
|
204
|
-
.map(function (el) { return el.value; });
|
|
205
|
-
signatures.push.apply(signatures, __spreadArray([], __read(successes.map(function (el) { return el.hash; })), false));
|
|
206
|
-
metadatas = confirmations.map(function (result) {
|
|
207
|
-
return result.status === "fulfilled"
|
|
208
|
-
? _this.formatMetadataId(result.value.events.find(function (item) { return item.event === "ContractCreated"; }).args[0])
|
|
209
|
-
: null;
|
|
210
|
-
});
|
|
211
|
-
metadataToRecipient = metadatas.reduce(function (acc, value, index) {
|
|
212
|
-
if (value) {
|
|
213
|
-
acc[value] = multipleStreamData.recipients[index];
|
|
214
|
-
}
|
|
215
|
-
return acc;
|
|
216
|
-
}, {});
|
|
217
|
-
failures = confirmations
|
|
218
|
-
.filter(function (el) { return el.status === "rejected"; })
|
|
219
|
-
.map(function (el) { return el.reason; });
|
|
220
|
-
return [2 /*return*/, {
|
|
221
|
-
txs: signatures,
|
|
222
|
-
metadatas: metadatas.filter(Boolean),
|
|
223
|
-
metadataToRecipient: metadataToRecipient,
|
|
224
|
-
errors: failures,
|
|
225
|
-
}];
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
};
|
|
230
|
-
EvmStreamClient.prototype.withdraw = function (_a) {
|
|
231
|
-
var id = _a.id, _b = _a.amount, amount = _b === void 0 ? constants_js_2.WITHDRAW_AVAILABLE_AMOUNT : _b;
|
|
232
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
233
|
-
var result;
|
|
234
|
-
return __generator(this, function (_c) {
|
|
235
|
-
switch (_c.label) {
|
|
236
|
-
case 0: return [4 /*yield*/, this.writeContract.withdraw(id, amount.toString())];
|
|
237
|
-
case 1:
|
|
238
|
-
result = _c.sent();
|
|
239
|
-
return [2 /*return*/, {
|
|
240
|
-
ixs: [],
|
|
241
|
-
txId: result.hash,
|
|
242
|
-
}];
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
};
|
|
247
|
-
EvmStreamClient.prototype.cancel = function (cancelData) {
|
|
248
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
249
|
-
var result;
|
|
250
|
-
return __generator(this, function (_a) {
|
|
251
|
-
switch (_a.label) {
|
|
252
|
-
case 0: return [4 /*yield*/, this.writeContract.cancel(cancelData.id)];
|
|
253
|
-
case 1:
|
|
254
|
-
result = _a.sent();
|
|
255
|
-
return [2 /*return*/, {
|
|
256
|
-
ixs: [],
|
|
257
|
-
txId: result.hash,
|
|
258
|
-
}];
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
};
|
|
263
|
-
EvmStreamClient.prototype.transfer = function (transferData) {
|
|
264
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
265
|
-
var result;
|
|
266
|
-
return __generator(this, function (_a) {
|
|
267
|
-
switch (_a.label) {
|
|
268
|
-
case 0: return [4 /*yield*/, this.writeContract.transfer(transferData.id, transferData.newRecipient)];
|
|
269
|
-
case 1:
|
|
270
|
-
result = _a.sent();
|
|
271
|
-
return [2 /*return*/, {
|
|
272
|
-
ixs: [],
|
|
273
|
-
txId: result.hash,
|
|
274
|
-
}];
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
};
|
|
279
|
-
EvmStreamClient.prototype.topup = function (topupData) {
|
|
280
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
281
|
-
var fees, sum, stream, result;
|
|
282
|
-
return __generator(this, function (_a) {
|
|
283
|
-
switch (_a.label) {
|
|
284
|
-
case 0: return [4 /*yield*/, this.readContract.getTopUpWithdrawalFees(topupData.id, topupData.amount.toString())];
|
|
285
|
-
case 1:
|
|
286
|
-
fees = _a.sent();
|
|
287
|
-
sum = topupData.amount.times((0, bignumber_js_1.default)(constants_js_2.BASE_FEE)).div((0, bignumber_js_1.default)(1000000));
|
|
288
|
-
return [4 /*yield*/, this.getOne({ id: topupData.id })];
|
|
289
|
-
case 2:
|
|
290
|
-
stream = _a.sent();
|
|
291
|
-
return [4 /*yield*/, this.approveTokens(stream.mint, sum)];
|
|
292
|
-
case 3:
|
|
293
|
-
_a.sent();
|
|
294
|
-
return [4 /*yield*/, this.writeContract.topUp(topupData.id, topupData.amount.toString(), {
|
|
295
|
-
value: fees.value,
|
|
296
|
-
})];
|
|
297
|
-
case 4:
|
|
298
|
-
result = _a.sent();
|
|
299
|
-
return [2 /*return*/, {
|
|
300
|
-
ixs: [],
|
|
301
|
-
txId: result.hash,
|
|
302
|
-
}];
|
|
303
|
-
}
|
|
304
|
-
});
|
|
45
|
+
async create(streamData) {
|
|
46
|
+
const multiParams = {
|
|
47
|
+
...streamData,
|
|
48
|
+
recipients: [streamData],
|
|
49
|
+
};
|
|
50
|
+
const fees = await this.readContract.getWithdrawalFees(...this.getFeeParams(multiParams));
|
|
51
|
+
const args = (await this.generateMultiPayloads(multiParams))[0];
|
|
52
|
+
const sum = streamData.amount.times((0, bignumber_js_1.default)(constants_js_2.BASE_FEE)).div((0, bignumber_js_1.default)(1000000));
|
|
53
|
+
await this.approveTokens(streamData.tokenId, sum);
|
|
54
|
+
const result = await this.writeContract.create(...args, {
|
|
55
|
+
value: fees.value,
|
|
305
56
|
});
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
var address = _a.address;
|
|
378
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
379
|
-
var fees;
|
|
380
|
-
return __generator(this, function (_b) {
|
|
381
|
-
switch (_b.label) {
|
|
382
|
-
case 0: return [4 /*yield*/, this.readContract.getFees(address)];
|
|
383
|
-
case 1:
|
|
384
|
-
fees = _b.sent();
|
|
385
|
-
if (!fees.exists) {
|
|
386
|
-
return [2 /*return*/, null];
|
|
387
|
-
}
|
|
388
|
-
return [2 /*return*/, {
|
|
389
|
-
streamflowFee: fees.streamflow_fee.toNumber() / 100,
|
|
390
|
-
partnerFee: fees.partner_fee.toNumber() / 100,
|
|
391
|
-
}];
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
});
|
|
395
|
-
};
|
|
396
|
-
EvmStreamClient.prototype.getDefaultStreamflowFee = function () {
|
|
397
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
398
|
-
var fee;
|
|
399
|
-
return __generator(this, function (_a) {
|
|
400
|
-
switch (_a.label) {
|
|
401
|
-
case 0: return [4 /*yield*/, this.readContract.getStreamflowFees()];
|
|
402
|
-
case 1:
|
|
403
|
-
fee = _a.sent();
|
|
404
|
-
return [2 /*return*/, fee.toNumber() / 100];
|
|
405
|
-
}
|
|
406
|
-
});
|
|
57
|
+
const confirmedTx = await result.wait();
|
|
58
|
+
const metadataId = this.formatMetadataId(confirmedTx.events.find((item) => item.event === "ContractCreated").args[0]);
|
|
59
|
+
return {
|
|
60
|
+
ixs: [],
|
|
61
|
+
txId: result.hash,
|
|
62
|
+
metadataId,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async createMultiple(multipleStreamData) {
|
|
66
|
+
const fees = await this.readContract.getWithdrawalFees(...this.getFeeParams(multipleStreamData));
|
|
67
|
+
const args = await this.generateMultiPayloads(multipleStreamData);
|
|
68
|
+
const sum = multipleStreamData.recipients
|
|
69
|
+
.reduce((acc, v) => acc.plus(v.amount), (0, bignumber_js_1.default)(0))
|
|
70
|
+
.times((0, bignumber_js_1.default)(constants_js_2.BASE_FEE))
|
|
71
|
+
.div((0, bignumber_js_1.default)(1000000));
|
|
72
|
+
await this.approveTokens(multipleStreamData.tokenId, sum);
|
|
73
|
+
const creationPromises = args.map((item) => this.writeContract.create(...item, { value: fees.value }));
|
|
74
|
+
const signatures = [];
|
|
75
|
+
const results = await Promise.all(creationPromises);
|
|
76
|
+
const confirmations = await Promise.allSettled(results.map((result) => result.wait()));
|
|
77
|
+
const successes = confirmations
|
|
78
|
+
.filter((el) => el.status === "fulfilled")
|
|
79
|
+
.map((el) => el.value);
|
|
80
|
+
signatures.push(...successes.map((el) => el.hash));
|
|
81
|
+
const metadatas = confirmations.map((result) => result.status === "fulfilled"
|
|
82
|
+
? this.formatMetadataId(result.value.events.find((item) => item.event === "ContractCreated").args[0])
|
|
83
|
+
: null);
|
|
84
|
+
const metadataToRecipient = metadatas.reduce((acc, value, index) => {
|
|
85
|
+
if (value) {
|
|
86
|
+
acc[value] = multipleStreamData.recipients[index];
|
|
87
|
+
}
|
|
88
|
+
return acc;
|
|
89
|
+
}, {});
|
|
90
|
+
const failures = confirmations
|
|
91
|
+
.filter((el) => el.status === "rejected")
|
|
92
|
+
.map((el) => el.reason);
|
|
93
|
+
return {
|
|
94
|
+
txs: signatures,
|
|
95
|
+
metadatas: metadatas.filter(Boolean),
|
|
96
|
+
metadataToRecipient,
|
|
97
|
+
errors: failures,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async withdraw({ id, amount = constants_js_2.WITHDRAW_AVAILABLE_AMOUNT }) {
|
|
101
|
+
const result = await this.writeContract.withdraw(id, amount.toString());
|
|
102
|
+
return {
|
|
103
|
+
ixs: [],
|
|
104
|
+
txId: result.hash,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async cancel(cancelData) {
|
|
108
|
+
const result = await this.writeContract.cancel(cancelData.id);
|
|
109
|
+
return {
|
|
110
|
+
ixs: [],
|
|
111
|
+
txId: result.hash,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async transfer(transferData) {
|
|
115
|
+
const result = await this.writeContract.transfer(transferData.id, transferData.newRecipient);
|
|
116
|
+
return {
|
|
117
|
+
ixs: [],
|
|
118
|
+
txId: result.hash,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
async topup(topupData) {
|
|
122
|
+
const fees = await this.readContract.getTopUpWithdrawalFees(topupData.id, topupData.amount.toString());
|
|
123
|
+
const sum = topupData.amount.times((0, bignumber_js_1.default)(constants_js_2.BASE_FEE)).div((0, bignumber_js_1.default)(1000000));
|
|
124
|
+
const stream = await this.getOne({ id: topupData.id });
|
|
125
|
+
await this.approveTokens(stream.mint, sum);
|
|
126
|
+
const result = await this.writeContract.topUp(topupData.id, topupData.amount.toString(), {
|
|
127
|
+
value: fees.value,
|
|
407
128
|
});
|
|
408
|
-
|
|
129
|
+
return {
|
|
130
|
+
ixs: [],
|
|
131
|
+
txId: result.hash,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async update(updateData) {
|
|
135
|
+
const result = await this.writeContract.update(updateData.id, updateData.enableAutomaticWithdrawal ? [true] : [], updateData.withdrawFrequency ? [updateData.withdrawFrequency.toString()] : [], updateData.amountPerPeriod ? [updateData.amountPerPeriod.toString()] : []);
|
|
136
|
+
return {
|
|
137
|
+
ixs: [],
|
|
138
|
+
txId: result.hash,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
async getOne({ id }) {
|
|
142
|
+
const result = await this.readContract.getById(id);
|
|
143
|
+
return new types_js_2.EvmContract(result);
|
|
144
|
+
}
|
|
145
|
+
async get({ address,
|
|
146
|
+
// type = StreamType.All,
|
|
147
|
+
direction = types_js_1.StreamDirection.All, }) {
|
|
148
|
+
const addresses = [];
|
|
149
|
+
if (direction === types_js_1.StreamDirection.All || direction === types_js_1.StreamDirection.Outgoing) {
|
|
150
|
+
const senderAddresses = await this.readContract.getBySender(address);
|
|
151
|
+
addresses.push(...senderAddresses);
|
|
152
|
+
}
|
|
153
|
+
if (direction === types_js_1.StreamDirection.All || direction === types_js_1.StreamDirection.Incoming) {
|
|
154
|
+
const recipientAddresses = await this.readContract.getByRecipient(address);
|
|
155
|
+
addresses.push(...recipientAddresses);
|
|
156
|
+
}
|
|
157
|
+
const uniqueAddresses = [...new Set(addresses)];
|
|
158
|
+
const results = await this.readContract.getMultiple(uniqueAddresses);
|
|
159
|
+
return results.map((result, index) => [uniqueAddresses[index], new types_js_2.EvmContract(result)]);
|
|
160
|
+
}
|
|
161
|
+
extractErrorCode(err) {
|
|
162
|
+
return (0, utils_js_1.extractEvmErrorCode)(err.toString() ?? "Unknown error!");
|
|
163
|
+
}
|
|
164
|
+
async getFees({ address }) {
|
|
165
|
+
const fees = await this.readContract.getFees(address);
|
|
166
|
+
if (!fees.exists) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
streamflowFee: fees.streamflow_fee.toNumber() / 100,
|
|
171
|
+
partnerFee: fees.partner_fee.toNumber() / 100,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
async getDefaultStreamflowFee() {
|
|
175
|
+
const fee = await this.readContract.getStreamflowFees();
|
|
176
|
+
return fee.toNumber() / 100;
|
|
177
|
+
}
|
|
409
178
|
/**
|
|
410
179
|
* Returns StreamClient protocol program ID.
|
|
411
180
|
*/
|
|
412
|
-
|
|
181
|
+
getProgramId() {
|
|
413
182
|
return this.programId;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
return [4 /*yield*/, tokenContract.allowance(address, this.programId)];
|
|
426
|
-
case 2:
|
|
427
|
-
allowance = _a.sent();
|
|
428
|
-
if ((0, bignumber_js_1.default)(allowance.toString()).gte(amount)) {
|
|
429
|
-
return [2 /*return*/];
|
|
430
|
-
}
|
|
431
|
-
return [4 /*yield*/, tokenContract.approve(this.programId, amount.toString())];
|
|
432
|
-
case 3:
|
|
433
|
-
approvalTx = _a.sent();
|
|
434
|
-
return [4 /*yield*/, approvalTx.wait()];
|
|
435
|
-
case 4:
|
|
436
|
-
_a.sent();
|
|
437
|
-
return [2 /*return*/];
|
|
438
|
-
}
|
|
439
|
-
});
|
|
440
|
-
});
|
|
441
|
-
};
|
|
183
|
+
}
|
|
184
|
+
async approveTokens(tokenId, amount) {
|
|
185
|
+
const tokenContract = new ethers_1.ethers.Contract(tokenId, ercAbi_js_1.default, this.signer);
|
|
186
|
+
const address = await this.signer.getAddress();
|
|
187
|
+
const allowance = await tokenContract.allowance(address, this.programId);
|
|
188
|
+
if ((0, bignumber_js_1.default)(allowance.toString()).gte(amount)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const approvalTx = await tokenContract.approve(this.programId, amount.toString());
|
|
192
|
+
await approvalTx.wait();
|
|
193
|
+
}
|
|
442
194
|
// Utility function to prepare transaction payloads for multiple recipients.
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
return
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
multipleStreamData.withdrawalFrequency || 0,
|
|
469
|
-
recipient.name,
|
|
470
|
-
],
|
|
471
|
-
recipient.recipient,
|
|
472
|
-
multipleStreamData.partner || sender,
|
|
473
|
-
];
|
|
474
|
-
})];
|
|
475
|
-
}
|
|
476
|
-
});
|
|
195
|
+
async generateMultiPayloads(multipleStreamData) {
|
|
196
|
+
const sender = await this.signer.getAddress();
|
|
197
|
+
return multipleStreamData.recipients.map((recipient) => {
|
|
198
|
+
return [
|
|
199
|
+
multipleStreamData.tokenId,
|
|
200
|
+
recipient.amount.toString(),
|
|
201
|
+
multipleStreamData.period,
|
|
202
|
+
recipient.amountPerPeriod.toString(),
|
|
203
|
+
multipleStreamData.start,
|
|
204
|
+
recipient.cliffAmount.toString(),
|
|
205
|
+
[
|
|
206
|
+
multipleStreamData.cancelableBySender,
|
|
207
|
+
multipleStreamData.cancelableByRecipient,
|
|
208
|
+
multipleStreamData.transferableBySender,
|
|
209
|
+
multipleStreamData.transferableByRecipient,
|
|
210
|
+
multipleStreamData.canTopup,
|
|
211
|
+
multipleStreamData.canPause || false,
|
|
212
|
+
multipleStreamData.canUpdateRate || false,
|
|
213
|
+
multipleStreamData.automaticWithdrawal || false,
|
|
214
|
+
multipleStreamData.withdrawalFrequency || 0,
|
|
215
|
+
recipient.name,
|
|
216
|
+
],
|
|
217
|
+
recipient.recipient,
|
|
218
|
+
multipleStreamData.partner || sender,
|
|
219
|
+
];
|
|
477
220
|
});
|
|
478
|
-
}
|
|
479
|
-
|
|
221
|
+
}
|
|
222
|
+
getFeeParams(multipleStreamData) {
|
|
480
223
|
return [
|
|
481
224
|
multipleStreamData.recipients[0].amount.toString(),
|
|
482
225
|
multipleStreamData.period,
|
|
@@ -486,11 +229,10 @@ var EvmStreamClient = /** @class */ (function (_super) {
|
|
|
486
229
|
multipleStreamData.automaticWithdrawal || false,
|
|
487
230
|
multipleStreamData.withdrawalFrequency || 0,
|
|
488
231
|
];
|
|
489
|
-
}
|
|
490
|
-
|
|
232
|
+
}
|
|
233
|
+
formatMetadataId(id) {
|
|
491
234
|
// 40 chars is ethereum address size
|
|
492
|
-
return (0, ethereum_checksum_address_1.toChecksumAddress)(
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
}(BaseStreamClient_js_1.BaseStreamClient));
|
|
235
|
+
return (0, ethereum_checksum_address_1.toChecksumAddress)(`0x${id.slice(-40)}`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
496
238
|
exports.default = EvmStreamClient;
|