@solana-mobile/dapp-store-cli 0.13.0 → 0.13.1
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/lib/CliUtils.js +37 -9
- package/lib/package.json +4 -2
- package/lib/upload/TurboStorageDriver.js +696 -0
- package/lib/upload/index.js +1 -0
- package/package.json +4 -2
- package/src/CliUtils.ts +20 -16
- package/src/upload/TurboStorageDriver.ts +248 -0
- package/src/upload/index.ts +1 -0
package/lib/CliUtils.js
CHANGED
|
@@ -139,7 +139,7 @@ function _ts_generator(thisArg, body) {
|
|
|
139
139
|
import fs from "fs";
|
|
140
140
|
import { Keypair, PublicKey } from "@solana/web3.js";
|
|
141
141
|
import debugModule from "debug";
|
|
142
|
-
import {
|
|
142
|
+
import { keypairIdentity, Metaplex, lamports } from "@metaplex-foundation/js";
|
|
143
143
|
import updateNotifier from "update-notifier";
|
|
144
144
|
import { readFile } from 'fs/promises';
|
|
145
145
|
var cliPackage = JSON.parse((await readFile(new URL("./package.json", import.meta.url))).toString());
|
|
@@ -147,6 +147,7 @@ import boxen from "boxen";
|
|
|
147
147
|
import ver from "semver";
|
|
148
148
|
import path from "path";
|
|
149
149
|
import { CachedStorageDriver } from "./upload/CachedStorageDriver.js";
|
|
150
|
+
import { TurboStorageDriver } from "./upload/TurboStorageDriver.js";
|
|
150
151
|
import { EnvVariables } from "./config/index.js";
|
|
151
152
|
import { S3Client } from "@aws-sdk/client-s3";
|
|
152
153
|
import { awsStorage } from "@metaplex-foundation/js-plugin-aws";
|
|
@@ -155,7 +156,7 @@ export var Constants = function Constants() {
|
|
|
155
156
|
"use strict";
|
|
156
157
|
_class_call_check(this, Constants);
|
|
157
158
|
};
|
|
158
|
-
_define_property(Constants, "CLI_VERSION", "0.13.
|
|
159
|
+
_define_property(Constants, "CLI_VERSION", "0.13.1");
|
|
159
160
|
_define_property(Constants, "CONFIG_FILE_NAME", "config.yaml");
|
|
160
161
|
_define_property(Constants, "DEFAULT_RPC_DEVNET", "https://api.devnet.solana.com");
|
|
161
162
|
_define_property(Constants, "DEFAULT_PRIORITY_FEE", 500000);
|
|
@@ -333,13 +334,40 @@ export var getMetaplexInstance = function(connection, keypair) {
|
|
|
333
334
|
var bucketPlugin = awsStorage(awsClient, s3Mgr.s3Config.bucketName);
|
|
334
335
|
metaplex.use(bucketPlugin);
|
|
335
336
|
} else {
|
|
336
|
-
var
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
337
|
+
var turboDriver = new TurboStorageDriver(keypair, isDevnet ? "devnet" : "mainnet", Number(process.env.TURBO_BUFFER_PERCENTAGE || 20));
|
|
338
|
+
var metaplexAdapter = {
|
|
339
|
+
upload: function upload(file) {
|
|
340
|
+
return _async_to_generator(function() {
|
|
341
|
+
return _ts_generator(this, function(_state) {
|
|
342
|
+
return [
|
|
343
|
+
2,
|
|
344
|
+
turboDriver.upload(file)
|
|
345
|
+
];
|
|
346
|
+
});
|
|
347
|
+
})();
|
|
348
|
+
},
|
|
349
|
+
getUploadPrice: function getUploadPrice(bytes) {
|
|
350
|
+
return _async_to_generator(function() {
|
|
351
|
+
var price;
|
|
352
|
+
return _ts_generator(this, function(_state) {
|
|
353
|
+
switch(_state.label){
|
|
354
|
+
case 0:
|
|
355
|
+
return [
|
|
356
|
+
4,
|
|
357
|
+
turboDriver.getUploadPrice(bytes)
|
|
358
|
+
];
|
|
359
|
+
case 1:
|
|
360
|
+
price = _state.sent();
|
|
361
|
+
return [
|
|
362
|
+
2,
|
|
363
|
+
lamports(price)
|
|
364
|
+
];
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
})();
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
metaplex.storage().setDriver(metaplexAdapter);
|
|
343
371
|
}
|
|
344
372
|
metaplex.storage().setDriver(new CachedStorageDriver(metaplex.storage().driver(), {
|
|
345
373
|
assetManifestPath: isDevnet ? "./.asset-manifest-devnet.json" : "./.asset-manifest.json"
|
package/lib/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana-mobile/dapp-store-cli",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -51,13 +51,15 @@
|
|
|
51
51
|
"ts-node": "^10.9.1"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
+
"@ardrive/turbo-sdk": "^1.31.1",
|
|
54
55
|
"@aws-sdk/client-s3": "^3.321.1",
|
|
55
56
|
"@metaplex-foundation/js-plugin-aws": "^0.20.0",
|
|
56
|
-
"@solana-mobile/dapp-store-publishing-tools": "workspace:0.13.
|
|
57
|
+
"@solana-mobile/dapp-store-publishing-tools": "workspace:0.13.1",
|
|
57
58
|
"@solana/web3.js": "1.92.1",
|
|
58
59
|
"@types/semver": "^7.3.13",
|
|
59
60
|
"ajv": "^8.11.0",
|
|
60
61
|
"boxen": "^7.0.1",
|
|
62
|
+
"bs58": "^5.0.0",
|
|
61
63
|
"chokidar": "^3.5.3",
|
|
62
64
|
"commander": "^9.4.1",
|
|
63
65
|
"debug": "^4.3.4",
|
|
@@ -0,0 +1,696 @@
|
|
|
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
|
+
var debug = debugModule("cli:turbo-storage");
|
|
251
|
+
var CONSTANTS = {
|
|
252
|
+
FREE_UPLOAD_LIMIT: 97280,
|
|
253
|
+
UPLOAD_DELAY_MS: 2000,
|
|
254
|
+
MAX_RETRIES: 5,
|
|
255
|
+
SOL_IN_LAMPORTS: 1000000000,
|
|
256
|
+
BACKOFF: {
|
|
257
|
+
BASE_MS: 500,
|
|
258
|
+
MAX_MS: 8000
|
|
259
|
+
},
|
|
260
|
+
GATEWAYS: {
|
|
261
|
+
devnet: "https://turbo.ardrive.dev/raw",
|
|
262
|
+
mainnet: "https://arweave.net"
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
var delay = function(ms) {
|
|
266
|
+
return new Promise(function(resolve) {
|
|
267
|
+
return setTimeout(resolve, ms);
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
export var TurboStorageDriver = /*#__PURE__*/ function() {
|
|
271
|
+
"use strict";
|
|
272
|
+
function TurboStorageDriver(keypair) {
|
|
273
|
+
var network = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "mainnet", bufferPercentage = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 20;
|
|
274
|
+
_class_call_check(this, TurboStorageDriver);
|
|
275
|
+
_define_property(this, "turbo", void 0);
|
|
276
|
+
_define_property(this, "bufferPercentage", void 0);
|
|
277
|
+
_define_property(this, "network", void 0);
|
|
278
|
+
_define_property(this, "uploadQueue", []);
|
|
279
|
+
_define_property(this, "isProcessingQueue", false);
|
|
280
|
+
this.network = network;
|
|
281
|
+
this.bufferPercentage = bufferPercentage;
|
|
282
|
+
this.turbo = TurboFactory.authenticated(_object_spread({
|
|
283
|
+
privateKey: bs58.encode(keypair.secretKey),
|
|
284
|
+
token: "solana"
|
|
285
|
+
}, this.getServiceUrls(network === "devnet")));
|
|
286
|
+
}
|
|
287
|
+
_create_class(TurboStorageDriver, [
|
|
288
|
+
{
|
|
289
|
+
key: "getServiceUrls",
|
|
290
|
+
value: function getServiceUrls(isDev) {
|
|
291
|
+
var base = isDev ? "ardrive.dev" : "ardrive.io";
|
|
292
|
+
return {
|
|
293
|
+
uploadUrl: "https://upload.".concat(base),
|
|
294
|
+
paymentUrl: "https://payment.".concat(base)
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
key: "getUploadPrice",
|
|
300
|
+
value: function getUploadPrice(bytes) {
|
|
301
|
+
return _async_to_generator(function() {
|
|
302
|
+
var _ref, cost, base;
|
|
303
|
+
return _ts_generator(this, function(_state) {
|
|
304
|
+
switch(_state.label){
|
|
305
|
+
case 0:
|
|
306
|
+
if (bytes <= CONSTANTS.FREE_UPLOAD_LIMIT) return [
|
|
307
|
+
2,
|
|
308
|
+
BigInt(0)
|
|
309
|
+
];
|
|
310
|
+
return [
|
|
311
|
+
4,
|
|
312
|
+
this.turbo.getUploadCosts({
|
|
313
|
+
bytes: [
|
|
314
|
+
bytes
|
|
315
|
+
]
|
|
316
|
+
})
|
|
317
|
+
];
|
|
318
|
+
case 1:
|
|
319
|
+
_ref = _sliced_to_array.apply(void 0, [
|
|
320
|
+
_state.sent(),
|
|
321
|
+
1
|
|
322
|
+
]), cost = _ref[0];
|
|
323
|
+
base = BigInt(String(cost.winc));
|
|
324
|
+
return [
|
|
325
|
+
2,
|
|
326
|
+
base * BigInt(100 + this.bufferPercentage) / BigInt(100)
|
|
327
|
+
];
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}).call(this);
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
key: "withRetry",
|
|
335
|
+
value: function withRetry(operation) {
|
|
336
|
+
var isRetriable = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : function(msg) {
|
|
337
|
+
return msg.includes("429") || msg.includes("Too Many Requests");
|
|
338
|
+
};
|
|
339
|
+
return _async_to_generator(function() {
|
|
340
|
+
var retry, error, errorMessage, delayMs;
|
|
341
|
+
return _ts_generator(this, function(_state) {
|
|
342
|
+
switch(_state.label){
|
|
343
|
+
case 0:
|
|
344
|
+
retry = 0;
|
|
345
|
+
_state.label = 1;
|
|
346
|
+
case 1:
|
|
347
|
+
if (!(retry <= CONSTANTS.MAX_RETRIES)) return [
|
|
348
|
+
3,
|
|
349
|
+
8
|
|
350
|
+
];
|
|
351
|
+
_state.label = 2;
|
|
352
|
+
case 2:
|
|
353
|
+
_state.trys.push([
|
|
354
|
+
2,
|
|
355
|
+
4,
|
|
356
|
+
,
|
|
357
|
+
7
|
|
358
|
+
]);
|
|
359
|
+
return [
|
|
360
|
+
4,
|
|
361
|
+
operation()
|
|
362
|
+
];
|
|
363
|
+
case 3:
|
|
364
|
+
return [
|
|
365
|
+
2,
|
|
366
|
+
_state.sent()
|
|
367
|
+
];
|
|
368
|
+
case 4:
|
|
369
|
+
error = _state.sent();
|
|
370
|
+
errorMessage = _instanceof(error, Error) ? error.message : String(error);
|
|
371
|
+
if (!(retry < CONSTANTS.MAX_RETRIES && isRetriable(errorMessage))) return [
|
|
372
|
+
3,
|
|
373
|
+
6
|
|
374
|
+
];
|
|
375
|
+
delayMs = Math.min(CONSTANTS.BACKOFF.BASE_MS * Math.pow(2, retry), CONSTANTS.BACKOFF.MAX_MS);
|
|
376
|
+
console.log("Rate limited, retrying after ".concat(delayMs, "ms (attempt ").concat(retry + 1, "/").concat(CONSTANTS.MAX_RETRIES, ")..."));
|
|
377
|
+
return [
|
|
378
|
+
4,
|
|
379
|
+
delay(delayMs)
|
|
380
|
+
];
|
|
381
|
+
case 5:
|
|
382
|
+
_state.sent();
|
|
383
|
+
return [
|
|
384
|
+
3,
|
|
385
|
+
7
|
|
386
|
+
];
|
|
387
|
+
case 6:
|
|
388
|
+
throw error;
|
|
389
|
+
case 7:
|
|
390
|
+
retry++;
|
|
391
|
+
return [
|
|
392
|
+
3,
|
|
393
|
+
1
|
|
394
|
+
];
|
|
395
|
+
case 8:
|
|
396
|
+
throw new Error("Max retries exceeded");
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
})();
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
key: "formatInsufficientFundsError",
|
|
404
|
+
value: function formatInsufficientFundsError(errorMessage) {
|
|
405
|
+
var match = errorMessage.match(/insufficient lamports (\d+), need (\d+)/);
|
|
406
|
+
if (!match) return;
|
|
407
|
+
var _ref = [
|
|
408
|
+
BigInt(match[1]),
|
|
409
|
+
BigInt(match[2])
|
|
410
|
+
], current = _ref[0], needed = _ref[1];
|
|
411
|
+
var _ref1 = [
|
|
412
|
+
Number(current) / 1e9,
|
|
413
|
+
Number(needed) / 1e9
|
|
414
|
+
], currentSOL = _ref1[0], neededSOL = _ref1[1];
|
|
415
|
+
console.error("\nInsufficient SOL balance for top-up:");
|
|
416
|
+
console.error(" Current: ".concat(currentSOL.toFixed(9), " SOL"));
|
|
417
|
+
console.error(" Required: ".concat(neededSOL.toFixed(9), " SOL"));
|
|
418
|
+
console.error(" Shortfall: ".concat((neededSOL - currentSOL).toFixed(9), " SOL\n"));
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
key: "topUpCredits",
|
|
423
|
+
value: function topUpCredits(wincAmount) {
|
|
424
|
+
return _async_to_generator(function() {
|
|
425
|
+
var _this, error, errorMessage;
|
|
426
|
+
return _ts_generator(this, function(_state) {
|
|
427
|
+
switch(_state.label){
|
|
428
|
+
case 0:
|
|
429
|
+
_this = this;
|
|
430
|
+
_state.label = 1;
|
|
431
|
+
case 1:
|
|
432
|
+
_state.trys.push([
|
|
433
|
+
1,
|
|
434
|
+
3,
|
|
435
|
+
,
|
|
436
|
+
4
|
|
437
|
+
]);
|
|
438
|
+
return [
|
|
439
|
+
4,
|
|
440
|
+
this.withRetry(function() {
|
|
441
|
+
return _async_to_generator(function() {
|
|
442
|
+
var _this_turbo_getWincForToken, _this_turbo, _this_turbo_topUpWithTokens, _this_turbo1, exchangeRate, wincPerSol, lamportsNeeded;
|
|
443
|
+
return _ts_generator(this, function(_state) {
|
|
444
|
+
switch(_state.label){
|
|
445
|
+
case 0:
|
|
446
|
+
return [
|
|
447
|
+
4,
|
|
448
|
+
(_this_turbo_getWincForToken = (_this_turbo = this.turbo).getWincForToken) === null || _this_turbo_getWincForToken === void 0 ? void 0 : _this_turbo_getWincForToken.call(_this_turbo, {
|
|
449
|
+
tokenAmount: CONSTANTS.SOL_IN_LAMPORTS
|
|
450
|
+
})
|
|
451
|
+
];
|
|
452
|
+
case 1:
|
|
453
|
+
exchangeRate = _state.sent();
|
|
454
|
+
if (!exchangeRate) {
|
|
455
|
+
throw new Error("Unable to get Winston Credits exchange rate");
|
|
456
|
+
}
|
|
457
|
+
wincPerSol = BigInt(String(exchangeRate.winc));
|
|
458
|
+
lamportsNeeded = wincAmount * BigInt(CONSTANTS.SOL_IN_LAMPORTS) / wincPerSol;
|
|
459
|
+
debug("Buying ".concat(wincAmount, " Winston Credits for ~").concat(Number(lamportsNeeded) / 1e9, " SOL"));
|
|
460
|
+
return [
|
|
461
|
+
4,
|
|
462
|
+
(_this_turbo_topUpWithTokens = (_this_turbo1 = this.turbo).topUpWithTokens) === null || _this_turbo_topUpWithTokens === void 0 ? void 0 : _this_turbo_topUpWithTokens.call(_this_turbo1, {
|
|
463
|
+
tokenAmount: String(lamportToTokenAmount(lamportsNeeded.toString()))
|
|
464
|
+
})
|
|
465
|
+
];
|
|
466
|
+
case 2:
|
|
467
|
+
_state.sent();
|
|
468
|
+
debug("Top-up initiated for ".concat(wincAmount, " Winston Credits"));
|
|
469
|
+
return [
|
|
470
|
+
2
|
|
471
|
+
];
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
}).call(_this);
|
|
475
|
+
})
|
|
476
|
+
];
|
|
477
|
+
case 2:
|
|
478
|
+
_state.sent();
|
|
479
|
+
return [
|
|
480
|
+
3,
|
|
481
|
+
4
|
|
482
|
+
];
|
|
483
|
+
case 3:
|
|
484
|
+
error = _state.sent();
|
|
485
|
+
errorMessage = _instanceof(error, Error) ? error.message : String(error);
|
|
486
|
+
debug("Top-up failed:", error);
|
|
487
|
+
if (errorMessage.includes("insufficient lamports")) {
|
|
488
|
+
this.formatInsufficientFundsError(errorMessage);
|
|
489
|
+
}
|
|
490
|
+
throw new Error("Failed to top up ".concat(wincAmount, " Winston Credits: ").concat(errorMessage));
|
|
491
|
+
case 4:
|
|
492
|
+
return [
|
|
493
|
+
2
|
|
494
|
+
];
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
}).call(this);
|
|
498
|
+
}
|
|
499
|
+
},
|
|
500
|
+
{
|
|
501
|
+
key: "checkBalanceAndTopUp",
|
|
502
|
+
value: function checkBalanceAndTopUp(requiredWinc) {
|
|
503
|
+
return _async_to_generator(function() {
|
|
504
|
+
var current, deficit;
|
|
505
|
+
return _ts_generator(this, function(_state) {
|
|
506
|
+
switch(_state.label){
|
|
507
|
+
case 0:
|
|
508
|
+
if (requiredWinc === BigInt(0)) return [
|
|
509
|
+
2
|
|
510
|
+
];
|
|
511
|
+
return [
|
|
512
|
+
4,
|
|
513
|
+
this.turbo.getBalance()
|
|
514
|
+
];
|
|
515
|
+
case 1:
|
|
516
|
+
current = BigInt.apply(void 0, [
|
|
517
|
+
String.apply(void 0, [
|
|
518
|
+
_state.sent().winc
|
|
519
|
+
])
|
|
520
|
+
]);
|
|
521
|
+
if (current >= requiredWinc) {
|
|
522
|
+
debug("Sufficient balance: ".concat(current, " Winston Credits (required: ").concat(requiredWinc, ")"));
|
|
523
|
+
return [
|
|
524
|
+
2
|
|
525
|
+
];
|
|
526
|
+
}
|
|
527
|
+
deficit = requiredWinc - current;
|
|
528
|
+
debug("Current: ".concat(current, ", Required: ").concat(requiredWinc, ", Topping up: ").concat(deficit));
|
|
529
|
+
return [
|
|
530
|
+
4,
|
|
531
|
+
this.topUpCredits(deficit)
|
|
532
|
+
];
|
|
533
|
+
case 2:
|
|
534
|
+
_state.sent();
|
|
535
|
+
return [
|
|
536
|
+
2
|
|
537
|
+
];
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
}).call(this);
|
|
541
|
+
}
|
|
542
|
+
},
|
|
543
|
+
{
|
|
544
|
+
key: "processQueue",
|
|
545
|
+
value: function processQueue() {
|
|
546
|
+
return _async_to_generator(function() {
|
|
547
|
+
var _this, _loop;
|
|
548
|
+
return _ts_generator(this, function(_state) {
|
|
549
|
+
switch(_state.label){
|
|
550
|
+
case 0:
|
|
551
|
+
_loop = function() {
|
|
552
|
+
var item, estimated, _item_file_tags, tags, uploadResult, gateway, url, error;
|
|
553
|
+
return _ts_generator(this, function(_state) {
|
|
554
|
+
switch(_state.label){
|
|
555
|
+
case 0:
|
|
556
|
+
item = _this.uploadQueue.shift();
|
|
557
|
+
if (!item) return [
|
|
558
|
+
2,
|
|
559
|
+
"continue"
|
|
560
|
+
];
|
|
561
|
+
_state.label = 1;
|
|
562
|
+
case 1:
|
|
563
|
+
_state.trys.push([
|
|
564
|
+
1,
|
|
565
|
+
7,
|
|
566
|
+
,
|
|
567
|
+
8
|
|
568
|
+
]);
|
|
569
|
+
debug("Processing upload for ".concat(item.file.fileName, " (").concat(item.file.buffer.length, " bytes)"));
|
|
570
|
+
return [
|
|
571
|
+
4,
|
|
572
|
+
_this.getUploadPrice(item.file.buffer.length)
|
|
573
|
+
];
|
|
574
|
+
case 2:
|
|
575
|
+
estimated = _state.sent();
|
|
576
|
+
return [
|
|
577
|
+
4,
|
|
578
|
+
_this.checkBalanceAndTopUp(estimated)
|
|
579
|
+
];
|
|
580
|
+
case 3:
|
|
581
|
+
_state.sent();
|
|
582
|
+
tags = _to_consumable_array((_item_file_tags = item.file.tags) !== null && _item_file_tags !== void 0 ? _item_file_tags : []);
|
|
583
|
+
if (item.file.contentType) {
|
|
584
|
+
tags.push({
|
|
585
|
+
name: "Content-Type",
|
|
586
|
+
value: item.file.contentType
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
return [
|
|
590
|
+
4,
|
|
591
|
+
_this.turbo.uploadFile({
|
|
592
|
+
fileStreamFactory: function() {
|
|
593
|
+
return item.file.buffer;
|
|
594
|
+
},
|
|
595
|
+
fileSizeFactory: function() {
|
|
596
|
+
return item.file.buffer.byteLength;
|
|
597
|
+
},
|
|
598
|
+
dataItemOpts: {
|
|
599
|
+
tags: tags
|
|
600
|
+
}
|
|
601
|
+
})
|
|
602
|
+
];
|
|
603
|
+
case 4:
|
|
604
|
+
uploadResult = _state.sent();
|
|
605
|
+
gateway = CONSTANTS.GATEWAYS[_this.network];
|
|
606
|
+
url = "".concat(gateway, "/").concat(uploadResult.id);
|
|
607
|
+
debug("Upload complete: ".concat(url));
|
|
608
|
+
item.resolve(url);
|
|
609
|
+
if (!(_this.uploadQueue.length > 0)) return [
|
|
610
|
+
3,
|
|
611
|
+
6
|
|
612
|
+
];
|
|
613
|
+
debug("Waiting ".concat(CONSTANTS.UPLOAD_DELAY_MS, "ms before next upload..."));
|
|
614
|
+
return [
|
|
615
|
+
4,
|
|
616
|
+
delay(CONSTANTS.UPLOAD_DELAY_MS)
|
|
617
|
+
];
|
|
618
|
+
case 5:
|
|
619
|
+
_state.sent();
|
|
620
|
+
_state.label = 6;
|
|
621
|
+
case 6:
|
|
622
|
+
return [
|
|
623
|
+
3,
|
|
624
|
+
8
|
|
625
|
+
];
|
|
626
|
+
case 7:
|
|
627
|
+
error = _state.sent();
|
|
628
|
+
item.reject(_instanceof(error, Error) ? error : new Error(String(error)));
|
|
629
|
+
return [
|
|
630
|
+
3,
|
|
631
|
+
8
|
|
632
|
+
];
|
|
633
|
+
case 8:
|
|
634
|
+
return [
|
|
635
|
+
2
|
|
636
|
+
];
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
};
|
|
640
|
+
if (this.isProcessingQueue || !this.uploadQueue.length) return [
|
|
641
|
+
2
|
|
642
|
+
];
|
|
643
|
+
this.isProcessingQueue = true;
|
|
644
|
+
_state.label = 1;
|
|
645
|
+
case 1:
|
|
646
|
+
if (!(this.uploadQueue.length > 0)) return [
|
|
647
|
+
3,
|
|
648
|
+
3
|
|
649
|
+
];
|
|
650
|
+
_this = this;
|
|
651
|
+
return [
|
|
652
|
+
5,
|
|
653
|
+
_ts_values(_loop())
|
|
654
|
+
];
|
|
655
|
+
case 2:
|
|
656
|
+
_state.sent();
|
|
657
|
+
return [
|
|
658
|
+
3,
|
|
659
|
+
1
|
|
660
|
+
];
|
|
661
|
+
case 3:
|
|
662
|
+
this.isProcessingQueue = false;
|
|
663
|
+
return [
|
|
664
|
+
2
|
|
665
|
+
];
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
}).call(this);
|
|
669
|
+
}
|
|
670
|
+
},
|
|
671
|
+
{
|
|
672
|
+
key: "upload",
|
|
673
|
+
value: function upload(file) {
|
|
674
|
+
return _async_to_generator(function() {
|
|
675
|
+
var _this;
|
|
676
|
+
return _ts_generator(this, function(_state) {
|
|
677
|
+
_this = this;
|
|
678
|
+
return [
|
|
679
|
+
2,
|
|
680
|
+
new Promise(function(resolve, reject) {
|
|
681
|
+
debug("Queueing upload for ".concat(file.fileName, " (").concat(file.buffer.length, " bytes)"));
|
|
682
|
+
_this.uploadQueue.push({
|
|
683
|
+
file: file,
|
|
684
|
+
resolve: resolve,
|
|
685
|
+
reject: reject
|
|
686
|
+
});
|
|
687
|
+
_this.processQueue().catch(reject);
|
|
688
|
+
})
|
|
689
|
+
];
|
|
690
|
+
});
|
|
691
|
+
}).call(this);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
]);
|
|
695
|
+
return TurboStorageDriver;
|
|
696
|
+
}();
|
package/lib/upload/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana-mobile/dapp-store-cli",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -44,13 +44,15 @@
|
|
|
44
44
|
"ts-node": "^10.9.1"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
+
"@ardrive/turbo-sdk": "^1.31.1",
|
|
47
48
|
"@aws-sdk/client-s3": "^3.321.1",
|
|
48
49
|
"@metaplex-foundation/js-plugin-aws": "^0.20.0",
|
|
49
|
-
"@solana-mobile/dapp-store-publishing-tools": "0.13.
|
|
50
|
+
"@solana-mobile/dapp-store-publishing-tools": "0.13.1",
|
|
50
51
|
"@solana/web3.js": "1.92.1",
|
|
51
52
|
"@types/semver": "^7.3.13",
|
|
52
53
|
"ajv": "^8.11.0",
|
|
53
54
|
"boxen": "^7.0.1",
|
|
55
|
+
"bs58": "^5.0.0",
|
|
54
56
|
"chokidar": "^3.5.3",
|
|
55
57
|
"commander": "^9.4.1",
|
|
56
58
|
"debug": "^4.3.4",
|
package/src/CliUtils.ts
CHANGED
|
@@ -2,11 +2,7 @@ import fs from "fs";
|
|
|
2
2
|
import type { Connection } from "@solana/web3.js";
|
|
3
3
|
import { Keypair, PublicKey } from "@solana/web3.js";
|
|
4
4
|
import debugModule from "debug";
|
|
5
|
-
import {
|
|
6
|
-
IrysStorageDriver,
|
|
7
|
-
keypairIdentity,
|
|
8
|
-
Metaplex,
|
|
9
|
-
} from "@metaplex-foundation/js";
|
|
5
|
+
import { keypairIdentity, Metaplex, type MetaplexFile, type Amount, lamports } from "@metaplex-foundation/js";
|
|
10
6
|
import updateNotifier from "update-notifier";
|
|
11
7
|
import { readFile } from 'fs/promises';
|
|
12
8
|
const cliPackage = JSON.parse((await readFile(new URL("./package.json", import.meta.url))).toString());
|
|
@@ -14,13 +10,14 @@ import boxen from "boxen";
|
|
|
14
10
|
import ver from "semver";
|
|
15
11
|
import path from "path";
|
|
16
12
|
import { CachedStorageDriver } from "./upload/CachedStorageDriver.js";
|
|
13
|
+
import { TurboStorageDriver } from "./upload/TurboStorageDriver.js";
|
|
17
14
|
import { EnvVariables } from "./config/index.js";
|
|
18
15
|
import { S3Client } from "@aws-sdk/client-s3";
|
|
19
16
|
import { awsStorage } from "@metaplex-foundation/js-plugin-aws";
|
|
20
17
|
import { S3StorageManager } from "./config/index.js";
|
|
21
18
|
|
|
22
19
|
export class Constants {
|
|
23
|
-
static CLI_VERSION = "0.13.
|
|
20
|
+
static CLI_VERSION = "0.13.1";
|
|
24
21
|
static CONFIG_FILE_NAME = "config.yaml";
|
|
25
22
|
static DEFAULT_RPC_DEVNET = "https://api.devnet.solana.com";
|
|
26
23
|
static DEFAULT_PRIORITY_FEE = 500000;
|
|
@@ -205,16 +202,23 @@ export const getMetaplexInstance = (
|
|
|
205
202
|
const bucketPlugin = awsStorage(awsClient, s3Mgr.s3Config.bucketName);
|
|
206
203
|
metaplex.use(bucketPlugin);
|
|
207
204
|
} else {
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
205
|
+
const turboDriver = new TurboStorageDriver(
|
|
206
|
+
keypair,
|
|
207
|
+
isDevnet ? "devnet" : "mainnet",
|
|
208
|
+
Number(process.env.TURBO_BUFFER_PERCENTAGE || 20)
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const metaplexAdapter = {
|
|
212
|
+
async upload(file: MetaplexFile): Promise<string> {
|
|
213
|
+
return turboDriver.upload(file);
|
|
214
|
+
},
|
|
215
|
+
async getUploadPrice(bytes: number): Promise<Amount> {
|
|
216
|
+
const price = await turboDriver.getUploadPrice(bytes);
|
|
217
|
+
return lamports(price);
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
metaplex.storage().setDriver(metaplexAdapter);
|
|
218
222
|
}
|
|
219
223
|
|
|
220
224
|
metaplex.storage().setDriver(
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import type { Keypair } from "@solana/web3.js";
|
|
2
|
+
import type { MetaplexFile } from "@metaplex-foundation/js";
|
|
3
|
+
import { TurboFactory, lamportToTokenAmount } from "@ardrive/turbo-sdk";
|
|
4
|
+
import bs58 from "bs58";
|
|
5
|
+
import debugModule from "debug";
|
|
6
|
+
|
|
7
|
+
const debug = debugModule("cli:turbo-storage");
|
|
8
|
+
|
|
9
|
+
interface TurboClient {
|
|
10
|
+
getUploadCosts(args: {
|
|
11
|
+
bytes: number[];
|
|
12
|
+
}): Promise<Array<{ winc: string | number | bigint }>>;
|
|
13
|
+
getBalance(): Promise<{ winc: string | number | bigint }>;
|
|
14
|
+
getWincForToken?(args: {
|
|
15
|
+
tokenAmount: number;
|
|
16
|
+
}): Promise<{ winc: string | number | bigint }>;
|
|
17
|
+
topUpWithTokens?(args: { tokenAmount: string | number }): Promise<unknown>;
|
|
18
|
+
uploadFile(args: {
|
|
19
|
+
fileStreamFactory: () => Buffer;
|
|
20
|
+
fileSizeFactory: () => number;
|
|
21
|
+
dataItemOpts?: { tags?: Array<{ name: string; value: string }> };
|
|
22
|
+
}): Promise<{ id: string }>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const CONSTANTS = {
|
|
26
|
+
FREE_UPLOAD_LIMIT: 97_280, // 95 KiB
|
|
27
|
+
UPLOAD_DELAY_MS: 2000,
|
|
28
|
+
MAX_RETRIES: 5,
|
|
29
|
+
SOL_IN_LAMPORTS: 1_000_000_000,
|
|
30
|
+
BACKOFF: {
|
|
31
|
+
BASE_MS: 500,
|
|
32
|
+
MAX_MS: 8000,
|
|
33
|
+
},
|
|
34
|
+
GATEWAYS: {
|
|
35
|
+
devnet: "https://turbo.ardrive.dev/raw",
|
|
36
|
+
mainnet: "https://arweave.net",
|
|
37
|
+
},
|
|
38
|
+
} as const;
|
|
39
|
+
|
|
40
|
+
const delay = (ms: number): Promise<void> =>
|
|
41
|
+
new Promise((resolve) => setTimeout(resolve, ms));
|
|
42
|
+
|
|
43
|
+
export class TurboStorageDriver {
|
|
44
|
+
private turbo: TurboClient;
|
|
45
|
+
private bufferPercentage: number;
|
|
46
|
+
private network: "devnet" | "mainnet";
|
|
47
|
+
|
|
48
|
+
private uploadQueue: Array<{
|
|
49
|
+
file: MetaplexFile;
|
|
50
|
+
resolve: (url: string) => void;
|
|
51
|
+
reject: (error: Error) => void;
|
|
52
|
+
}> = [];
|
|
53
|
+
private isProcessingQueue = false;
|
|
54
|
+
|
|
55
|
+
constructor(
|
|
56
|
+
keypair: Keypair,
|
|
57
|
+
network: "devnet" | "mainnet" = "mainnet",
|
|
58
|
+
bufferPercentage = 20
|
|
59
|
+
) {
|
|
60
|
+
this.network = network;
|
|
61
|
+
this.bufferPercentage = bufferPercentage;
|
|
62
|
+
|
|
63
|
+
this.turbo = TurboFactory.authenticated({
|
|
64
|
+
privateKey: bs58.encode(keypair.secretKey),
|
|
65
|
+
token: "solana",
|
|
66
|
+
...this.getServiceUrls(network === "devnet"),
|
|
67
|
+
}) as TurboClient;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private getServiceUrls(isDev: boolean) {
|
|
71
|
+
const base = isDev ? "ardrive.dev" : "ardrive.io";
|
|
72
|
+
return {
|
|
73
|
+
uploadUrl: `https://upload.${base}`,
|
|
74
|
+
paymentUrl: `https://payment.${base}`,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async getUploadPrice(bytes: number): Promise<bigint> {
|
|
79
|
+
if (bytes <= CONSTANTS.FREE_UPLOAD_LIMIT) return BigInt(0);
|
|
80
|
+
|
|
81
|
+
const [cost] = await this.turbo.getUploadCosts({ bytes: [bytes] });
|
|
82
|
+
const base = BigInt(String(cost.winc));
|
|
83
|
+
return (base * BigInt(100 + this.bufferPercentage)) / BigInt(100);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private async withRetry<T>(
|
|
87
|
+
operation: () => Promise<T>,
|
|
88
|
+
isRetriable: (error: string) => boolean = (msg) =>
|
|
89
|
+
msg.includes("429") || msg.includes("Too Many Requests")
|
|
90
|
+
): Promise<T> {
|
|
91
|
+
for (let retry = 0; retry <= CONSTANTS.MAX_RETRIES; retry++) {
|
|
92
|
+
try {
|
|
93
|
+
return await operation();
|
|
94
|
+
} catch (error) {
|
|
95
|
+
const errorMessage =
|
|
96
|
+
error instanceof Error ? error.message : String(error);
|
|
97
|
+
|
|
98
|
+
if (retry < CONSTANTS.MAX_RETRIES && isRetriable(errorMessage)) {
|
|
99
|
+
const delayMs = Math.min(
|
|
100
|
+
CONSTANTS.BACKOFF.BASE_MS * Math.pow(2, retry),
|
|
101
|
+
CONSTANTS.BACKOFF.MAX_MS
|
|
102
|
+
);
|
|
103
|
+
console.log(
|
|
104
|
+
`Rate limited, retrying after ${delayMs}ms (attempt ${retry + 1}/${
|
|
105
|
+
CONSTANTS.MAX_RETRIES
|
|
106
|
+
})...`
|
|
107
|
+
);
|
|
108
|
+
await delay(delayMs);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
throw new Error("Max retries exceeded");
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private formatInsufficientFundsError(errorMessage: string): void {
|
|
118
|
+
const match = errorMessage.match(/insufficient lamports (\d+), need (\d+)/);
|
|
119
|
+
if (!match) return;
|
|
120
|
+
|
|
121
|
+
const [current, needed] = [BigInt(match[1]), BigInt(match[2])];
|
|
122
|
+
const [currentSOL, neededSOL] = [
|
|
123
|
+
Number(current) / 1e9,
|
|
124
|
+
Number(needed) / 1e9,
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
console.error(`\nInsufficient SOL balance for top-up:`);
|
|
128
|
+
console.error(` Current: ${currentSOL.toFixed(9)} SOL`);
|
|
129
|
+
console.error(` Required: ${neededSOL.toFixed(9)} SOL`);
|
|
130
|
+
console.error(` Shortfall: ${(neededSOL - currentSOL).toFixed(9)} SOL\n`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private async topUpCredits(wincAmount: bigint): Promise<void> {
|
|
134
|
+
try {
|
|
135
|
+
await this.withRetry(async () => {
|
|
136
|
+
const exchangeRate = await this.turbo.getWincForToken?.({
|
|
137
|
+
tokenAmount: CONSTANTS.SOL_IN_LAMPORTS,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (!exchangeRate) {
|
|
141
|
+
throw new Error("Unable to get Winston Credits exchange rate");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const wincPerSol = BigInt(String(exchangeRate.winc));
|
|
145
|
+
const lamportsNeeded =
|
|
146
|
+
(wincAmount * BigInt(CONSTANTS.SOL_IN_LAMPORTS)) / wincPerSol;
|
|
147
|
+
|
|
148
|
+
debug(
|
|
149
|
+
`Buying ${wincAmount} Winston Credits for ~${
|
|
150
|
+
Number(lamportsNeeded) / 1e9
|
|
151
|
+
} SOL`
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
await this.turbo.topUpWithTokens?.({
|
|
155
|
+
tokenAmount: String(lamportToTokenAmount(lamportsNeeded.toString())),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
debug(`Top-up initiated for ${wincAmount} Winston Credits`);
|
|
159
|
+
});
|
|
160
|
+
} catch (error) {
|
|
161
|
+
const errorMessage =
|
|
162
|
+
error instanceof Error ? error.message : String(error);
|
|
163
|
+
debug("Top-up failed:", error);
|
|
164
|
+
|
|
165
|
+
if (errorMessage.includes("insufficient lamports")) {
|
|
166
|
+
this.formatInsufficientFundsError(errorMessage);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
throw new Error(
|
|
170
|
+
`Failed to top up ${wincAmount} Winston Credits: ${errorMessage}`
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private async checkBalanceAndTopUp(requiredWinc: bigint): Promise<void> {
|
|
176
|
+
if (requiredWinc === BigInt(0)) return;
|
|
177
|
+
|
|
178
|
+
const current = BigInt(String((await this.turbo.getBalance()).winc));
|
|
179
|
+
|
|
180
|
+
if (current >= requiredWinc) {
|
|
181
|
+
debug(
|
|
182
|
+
`Sufficient balance: ${current} Winston Credits (required: ${requiredWinc})`
|
|
183
|
+
);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const deficit = requiredWinc - current;
|
|
188
|
+
debug(
|
|
189
|
+
`Current: ${current}, Required: ${requiredWinc}, Topping up: ${deficit}`
|
|
190
|
+
);
|
|
191
|
+
await this.topUpCredits(deficit);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private async processQueue(): Promise<void> {
|
|
195
|
+
if (this.isProcessingQueue || !this.uploadQueue.length) return;
|
|
196
|
+
|
|
197
|
+
this.isProcessingQueue = true;
|
|
198
|
+
|
|
199
|
+
while (this.uploadQueue.length > 0) {
|
|
200
|
+
const item = this.uploadQueue.shift();
|
|
201
|
+
if (!item) continue;
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
debug(
|
|
205
|
+
`Processing upload for ${item.file.fileName} (${item.file.buffer.length} bytes)`
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
const estimated = await this.getUploadPrice(item.file.buffer.length);
|
|
209
|
+
await this.checkBalanceAndTopUp(estimated);
|
|
210
|
+
|
|
211
|
+
const tags = [...(item.file.tags ?? [])];
|
|
212
|
+
if (item.file.contentType) {
|
|
213
|
+
tags.push({ name: "Content-Type", value: item.file.contentType });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const uploadResult = await this.turbo.uploadFile({
|
|
217
|
+
fileStreamFactory: () => item.file.buffer,
|
|
218
|
+
fileSizeFactory: () => item.file.buffer.byteLength,
|
|
219
|
+
dataItemOpts: { tags },
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const gateway = CONSTANTS.GATEWAYS[this.network];
|
|
223
|
+
const url = `${gateway}/${uploadResult.id}`;
|
|
224
|
+
debug(`Upload complete: ${url}`);
|
|
225
|
+
item.resolve(url);
|
|
226
|
+
|
|
227
|
+
if (this.uploadQueue.length > 0) {
|
|
228
|
+
debug(`Waiting ${CONSTANTS.UPLOAD_DELAY_MS}ms before next upload...`);
|
|
229
|
+
await delay(CONSTANTS.UPLOAD_DELAY_MS);
|
|
230
|
+
}
|
|
231
|
+
} catch (error) {
|
|
232
|
+
item.reject(error instanceof Error ? error : new Error(String(error)));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
this.isProcessingQueue = false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async upload(file: MetaplexFile): Promise<string> {
|
|
240
|
+
return new Promise((resolve, reject) => {
|
|
241
|
+
debug(
|
|
242
|
+
`Queueing upload for ${file.fileName} (${file.buffer.length} bytes)`
|
|
243
|
+
);
|
|
244
|
+
this.uploadQueue.push({ file, resolve, reject });
|
|
245
|
+
this.processQueue().catch(reject);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
package/src/upload/index.ts
CHANGED