Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/payments/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function createSpinner() {

if (isTTY) {
// Use the real spinner for TTY
return clackSpinner()
return clackSpinner({ indicator: 'timer' })
} else {
// Non-TTY fallback - just print messages without spinners
return {
Expand Down Expand Up @@ -252,7 +252,7 @@ export async function runAutoSetup(options: PaymentSetupOptions): Promise<void>
// Display new permissions with capacity info
const monthlyRate = allowances.ratePerEpoch * TIME_CONSTANTS.EPOCHS_PER_MONTH
displayServicePermissions(
'New WarmStorage Service Permissions:',
'New WarmStorage Service Limits:',
monthlyRate,
allowances.lockupAmount,
totalDeposit,
Expand All @@ -264,7 +264,7 @@ export async function runAutoSetup(options: PaymentSetupOptions): Promise<void>
// Display current permissions with capacity info
const monthlyRate = currentAllowances.rateAllowance * TIME_CONSTANTS.EPOCHS_PER_MONTH
displayServicePermissions(
'Current WarmStorage Service Permissions:',
'Your Current WarmStorage Service Limits:',
monthlyRate,
currentAllowances.lockupAllowance,
totalDeposit,
Expand Down
51 changes: 14 additions & 37 deletions src/payments/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
checkUSDFCBalance,
createProvider,
depositUSDFC,
displayCapacity,
displayDepositWarning,
displayServicePermissions,
formatFIL,
Expand Down Expand Up @@ -80,7 +81,7 @@ export async function runInteractiveSetup(options: PaymentSetupOptions): Promise
}

// Step 2: Initialize Synapse
const s = spinner()
const s = spinner({ indicator: 'timer' })
s.start('Initializing connection...')

const rpcUrl = options.rpcUrl || RPC_URLS.calibration.websocket
Expand Down Expand Up @@ -206,7 +207,7 @@ export async function runInteractiveSetup(options: PaymentSetupOptions): Promise
}

// Step 5: Set storage allowances
console.log(`\n${pc.bold('WarmStorage Service Payment Limits:')}`)
console.log(`\n${pc.bold('Your Current WarmStorage Service Limits:')}`)

// Show current allowances
let currentAllowances = status.currentAllowances
Expand All @@ -223,17 +224,9 @@ export async function runInteractiveSetup(options: PaymentSetupOptions): Promise
console.log(` Max payment: ${formatUSDFC(monthlyRate)} USDFC/month`)
console.log(` Max reserve: ${formatUSDFC(currentAllowances.lockupAllowance)} USDFC (10-day lockup)`)

if (capacity.isDepositLimited) {
console.log(
` → Current capacity: ~${capacity.actualGB.toLocaleString()} GB/month ${pc.yellow('(deposit-limited)')}`
)
console.log(` → Potential: ~${capacity.potentialGB.toLocaleString()} GB/month`)
} else {
console.log(` → Estimated capacity: ~${capacity.actualGB.toLocaleString()} GB/month`)
console.log(pc.gray(' (varies with CDN usage and data set fees)'))
}
displayCapacity(capacity)
} else {
console.log(pc.gray(' No WarmStorage service permissions set yet'))
console.log(pc.gray(' No limits set yet'))
}

// Ask about setting/updating storage limits
Expand All @@ -252,7 +245,7 @@ export async function runInteractiveSetup(options: PaymentSetupOptions): Promise
console.log(`\n${pc.bold('Storage Pricing:')}`)
console.log(` 1 GiB/month: ${formatUSDFC(pricePerGiBPerMonth)} USDFC`)
console.log(` 1 TiB/month: ${formatUSDFC(pricePerTiBPerMonth)} USDFC`)
console.log(pc.gray(' (WarmStorage service will reserve 10 days of costs as security)'))
console.log(pc.gray(' (for each upload, WarmStorage service will reserve 10 days of costs as security)'))

const allowanceStr = await text({
message: 'Enter storage allowance',
Expand Down Expand Up @@ -299,7 +292,7 @@ export async function runInteractiveSetup(options: PaymentSetupOptions): Promise

// Display the new permissions with capacity info
displayServicePermissions(
'New WarmStorage Service Permissions:',
'New WarmStorage Service Limits:',
monthlyRate,
allowances.lockupAmount,
totalDeposit,
Expand Down Expand Up @@ -420,14 +413,6 @@ export async function runInteractiveSetup(options: PaymentSetupOptions): Promise
const finalStatus = await getPaymentStatus(synapse)
s.stop(`${pc.green('✓')} Setup complete`)

// Calculate actual vs potential capacity using existing pricePerTiBPerEpoch
const capacity = calculateActualCapacity(
finalStatus.depositedAmount,
finalStatus.currentAllowances.rateAllowance,
finalStatus.currentAllowances.lockupAllowance,
pricePerTiBPerEpoch
)

// Final summary with three clear sections
outro(pc.bold('━━━ Setup Complete ━━━'))
console.log(`Network: ${pc.bold(network)}`)
Expand All @@ -444,21 +429,13 @@ export async function runInteractiveSetup(options: PaymentSetupOptions): Promise

// Section 3: WarmStorage service permissions
const monthlyRate = finalStatus.currentAllowances.rateAllowance * TIME_CONSTANTS.EPOCHS_PER_MONTH
console.log(`\n${pc.bold('WarmStorage Service Permissions')}`)
console.log(` Max payment: ${formatUSDFC(monthlyRate)} USDFC/month`)
console.log(` Max reserve: ${formatUSDFC(finalStatus.currentAllowances.lockupAllowance)} USDFC (10-day lockup)`)

if (capacity.isDepositLimited) {
console.log(
` → Current capacity: ~${capacity.actualGB.toLocaleString()} GB/month ${pc.yellow('(deposit-limited)')}`
)
console.log(
` → Potential: ~${capacity.potentialGB.toLocaleString()} GB/month (deposit ${formatUSDFC(capacity.additionalDepositNeeded)} more)`
)
} else {
console.log(` → Estimated capacity: ~${capacity.actualGB.toLocaleString()} GB/month`)
console.log(pc.gray(' (varies with CDN usage and data set fees)'))
}
displayServicePermissions(
'Your WarmStorage Service Limits',
monthlyRate,
finalStatus.currentAllowances.lockupAllowance,
finalStatus.depositedAmount,
pricePerTiBPerEpoch
)

// Show deposit warning if needed
displayDepositWarning(finalStatus.depositedAmount, finalStatus.currentAllowances.lockupUsed)
Expand Down
56 changes: 28 additions & 28 deletions src/payments/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ export async function calculateStorageAllowances(synapse: Synapse, tibPerMonth:
const pricePerTiBPerEpoch = storageInfo.pricing.noCDN.perTiBPerEpoch

// Calculate rate allowance (price for desired storage per epoch)
const ratePerEpoch = pricePerTiBPerEpoch * BigInt(Math.floor(tibPerMonth))
// Handle fractional TiB by converting to milliTiB for precision
const milliTiBPerMonth = Math.round(tibPerMonth * 1000)
const ratePerEpoch = (pricePerTiBPerEpoch * BigInt(milliTiBPerMonth)) / 1000n

// Lockup is always 10 days worth of payments for WarmStorage
const lockupAmount = ratePerEpoch * TIME_CONSTANTS.EPOCHS_PER_DAY * BigInt(DEFAULT_LOCKUP_DAYS)
Expand Down Expand Up @@ -469,27 +471,16 @@ export function displayPaymentSummary(
// Section 3: WarmStorage service permissions
if (rateAllowance > 0n) {
const monthlyRate = rateAllowance * TIME_CONSTANTS.EPOCHS_PER_MONTH
console.log(`\n${pc.bold('WarmStorage Service Permissions')}`)
console.log(`\n${pc.bold('Your WarmStorage Service Limits')}`)
console.log(` Max payment: ${formatUSDFC(monthlyRate)} USDFC/month`)
console.log(` Max reserve: ${formatUSDFC(lockupAllowance)} USDFC (10-day lockup)`)

// Calculate actual vs potential capacity
// Calculate and display capacity
const capacity = calculateActualCapacity(depositedAmount, rateAllowance, lockupAllowance, pricePerTiBPerEpoch)

if (capacity.isDepositLimited) {
console.log(
` → Current capacity: ~${capacity.actualGB.toLocaleString()} GB/month ${pc.yellow('(deposit-limited)')}`
)
console.log(
` → Potential: ~${capacity.potentialGB.toLocaleString()} GB/month (deposit ${formatUSDFC(capacity.additionalDepositNeeded)} more)`
)
} else {
console.log(` → Estimated capacity: ~${capacity.actualGB.toLocaleString()} GB/month`)
console.log(pc.gray(' (varies with CDN usage and data set fees)'))
}
displayCapacity(capacity)
} else {
console.log(`\n${pc.bold('WarmStorage Service Permissions')}`)
console.log(pc.gray(' No permissions set'))
console.log(`\n${pc.bold('Your WarmStorage Service Limits')}`)
console.log(pc.gray(' No limits set'))
}
}

Expand Down Expand Up @@ -522,6 +513,25 @@ export function displayDepositWarning(depositedAmount: bigint, lockupUsed: bigin
}
}

/**
* Display capacity information based on deposit and limits
*
* @param capacity - Calculated capacity information
*/
export function displayCapacity(capacity: ReturnType<typeof calculateActualCapacity>): void {
if (capacity.isDepositLimited) {
console.log(
` → Current capacity: ~${capacity.actualGB.toLocaleString()} GB/month ${pc.yellow('(deposit-limited)')}`
)
console.log(
` → Potential: ~${capacity.potentialGB.toLocaleString()} GB/month (deposit ${formatUSDFC(capacity.additionalDepositNeeded)} more)`
)
} else {
console.log(` → Estimated capacity: ~${capacity.actualGB.toLocaleString()} GB/month`)
console.log(pc.gray(' (excludes data set creation fee and optional CDN add-on rates)'))
}
}

/**
* Display WarmStorage service permissions with capacity information
*
Expand All @@ -546,17 +556,7 @@ export function displayServicePermissions(
console.log(` Max payment: ${formatUSDFC(monthlyRate)} USDFC/month`)
console.log(` Max reserve: ${formatUSDFC(lockupAmount)} USDFC (10-day lockup)`)

if (capacity.isDepositLimited) {
console.log(
` → Current capacity: ~${capacity.actualGB.toLocaleString()} GB/month ${pc.yellow('(deposit-limited)')}`
)
console.log(
` → Potential: ~${capacity.potentialGB.toLocaleString()} GB/month (deposit ${formatUSDFC(capacity.additionalDepositNeeded)} more)`
)
} else {
console.log(` → Estimated capacity: ~${capacity.actualGB.toLocaleString()} GB/month`)
console.log(pc.gray(' (varies with CDN usage and data set fees)'))
}
displayCapacity(capacity)
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/test/unit/payments-setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,19 @@ describe('Payment Setup Tests', () => {
const allowances = await calculateStorageAllowances(mockSynapse, 0.5)

expect(allowances.tibPerMonth).toBe(0.5)
expect(allowances.ratePerEpoch).toBe(0n) // Floor of 0.5 is 0
// 0.5 TiB = 500 milliTiB, so (0.0000565 * 500) / 1000 = 0.00002825
expect(allowances.ratePerEpoch).toBe(ethers.parseUnits('0.00002825', 18))
})

it('should calculate allowances for 1.5 TiB correctly', async () => {
const allowances = await calculateStorageAllowances(mockSynapse, 1.5)

expect(allowances.tibPerMonth).toBe(1.5)
// 1.5 TiB = 1500 milliTiB, so (0.0000565 * 1500) / 1000 = 0.00008475
expect(allowances.ratePerEpoch).toBe(ethers.parseUnits('0.00008475', 18))
expect(allowances.lockupAmount).toBe(
ethers.parseUnits('0.00008475', 18) * 2880n * 10n // rate * epochs/day * 10 days
)
})
})

Expand Down
Loading