@solana-mobile/dapp-store-cli 0.16.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/bin/dapp-store.js +3 -1
  2. package/lib/CliSetup.js +304 -505
  3. package/lib/CliUtils.js +6 -376
  4. package/lib/__tests__/CliSetupTest.js +484 -74
  5. package/lib/cli/__tests__/parseErrors.test.js +25 -0
  6. package/lib/cli/__tests__/signer.test.js +436 -0
  7. package/lib/cli/constants.js +23 -0
  8. package/lib/cli/messages.js +21 -0
  9. package/lib/cli/parseErrors.js +41 -0
  10. package/lib/{commands/publish/PublishCliSupport.js → cli/selfUpdate.js} +72 -38
  11. package/lib/{commands/publish/PublishCliRemove.js → cli/signer.js} +35 -56
  12. package/lib/index.js +96 -5
  13. package/lib/package.json +5 -24
  14. package/lib/portal/__tests__/releaseMetadata.test.js +647 -0
  15. package/lib/portal/__tests__/translators.test.js +76 -0
  16. package/lib/portal/__tests__/workflowClient.test.js +457 -0
  17. package/lib/portal/attestationClient.js +143 -0
  18. package/lib/portal/files.js +64 -0
  19. package/lib/portal/http.js +364 -0
  20. package/lib/portal/records.js +64 -0
  21. package/lib/portal/releaseMetadata.js +748 -0
  22. package/lib/portal/translators.js +460 -0
  23. package/lib/portal/types.js +1 -0
  24. package/lib/portal/workflowClient.js +704 -0
  25. package/lib/publication/PublicationProgressReporter.js +1051 -0
  26. package/lib/publication/__tests__/PublicationProgressReporter.test.js +174 -0
  27. package/lib/{commands/ValidateCommand.js → publication/__tests__/fundingPreflight.test.js} +90 -66
  28. package/lib/publication/__tests__/publicationSummary.test.js +26 -0
  29. package/lib/publication/cliValidation.js +482 -0
  30. package/lib/publication/fundingPreflight.js +246 -0
  31. package/lib/publication/publicationSummary.js +99 -0
  32. package/lib/{commands/utils.js → publication/runPublicationWorkflow.js} +16 -46
  33. package/package.json +5 -24
  34. package/src/CliSetup.ts +370 -505
  35. package/src/CliUtils.ts +9 -233
  36. package/src/__tests__/CliSetupTest.ts +272 -120
  37. package/src/cli/__tests__/parseErrors.test.ts +34 -0
  38. package/src/cli/__tests__/signer.test.ts +359 -0
  39. package/src/cli/constants.ts +3 -0
  40. package/src/cli/messages.ts +27 -0
  41. package/src/cli/parseErrors.ts +62 -0
  42. package/src/cli/selfUpdate.ts +59 -0
  43. package/src/cli/signer.ts +38 -0
  44. package/src/index.ts +31 -4
  45. package/src/portal/__tests__/releaseMetadata.test.ts +508 -0
  46. package/src/portal/__tests__/translators.test.ts +82 -0
  47. package/src/portal/__tests__/workflowClient.test.ts +278 -0
  48. package/src/portal/attestationClient.ts +19 -0
  49. package/src/portal/files.ts +73 -0
  50. package/src/portal/http.ts +170 -0
  51. package/src/portal/records.ts +38 -0
  52. package/src/portal/releaseMetadata.ts +489 -0
  53. package/src/portal/translators.ts +750 -0
  54. package/src/portal/types.ts +27 -0
  55. package/src/portal/workflowClient.ts +575 -0
  56. package/src/publication/PublicationProgressReporter.ts +1026 -0
  57. package/src/publication/__tests__/PublicationProgressReporter.test.ts +210 -0
  58. package/src/publication/__tests__/fundingPreflight.test.ts +78 -0
  59. package/src/publication/__tests__/publicationSummary.test.ts +30 -0
  60. package/src/publication/cliValidation.ts +264 -0
  61. package/src/publication/fundingPreflight.ts +123 -0
  62. package/src/publication/publicationSummary.ts +26 -0
  63. package/src/publication/runPublicationWorkflow.ts +46 -0
  64. package/lib/commands/create/CreateCliApp.js +0 -223
  65. package/lib/commands/create/CreateCliRelease.js +0 -290
  66. package/lib/commands/create/index.js +0 -40
  67. package/lib/commands/index.js +0 -3
  68. package/lib/commands/publish/PublishCliSubmit.js +0 -208
  69. package/lib/commands/publish/PublishCliUpdate.js +0 -211
  70. package/lib/commands/publish/index.js +0 -22
  71. package/lib/commands/scaffolding/ScaffoldInit.js +0 -15
  72. package/lib/commands/scaffolding/index.js +0 -1
  73. package/lib/config/EnvVariables.js +0 -59
  74. package/lib/config/PublishDetails.js +0 -915
  75. package/lib/config/S3StorageManager.js +0 -93
  76. package/lib/config/index.js +0 -2
  77. package/lib/generated/config_obj.json +0 -1
  78. package/lib/generated/config_schema.json +0 -1
  79. package/lib/prebuild_schema/publishing_source.yaml +0 -64
  80. package/lib/prebuild_schema/schemagen.js +0 -25
  81. package/lib/upload/CachedStorageDriver.js +0 -458
  82. package/lib/upload/TurboStorageDriver.js +0 -718
  83. package/lib/upload/__tests__/CachedStorageDriver.test.js +0 -437
  84. package/lib/upload/__tests__/TurboStorageDriver.test.js +0 -17
  85. package/lib/upload/__tests__/contentGateway.test.js +0 -17
  86. package/lib/upload/contentGateway.js +0 -23
  87. package/lib/upload/index.js +0 -2
  88. package/src/commands/ValidateCommand.ts +0 -82
  89. package/src/commands/create/CreateCliApp.ts +0 -93
  90. package/src/commands/create/CreateCliRelease.ts +0 -149
  91. package/src/commands/create/index.ts +0 -47
  92. package/src/commands/index.ts +0 -3
  93. package/src/commands/publish/PublishCliRemove.ts +0 -66
  94. package/src/commands/publish/PublishCliSubmit.ts +0 -93
  95. package/src/commands/publish/PublishCliSupport.ts +0 -66
  96. package/src/commands/publish/PublishCliUpdate.ts +0 -101
  97. package/src/commands/publish/index.ts +0 -29
  98. package/src/commands/scaffolding/ScaffoldInit.ts +0 -20
  99. package/src/commands/scaffolding/index.ts +0 -1
  100. package/src/commands/utils.ts +0 -33
  101. package/src/config/EnvVariables.ts +0 -39
  102. package/src/config/PublishDetails.ts +0 -456
  103. package/src/config/S3StorageManager.ts +0 -47
  104. package/src/config/index.ts +0 -2
  105. package/src/prebuild_schema/publishing_source.yaml +0 -64
  106. package/src/prebuild_schema/schemagen.js +0 -31
  107. package/src/upload/CachedStorageDriver.ts +0 -179
  108. package/src/upload/TurboStorageDriver.ts +0 -283
  109. package/src/upload/__tests__/CachedStorageDriver.test.ts +0 -246
  110. package/src/upload/__tests__/TurboStorageDriver.test.ts +0 -15
  111. package/src/upload/__tests__/contentGateway.test.ts +0 -31
  112. package/src/upload/contentGateway.ts +0 -37
  113. package/src/upload/index.ts +0 -2
@@ -1,718 +0,0 @@
1
- function _array_like_to_array(arr, len) {
2
- if (len == null || len > arr.length) len = arr.length;
3
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
- return arr2;
5
- }
6
- function _array_with_holes(arr) {
7
- if (Array.isArray(arr)) return arr;
8
- }
9
- function _array_without_holes(arr) {
10
- if (Array.isArray(arr)) return _array_like_to_array(arr);
11
- }
12
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
13
- try {
14
- var info = gen[key](arg);
15
- var value = info.value;
16
- } catch (error) {
17
- reject(error);
18
- return;
19
- }
20
- if (info.done) {
21
- resolve(value);
22
- } else {
23
- Promise.resolve(value).then(_next, _throw);
24
- }
25
- }
26
- function _async_to_generator(fn) {
27
- return function() {
28
- var self = this, args = arguments;
29
- return new Promise(function(resolve, reject) {
30
- var gen = fn.apply(self, args);
31
- function _next(value) {
32
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
33
- }
34
- function _throw(err) {
35
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
36
- }
37
- _next(undefined);
38
- });
39
- };
40
- }
41
- function _class_call_check(instance, Constructor) {
42
- if (!(instance instanceof Constructor)) {
43
- throw new TypeError("Cannot call a class as a function");
44
- }
45
- }
46
- function _defineProperties(target, props) {
47
- for(var i = 0; i < props.length; i++){
48
- var descriptor = props[i];
49
- descriptor.enumerable = descriptor.enumerable || false;
50
- descriptor.configurable = true;
51
- if ("value" in descriptor) descriptor.writable = true;
52
- Object.defineProperty(target, descriptor.key, descriptor);
53
- }
54
- }
55
- function _create_class(Constructor, protoProps, staticProps) {
56
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
57
- if (staticProps) _defineProperties(Constructor, staticProps);
58
- return Constructor;
59
- }
60
- function _define_property(obj, key, value) {
61
- if (key in obj) {
62
- Object.defineProperty(obj, key, {
63
- value: value,
64
- enumerable: true,
65
- configurable: true,
66
- writable: true
67
- });
68
- } else {
69
- obj[key] = value;
70
- }
71
- return obj;
72
- }
73
- function _instanceof(left, right) {
74
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
75
- return !!right[Symbol.hasInstance](left);
76
- } else {
77
- return left instanceof right;
78
- }
79
- }
80
- function _iterable_to_array(iter) {
81
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
82
- }
83
- function _iterable_to_array_limit(arr, i) {
84
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
85
- if (_i == null) return;
86
- var _arr = [];
87
- var _n = true;
88
- var _d = false;
89
- var _s, _e;
90
- try {
91
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
92
- _arr.push(_s.value);
93
- if (i && _arr.length === i) break;
94
- }
95
- } catch (err) {
96
- _d = true;
97
- _e = err;
98
- } finally{
99
- try {
100
- if (!_n && _i["return"] != null) _i["return"]();
101
- } finally{
102
- if (_d) throw _e;
103
- }
104
- }
105
- return _arr;
106
- }
107
- function _non_iterable_rest() {
108
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
109
- }
110
- function _non_iterable_spread() {
111
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
112
- }
113
- function _object_spread(target) {
114
- for(var i = 1; i < arguments.length; i++){
115
- var source = arguments[i] != null ? arguments[i] : {};
116
- var ownKeys = Object.keys(source);
117
- if (typeof Object.getOwnPropertySymbols === "function") {
118
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
119
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
120
- }));
121
- }
122
- ownKeys.forEach(function(key) {
123
- _define_property(target, key, source[key]);
124
- });
125
- }
126
- return target;
127
- }
128
- function _sliced_to_array(arr, i) {
129
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
130
- }
131
- function _to_consumable_array(arr) {
132
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
133
- }
134
- function _unsupported_iterable_to_array(o, minLen) {
135
- if (!o) return;
136
- if (typeof o === "string") return _array_like_to_array(o, minLen);
137
- var n = Object.prototype.toString.call(o).slice(8, -1);
138
- if (n === "Object" && o.constructor) n = o.constructor.name;
139
- if (n === "Map" || n === "Set") return Array.from(n);
140
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
141
- }
142
- function _ts_generator(thisArg, body) {
143
- var f, y, t, _ = {
144
- label: 0,
145
- sent: function() {
146
- if (t[0] & 1) throw t[1];
147
- return t[1];
148
- },
149
- trys: [],
150
- ops: []
151
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
152
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
153
- return this;
154
- }), g;
155
- function verb(n) {
156
- return function(v) {
157
- return step([
158
- n,
159
- v
160
- ]);
161
- };
162
- }
163
- function step(op) {
164
- if (f) throw new TypeError("Generator is already executing.");
165
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
166
- 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;
167
- if (y = 0, t) op = [
168
- op[0] & 2,
169
- t.value
170
- ];
171
- switch(op[0]){
172
- case 0:
173
- case 1:
174
- t = op;
175
- break;
176
- case 4:
177
- _.label++;
178
- return {
179
- value: op[1],
180
- done: false
181
- };
182
- case 5:
183
- _.label++;
184
- y = op[1];
185
- op = [
186
- 0
187
- ];
188
- continue;
189
- case 7:
190
- op = _.ops.pop();
191
- _.trys.pop();
192
- continue;
193
- default:
194
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
195
- _ = 0;
196
- continue;
197
- }
198
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
199
- _.label = op[1];
200
- break;
201
- }
202
- if (op[0] === 6 && _.label < t[1]) {
203
- _.label = t[1];
204
- t = op;
205
- break;
206
- }
207
- if (t && _.label < t[2]) {
208
- _.label = t[2];
209
- _.ops.push(op);
210
- break;
211
- }
212
- if (t[2]) _.ops.pop();
213
- _.trys.pop();
214
- continue;
215
- }
216
- op = body.call(thisArg, _);
217
- } catch (e) {
218
- op = [
219
- 6,
220
- e
221
- ];
222
- y = 0;
223
- } finally{
224
- f = t = 0;
225
- }
226
- if (op[0] & 5) throw op[1];
227
- return {
228
- value: op[0] ? op[1] : void 0,
229
- done: true
230
- };
231
- }
232
- }
233
- function _ts_values(o) {
234
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
235
- if (m) return m.call(o);
236
- if (o && typeof o.length === "number") return {
237
- next: function() {
238
- if (o && i >= o.length) o = void 0;
239
- return {
240
- value: o && o[i++],
241
- done: !o
242
- };
243
- }
244
- };
245
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
246
- }
247
- import { TurboFactory, lamportToTokenAmount } from "@ardrive/turbo-sdk";
248
- import bs58 from "bs58";
249
- import debugModule from "debug";
250
- import { buildPublicContentUrl } from "./contentGateway.js";
251
- var debug = debugModule("cli:turbo-storage");
252
- var SOL_IN_LAMPORTS = 1000000000;
253
- var MIN_TOP_UP_LAMPORTS = 1000000;
254
- var MIN_TOP_UP_SOL = MIN_TOP_UP_LAMPORTS / SOL_IN_LAMPORTS;
255
- var CONSTANTS = {
256
- FREE_UPLOAD_LIMIT: 97280,
257
- UPLOAD_DELAY_MS: 2000,
258
- MAX_RETRIES: 5,
259
- SOL_IN_LAMPORTS: SOL_IN_LAMPORTS,
260
- MIN_TOP_UP_SOL: MIN_TOP_UP_SOL,
261
- MIN_TOP_UP_LAMPORTS: MIN_TOP_UP_LAMPORTS,
262
- BACKOFF: {
263
- BASE_MS: 500,
264
- MAX_MS: 8000
265
- },
266
- SERVICE_URLS: {
267
- devnet: {
268
- gatewayUrl: "https://api.devnet.solana.com",
269
- uploadServiceConfig: {
270
- url: "https://upload.ardrive.dev"
271
- },
272
- paymentServiceConfig: {
273
- url: "https://payment.ardrive.dev"
274
- }
275
- }
276
- }
277
- };
278
- var delay = function(ms) {
279
- return new Promise(function(resolve) {
280
- return setTimeout(resolve, ms);
281
- });
282
- };
283
- export var TurboStorageDriver = /*#__PURE__*/ function() {
284
- "use strict";
285
- function TurboStorageDriver(keypair) {
286
- var network = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "mainnet", bufferPercentage = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 20;
287
- _class_call_check(this, TurboStorageDriver);
288
- _define_property(this, "turbo", void 0);
289
- _define_property(this, "bufferPercentage", void 0);
290
- _define_property(this, "network", void 0);
291
- _define_property(this, "uploadQueue", []);
292
- _define_property(this, "isProcessingQueue", false);
293
- this.network = network;
294
- this.bufferPercentage = bufferPercentage;
295
- this.turbo = TurboFactory.authenticated(_object_spread({
296
- privateKey: bs58.encode(keypair.secretKey),
297
- token: "solana"
298
- }, getTurboServiceConfig(network)));
299
- }
300
- _create_class(TurboStorageDriver, [
301
- {
302
- key: "getUploadPrice",
303
- value: function getUploadPrice(bytes) {
304
- return _async_to_generator(function() {
305
- var _ref, cost, base;
306
- return _ts_generator(this, function(_state) {
307
- switch(_state.label){
308
- case 0:
309
- if (bytes <= CONSTANTS.FREE_UPLOAD_LIMIT) return [
310
- 2,
311
- BigInt(0)
312
- ];
313
- return [
314
- 4,
315
- this.turbo.getUploadCosts({
316
- bytes: [
317
- bytes
318
- ]
319
- })
320
- ];
321
- case 1:
322
- _ref = _sliced_to_array.apply(void 0, [
323
- _state.sent(),
324
- 1
325
- ]), cost = _ref[0];
326
- base = BigInt(String(cost.winc));
327
- return [
328
- 2,
329
- base * BigInt(100 + this.bufferPercentage) / BigInt(100)
330
- ];
331
- }
332
- });
333
- }).call(this);
334
- }
335
- },
336
- {
337
- key: "withRetry",
338
- value: function withRetry(operation) {
339
- var isRetriable = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : function(msg) {
340
- return msg.includes("429") || msg.includes("Too Many Requests");
341
- };
342
- return _async_to_generator(function() {
343
- var retry, error, errorMessage, delayMs;
344
- return _ts_generator(this, function(_state) {
345
- switch(_state.label){
346
- case 0:
347
- retry = 0;
348
- _state.label = 1;
349
- case 1:
350
- if (!(retry <= CONSTANTS.MAX_RETRIES)) return [
351
- 3,
352
- 8
353
- ];
354
- _state.label = 2;
355
- case 2:
356
- _state.trys.push([
357
- 2,
358
- 4,
359
- ,
360
- 7
361
- ]);
362
- return [
363
- 4,
364
- operation()
365
- ];
366
- case 3:
367
- return [
368
- 2,
369
- _state.sent()
370
- ];
371
- case 4:
372
- error = _state.sent();
373
- errorMessage = _instanceof(error, Error) ? error.message : String(error);
374
- if (!(retry < CONSTANTS.MAX_RETRIES && isRetriable(errorMessage))) return [
375
- 3,
376
- 6
377
- ];
378
- delayMs = Math.min(CONSTANTS.BACKOFF.BASE_MS * Math.pow(2, retry), CONSTANTS.BACKOFF.MAX_MS);
379
- console.log("Rate limited, retrying after ".concat(delayMs, "ms (attempt ").concat(retry + 1, "/").concat(CONSTANTS.MAX_RETRIES, ")..."));
380
- return [
381
- 4,
382
- delay(delayMs)
383
- ];
384
- case 5:
385
- _state.sent();
386
- return [
387
- 3,
388
- 7
389
- ];
390
- case 6:
391
- throw error;
392
- case 7:
393
- retry++;
394
- return [
395
- 3,
396
- 1
397
- ];
398
- case 8:
399
- throw new Error("Max retries exceeded");
400
- }
401
- });
402
- })();
403
- }
404
- },
405
- {
406
- key: "formatInsufficientFundsError",
407
- value: function formatInsufficientFundsError(errorMessage) {
408
- var match = errorMessage.match(/insufficient lamports (\d+), need (\d+)/);
409
- if (!match) return;
410
- var _ref = [
411
- BigInt(match[1]),
412
- BigInt(match[2])
413
- ], current = _ref[0], needed = _ref[1];
414
- var _ref1 = [
415
- Number(current) / 1e9,
416
- Number(needed) / 1e9
417
- ], currentSOL = _ref1[0], neededSOL = _ref1[1];
418
- console.error("\nInsufficient SOL balance for top-up:");
419
- console.error(" Current: ".concat(currentSOL.toFixed(9), " SOL"));
420
- console.error(" Required: ".concat(neededSOL.toFixed(9), " SOL"));
421
- console.error(" Shortfall: ".concat((neededSOL - currentSOL).toFixed(9), " SOL\n"));
422
- }
423
- },
424
- {
425
- key: "topUpCredits",
426
- value: function topUpCredits(wincAmount) {
427
- return _async_to_generator(function() {
428
- var _this, error, errorMessage;
429
- return _ts_generator(this, function(_state) {
430
- switch(_state.label){
431
- case 0:
432
- _this = this;
433
- if (wincAmount === 0n) {
434
- debug("No Winston Credits requested; skipping top-up.");
435
- return [
436
- 2
437
- ];
438
- }
439
- _state.label = 1;
440
- case 1:
441
- _state.trys.push([
442
- 1,
443
- 3,
444
- ,
445
- 4
446
- ]);
447
- return [
448
- 4,
449
- this.withRetry(function() {
450
- return _async_to_generator(function() {
451
- var _this_turbo_getWincForToken, _this_turbo, _this_turbo_topUpWithTokens, _this_turbo1, exchangeRate, wincPerSol, solInLamports, numerator, lamportsCalculated, minLamports, lamportsToUse, solAmount;
452
- return _ts_generator(this, function(_state) {
453
- switch(_state.label){
454
- case 0:
455
- return [
456
- 4,
457
- (_this_turbo_getWincForToken = (_this_turbo = this.turbo).getWincForToken) === null || _this_turbo_getWincForToken === void 0 ? void 0 : _this_turbo_getWincForToken.call(_this_turbo, {
458
- tokenAmount: CONSTANTS.SOL_IN_LAMPORTS
459
- })
460
- ];
461
- case 1:
462
- exchangeRate = _state.sent();
463
- if (!exchangeRate) {
464
- throw new Error("Unable to get Winston Credits exchange rate");
465
- }
466
- wincPerSol = BigInt(String(exchangeRate.winc));
467
- if (wincPerSol <= 0n) {
468
- throw new Error("Invalid Winston Credits exchange rate");
469
- }
470
- solInLamports = BigInt(CONSTANTS.SOL_IN_LAMPORTS);
471
- numerator = wincAmount * solInLamports;
472
- lamportsCalculated = (numerator + (wincPerSol - 1n)) / wincPerSol;
473
- minLamports = BigInt(CONSTANTS.MIN_TOP_UP_LAMPORTS);
474
- lamportsToUse = lamportsCalculated < minLamports ? minLamports : lamportsCalculated;
475
- if (lamportsToUse > lamportsCalculated) {
476
- debug("Applying minimum top-up of ".concat(CONSTANTS.MIN_TOP_UP_SOL, " SOL (").concat(CONSTANTS.MIN_TOP_UP_LAMPORTS, " lamports)"));
477
- }
478
- solAmount = Number(lamportsToUse) / CONSTANTS.SOL_IN_LAMPORTS;
479
- debug("Buying at least ".concat(wincAmount, " Winston Credits (~").concat(solAmount.toFixed(9), " SOL / ").concat(lamportsToUse, " lamports)"));
480
- return [
481
- 4,
482
- (_this_turbo_topUpWithTokens = (_this_turbo1 = this.turbo).topUpWithTokens) === null || _this_turbo_topUpWithTokens === void 0 ? void 0 : _this_turbo_topUpWithTokens.call(_this_turbo1, {
483
- tokenAmount: String(lamportToTokenAmount(lamportsToUse.toString()))
484
- })
485
- ];
486
- case 2:
487
- _state.sent();
488
- debug("Top-up initiated for ".concat(wincAmount, " Winston Credits"));
489
- return [
490
- 2
491
- ];
492
- }
493
- });
494
- }).call(_this);
495
- })
496
- ];
497
- case 2:
498
- _state.sent();
499
- return [
500
- 3,
501
- 4
502
- ];
503
- case 3:
504
- error = _state.sent();
505
- errorMessage = _instanceof(error, Error) ? error.message : String(error);
506
- debug("Top-up failed:", error);
507
- if (errorMessage.includes("insufficient lamports")) {
508
- this.formatInsufficientFundsError(errorMessage);
509
- }
510
- throw new Error("Failed to top up ".concat(wincAmount, " Winston Credits: ").concat(errorMessage));
511
- case 4:
512
- return [
513
- 2
514
- ];
515
- }
516
- });
517
- }).call(this);
518
- }
519
- },
520
- {
521
- key: "checkBalanceAndTopUp",
522
- value: function checkBalanceAndTopUp(requiredWinc) {
523
- return _async_to_generator(function() {
524
- var current, deficit;
525
- return _ts_generator(this, function(_state) {
526
- switch(_state.label){
527
- case 0:
528
- if (requiredWinc === BigInt(0)) return [
529
- 2
530
- ];
531
- return [
532
- 4,
533
- this.turbo.getBalance()
534
- ];
535
- case 1:
536
- current = BigInt.apply(void 0, [
537
- String.apply(void 0, [
538
- _state.sent().winc
539
- ])
540
- ]);
541
- if (current >= requiredWinc) {
542
- debug("Sufficient balance: ".concat(current, " Winston Credits (required: ").concat(requiredWinc, ")"));
543
- return [
544
- 2
545
- ];
546
- }
547
- deficit = requiredWinc - current;
548
- debug("Current: ".concat(current, ", Required: ").concat(requiredWinc, ", Topping up: ").concat(deficit));
549
- return [
550
- 4,
551
- this.topUpCredits(deficit)
552
- ];
553
- case 2:
554
- _state.sent();
555
- return [
556
- 2
557
- ];
558
- }
559
- });
560
- }).call(this);
561
- }
562
- },
563
- {
564
- key: "processQueue",
565
- value: function processQueue() {
566
- return _async_to_generator(function() {
567
- var _this, _loop;
568
- return _ts_generator(this, function(_state) {
569
- switch(_state.label){
570
- case 0:
571
- _loop = function() {
572
- var item, estimated, _item_file_tags, tags, uploadResult, url, error;
573
- return _ts_generator(this, function(_state) {
574
- switch(_state.label){
575
- case 0:
576
- item = _this.uploadQueue.shift();
577
- if (!item) return [
578
- 2,
579
- "continue"
580
- ];
581
- _state.label = 1;
582
- case 1:
583
- _state.trys.push([
584
- 1,
585
- 7,
586
- ,
587
- 8
588
- ]);
589
- debug("Processing upload for ".concat(item.file.fileName, " (").concat(item.file.buffer.length, " bytes)"));
590
- return [
591
- 4,
592
- _this.getUploadPrice(item.file.buffer.length)
593
- ];
594
- case 2:
595
- estimated = _state.sent();
596
- return [
597
- 4,
598
- _this.checkBalanceAndTopUp(estimated)
599
- ];
600
- case 3:
601
- _state.sent();
602
- tags = _to_consumable_array((_item_file_tags = item.file.tags) !== null && _item_file_tags !== void 0 ? _item_file_tags : []);
603
- if (item.file.contentType) {
604
- tags.push({
605
- name: "Content-Type",
606
- value: item.file.contentType
607
- });
608
- }
609
- return [
610
- 4,
611
- _this.turbo.uploadFile({
612
- fileStreamFactory: function() {
613
- return item.file.buffer;
614
- },
615
- fileSizeFactory: function() {
616
- return item.file.buffer.byteLength;
617
- },
618
- dataItemOpts: {
619
- tags: tags
620
- }
621
- })
622
- ];
623
- case 4:
624
- uploadResult = _state.sent();
625
- url = buildPublicContentUrl(uploadResult.id, _this.network);
626
- debug("Upload complete: ".concat(url));
627
- item.resolve(url);
628
- if (!(_this.uploadQueue.length > 0)) return [
629
- 3,
630
- 6
631
- ];
632
- debug("Waiting ".concat(CONSTANTS.UPLOAD_DELAY_MS, "ms before next upload..."));
633
- return [
634
- 4,
635
- delay(CONSTANTS.UPLOAD_DELAY_MS)
636
- ];
637
- case 5:
638
- _state.sent();
639
- _state.label = 6;
640
- case 6:
641
- return [
642
- 3,
643
- 8
644
- ];
645
- case 7:
646
- error = _state.sent();
647
- item.reject(_instanceof(error, Error) ? error : new Error(String(error)));
648
- return [
649
- 3,
650
- 8
651
- ];
652
- case 8:
653
- return [
654
- 2
655
- ];
656
- }
657
- });
658
- };
659
- if (this.isProcessingQueue || !this.uploadQueue.length) return [
660
- 2
661
- ];
662
- this.isProcessingQueue = true;
663
- _state.label = 1;
664
- case 1:
665
- if (!(this.uploadQueue.length > 0)) return [
666
- 3,
667
- 3
668
- ];
669
- _this = this;
670
- return [
671
- 5,
672
- _ts_values(_loop())
673
- ];
674
- case 2:
675
- _state.sent();
676
- return [
677
- 3,
678
- 1
679
- ];
680
- case 3:
681
- this.isProcessingQueue = false;
682
- return [
683
- 2
684
- ];
685
- }
686
- });
687
- }).call(this);
688
- }
689
- },
690
- {
691
- key: "upload",
692
- value: function upload(file) {
693
- return _async_to_generator(function() {
694
- var _this;
695
- return _ts_generator(this, function(_state) {
696
- _this = this;
697
- return [
698
- 2,
699
- new Promise(function(resolve, reject) {
700
- debug("Queueing upload for ".concat(file.fileName, " (").concat(file.buffer.length, " bytes)"));
701
- _this.uploadQueue.push({
702
- file: file,
703
- resolve: resolve,
704
- reject: reject
705
- });
706
- _this.processQueue().catch(reject);
707
- })
708
- ];
709
- });
710
- }).call(this);
711
- }
712
- }
713
- ]);
714
- return TurboStorageDriver;
715
- }();
716
- export var getTurboServiceConfig = function(network) {
717
- return network === "devnet" ? CONSTANTS.SERVICE_URLS.devnet : {};
718
- };