Skip to content

Commit f498169

Browse files
CopilotBigLep
andauthored
fix(action): use fileSize to determine capacity when spendrate=0 (#132)
* Initial plan * Fix payment balance calculation to account for upcoming file uploads Co-authored-by: BigLep <[email protected]> * Apply linting fixes to test file Co-authored-by: BigLep <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BigLep <[email protected]>
1 parent 5ceb925 commit f498169

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

src/test/unit/payments.compute.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TIME_CONSTANTS } from '@filoz/synapse-sdk'
22
import { describe, expect, it } from 'vitest'
33
import {
44
computeAdjustmentForExactDays,
5+
computeAdjustmentForExactDaysWithFile,
56
computeAdjustmentForExactDeposit,
67
computeTopUpForDuration,
78
type PaymentStatus,
@@ -142,3 +143,38 @@ describe('computeAdjustmentForExactDeposit', () => {
142143
expect(res.clampedTarget).toBe(1_500n)
143144
})
144145
})
146+
147+
describe('computeAdjustmentForExactDaysWithFile', () => {
148+
it('calculates deposit for new file when rateUsed is 0', () => {
149+
// Scenario: No existing storage, uploading first file
150+
const status = makeStatus({ depositedAmount: 0n, lockupUsed: 0n, rateUsed: 0n })
151+
const carSizeBytes = 1024 * 1024 * 1024 // 1 GiB
152+
const pricePerTiBPerEpoch = 1_000_000_000_000_000n // 0.001 USDFC per TiB per epoch
153+
const days = 30
154+
155+
const res = computeAdjustmentForExactDaysWithFile(status, days, carSizeBytes, pricePerTiBPerEpoch)
156+
157+
// Should require deposit for both lockup and runway
158+
expect(res.delta).toBeGreaterThan(0n)
159+
expect(res.newRateUsed).toBeGreaterThan(0n)
160+
expect(res.newLockupUsed).toBeGreaterThan(0n)
161+
})
162+
163+
it('adds file requirements to existing usage', () => {
164+
// Scenario: Existing storage, adding another file
165+
const rateUsed = 1_000_000_000_000_000_000n // 1 USDFC/epoch
166+
const lockupUsed = rateUsed * BigInt(10) * TIME_CONSTANTS.EPOCHS_PER_DAY // 10 days worth
167+
const depositedAmount = (lockupUsed * 12n) / 10n // 20% buffer
168+
const status = makeStatus({ depositedAmount, lockupUsed, rateUsed })
169+
170+
const carSizeBytes = 1024 * 1024 * 1024 // 1 GiB
171+
const pricePerTiBPerEpoch = 1_000_000_000_000_000n // 0.001 USDFC per TiB per epoch
172+
const days = 30
173+
174+
const res = computeAdjustmentForExactDaysWithFile(status, days, carSizeBytes, pricePerTiBPerEpoch)
175+
176+
// New rate should be higher than existing
177+
expect(res.newRateUsed).toBeGreaterThan(rateUsed)
178+
expect(res.newLockupUsed).toBeGreaterThan(lockupUsed)
179+
})
180+
})

upload-action/src/filecoin.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { promises as fs } from 'node:fs'
22
import { RPC_URLS } from '@filoz/synapse-sdk'
33
import {
44
calculateStorageRunway,
5+
computeAdjustmentForExactDaysWithFile,
56
computeTopUpForDuration,
67
depositUSDFC,
78
getPaymentStatus,
@@ -71,7 +72,7 @@ export async function initializeSynapse(config, logger) {
7172
* @returns {Promise<PaymentStatus>} Updated payment status
7273
*/
7374
export async function handlePayments(synapse, options, logger) {
74-
const { minStorageDays, filecoinPayBalanceLimit } = options
75+
const { minStorageDays, filecoinPayBalanceLimit, carSizeBytes } = options
7576

7677
console.log('Checking current Filecoin Pay account balance...')
7778
const initialStatus = await getPaymentStatus(synapse)
@@ -82,9 +83,32 @@ export async function handlePayments(synapse, options, logger) {
8283
let requiredTopUp = 0n
8384

8485
if (minStorageDays > 0) {
85-
const { topUp } = computeTopUpForDuration(initialStatus, minStorageDays)
86-
requiredTopUp = topUp
87-
console.log(`Required top-up for ${minStorageDays} days of storage: ${formatUSDFC(requiredTopUp)} USDFC`)
86+
const rateUsed = initialStatus.currentAllowances.rateUsed ?? 0n
87+
88+
// If there's an upcoming file upload and no existing usage, calculate deposit based on file size
89+
if (rateUsed === 0n && carSizeBytes != null && carSizeBytes > 0) {
90+
// Get pricing information to calculate file requirements
91+
const storageInfo = await synapse.storage.getStorageInfo()
92+
const pricePerTiBPerEpoch = storageInfo.pricing.noCDN.perTiBPerEpoch
93+
94+
// Calculate required deposit accounting for the new file
95+
const { delta } = computeAdjustmentForExactDaysWithFile(
96+
initialStatus,
97+
minStorageDays,
98+
carSizeBytes,
99+
pricePerTiBPerEpoch
100+
)
101+
102+
requiredTopUp = delta > 0n ? delta : 0n
103+
console.log(
104+
`Required top-up for ${minStorageDays} days of storage (including upcoming upload): ${formatUSDFC(requiredTopUp)} USDFC`
105+
)
106+
} else {
107+
// Use existing logic for maintaining current usage
108+
const { topUp } = computeTopUpForDuration(initialStatus, minStorageDays)
109+
requiredTopUp = topUp
110+
console.log(`Required top-up for ${minStorageDays} days of storage: ${formatUSDFC(requiredTopUp)} USDFC`)
111+
}
88112
}
89113

90114
// Check if deposit would exceed maximum balance if specified

upload-action/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export interface PRMetadata {
7070
export interface PaymentConfig {
7171
minStorageDays: number
7272
filecoinPayBalanceLimit?: bigint | undefined
73+
carSizeBytes?: number | undefined
7374
}
7475

7576
export interface UploadConfig {

upload-action/src/upload.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ export async function runUpload(buildContext = {}) {
154154
summary: 'Verifying account balance and calculating required deposits...',
155155
})
156156

157-
paymentStatus = await handlePayments(synapse, { minStorageDays, filecoinPayBalanceLimit }, logger)
157+
paymentStatus = await handlePayments(
158+
synapse,
159+
{ minStorageDays, filecoinPayBalanceLimit, carSizeBytes: context.carSize },
160+
logger
161+
)
158162

159163
console.log('✓ Funding phase complete')
160164

0 commit comments

Comments
 (0)