-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathindex.ts
More file actions
733 lines (640 loc) · 24.7 KB
/
index.ts
File metadata and controls
733 lines (640 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
import crypto from 'crypto';
import path from 'path';
import chalk from 'chalk';
import fs from 'fs-extra';
import * as toolbox from 'gluegun';
import immutable from 'immutable';
import type { IPFSHTTPClient } from 'ipfs-http-client';
import yaml from 'js-yaml';
import { Spinner, step, withSpinner } from '../command-helpers/spinner';
import debug from '../debug';
import { applyMigrations } from '../migrations';
import Protocol from '../protocols';
import Subgraph from '../subgraph';
import Watcher from '../watcher';
import * as asc from './asc';
const compilerDebug = debug('graph-cli:compiler');
interface CompilerOptions {
ipfs: any;
subgraphManifest: string;
outputDir: string;
outputFormat: string;
skipMigrations: boolean;
blockIpfsMethods?: RegExpMatchArray;
protocol: Protocol;
}
export default class Compiler {
private ipfs: IPFSHTTPClient;
private sourceDir: string;
private blockIpfsMethods?: RegExpMatchArray;
private libsDirs: string[];
private protocol: Protocol;
private ABI: any;
constructor(private options: CompilerOptions) {
this.options = options;
this.ipfs = options.ipfs;
this.sourceDir = path.dirname(options.subgraphManifest);
this.blockIpfsMethods = options.blockIpfsMethods;
this.libsDirs = [];
this.protocol = this.options.protocol;
this.ABI = this.protocol.getABI();
if (options.protocol.name === 'substreams') {
return;
}
process.on('uncaughtException', e => {
toolbox.print.error(`UNCAUGHT EXCEPTION: ${e}`);
});
}
subgraphDir(parent: string, subgraph: immutable.Map<any, any>) {
return path.join(parent, subgraph.get('name'));
}
displayPath(p: string) {
return path.relative(process.cwd(), p);
}
cacheKeyForFile(filename: string) {
const hash = crypto.createHash('sha1');
hash.update(fs.readFileSync(filename));
return hash.digest('hex');
}
async compile({
validate = false,
}: {
/**
* Whether to validate the compiled artifacts.
*/
validate: boolean;
}) {
try {
if (!this.options.skipMigrations) {
await applyMigrations({
sourceDir: this.sourceDir,
manifestFile: this.options.subgraphManifest,
});
}
const subgraph = await this.loadSubgraph();
const compiledSubgraph = await this.compileSubgraph(subgraph, validate);
const localSubgraph = await this.writeSubgraphToOutputDirectory(
this.options.protocol,
compiledSubgraph,
);
if (this.ipfs !== undefined) {
const ipfsHash = await this.uploadSubgraphToIPFS(localSubgraph);
this.completed(ipfsHash);
return ipfsHash;
}
this.completed(path.join(this.options.outputDir, 'subgraph.yaml'));
return true;
} catch (e) {
toolbox.print.error(e);
return false;
}
}
completed(ipfsHashOrPath: string) {
toolbox.print.info('');
toolbox.print.success(`Build completed: ${chalk.blue(ipfsHashOrPath)}`);
toolbox.print.info('');
}
async loadSubgraph({ quiet } = { quiet: false }) {
const subgraphLoadOptions = { protocol: this.protocol, skipValidation: false };
if (quiet) {
return (await Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions)).result;
}
const manifestPath = this.displayPath(this.options.subgraphManifest);
return await withSpinner(
`Load subgraph from ${manifestPath}`,
`Failed to load subgraph from ${manifestPath}`,
`Warnings loading subgraph from ${manifestPath}`,
async () => {
return Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions);
},
);
}
async getFilesToWatch() {
try {
const files = [];
const subgraph = await this.loadSubgraph({ quiet: true });
// Add the subgraph manifest file
files.push(this.options.subgraphManifest);
// Add all file paths specified in manifest
files.push(path.resolve(subgraph.getIn(['schema', 'file'])));
subgraph.get('dataSources').map((dataSource: any) => {
files.push(dataSource.getIn(['mapping', 'file']));
// Only watch ABI related files if the target protocol has support/need for them.
if (this.protocol.hasABIs()) {
dataSource.getIn(['mapping', 'abis']).map((abi: any) => {
files.push(abi.get('file'));
});
}
});
// Make paths absolute
return files.map(file => path.resolve(file));
} catch (e) {
throw Error(`Failed to load subgraph: ${e.message}`);
}
}
async watchAndCompile(onCompiled?: (ipfsHash: string) => void) {
const compiler = this;
let spinner: Spinner;
// Create watcher and recompile once and then on every change to a watched file
const watcher = new Watcher({
onReady: () => (spinner = toolbox.print.spin('Watching subgraph files')),
onTrigger: async changedFile => {
if (changedFile !== undefined) {
spinner.info(`File change detected: ${this.displayPath(changedFile)}\n`);
}
const ipfsHash = await compiler.compile({ validate: false });
onCompiled?.(ipfsHash);
spinner.start();
},
onCollectFiles: async () => await compiler.getFilesToWatch(),
onError: error => {
spinner.stop();
toolbox.print.error(`${error.message}\n`);
spinner.start();
},
});
// Catch keyboard interrupt: close watcher and exit process
process.on('SIGINT', () => {
watcher.close();
process.exit();
});
try {
await watcher.watch();
} catch (e) {
toolbox.print.error(String(e.message));
}
}
_writeSubgraphFile(
maybeRelativeFile: string,
data: string | Buffer,
sourceDir: string,
targetDir: string,
spinner: Spinner,
) {
const absoluteSourceFile = path.resolve(sourceDir, maybeRelativeFile);
const relativeSourceFile = path.relative(sourceDir, absoluteSourceFile);
const targetFile = path.join(targetDir, relativeSourceFile);
step(spinner, 'Write subgraph file', this.displayPath(targetFile));
fs.mkdirsSync(path.dirname(targetFile));
fs.writeFileSync(targetFile, data);
return targetFile;
}
async compileSubgraph(subgraph: any, validate = false) {
return await withSpinner(
`Compile subgraph`,
`Failed to compile subgraph`,
`Warnings while compiling subgraph`,
async spinner => {
// Cache compiled files so identical input files are only compiled once
const compiledFiles = new Map();
await asc.ready();
subgraph = subgraph.update('dataSources', (dataSources: any[]) =>
dataSources.map((dataSource: any) =>
dataSource.updateIn(['mapping', 'file'], (mappingPath: string) =>
this._compileDataSourceMapping(
this.protocol,
dataSource,
mappingPath,
compiledFiles,
spinner,
validate,
),
),
),
);
subgraph = subgraph.update('templates', (templates: any) =>
templates === undefined
? templates
: templates.map((template: any) =>
template.updateIn(['mapping', 'file'], (mappingPath: string) =>
this._compileTemplateMapping(template, mappingPath, compiledFiles, spinner),
),
),
);
return subgraph;
},
);
}
/**
* Validate that the compiled WASM has all the handlers that are defined in the subgraph manifest
*
* @returns a list of handlers that are missing from the compiled WASM
*
* This is a temporary solution to validate that the compiled WASM has all the event handlers.
* A better way would be if we can do this even before compiling
* but requires a larger refactor so we are running additional validations before compilation
*/
_validateHandlersInWasm({
pathToWasm,
dataSource,
}: {
pathToWasm: string;
dataSource: immutable.Map<any, any>;
}) {
const getHandlerNames = (handlerName: string) =>
(dataSource as any)
.getIn(['mapping', handlerName])
// if there is no handler, it will be undefined
?.toJS()
?.map((e: { handler: string; event: string }) => e.handler) || [];
// Load the compiled WASM file
const buffer = fs.readFileSync(pathToWasm);
const wasmMod = new WebAssembly.Module(buffer);
// Apologies to TS gods for `any` usage
// Yet another reason to refactor out immutable.js
const handlerNamesFromDataSources = [
// TODO: this is hacky, better is figuring out how to utilize the `protocol.getSubgraph().handlerTypes()`
...getHandlerNames('eventHandlers'),
...getHandlerNames('callHandlers'),
...getHandlerNames('blockHandlers'),
...getHandlerNames('transactionHandlers'),
...getHandlerNames('messageHandlers'),
...getHandlerNames('receiptHandlers'),
];
// We can check the WASM module for the exported functions
// https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Module/exports
// Using a Set to avoid duplicates and makes it easier to check if a value is present
const handlerNamesFromWasm = new Set(
WebAssembly.Module.exports(wasmMod)
.filter(e => e.kind === 'function')
.map(e => e.name),
);
// Figure out which handlers are missing
const missingHandlers = handlerNamesFromDataSources.filter(
(handler: string) => !handlerNamesFromWasm.has(handler),
);
return missingHandlers;
}
_compileDataSourceMapping(
protocol: Protocol,
dataSource: immutable.Map<any, any>,
mappingPath: string,
compiledFiles: Map<any, any>,
spinner: Spinner,
validate = false,
) {
if (protocol.name == 'substreams') {
return;
}
try {
const dataSourceName = dataSource.getIn(['name']);
const baseDir = this.sourceDir;
const absoluteMappingPath = path.resolve(baseDir, mappingPath);
const inputFile = path.relative(baseDir, absoluteMappingPath);
this._validateMappingContent(absoluteMappingPath);
// If the file has already been compiled elsewhere, just use that output
// file and return early
const inputCacheKey = this.cacheKeyForFile(absoluteMappingPath);
const alreadyCompiled = compiledFiles.has(inputCacheKey);
if (alreadyCompiled) {
const outFile = compiledFiles.get(inputCacheKey);
step(
spinner,
'Compile data source:',
`${dataSourceName} => ${this.displayPath(outFile)} (already compiled)`,
);
return outFile;
}
const outFile = path.resolve(
this.subgraphDir(this.options.outputDir, dataSource),
this.options.outputFormat == 'wasm' ? `${dataSourceName}.wasm` : `${dataSourceName}.wast`,
);
step(spinner, 'Compile data source:', `${dataSourceName} => ${this.displayPath(outFile)}`);
const outputFile = path.relative(baseDir, outFile);
// Create output directory
fs.mkdirsSync(path.dirname(outFile));
const libs = this.libsDirs.join(',');
const global = require.resolve('@graphprotocol/graph-ts/global/global.ts');
compilerDebug('Global: %s', global);
asc.compile({
inputFile,
global,
baseDir,
libs,
outputFile,
});
if (validate) {
const missingHandlers = this._validateHandlersInWasm({
pathToWasm: outFile,
dataSource,
});
if (missingHandlers.length > 0) {
throw Error(`\n\tMissing handlers in WASM: ${missingHandlers.join(', ')}`);
}
}
// Remember the output file to avoid compiling the same file again
compiledFiles.set(inputCacheKey, outFile);
return outFile;
} catch (e) {
throw Error(`Failed to compile data source mapping: ${e.message}`);
}
}
_compileTemplateMapping(
template: immutable.Collection<any, any>,
mappingPath: string,
compiledFiles: Map<any, any>,
spinner: Spinner,
) {
try {
const templateName = template.get('name');
const baseDir = this.sourceDir;
const absoluteMappingPath = path.resolve(baseDir, mappingPath);
const inputFile = path.relative(baseDir, absoluteMappingPath);
this._validateMappingContent(absoluteMappingPath);
// If the file has already been compiled elsewhere, just use that output
// file and return early
const inputCacheKey = this.cacheKeyForFile(absoluteMappingPath);
const alreadyCompiled = compiledFiles.has(inputCacheKey);
if (alreadyCompiled) {
const outFile = compiledFiles.get(inputCacheKey);
step(
spinner,
'Compile data source template:',
`${templateName} => ${this.displayPath(outFile)} (already compiled)`,
);
return outFile;
}
const outFile = path.resolve(
this.options.outputDir,
'templates',
templateName,
this.options.outputFormat == 'wasm' ? `${templateName}.wasm` : `${templateName}.wast`,
);
step(
spinner,
'Compile data source template:',
`${templateName} => ${this.displayPath(outFile)}`,
);
const outputFile = path.relative(baseDir, outFile);
// Create output directory
fs.mkdirsSync(path.dirname(outFile));
const libs = this.libsDirs.join(',');
const global = require.resolve('@graphprotocol/graph-ts/global/global.ts');
compilerDebug('Global: %s', global);
asc.compile({
inputFile,
global,
baseDir,
libs,
outputFile,
});
// Remember the output file to avoid compiling the same file again
compiledFiles.set(inputCacheKey, outFile);
return outFile;
} catch (e) {
throw Error(`Failed to compile data source template: ${e.message}`);
}
}
_validateMappingContent(filePath: string) {
const data = fs.readFileSync(filePath);
if (this.blockIpfsMethods && (data.includes('ipfs.cat') || data.includes('ipfs.map'))) {
throw Error(`
Subgraph Studio does not support mappings with ipfs methods.
Please remove all instances of ipfs.cat and ipfs.map from
${filePath}
`);
}
}
async writeSubgraphToOutputDirectory(protocol: Protocol, subgraph: immutable.Map<any, any>) {
const displayDir = `${this.displayPath(this.options.outputDir)}${toolbox.filesystem.separator}`;
// ensure that the output directory exists
fs.mkdirsSync(this.options.outputDir);
return await withSpinner(
`Write compiled subgraph to ${displayDir}`,
`Failed to write compiled subgraph to ${displayDir}`,
`Warnings while writing compiled subgraph to ${displayDir}`,
async spinner => {
// Copy schema and update its path
subgraph = subgraph.updateIn(['schema', 'file'], schemaFile => {
const schemaFilePath = path.resolve(this.sourceDir, schemaFile as string);
const schemaFileName = path.basename(schemaFile as string);
const targetFile = path.resolve(this.options.outputDir, schemaFileName);
step(spinner, 'Copy schema file', this.displayPath(targetFile));
fs.copyFileSync(schemaFilePath, targetFile);
return path.relative(this.options.outputDir, targetFile);
});
// Copy data source files and update their paths
subgraph = subgraph.update('dataSources', (dataSources: any[]) =>
dataSources.map(dataSource => {
let updatedDataSource = dataSource;
if (this.protocol.hasABIs()) {
updatedDataSource = updatedDataSource
// Write data source ABIs to the output directory
.updateIn(['mapping', 'abis'], (abis: any[]) =>
abis.map((abi: any) =>
abi.update('file', (abiFile: string) => {
abiFile = path.resolve(this.sourceDir, abiFile);
const abiData = this.ABI.load(abi.get('name'), abiFile);
return path.relative(
this.options.outputDir,
this._writeSubgraphFile(
abiFile,
JSON.stringify(abiData.data.toJS(), null, 2),
this.sourceDir,
this.subgraphDir(this.options.outputDir, dataSource),
spinner,
),
);
}),
),
);
}
if (protocol.name == 'substreams') {
updatedDataSource = updatedDataSource
// Write data source ABIs to the output directory
.updateIn(['source', 'package'], (substreamsPackage: any) =>
substreamsPackage.update('file', (packageFile: string) => {
packageFile = path.resolve(this.sourceDir, packageFile);
const packageContent = fs.readFileSync(packageFile);
return path.relative(
this.options.outputDir,
this._writeSubgraphFile(
packageFile,
packageContent,
this.sourceDir,
this.subgraphDir(this.options.outputDir, dataSource),
spinner,
),
);
}),
);
return updatedDataSource;
}
// The mapping file is already being written to the output
// directory by the AssemblyScript compiler
return updatedDataSource.updateIn(['mapping', 'file'], (mappingFile: string) =>
path.relative(this.options.outputDir, path.resolve(this.sourceDir, mappingFile)),
);
}),
);
// Copy template files and update their paths
subgraph = subgraph.update('templates', templates =>
templates === undefined
? templates
: templates.map((template: any) => {
let updatedTemplate = template;
if (this.protocol.hasABIs()) {
updatedTemplate = updatedTemplate
// Write template ABIs to the output directory
.updateIn(['mapping', 'abis'], (abis: any[]) =>
abis.map(abi =>
abi.update('file', (abiFile: string) => {
abiFile = path.resolve(this.sourceDir, abiFile);
const abiData = this.ABI.load(abi.get('name'), abiFile);
return path.relative(
this.options.outputDir,
this._writeSubgraphFile(
abiFile,
JSON.stringify(abiData.data.toJS(), null, 2),
this.sourceDir,
this.subgraphDir(this.options.outputDir, template),
spinner,
),
);
}),
),
);
}
// The mapping file is already being written to the output
// directory by the AssemblyScript compiler
return updatedTemplate.updateIn(['mapping', 'file'], (mappingFile: string) =>
path.relative(this.options.outputDir, path.resolve(this.sourceDir, mappingFile)),
);
}),
);
// Write the subgraph manifest itself
const outputFilename = path.join(this.options.outputDir, 'subgraph.yaml');
step(spinner, 'Write subgraph manifest', this.displayPath(outputFilename));
await Subgraph.write(subgraph, outputFilename);
return subgraph;
},
);
}
async uploadSubgraphToIPFS(subgraph: immutable.Map<any, any>) {
return withSpinner(
`Upload subgraph to IPFS`,
`Failed to upload subgraph to IPFS`,
`Warnings while uploading subgraph to IPFS`,
async spinner => {
// Cache uploaded IPFS files so identical files are only uploaded once
const uploadedFiles = new Map();
// Collect all source (path -> hash) updates to apply them later
const updates = [];
// Upload the schema to IPFS
updates.push({
keyPath: ['schema', 'file'],
value: await this._uploadFileToIPFS(
subgraph.getIn(['schema', 'file']) as string,
uploadedFiles,
spinner,
),
});
if (this.protocol.hasABIs()) {
for (const [i, dataSource] of subgraph.get('dataSources').entries()) {
for (const [j, abi] of dataSource.getIn(['mapping', 'abis']).entries()) {
updates.push({
keyPath: ['dataSources', i, 'mapping', 'abis', j, 'file'],
value: await this._uploadFileToIPFS(abi.get('file'), uploadedFiles, spinner),
});
}
}
}
// Upload all mappings
if (this.protocol.name === 'substreams') {
for (const [i, dataSource] of subgraph.get('dataSources').entries()) {
updates.push({
keyPath: ['dataSources', i, 'source', 'package', 'file'],
value: await this._uploadFileToIPFS(
dataSource.getIn(['source', 'package', 'file']),
uploadedFiles,
spinner,
),
});
}
} else {
for (const [i, dataSource] of subgraph.get('dataSources').entries()) {
updates.push({
keyPath: ['dataSources', i, 'mapping', 'file'],
value: await this._uploadFileToIPFS(
dataSource.getIn(['mapping', 'file']),
uploadedFiles,
spinner,
),
});
}
}
for (const [i, template] of subgraph.get('templates', immutable.List()).entries()) {
if (this.protocol.hasABIs()) {
for (const [j, abi] of template.getIn(['mapping', 'abis']).entries()) {
updates.push({
keyPath: ['templates', i, 'mapping', 'abis', j, 'file'],
value: await this._uploadFileToIPFS(abi.get('file'), uploadedFiles, spinner),
});
}
}
updates.push({
keyPath: ['templates', i, 'mapping', 'file'],
value: await this._uploadFileToIPFS(
template.getIn(['mapping', 'file']),
uploadedFiles,
spinner,
),
});
}
// Apply all updates to the subgraph
for (const update of updates) {
subgraph = subgraph.setIn(update.keyPath, update.value);
}
// Upload the subgraph itself
return await this._uploadSubgraphDefinitionToIPFS(subgraph);
},
);
}
async _uploadFileToIPFS(
maybeRelativeFile: string,
uploadedFiles: Map<any, any>,
spinner: Spinner,
) {
compilerDebug(
'Resolving IPFS file "%s" from output dir "%s"',
maybeRelativeFile,
this.options.outputDir,
);
const absoluteFile = path.resolve(this.options.outputDir, maybeRelativeFile);
step(spinner, 'Add file to IPFS', this.displayPath(absoluteFile));
const uploadCacheKey = this.cacheKeyForFile(absoluteFile);
const alreadyUploaded = uploadedFiles.has(uploadCacheKey);
if (!alreadyUploaded) {
// @ts-expect-error Buffer.from with Buffer data can indeed accept utf-8
const content = Buffer.from(await fs.readFile(absoluteFile), 'utf-8');
const hash = await this._uploadToIPFS({
path: path.relative(this.options.outputDir, absoluteFile),
content,
});
uploadedFiles.set(uploadCacheKey, hash);
}
const hash = uploadedFiles.get(uploadCacheKey);
step(spinner, ' ..', `${hash}${alreadyUploaded ? ' (already uploaded)' : ''}`);
return immutable.fromJS({ '/': `/ipfs/${hash}` });
}
async _uploadSubgraphDefinitionToIPFS(subgraph: immutable.Map<any, any>) {
const str = yaml.safeDump(subgraph.toJS(), { noRefs: true, sortKeys: true });
const file = { path: 'subgraph.yaml', content: Buffer.from(str, 'utf-8') };
return await this._uploadToIPFS(file);
}
async _uploadToIPFS(file: { path: string; content: Buffer }) {
try {
const files = this.ipfs.addAll([file]);
// We get back async iterable
const filesIterator = files[Symbol.asyncIterator]();
// We only care about the first item, since that is the file, rest could be directories
const { value } = await filesIterator.next();
// we grab the file and pin it
const uploadedFile = value as Awaited<ReturnType<typeof this.ipfs.add>>;
await this.ipfs.pin.add(uploadedFile.cid);
return uploadedFile.cid.toString();
} catch (e) {
throw Error(`Failed to upload file to IPFS: ${e.message}`);
}
}
}