|
| 1 | +import type { Synapse } from '@filoz/synapse-sdk' |
| 2 | +import type { Logger } from 'pino' |
| 3 | +import { formatUSDFC } from '../utils/index.js' |
| 4 | +import { |
| 5 | + calculatePieceUploadRequirements, |
| 6 | + computeAdjustmentForExactDaysWithPiece, |
| 7 | + computeTopUpForDuration, |
| 8 | + depositUSDFC, |
| 9 | + getPaymentStatus, |
| 10 | +} from './index.js' |
| 11 | +import type { PaymentStatus, TopUpCalculation, TopUpReasonCode, TopUpResult } from './types.js' |
| 12 | + |
| 13 | +/** |
| 14 | + * Format a human-readable message for a top-up calculation |
| 15 | + * |
| 16 | + * @param calc - Top-up calculation result |
| 17 | + * @returns Human-readable message explaining the top-up requirement |
| 18 | + */ |
| 19 | +export function formatTopUpReason(calc: TopUpCalculation): string { |
| 20 | + if (calc.requiredTopUp === 0n) { |
| 21 | + return 'No top-up required' |
| 22 | + } |
| 23 | + |
| 24 | + switch (calc.reasonCode) { |
| 25 | + case 'piece-upload': |
| 26 | + return 'Required top-up for file upload (lockup requirement)' |
| 27 | + case 'required-runway': |
| 28 | + return `Required top-up for ${calc.calculation.minStorageDays} days of storage` |
| 29 | + case 'required-runway-plus-upload': |
| 30 | + return `Required top-up for ${calc.calculation.minStorageDays} days of storage (including upcoming upload)` |
| 31 | + default: |
| 32 | + return 'Required top-up' |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Calculate required top-up for a specific storage scenario |
| 38 | + * |
| 39 | + * This function determines how much USDFC needs to be deposited to meet |
| 40 | + * the specified storage requirements, taking into account current usage |
| 41 | + * and the size of any upcoming upload. |
| 42 | + * |
| 43 | + * @param status - Current payment status |
| 44 | + * @param options - Storage requirements |
| 45 | + * @returns Top-up calculation with reasoning |
| 46 | + */ |
| 47 | +export function calculateRequiredTopUp( |
| 48 | + status: PaymentStatus, |
| 49 | + options: { |
| 50 | + minStorageDays?: number | undefined |
| 51 | + pieceSizeBytes?: number | undefined |
| 52 | + pricePerTiBPerEpoch?: bigint | undefined |
| 53 | + } |
| 54 | +): TopUpCalculation { |
| 55 | + const { minStorageDays = 0, pieceSizeBytes, pricePerTiBPerEpoch } = options |
| 56 | + const rateUsed = status.currentAllowances.rateUsed ?? 0n |
| 57 | + const lockupUsed = status.currentAllowances.lockupUsed ?? 0n |
| 58 | + |
| 59 | + let requiredTopUp = 0n |
| 60 | + let reasonCode: TopUpReasonCode = 'none' |
| 61 | + |
| 62 | + // Calculate piece upload requirements if we have piece info |
| 63 | + let pieceUploadTopUp = 0n |
| 64 | + if (pieceSizeBytes != null && pieceSizeBytes > 0 && pricePerTiBPerEpoch != null) { |
| 65 | + const uploadRequirements = calculatePieceUploadRequirements(status, pieceSizeBytes, pricePerTiBPerEpoch) |
| 66 | + if (uploadRequirements.insufficientDeposit > 0n) { |
| 67 | + pieceUploadTopUp = uploadRequirements.insufficientDeposit |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Calculate runway requirements if specified |
| 72 | + let runwayTopUp = 0n |
| 73 | + let isRunwayWithUpload = false |
| 74 | + if (minStorageDays > 0) { |
| 75 | + if (rateUsed === 0n && pieceSizeBytes != null && pieceSizeBytes > 0 && pricePerTiBPerEpoch != null) { |
| 76 | + // New piece upload with runway requirement |
| 77 | + const { delta } = computeAdjustmentForExactDaysWithPiece( |
| 78 | + status, |
| 79 | + minStorageDays, |
| 80 | + pieceSizeBytes, |
| 81 | + pricePerTiBPerEpoch |
| 82 | + ) |
| 83 | + runwayTopUp = delta |
| 84 | + isRunwayWithUpload = true |
| 85 | + } else { |
| 86 | + // Existing usage with runway requirement |
| 87 | + const { topUp } = computeTopUpForDuration(status, minStorageDays) |
| 88 | + runwayTopUp = topUp |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Determine the final top-up requirement (take the maximum of file upload and runway needs) |
| 93 | + if (runwayTopUp > pieceUploadTopUp) { |
| 94 | + requiredTopUp = runwayTopUp |
| 95 | + reasonCode = isRunwayWithUpload ? 'required-runway-plus-upload' : 'required-runway' |
| 96 | + } else if (pieceUploadTopUp > 0n) { |
| 97 | + requiredTopUp = pieceUploadTopUp |
| 98 | + reasonCode = 'piece-upload' |
| 99 | + } |
| 100 | + |
| 101 | + return { |
| 102 | + requiredTopUp, |
| 103 | + reasonCode, |
| 104 | + calculation: { |
| 105 | + minStorageDays, |
| 106 | + pieceSizeBytes, |
| 107 | + pricePerTiBPerEpoch, |
| 108 | + currentRateUsed: rateUsed, |
| 109 | + currentLockupUsed: lockupUsed, |
| 110 | + currentDeposited: status.filecoinPayBalance, |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Execute a top-up operation with balance limit checking |
| 117 | + * |
| 118 | + * This function handles the complete top-up process including: |
| 119 | + * - Checking if top-up is needed |
| 120 | + * - Validating against balance limits |
| 121 | + * - Executing the deposit transaction |
| 122 | + * - Providing detailed feedback |
| 123 | + * |
| 124 | + * @param synapse - Initialized Synapse instance |
| 125 | + * @param topUpAmount - Amount of USDFC to deposit |
| 126 | + * @param options - Options for top-up execution |
| 127 | + * @returns Top-up execution result |
| 128 | + */ |
| 129 | +export async function executeTopUp( |
| 130 | + synapse: Synapse, |
| 131 | + topUpAmount: bigint, |
| 132 | + options: { |
| 133 | + balanceLimit?: bigint | undefined |
| 134 | + logger?: Logger | undefined |
| 135 | + } = {} |
| 136 | +): Promise<TopUpResult> { |
| 137 | + const { balanceLimit, logger } = options |
| 138 | + const warnings: string[] = [] |
| 139 | + |
| 140 | + if (topUpAmount <= 0n) { |
| 141 | + return { |
| 142 | + success: true, |
| 143 | + deposited: 0n, |
| 144 | + message: 'No deposit required - sufficient balance available', |
| 145 | + warnings, |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Get current status for limit checking |
| 150 | + const currentStatus = await getPaymentStatus(synapse) |
| 151 | + |
| 152 | + // Check if deposit would exceed maximum balance if specified |
| 153 | + if (balanceLimit != null && balanceLimit >= 0n) { |
| 154 | + // Check if current balance already equals or exceeds limit |
| 155 | + if (currentStatus.filecoinPayBalance >= balanceLimit) { |
| 156 | + const message = `Current balance (${formatUSDFC(currentStatus.filecoinPayBalance)}) already equals or exceeds the configured balance limit (${formatUSDFC(balanceLimit)}). No additional deposits will be made.` |
| 157 | + logger?.warn(`${message}`) |
| 158 | + return { |
| 159 | + success: true, |
| 160 | + deposited: 0n, |
| 161 | + message, |
| 162 | + warnings, |
| 163 | + } |
| 164 | + } else { |
| 165 | + // Check if required top-up would exceed the limit |
| 166 | + const projectedBalance = currentStatus.filecoinPayBalance + topUpAmount |
| 167 | + if (projectedBalance > balanceLimit) { |
| 168 | + // Calculate the maximum allowed top-up that won't exceed the limit |
| 169 | + const maxAllowedTopUp = balanceLimit - currentStatus.filecoinPayBalance |
| 170 | + if (maxAllowedTopUp > 0n) { |
| 171 | + const warning = `Required top-up (${formatUSDFC(topUpAmount)}) would exceed the configured balance limit (${formatUSDFC(balanceLimit)}). Reducing to ${formatUSDFC(maxAllowedTopUp)}.` |
| 172 | + logger?.warn(`${warning}`) |
| 173 | + warnings.push(warning) |
| 174 | + topUpAmount = maxAllowedTopUp |
| 175 | + } else { |
| 176 | + return { |
| 177 | + success: true, |
| 178 | + deposited: 0n, |
| 179 | + message: 'Cannot deposit - would exceed balance limit', |
| 180 | + warnings, |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // Ensure wallet has sufficient USDFC for the deposit |
| 188 | + if (currentStatus.walletUsdfcBalance < topUpAmount) { |
| 189 | + const message = `Insufficient USDFC in wallet for deposit. Needed ${formatUSDFC(topUpAmount)}, available ${formatUSDFC(currentStatus.walletUsdfcBalance)}.` |
| 190 | + logger?.warn(`${message}`) |
| 191 | + return { |
| 192 | + success: false, |
| 193 | + deposited: 0n, |
| 194 | + message, |
| 195 | + warnings, |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + try { |
| 200 | + // Execute the deposit |
| 201 | + const result = await depositUSDFC(synapse, topUpAmount) |
| 202 | + |
| 203 | + // Verify the deposit was successful |
| 204 | + const newStatus = await getPaymentStatus(synapse) |
| 205 | + const depositDifference = newStatus.filecoinPayBalance - currentStatus.filecoinPayBalance |
| 206 | + |
| 207 | + let message = '' |
| 208 | + if (depositDifference > 0n) { |
| 209 | + message = `Deposit verified: ${formatUSDFC(depositDifference)} USDFC added to Filecoin Pay` |
| 210 | + } else { |
| 211 | + message = 'Deposit transaction submitted but not yet reflected in balance' |
| 212 | + warnings.push('Transaction may take a moment to process') |
| 213 | + } |
| 214 | + |
| 215 | + return { |
| 216 | + success: true, |
| 217 | + deposited: depositDifference, |
| 218 | + transactionHash: result.depositTx, |
| 219 | + message, |
| 220 | + warnings, |
| 221 | + } |
| 222 | + } catch (error) { |
| 223 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 224 | + return { |
| 225 | + success: false, |
| 226 | + deposited: 0n, |
| 227 | + message: `Deposit failed: ${errorMessage}`, |
| 228 | + warnings, |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments