@functionland/react-native-fula 1.36.2 → 1.36.4

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.
@@ -1,419 +1,427 @@
1
- import Fula from '../interfaces/fulaNativeModule';
2
- import {
3
- init as chainApiInit,
4
- batchUploadManifest,
5
- checkAccountBalance,
6
- getAccountIdFromSeed,
7
- } from './chain-api';
8
- import { ApiPromise } from '@polkadot/api';
9
-
10
- /**
11
- * Get gets the value corresponding to the given key from the local datastore.
12
- // The key must be a valid ipld.Link.
13
- * @param config
14
- * @returns boolean
15
- */
16
-
17
- export const init = (
18
- identity: string, //privateKey of did identity
19
- storePath: string,
20
- bloxAddr: string,
21
- exchange: string,
22
- autoFlush: boolean = false,
23
- rootCid: string | null = null,
24
- useRelay: boolean = true,
25
- refresh: boolean = false
26
- ): Promise<{ peerId: string; rootCid: string }> => {
27
- console.log(
28
- 'init in react-native started',
29
- identity,
30
- storePath,
31
- bloxAddr,
32
- exchange,
33
- autoFlush,
34
- useRelay
35
- );
36
- return Fula.initFula(
37
- identity,
38
- storePath,
39
- bloxAddr,
40
- exchange,
41
- autoFlush,
42
- rootCid,
43
- useRelay,
44
- refresh
45
- );
46
- };
47
-
48
- /**
49
- * Get gets the value corresponding to the given key from the local datastore.
50
- // The key must be a valid ipld.Link.
51
- * @param config
52
- * @returns boolean
53
- */
54
-
55
- export const newClient = (
56
- identity: string, //privateKey of did identity
57
- storePath: string,
58
- bloxAddr: string,
59
- exchange: string,
60
- autoFlush: boolean = false,
61
- useRelay: boolean = true,
62
- refresh: boolean = false
63
- ): Promise<string> => {
64
- console.log(
65
- 'newClient in react-native started',
66
- identity,
67
- storePath,
68
- bloxAddr,
69
- exchange,
70
- autoFlush,
71
- useRelay,
72
- refresh
73
- );
74
- return Fula.newClient(
75
- identity,
76
- storePath,
77
- bloxAddr,
78
- exchange,
79
- autoFlush,
80
- useRelay,
81
- refresh
82
- );
83
- };
84
-
85
- /**
86
- * rm removes all data
87
- * @param path
88
- * @returns string: new cid of the root
89
- */
90
- export const logout = (
91
- identity: string,
92
- storePath: string
93
- ): Promise<boolean> => {
94
- return Fula.logout(identity, storePath);
95
- };
96
-
97
- /**
98
- * Checks if there are any un-synced changes on the device
99
- */
100
- export const checkFailedActions = (
101
- retry: boolean = false,
102
- timeout: number = 20
103
- ): Promise<boolean> => {
104
- return Fula.checkFailedActions(retry, timeout);
105
- };
106
-
107
- /**
108
- * Lists the cids that failed to be sent to backend and are kept only locally
109
- */
110
- export const listFailedActions = (cids: string[] = []): Promise<string[]> => {
111
- return Fula.listFailedActions(cids);
112
- };
113
-
114
- /**
115
- * Lists the cids that are recent
116
- */
117
- export const listRecentCidsAsString = (): Promise<string[]> => {
118
- return Fula.listRecentCidsAsString();
119
- };
120
-
121
- /**
122
- * Clears the cids that ar recent
123
- */
124
- export const clearCidsFromRecent = (cids: string[] = []): Promise<boolean> => {
125
- return Fula.clearCidsFromRecent(cids);
126
- };
127
-
128
- /**
129
- * Checks if there are any un-synced changes on the device
130
- */
131
- export const checkConnection = (timeout: number = 20): Promise<boolean> => {
132
- return Fula.checkConnection(timeout);
133
- };
134
-
135
- /**
136
- * Get gets the value corresponding to the given key from the local datastore.
137
- // The key must be a valid ipld.Link.
138
- * @param key
139
- * @returns value
140
- */
141
- export const get = (key: string): Promise<string> => {
142
- return Fula.get(key);
143
- };
144
-
145
- /**
146
- * Has checks whether the value corresponding to the given key is present in the local datastore.
147
- // The key must be a valid ipld.Link.
148
- * @param key
149
- * @returns boolean
150
- */
151
- export const has = (key: Uint8Array): Promise<boolean> => {
152
- return Fula.has(key);
153
- };
154
-
155
- /**
156
- * Push requests the given addr to download the root cid from this node.
157
- // The addr must be a valid multiaddr that includes peer ID.
158
- // this function.
159
- * @param addr
160
- * @returns null or error
161
- */
162
- export const push = (): Promise<string> => {
163
- return Fula.push();
164
- };
165
-
166
- //This method sends some test data to backedn
167
- export const testData = (
168
- identity: string,
169
- bloxAddr: string
170
- ): Promise<string> => {
171
- return Fula.testData(identity, bloxAddr);
172
- };
173
-
174
- /**
175
- * Put stores the given key value onto the local datastore.
176
- // The key must be a valid ipld.Link and the value must be the valid encoded ipld.Node corresponding
177
- // to the given key.
178
- * @param key, value
179
- * @returns null or string
180
- */
181
- export const put = (value: string, codec: string): Promise<string> => {
182
- return Fula.put(value, codec);
183
- };
184
-
185
- /**
186
- * mkdir creates a directory at the given path.
187
- * @param path
188
- * @returns string: new cid of the root
189
- */
190
- export const mkdir = (path: string): Promise<string> => {
191
- return Fula.mkdir(path);
192
- };
193
-
194
- /**
195
- * writeFileContent writes content at a given path
196
- * @param path
197
- * @returns string: new cid of the root
198
- */
199
- export const writeFileContent = (
200
- path: string,
201
- content: string
202
- ): Promise<string> => {
203
- return Fula.writeFileContent(path, content);
204
- };
205
-
206
- /*
207
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
208
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
209
- // It keeps the original file modiifcation date
210
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
211
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
212
- // Returns: new cid of the root after this file is placed in the tree
213
- */
214
- export const writeFile = (
215
- fulaTargetFilename: string,
216
- localFilename: string
217
- ): Promise<string> => {
218
- return Fula.writeFile(fulaTargetFilename, localFilename);
219
- };
220
-
221
- /**
222
- * ls lists the name of files and folders at a given path
223
- * @param path
224
- * @returns string: list of items
225
- * TODO: Findout how is the string and convert to array
226
- */
227
- export const ls = (path: string): Promise<void | JSON> => {
228
- return Fula.ls(path)
229
- .then((res) => {
230
- let lsResult = [];
231
- let lsRows = res.split('!!!');
232
- for (const element of lsRows) {
233
- let rowItems = element.split('???');
234
- if (rowItems && rowItems[0]) {
235
- let item = {
236
- name: '',
237
- created: '',
238
- modified: '',
239
- };
240
- item.name = rowItems[0];
241
- if (rowItems[1]) {
242
- item.created = rowItems[1];
243
- }
244
- if (rowItems[2]) {
245
- item.modified = rowItems[2];
246
- }
247
- lsResult.push(item);
248
- }
249
- }
250
- let jsonRes = JSON.parse(JSON.stringify(lsResult));
251
- return jsonRes;
252
- })
253
- .catch((e) => {
254
- return e;
255
- });
256
- };
257
-
258
- /**
259
- * rm removes all files and folders at a given path
260
- * @param path
261
- * @returns string: new cid of the root
262
- */
263
- export const rm = (path: string): Promise<string> => {
264
- return Fula.rm(path);
265
- };
266
-
267
- /**
268
- * cp copies the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
269
- * @param sourcePath, targetPath
270
- * @returns string: new cid of the root
271
- */
272
- export const cp = (sourcePath: string, targetPath: string): Promise<string> => {
273
- return Fula.cp(sourcePath, targetPath);
274
- };
275
-
276
- /**
277
- * mv moves the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
278
- * @param sourcePath, targetPath
279
- * @returns string: new cid of the root
280
- */
281
- export const mv = (sourcePath: string, targetPath: string): Promise<string> => {
282
- return Fula.mv(sourcePath, targetPath);
283
- };
284
-
285
- /*
286
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
287
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
288
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
289
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
290
- // Returns: new cid of the root after this file is placed in the tree
291
- */
292
- export const readFile = (
293
- fulaTargetFilename: string,
294
- localFilename: string
295
- ): Promise<string> => {
296
- return Fula.readFile(fulaTargetFilename, localFilename);
297
- };
298
-
299
- /**
300
- * readFile reads content of a given path
301
- * @param path
302
- * @returns string: cotent
303
- */
304
- export const readFileContent = (path: string): Promise<string> => {
305
- return Fula.readFileContent(path);
306
- };
307
-
308
- /**
309
- * Shutdown closes all resources used by Client.
310
- // After calling this function Client must be discarded.
311
- * @param
312
- * @returns
313
- */
314
- export const shutdown = (): Promise<void> => {
315
- return Fula.shutdown();
316
- };
317
-
318
- /**
319
- * setAuth adds or removes a peer from the list of peers that are allowed to push to this node.
320
- * This can only be called on a peer that is added as an owner of blox by --authorizer parameter
321
- * @param peerId, allow
322
- * @returns boolean: true if successful or false if not
323
- */
324
- export const setAuth = (peerId: string, allow: boolean): Promise<boolean> => {
325
- return Fula.setAuth(peerId, allow);
326
- };
327
-
328
- /**
329
- * isReady checks if the connection is ready to be used.
330
- * @param filesystemCheck: also check if the wnfs is ready
331
- * @returns boolean: true if ready or false if not
332
- */
333
- export const isReady = (filesystemCheck: boolean = true): Promise<boolean> => {
334
- return Fula.isReady(filesystemCheck);
335
- };
336
-
337
- /**
338
- * replicate replicates data on the nework
339
- */
340
- export const replicateRecentCids = async (
341
- api: ApiPromise,
342
- seed: string,
343
- poolId: number,
344
- replicationNo: number = 4
345
- ): Promise<{ status: boolean; msg: string }> => {
346
- let status = true;
347
- let msg = '';
348
- if (!api) {
349
- api = await chainApiInit();
350
- }
351
- if (api) {
352
- console.log('uploading manifests');
353
- try {
354
- let account = await getAccountIdFromSeed(seed);
355
- console.log('account: ' + account);
356
- const accountBal = await checkAccountBalance(api, account);
357
- console.log('account balance: ' + accountBal);
358
- if (accountBal !== '0') {
359
- const recentCids = await listRecentCidsAsString();
360
- console.log(recentCids);
361
- if (recentCids) {
362
- console.log({
363
- api,
364
- seed,
365
- recentCids,
366
- poolId,
367
- replicationNo,
368
- });
369
- const res = await batchUploadManifest(
370
- api,
371
- seed,
372
- recentCids,
373
- poolId,
374
- replicationNo
375
- );
376
- console.log('batchUploadManifest res received');
377
- console.log(res);
378
- if (res && res.hash) {
379
- const signedBlock = await api.rpc.chain.getBlock(res.hash);
380
- if (signedBlock?.block?.extrinsics?.length) {
381
- await clearCidsFromRecent(recentCids);
382
- msg = res.hash;
383
- } else {
384
- status = false;
385
- msg = 'block data is not found';
386
- }
387
- } else {
388
- status = false;
389
- msg = 'hash is not returned';
390
- }
391
- } else {
392
- status = false;
393
- msg = 'No recent Cids found';
394
- }
395
- } else {
396
- status = false;
397
- msg = 'Account balance is not enough or account does not exists';
398
- }
399
- } catch (e: any) {
400
- console.log('res failed');
401
- console.log(e);
402
- let errorMessage = '';
403
-
404
- if (e instanceof Error) {
405
- // If it's an Error instance, use the message property
406
- errorMessage = e.message;
407
- } else {
408
- // If it's not an Error instance, convert it to string
409
- errorMessage = e.toString();
410
- }
411
- status = false;
412
- msg = errorMessage;
413
- }
414
- }
415
-
416
- // Return a value (true/false) depending on the outcome of the function
417
- // For example:
418
- return { status: status, msg: msg }; // or false, depending on your logic
419
- };
1
+ import Fula from '../interfaces/fulaNativeModule';
2
+ import {
3
+ init as chainApiInit,
4
+ batchUploadManifest,
5
+ checkAccountBalance,
6
+ getAccountIdFromSeed,
7
+ } from './chain-api';
8
+ import { ApiPromise } from '@polkadot/api';
9
+
10
+ /**
11
+ * Register the app's lifecycle listeners to handle foreground, background, and termination states.
12
+ */
13
+ export const registerLifecycleListener = (): Promise<void> => {
14
+ console.log('called registerLifecycleListener');
15
+ return Fula.registerLifecycleListener();
16
+ };
17
+
18
+ /**
19
+ * Get gets the value corresponding to the given key from the local datastore.
20
+ // The key must be a valid ipld.Link.
21
+ * @param config
22
+ * @returns boolean
23
+ */
24
+
25
+ export const init = (
26
+ identity: string, //privateKey of did identity
27
+ storePath: string,
28
+ bloxAddr: string,
29
+ exchange: string,
30
+ autoFlush: boolean = false,
31
+ rootCid: string | null = null,
32
+ useRelay: boolean = true,
33
+ refresh: boolean = false
34
+ ): Promise<{ peerId: string; rootCid: string }> => {
35
+ console.log(
36
+ 'init in react-native started',
37
+ identity,
38
+ storePath,
39
+ bloxAddr,
40
+ exchange,
41
+ autoFlush,
42
+ useRelay
43
+ );
44
+ return Fula.initFula(
45
+ identity,
46
+ storePath,
47
+ bloxAddr,
48
+ exchange,
49
+ autoFlush,
50
+ rootCid,
51
+ useRelay,
52
+ refresh
53
+ );
54
+ };
55
+
56
+ /**
57
+ * Get gets the value corresponding to the given key from the local datastore.
58
+ // The key must be a valid ipld.Link.
59
+ * @param config
60
+ * @returns boolean
61
+ */
62
+
63
+ export const newClient = (
64
+ identity: string, //privateKey of did identity
65
+ storePath: string,
66
+ bloxAddr: string,
67
+ exchange: string,
68
+ autoFlush: boolean = false,
69
+ useRelay: boolean = true,
70
+ refresh: boolean = false
71
+ ): Promise<string> => {
72
+ console.log(
73
+ 'newClient in react-native started',
74
+ identity,
75
+ storePath,
76
+ bloxAddr,
77
+ exchange,
78
+ autoFlush,
79
+ useRelay,
80
+ refresh
81
+ );
82
+ return Fula.newClient(
83
+ identity,
84
+ storePath,
85
+ bloxAddr,
86
+ exchange,
87
+ autoFlush,
88
+ useRelay,
89
+ refresh
90
+ );
91
+ };
92
+
93
+ /**
94
+ * rm removes all data
95
+ * @param path
96
+ * @returns string: new cid of the root
97
+ */
98
+ export const logout = (
99
+ identity: string,
100
+ storePath: string
101
+ ): Promise<boolean> => {
102
+ return Fula.logout(identity, storePath);
103
+ };
104
+
105
+ /**
106
+ * Checks if there are any un-synced changes on the device
107
+ */
108
+ export const checkFailedActions = (
109
+ retry: boolean = false,
110
+ timeout: number = 20
111
+ ): Promise<boolean> => {
112
+ return Fula.checkFailedActions(retry, timeout);
113
+ };
114
+
115
+ /**
116
+ * Lists the cids that failed to be sent to backend and are kept only locally
117
+ */
118
+ export const listFailedActions = (cids: string[] = []): Promise<string[]> => {
119
+ return Fula.listFailedActions(cids);
120
+ };
121
+
122
+ /**
123
+ * Lists the cids that are recent
124
+ */
125
+ export const listRecentCidsAsString = (): Promise<string[]> => {
126
+ return Fula.listRecentCidsAsString();
127
+ };
128
+
129
+ /**
130
+ * Clears the cids that ar recent
131
+ */
132
+ export const clearCidsFromRecent = (cids: string[] = []): Promise<boolean> => {
133
+ return Fula.clearCidsFromRecent(cids);
134
+ };
135
+
136
+ /**
137
+ * Checks if there are any un-synced changes on the device
138
+ */
139
+ export const checkConnection = (timeout: number = 20): Promise<boolean> => {
140
+ return Fula.checkConnection(timeout);
141
+ };
142
+
143
+ /**
144
+ * Get gets the value corresponding to the given key from the local datastore.
145
+ // The key must be a valid ipld.Link.
146
+ * @param key
147
+ * @returns value
148
+ */
149
+ export const get = (key: string): Promise<string> => {
150
+ return Fula.get(key);
151
+ };
152
+
153
+ /**
154
+ * Has checks whether the value corresponding to the given key is present in the local datastore.
155
+ // The key must be a valid ipld.Link.
156
+ * @param key
157
+ * @returns boolean
158
+ */
159
+ export const has = (key: Uint8Array): Promise<boolean> => {
160
+ return Fula.has(key);
161
+ };
162
+
163
+ /**
164
+ * Push requests the given addr to download the root cid from this node.
165
+ // The addr must be a valid multiaddr that includes peer ID.
166
+ // this function.
167
+ * @param addr
168
+ * @returns null or error
169
+ */
170
+ export const push = (): Promise<string> => {
171
+ return Fula.push();
172
+ };
173
+
174
+ //This method sends some test data to backedn
175
+ export const testData = (
176
+ identity: string,
177
+ bloxAddr: string
178
+ ): Promise<string> => {
179
+ return Fula.testData(identity, bloxAddr);
180
+ };
181
+
182
+ /**
183
+ * Put stores the given key value onto the local datastore.
184
+ // The key must be a valid ipld.Link and the value must be the valid encoded ipld.Node corresponding
185
+ // to the given key.
186
+ * @param key, value
187
+ * @returns null or string
188
+ */
189
+ export const put = (value: string, codec: string): Promise<string> => {
190
+ return Fula.put(value, codec);
191
+ };
192
+
193
+ /**
194
+ * mkdir creates a directory at the given path.
195
+ * @param path
196
+ * @returns string: new cid of the root
197
+ */
198
+ export const mkdir = (path: string): Promise<string> => {
199
+ return Fula.mkdir(path);
200
+ };
201
+
202
+ /**
203
+ * writeFileContent writes content at a given path
204
+ * @param path
205
+ * @returns string: new cid of the root
206
+ */
207
+ export const writeFileContent = (
208
+ path: string,
209
+ content: string
210
+ ): Promise<string> => {
211
+ return Fula.writeFileContent(path, content);
212
+ };
213
+
214
+ /*
215
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
216
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
217
+ // It keeps the original file modiifcation date
218
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
219
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
220
+ // Returns: new cid of the root after this file is placed in the tree
221
+ */
222
+ export const writeFile = (
223
+ fulaTargetFilename: string,
224
+ localFilename: string
225
+ ): Promise<string> => {
226
+ return Fula.writeFile(fulaTargetFilename, localFilename);
227
+ };
228
+
229
+ /**
230
+ * ls lists the name of files and folders at a given path
231
+ * @param path
232
+ * @returns string: list of items
233
+ * TODO: Findout how is the string and convert to array
234
+ */
235
+ export const ls = (path: string): Promise<void | JSON> => {
236
+ return Fula.ls(path)
237
+ .then((res) => {
238
+ let lsResult = [];
239
+ let lsRows = res.split('!!!');
240
+ for (const element of lsRows) {
241
+ let rowItems = element.split('???');
242
+ if (rowItems && rowItems[0]) {
243
+ let item = {
244
+ name: '',
245
+ created: '',
246
+ modified: '',
247
+ };
248
+ item.name = rowItems[0];
249
+ if (rowItems[1]) {
250
+ item.created = rowItems[1];
251
+ }
252
+ if (rowItems[2]) {
253
+ item.modified = rowItems[2];
254
+ }
255
+ lsResult.push(item);
256
+ }
257
+ }
258
+ let jsonRes = JSON.parse(JSON.stringify(lsResult));
259
+ return jsonRes;
260
+ })
261
+ .catch((e) => {
262
+ return e;
263
+ });
264
+ };
265
+
266
+ /**
267
+ * rm removes all files and folders at a given path
268
+ * @param path
269
+ * @returns string: new cid of the root
270
+ */
271
+ export const rm = (path: string): Promise<string> => {
272
+ return Fula.rm(path);
273
+ };
274
+
275
+ /**
276
+ * cp copies the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
277
+ * @param sourcePath, targetPath
278
+ * @returns string: new cid of the root
279
+ */
280
+ export const cp = (sourcePath: string, targetPath: string): Promise<string> => {
281
+ return Fula.cp(sourcePath, targetPath);
282
+ };
283
+
284
+ /**
285
+ * mv moves the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
286
+ * @param sourcePath, targetPath
287
+ * @returns string: new cid of the root
288
+ */
289
+ export const mv = (sourcePath: string, targetPath: string): Promise<string> => {
290
+ return Fula.mv(sourcePath, targetPath);
291
+ };
292
+
293
+ /*
294
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
295
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
296
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
297
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
298
+ // Returns: new cid of the root after this file is placed in the tree
299
+ */
300
+ export const readFile = (
301
+ fulaTargetFilename: string,
302
+ localFilename: string
303
+ ): Promise<string> => {
304
+ return Fula.readFile(fulaTargetFilename, localFilename);
305
+ };
306
+
307
+ /**
308
+ * readFile reads content of a given path
309
+ * @param path
310
+ * @returns string: cotent
311
+ */
312
+ export const readFileContent = (path: string): Promise<string> => {
313
+ return Fula.readFileContent(path);
314
+ };
315
+
316
+ /**
317
+ * Shutdown closes all resources used by Client.
318
+ // After calling this function Client must be discarded.
319
+ * @param
320
+ * @returns
321
+ */
322
+ export const shutdown = (): Promise<void> => {
323
+ return Fula.shutdown();
324
+ };
325
+
326
+ /**
327
+ * setAuth adds or removes a peer from the list of peers that are allowed to push to this node.
328
+ * This can only be called on a peer that is added as an owner of blox by --authorizer parameter
329
+ * @param peerId, allow
330
+ * @returns boolean: true if successful or false if not
331
+ */
332
+ export const setAuth = (peerId: string, allow: boolean): Promise<boolean> => {
333
+ return Fula.setAuth(peerId, allow);
334
+ };
335
+
336
+ /**
337
+ * isReady checks if the connection is ready to be used.
338
+ * @param filesystemCheck: also check if the wnfs is ready
339
+ * @returns boolean: true if ready or false if not
340
+ */
341
+ export const isReady = (filesystemCheck: boolean = true): Promise<boolean> => {
342
+ return Fula.isReady(filesystemCheck);
343
+ };
344
+
345
+ /**
346
+ * replicate replicates data on the nework
347
+ */
348
+ export const replicateRecentCids = async (
349
+ api: ApiPromise,
350
+ seed: string,
351
+ poolId: number,
352
+ replicationNo: number = 4
353
+ ): Promise<{ status: boolean; msg: string }> => {
354
+ let status = true;
355
+ let msg = '';
356
+ if (!api) {
357
+ api = await chainApiInit();
358
+ }
359
+ if (api) {
360
+ console.log('uploading manifests');
361
+ try {
362
+ let account = await getAccountIdFromSeed(seed);
363
+ console.log('account: ' + account);
364
+ const accountBal = await checkAccountBalance(api, account);
365
+ console.log('account balance: ' + accountBal);
366
+ if (accountBal !== '0') {
367
+ const recentCids = await listRecentCidsAsString();
368
+ console.log(recentCids);
369
+ if (recentCids) {
370
+ console.log({
371
+ api,
372
+ seed,
373
+ recentCids,
374
+ poolId,
375
+ replicationNo,
376
+ });
377
+ const res = await batchUploadManifest(
378
+ api,
379
+ seed,
380
+ recentCids,
381
+ poolId,
382
+ replicationNo
383
+ );
384
+ console.log('batchUploadManifest res received');
385
+ console.log(res);
386
+ if (res && res.hash) {
387
+ const signedBlock = await api.rpc.chain.getBlock(res.hash);
388
+ if (signedBlock?.block?.extrinsics?.length) {
389
+ await clearCidsFromRecent(recentCids);
390
+ msg = res.hash;
391
+ } else {
392
+ status = false;
393
+ msg = 'block data is not found';
394
+ }
395
+ } else {
396
+ status = false;
397
+ msg = 'hash is not returned';
398
+ }
399
+ } else {
400
+ status = false;
401
+ msg = 'No recent Cids found';
402
+ }
403
+ } else {
404
+ status = false;
405
+ msg = 'Account balance is not enough or account does not exists';
406
+ }
407
+ } catch (e: any) {
408
+ console.log('res failed');
409
+ console.log(e);
410
+ let errorMessage = '';
411
+
412
+ if (e instanceof Error) {
413
+ // If it's an Error instance, use the message property
414
+ errorMessage = e.message;
415
+ } else {
416
+ // If it's not an Error instance, convert it to string
417
+ errorMessage = e.toString();
418
+ }
419
+ status = false;
420
+ msg = errorMessage;
421
+ }
422
+ }
423
+
424
+ // Return a value (true/false) depending on the outcome of the function
425
+ // For example:
426
+ return { status: status, msg: msg }; // or false, depending on your logic
427
+ };