6
6
* options to complete the setup without user interaction.
7
7
*/
8
8
9
- import { RPC_URLS , Synapse , TIME_CONSTANTS } from '@filoz/synapse-sdk'
9
+ import { RPC_URLS , Synapse } from '@filoz/synapse-sdk'
10
10
import { ethers } from 'ethers'
11
11
import pc from 'picocolors'
12
+ import { calculateDepositCapacity , checkAndSetAllowances } from '../synapse/payments.js'
12
13
import { cleanupProvider } from '../synapse/service.js'
13
14
import { cancel , createSpinner , intro , outro } from '../utils/cli-helpers.js'
14
15
import { log } from '../utils/cli-logger.js'
15
16
import {
16
- calculateStorageAllowances ,
17
- calculateStorageFromUSDFC ,
18
17
checkFILBalance ,
19
18
checkUSDFCBalance ,
20
19
depositUSDFC ,
21
20
displayAccountInfo ,
22
21
displayDepositWarning ,
23
- displayPaymentSummary ,
24
- displayServicePermissions ,
25
22
formatUSDFC ,
26
23
getPaymentStatus ,
27
- parseStorageAllowance ,
28
- setServiceApprovals ,
29
24
validatePaymentRequirements ,
30
25
} from './setup.js'
31
- import type { PaymentSetupOptions , StorageAllowances } from './types.js'
26
+ import type { PaymentSetupOptions } from './types.js'
32
27
33
28
/**
34
29
* Run automatic payment setup with defaults
@@ -67,27 +62,6 @@ export async function runAutoSetup(options: PaymentSetupOptions): Promise<void>
67
62
process . exit ( 1 )
68
63
}
69
64
70
- // 4. Parse storage allowance early to validate format
71
- if ( ! options . rateAllowance ) {
72
- console . error ( pc . red ( 'Error: Storage allowance is required' ) )
73
- process . exit ( 1 )
74
- }
75
-
76
- let parsedTiB : number | null
77
- let rawUsdfcPerEpoch : string | null = null
78
- try {
79
- parsedTiB = parseStorageAllowance ( options . rateAllowance )
80
- if ( parsedTiB === null ) {
81
- // parseStorageAllowance already validated it's a valid USDFC amount
82
- // (it would have thrown otherwise), so we can safely save it
83
- rawUsdfcPerEpoch = options . rateAllowance
84
- }
85
- } catch ( error ) {
86
- console . error ( pc . red ( `Error: Invalid storage allowance '${ options . rateAllowance } '` ) )
87
- console . error ( pc . red ( error instanceof Error ? error . message : String ( error ) ) )
88
- process . exit ( 1 )
89
- }
90
-
91
65
const spinner = createSpinner ( )
92
66
spinner . start ( 'Initializing connection...' )
93
67
@@ -150,25 +124,23 @@ export async function runAutoSetup(options: PaymentSetupOptions): Promise<void>
150
124
const storageInfo = await synapse . storage . getStorageInfo ( )
151
125
const pricePerTiBPerEpoch = storageInfo . pricing . noCDN . perTiBPerEpoch
152
126
153
- // Calculate storage allowances now that we have synapse
154
- let allowances : StorageAllowances
155
- if ( parsedTiB !== null ) {
156
- // User specified TiB/month, calculate allowances
157
- allowances = calculateStorageAllowances ( parsedTiB , pricePerTiBPerEpoch )
158
- } else if ( rawUsdfcPerEpoch !== null ) {
159
- // User specified USDFC per epoch directly
160
- const usdfcPerEpochBigint = ethers . parseUnits ( rawUsdfcPerEpoch , 18 )
161
- const capacityTiB = calculateStorageFromUSDFC ( usdfcPerEpochBigint , pricePerTiBPerEpoch )
162
- allowances = calculateStorageAllowances ( capacityTiB , pricePerTiBPerEpoch )
127
+ // Track if any changes were made
128
+ let actionsTaken = false
129
+ let actualDepositAmount = 0n
130
+
131
+ // Auto-set max allowances for WarmStorage
132
+ spinner . start ( 'Configuring WarmStorage permissions...' )
133
+ const allowanceResult = await checkAndSetAllowances ( synapse )
134
+ if ( allowanceResult . updated ) {
135
+ spinner . stop ( `${ pc . green ( '✓' ) } WarmStorage permissions configured` )
136
+ log . line ( pc . bold ( 'Transaction:' ) )
137
+ log . indent ( pc . gray ( allowanceResult . transactionHash || 'Unknown' ) )
138
+ log . flush ( )
139
+ actionsTaken = true
163
140
} else {
164
- // This shouldn't happen due to earlier validation
165
- throw new Error ( 'Invalid storage allowance state' )
141
+ spinner . stop ( `${ pc . green ( '✓' ) } WarmStorage permissions already configured` )
166
142
}
167
143
168
- // Handle deposits
169
- let actualDepositAmount = 0n
170
- let actionsTaken = false // Track if any changes were made
171
-
172
144
if ( status . depositedAmount < targetDeposit ) {
173
145
const depositAmount = targetDeposit - status . depositedAmount
174
146
actualDepositAmount = depositAmount
@@ -199,83 +171,27 @@ export async function runAutoSetup(options: PaymentSetupOptions): Promise<void>
199
171
spinner . stop ( `${ pc . green ( '✓' ) } Deposit already sufficient (${ formatUSDFC ( status . depositedAmount ) } USDFC)` )
200
172
}
201
173
202
- // Set storage allowances
203
- spinner . start ( `Checking WarmStorage service allowances (${ options . rateAllowance } )...` )
204
-
205
- // Check if we need to update allowances
206
- const currentAllowances = status . currentAllowances
207
- let needsUpdate = false
208
-
209
- if ( currentAllowances . rateAllowance < allowances . rateAllowance ) {
210
- needsUpdate = true
211
- }
212
-
213
- if ( currentAllowances . lockupAllowance < allowances . lockupAllowance ) {
214
- needsUpdate = true
215
- }
216
-
217
- // Calculate total deposit for capacity display
174
+ // Calculate capacity for final summary
218
175
const totalDeposit = status . depositedAmount + actualDepositAmount
176
+ const capacity = calculateDepositCapacity ( totalDeposit , pricePerTiBPerEpoch )
219
177
220
- if ( needsUpdate ) {
221
- spinner . message ( 'Setting WarmStorage service approvals...' )
222
- const approvalTx = await setServiceApprovals ( synapse , allowances . rateAllowance , allowances . lockupAllowance )
223
- spinner . stop ( `${ pc . green ( '✓' ) } WarmStorage service approvals updated` )
224
- actionsTaken = true
225
-
226
- log . line ( pc . bold ( 'Transaction:' ) )
227
- log . indent ( pc . gray ( approvalTx ) )
228
- log . flush ( )
229
-
230
- // Display new permissions with capacity info
231
- const monthlyRate = allowances . rateAllowance * TIME_CONSTANTS . EPOCHS_PER_MONTH
232
- displayServicePermissions (
233
- 'New WarmStorage Service Limits:' ,
234
- monthlyRate ,
235
- allowances . lockupAllowance ,
236
- totalDeposit ,
237
- pricePerTiBPerEpoch
238
- )
239
- } else {
240
- spinner . stop ( `${ pc . green ( '✓' ) } WarmStorage service permissions already sufficient` )
241
-
242
- // Display current permissions with capacity info
243
- const monthlyRate = currentAllowances . rateAllowance * TIME_CONSTANTS . EPOCHS_PER_MONTH
244
- displayServicePermissions (
245
- 'Your Current WarmStorage Service Limits:' ,
246
- monthlyRate ,
247
- currentAllowances . lockupAllowance ,
248
- totalDeposit ,
249
- pricePerTiBPerEpoch
250
- )
251
- }
178
+ // Final summary
179
+ spinner . start ( 'Completing setup...' )
180
+ spinner . stop ( '━━━ Configuration Summary ━━━' )
252
181
253
- // Get final values
254
- let finalRateAllowance : bigint
255
- let finalLockupAllowance : bigint
182
+ log . line ( `Network: ${ pc . bold ( network ) } ` )
183
+ log . line ( `Deposit: ${ formatUSDFC ( totalDeposit ) } USDFC` )
256
184
257
- if ( needsUpdate ) {
258
- finalRateAllowance = allowances . rateAllowance
259
- finalLockupAllowance = allowances . lockupAllowance
260
- } else {
261
- finalRateAllowance = currentAllowances . rateAllowance
262
- finalLockupAllowance = currentAllowances . lockupAllowance
185
+ if ( capacity . gibPerMonth > 0 ) {
186
+ const capacityStr =
187
+ capacity . gibPerMonth >= 1024
188
+ ? ` ${ ( capacity . gibPerMonth / 1024 ) . toFixed ( 1 ) } TiB`
189
+ : ` ${ capacity . gibPerMonth . toFixed ( 1 ) } GiB`
190
+ log . line ( `Storage: ~ ${ capacityStr } for 1 month` )
263
191
}
264
192
265
- // Final summary
266
- spinner . start ( 'Completing setup...' )
267
- spinner . stop ( '━━━ Setup Complete ━━━' )
268
-
269
- displayPaymentSummary (
270
- network ,
271
- filStatus . balance ,
272
- filStatus . isCalibnet ,
273
- usdfcBalance ,
274
- totalDeposit ,
275
- finalRateAllowance ,
276
- finalLockupAllowance ,
277
- pricePerTiBPerEpoch
278
- )
193
+ log . line ( `Status: ${ pc . green ( 'Ready to upload' ) } ` )
194
+ log . flush ( )
279
195
280
196
// Show deposit warning if needed
281
197
displayDepositWarning ( totalDeposit , status . currentAllowances . lockupUsed )
@@ -284,7 +200,7 @@ export async function runAutoSetup(options: PaymentSetupOptions): Promise<void>
284
200
if ( actionsTaken ) {
285
201
outro ( 'Payment setup completed successfully' )
286
202
} else {
287
- outro ( 'Payment setup already configured - no changes needed ' )
203
+ outro ( 'Payment setup already configured - ready to use ' )
288
204
}
289
205
} catch ( error ) {
290
206
spinner . stop ( ) // Stop spinner without message
0 commit comments