diff --git a/src/core/payments/constants.ts b/src/core/payments/constants.ts index 6f044cc..023a8f8 100644 --- a/src/core/payments/constants.ts +++ b/src/core/payments/constants.ts @@ -51,3 +51,19 @@ export const BUFFER_DENOMINATOR = 10n */ export const STORAGE_SCALE_MAX = 10_000_000 export const STORAGE_SCALE_MAX_BI = BigInt(STORAGE_SCALE_MAX) + +/** PDP Leaf Size - the payment rate is based on `rawSize` bytes rounded up to the next multiple of 32. + * + * @see - https://github.com/FilOzone/synapse-sdk/issues/339#issue-3539254596 + */ +export const PDP_LEAF_SIZE = 32 + +/** + * Pad raw size to the next multiple of 32 bytes + * + * @param rawSizeBytes - The actual size in bytes + * @returns Padded size (next multiple of 32) + */ +export function padSizeToPDPLeaves(rawSizeBytes: number): number { + return Math.ceil(rawSizeBytes / PDP_LEAF_SIZE) * PDP_LEAF_SIZE +} diff --git a/src/core/payments/index.ts b/src/core/payments/index.ts index 2100abc..ecc76d6 100644 --- a/src/core/payments/index.ts +++ b/src/core/payments/index.ts @@ -26,6 +26,7 @@ import { MAX_LOCKUP_ALLOWANCE, MAX_RATE_ALLOWANCE, MIN_FIL_FOR_GAS, + padSizeToPDPLeaves, STORAGE_SCALE_MAX, STORAGE_SCALE_MAX_BI, USDFC_DECIMALS, @@ -826,7 +827,8 @@ export function calculateDepositCapacity( * @returns Required allowances for the piece */ export function calculateRequiredAllowances(pieceSizeBytes: number, pricePerTiBPerEpoch: bigint): StorageAllowances { - const storageTiB = pieceSizeBytes / Number(SIZE_CONSTANTS.TiB) + const paddedSizeBytes = padSizeToPDPLeaves(pieceSizeBytes) + const storageTiB = paddedSizeBytes / Number(SIZE_CONSTANTS.TiB) return calculateStorageAllowances(storageTiB, pricePerTiBPerEpoch) }