-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathupload-flow.ts
More file actions
321 lines (288 loc) · 9.82 KB
/
Copy pathupload-flow.ts
File metadata and controls
321 lines (288 loc) · 9.82 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
/**
* Common upload flow shared between import and add commands
*
* This module provides reusable functions for the Synapse upload workflow
* including payment validation, storage context creation, and result display.
*/
import type { Synapse } from '@filoz/synapse-sdk'
import type { CID } from 'multiformats/cid'
import pc from 'picocolors'
import type { Logger } from 'pino'
import type { PaymentCapacityCheck } from '../core/payments/index.js'
import { cleanupSynapseService, type SynapseService } from '../core/synapse/index.js'
import { checkUploadReadiness, executeUpload, getDownloadURL, type SynapseUploadResult } from '../core/upload/index.js'
import { formatUSDFC } from '../core/utils/format.js'
import { autoFund } from '../payments/fund.js'
import type { AutoFundOptions } from '../payments/types.js'
import type { Spinner } from '../utils/cli-helpers.js'
import { cancel, formatFileSize } from '../utils/cli-helpers.js'
import { log } from '../utils/cli-logger.js'
export interface UploadFlowOptions {
/**
* Context identifier for logging (e.g., 'import', 'add')
*/
contextType: string
/**
* Size of the file being uploaded in bytes
*/
fileSize: number
/**
* Logger instance
*/
logger: Logger
/**
* Optional spinner for progress updates
*/
spinner?: Spinner
}
export interface UploadFlowResult extends SynapseUploadResult {
network: string
transactionHash?: string | undefined
}
/**
* Perform auto-funding if requested
* Automatically ensures a minimum of 10 days of runway based on current usage + new file requirements
*
* @param synapse - Initialized Synapse instance
* @param fileSize - Size of file being uploaded (in bytes)
* @param spinner - Optional spinner for progress
*/
export async function performAutoFunding(synapse: Synapse, fileSize: number, spinner?: Spinner): Promise<void> {
spinner?.start('Checking funding requirements for upload...')
try {
const fundOptions: AutoFundOptions = {
synapse,
fileSize,
}
if (spinner !== undefined) {
fundOptions.spinner = spinner
}
const result = await autoFund(fundOptions)
spinner?.stop(`${pc.green('✓')} Funding requirements met`)
if (result.adjusted) {
log.line('')
log.line(pc.bold('Auto-funding completed:'))
log.indent(`Deposited ${formatUSDFC(result.delta)} USDFC`)
log.indent(`Total deposited: ${formatUSDFC(result.newDepositedAmount)} USDFC`)
log.indent(
`Runway: ~${result.newRunwayDays} day(s)${result.newRunwayHours > 0 ? ` ${result.newRunwayHours} hour(s)` : ''}`
)
if (result.approvalTx) {
log.indent(pc.gray(`Approval tx: ${result.approvalTx}`))
}
if (result.transactionHash) {
log.indent(pc.gray(`Transaction: ${result.transactionHash}`))
}
log.line('')
log.flush()
}
} catch (error) {
spinner?.stop(`${pc.red('✗')} Auto-funding failed`)
log.line('')
log.line(`${pc.red('Error:')} ${error instanceof Error ? error.message : String(error)}`)
log.flush()
await cleanupSynapseService()
cancel('Operation cancelled - auto-funding failed')
process.exit(1)
}
}
/**
* Validate payment setup and capacity for upload
*
* @param synapse - Initialized Synapse instance
* @param fileSize - Size of file to upload in bytes
* @param spinner - Optional spinner for progress
* @returns true if validation passes, exits process if not
*/
export async function validatePaymentSetup(synapse: Synapse, fileSize: number, spinner?: Spinner): Promise<void> {
const readiness = await checkUploadReadiness({
synapse,
fileSize,
onProgress: (event) => {
if (!spinner) return
switch (event.type) {
case 'checking-balances': {
spinner.message('Checking payment setup requirements...')
return
}
case 'checking-allowances': {
spinner.message('Checking WarmStorage permissions...')
return
}
case 'configuring-allowances': {
spinner.message('Configuring WarmStorage permissions (one-time setup)...')
return
}
case 'validating-capacity': {
spinner.message('Validating payment capacity...')
return
}
case 'allowances-configured': {
// No spinner change; we log once readiness completes.
return
}
}
},
})
const { validation, allowances, capacity, suggestions } = readiness
if (!validation.isValid) {
spinner?.stop(`${pc.red('✗')} Payment setup incomplete`)
log.line('')
log.line(`${pc.red('✗')} ${validation.errorMessage}`)
if (validation.helpMessage) {
log.line('')
log.line(` ${pc.cyan(validation.helpMessage)}`)
}
log.line('')
log.line(`${pc.yellow('⚠')} Your payment setup is not complete. Please run:`)
log.indent(pc.cyan('filecoin-pin payments setup'))
log.line('')
log.line('For more information, run:')
log.indent(pc.cyan('filecoin-pin payments status'))
log.flush()
await cleanupSynapseService()
cancel('Operation cancelled - payment setup required')
process.exit(1)
}
if (allowances.updated) {
spinner?.stop(`${pc.green('✓')} WarmStorage permissions configured`)
if (allowances.transactionHash) {
log.indent(pc.gray(`Transaction: ${allowances.transactionHash}`))
log.flush()
}
spinner?.start('Validating payment capacity...')
} else {
spinner?.message('Validating payment capacity...')
}
if (!capacity?.canUpload) {
if (capacity) {
displayPaymentIssues(capacity, fileSize, spinner)
}
await cleanupSynapseService()
cancel('Operation cancelled - insufficient payment capacity')
process.exit(1)
}
// Show warning if suggestions exist (even if upload is possible)
if (suggestions.length > 0 && capacity?.canUpload) {
spinner?.stop(`${pc.yellow('⚠')} Payment capacity check passed with warnings`)
log.line('')
log.line(pc.bold('Suggestions:'))
suggestions.forEach((suggestion) => {
log.indent(`• ${suggestion}`)
})
log.flush()
} else {
spinner?.stop(`${pc.green('✓')} Payment capacity verified`)
}
}
/**
* Display payment capacity issues and suggestions
*/
function displayPaymentIssues(capacityCheck: PaymentCapacityCheck, fileSize: number, spinner?: Spinner): void {
spinner?.stop(`${pc.red('✗')} Insufficient deposit for this file`)
log.line('')
log.line(pc.bold('File Requirements:'))
log.indent(`File size: ${formatFileSize(fileSize)} (${capacityCheck.storageTiB.toFixed(4)} TiB)`)
log.indent(`Storage cost: ${formatUSDFC(capacityCheck.required.rateAllowance)} USDFC/epoch`)
log.indent(
`Required deposit: ${formatUSDFC(capacityCheck.required.lockupAllowance + capacityCheck.required.lockupAllowance / 10n)} USDFC`
)
log.indent(pc.gray('(includes 10-day safety reserve)'))
log.line('')
log.line(pc.bold('Suggested actions:'))
capacityCheck.suggestions.forEach((suggestion: string) => {
log.indent(`• ${suggestion}`)
})
log.line('')
// Calculate suggested deposit
const suggestedDeposit = capacityCheck.issues.insufficientDeposit
? formatUSDFC(capacityCheck.issues.insufficientDeposit)
: '0'
log.line(`${pc.yellow('⚠')} To fix this, run:`)
log.indent(pc.cyan(`filecoin-pin payments setup --deposit ${suggestedDeposit} --auto`))
log.flush()
}
/**
* Upload CAR data to Synapse with progress tracking
*
* @param synapseService - Initialized Synapse service with storage context
* @param carData - CAR file data as Uint8Array
* @param rootCid - Root CID of the content
* @param options - Upload flow options
* @returns Upload result with transaction hash
*/
export async function performUpload(
synapseService: SynapseService,
carData: Uint8Array,
rootCid: CID,
options: UploadFlowOptions
): Promise<UploadFlowResult> {
const { contextType, logger, spinner } = options
spinner?.start('Uploading to Filecoin...')
const uploadResult = await executeUpload(synapseService, carData, rootCid, {
logger,
contextId: `${contextType}-${Date.now()}`,
ipniValidation: { enabled: false },
callbacks: {
onUploadComplete: () => {
spinner?.message('Upload complete, adding to data set...')
},
onPieceAdded: (transaction) => {
if (transaction) {
spinner?.message('Piece added to data set, confirming on-chain...')
}
},
},
})
return {
...uploadResult,
network: synapseService.synapse.getNetwork(),
transactionHash: uploadResult.transactionHash,
}
}
/**
* Display results for import or add command
*
* @param result - Result data to display
* @param operation - Operation name ('Import' or 'Add')
*/
export function displayUploadResults(
result: {
filePath: string
fileSize: number
rootCid: string
pieceCid: string
pieceId?: number | undefined
dataSetId: string
providerInfo: any
transactionHash?: string | undefined
},
operation: string,
network: string
): void {
log.line(`Network: ${pc.bold(network)}`)
log.line('')
log.line(pc.bold(`${operation} Details`))
log.indent(`File: ${result.filePath}`)
log.indent(`Size: ${formatFileSize(result.fileSize)}`)
log.indent(`Root CID: ${result.rootCid}`)
log.line('')
log.line(pc.bold('Filecoin Storage'))
log.indent(`Piece CID: ${result.pieceCid}`)
log.indent(`Piece ID: ${result.pieceId?.toString() || 'N/A'}`)
log.indent(`Data Set ID: ${result.dataSetId}`)
log.line('')
log.line(pc.bold('Storage Provider'))
log.indent(`Provider ID: ${result.providerInfo.id}`)
log.indent(`Name: ${result.providerInfo.name}`)
const downloadURL = getDownloadURL(result.providerInfo, result.pieceCid)
if (downloadURL) {
log.indent(`Direct Download URL: ${downloadURL}`)
}
if (result.transactionHash) {
log.line('')
log.line(pc.bold('Transaction'))
log.indent(`Hash: ${result.transactionHash}`)
}
log.flush()
}