# utils

## Namespaces

| Namespace | Description |
| ------ | ------ |
| [addPiecesFits](/reference/filoz/synapse-core/utils/namespaces/addpiecesfits/toc/) | - |
| [assertAddPiecesFit](/reference/filoz/synapse-core/utils/namespaces/assertaddpiecesfit/toc/) | - |
| [assertPieceCidSize](/reference/filoz/synapse-core/utils/namespaces/assertpiececidsize/toc/) | - |
| [assertPieceValid](/reference/filoz/synapse-core/utils/namespaces/assertpiecevalid/toc/) | - |
| [calculateUploadCosts](/reference/filoz/synapse-core/utils/namespaces/calculateuploadcosts/toc/) | - |
| [checkAddPiecesFit](/reference/filoz/synapse-core/utils/namespaces/checkaddpiecesfit/toc/) | - |
| [createPieceUrl](/reference/filoz/synapse-core/utils/namespaces/createpieceurl/toc/) | - |
| [createPieceUrlFilBeam](/reference/filoz/synapse-core/utils/namespaces/createpieceurlfilbeam/toc/) | - |
| [createPieceUrlPDP](/reference/filoz/synapse-core/utils/namespaces/createpieceurlpdp/toc/) | - |
| [dedupePieces](/reference/filoz/synapse-core/utils/namespaces/dedupepieces/toc/) | - |
| [estimateAddPiecesCalldataSize](/reference/filoz/synapse-core/utils/namespaces/estimateaddpiecescalldatasize/toc/) | - |
| [maxFittingPieces](/reference/filoz/synapse-core/utils/namespaces/maxfittingpieces/toc/) | - |

## Interfaces

| Interface | Description |
| ------ | ------ |
| [MetadataDataSetInternal](/reference/filoz/synapse-core/utils/interfaces/metadatadatasetinternal/) | - |
| [MetadataPieceInternal](/reference/filoz/synapse-core/utils/interfaces/metadatapieceinternal/) | - |

## Type Aliases

| Type Alias | Description |
| ------ | ------ |
| [AddPiecesFitResult](/reference/filoz/synapse-core/utils/type-aliases/addpiecesfitresult/) | Why a piece list does not fit in one addPieces / createAndAdd message. |
| [CalculateBufferAmountFromStateOptions](/reference/filoz/synapse-core/utils/type-aliases/calculatebufferamountfromstateoptions/) | - |
| [CalculateRunwayAmountFromStateOptions](/reference/filoz/synapse-core/utils/type-aliases/calculaterunwayamountfromstateoptions/) | - |
| [ClaimTokenResponse](/reference/filoz/synapse-core/utils/type-aliases/claimtokenresponse/) | - |
| [ClaimTokenResponseError](/reference/filoz/synapse-core/utils/type-aliases/claimtokenresponseerror/) | - |
| [ClaimTokensOptions](/reference/filoz/synapse-core/utils/type-aliases/claimtokensoptions/) | - |
| [DroppedPiece](/reference/filoz/synapse-core/utils/type-aliases/droppedpiece/) | An input removed by [dedupePieces](/reference/filoz/synapse-core/utils/functions/dedupepieces/) because an earlier input has the same PieceCID. |
| [FormatUnitsOptions](/reference/filoz/synapse-core/utils/type-aliases/formatunitsoptions/) | - |
| [Limit](/reference/filoz/synapse-core/utils/type-aliases/limit/) | A function that runs a task once fewer than its limit of tasks are running. |
| [Limiter](/reference/filoz/synapse-core/utils/type-aliases/limiter/) | `true` if `pieces` still fit in one addPieces / createAndAdd operation. |
| [LimiterOptions](/reference/filoz/synapse-core/utils/type-aliases/limiteroptions/) | - |
| [LimiterPiece](/reference/filoz/synapse-core/utils/type-aliases/limiterpiece/) | - |
| [MetadataArray](/reference/filoz/synapse-core/utils/type-aliases/metadataarray/) | The metadata array is a tuple of two arrays: the keys and the values. Return type from the getAllDataSetMetadata function. |
| [MetadataEntry](/reference/filoz/synapse-core/utils/type-aliases/metadataentry/) | - |
| [MetadataObject](/reference/filoz/synapse-core/utils/type-aliases/metadataobject/) | - |
| [StringErrorType](/reference/filoz/synapse-core/utils/type-aliases/stringerrortype/) | FOC contracts errors types |

## Variables

| Variable | Description |
| ------ | ------ |
| [DEFAULT\_BUFFER\_EPOCHS](/reference/filoz/synapse-core/utils/variables/default_buffer_epochs/) | Default safety margin in epochs when calculating deposit amounts. Accounts for epoch drift between balance check and on-chain execution. |
| [DEFAULT\_RUNWAY\_EPOCHS](/reference/filoz/synapse-core/utils/variables/default_runway_epochs/) | Default extra runway in epochs beyond the required lockup. 0n means no additional runway beyond the lockup period. |
| [METADATA\_LIMITS](/reference/filoz/synapse-core/utils/variables/metadata_limits/) | - |
| [PDP\_OFFERING\_KEYS](/reference/filoz/synapse-core/utils/variables/pdp_offering_keys/) | Capability keys that are decoded into typed PDPOffering fields. (must match ServiceProviderRegistry.sol REQUIRED_PDP_KEYS) |
| [PDP\_OFFERING\_KEYS\_SET](/reference/filoz/synapse-core/utils/variables/pdp_offering_keys_set/) | Set of PDP offering keys. |
| [PDPOfferingSchema](/reference/filoz/synapse-core/utils/variables/pdpofferingschema/) | Zod schema for PDP offering |
| [RETRY\_CONSTANTS](/reference/filoz/synapse-core/utils/variables/retry_constants/) | - |
| [SERVICE\_PROVIDER\_REGISTRY](/reference/filoz/synapse-core/utils/variables/service_provider_registry/) | Limits mirrored from [ServiceProviderRegistry.sol](https://github.com/FilOzone/filecoin-services/blob/main/service_contracts/src/ServiceProviderRegistry.sol). Sync with `VERSION` in that contract (currently `1.1.0`) when upgrading. |
| [SIZE\_CONSTANTS](/reference/filoz/synapse-core/utils/variables/size_constants/) | Data size constants |
| [STRING\_ERRORS](/reference/filoz/synapse-core/utils/variables/string_errors/) | FOC contracts errors strings |
| [TIME\_CONSTANTS](/reference/filoz/synapse-core/utils/variables/time_constants/) | Time and size constants |
| [zAddress](/reference/filoz/synapse-core/utils/variables/zaddress/) | - |
| [zAddressLoose](/reference/filoz/synapse-core/utils/variables/zaddressloose/) | - |
| [zHex](/reference/filoz/synapse-core/utils/variables/zhex/) | - |
| [zNumberToBigInt](/reference/filoz/synapse-core/utils/variables/znumbertobigint/) | - |
| [zPieceCid](/reference/filoz/synapse-core/utils/variables/zpiececid/) | - |
| [zPieceCidString](/reference/filoz/synapse-core/utils/variables/zpiececidstring/) | - |
| [zStringToCid](/reference/filoz/synapse-core/utils/variables/zstringtocid/) | - |

## Functions

| Function | Description |
| ------ | ------ |
| [abortable](/reference/filoz/synapse-core/utils/functions/abortable/) | Reject with iso-web's AbortError when the signal aborts, otherwise settle like the promise. |
| [addPiecesFits](/reference/filoz/synapse-core/utils/functions/addpiecesfits/) | Whether a candidate piece list fits in one addPieces / createAndAdd message. |
| [assertAddPiecesFit](/reference/filoz/synapse-core/utils/functions/assertaddpiecesfit/) | Throw if `pieces` is empty, a PieceCID is outside Curio's size bounds, or the list does not fit in one addPieces / createAndAdd message. |
| [assertPieceCidSize](/reference/filoz/synapse-core/utils/functions/assertpiececidsize/) | Throw if a PieceCID's encoded raw size is outside Curio's upload bounds ([SIZE\_CONSTANTS.MIN\_UPLOAD\_SIZE](/reference/filoz/synapse-core/utils/variables/size_constants/#min_upload_size)–[SIZE\_CONSTANTS.MAX\_UPLOAD\_SIZE](/reference/filoz/synapse-core/utils/variables/size_constants/#max_upload_size)). |
| [assertPieceValid](/reference/filoz/synapse-core/utils/functions/assertpiecevalid/) | Throw if a piece can never be added: its PieceCID size is outside Curio's upload bounds or its metadata exceeds the contract limits. Use it to reject bad pieces before any network work. |
| [calculateBufferAmountFromState](/reference/filoz/synapse-core/utils/functions/calculatebufferamountfromstate/) | Calculate the account funding buffer for transaction execution delay. |
| [calculateLastProofDate](/reference/filoz/synapse-core/utils/functions/calculatelastproofdate/) | Calculate when the last proof should have been submitted based on current time |
| [calculateRunwayAmountFromState](/reference/filoz/synapse-core/utils/functions/calculaterunwayamountfromstate/) | Calculate extra account funding for the requested post-upload runway. |
| [calculateUploadCosts](/reference/filoz/synapse-core/utils/functions/calculateuploadcosts/) | Calculate upload costs from already-resolved pricing, account, and data-set state. |
| [capabilitiesListToObject](/reference/filoz/synapse-core/utils/functions/capabilitieslisttoobject/) | Convert capability arrays to object map |
| [checkAddPiecesFit](/reference/filoz/synapse-core/utils/functions/checkaddpiecesfit/) | Whether a candidate piece list fits in one addPieces / createAndAdd message, and why not. |
| [claimTokens](/reference/filoz/synapse-core/utils/functions/claimtokens/) | - |
| [clientFromTransport](/reference/filoz/synapse-core/utils/functions/clientfromtransport/) | Create a Viem public client from a transport configuration |
| [createLimit](/reference/filoz/synapse-core/utils/functions/createlimit/) | Create a limit that runs at most `concurrency` tasks at a time, in call order. |
| [createPieceUrl](/reference/filoz/synapse-core/utils/functions/createpieceurl/) | Create a piece URL for the CDN or PDP API |
| [createPieceUrlFilBeam](/reference/filoz/synapse-core/utils/functions/createpieceurlfilbeam/) | Create a piece URL for FilBeam. |
| [createPieceUrlPDP](/reference/filoz/synapse-core/utils/functions/createpieceurlpdp/) | Create a piece URL for the PDP API |
| [datasetMetadataObjectToEntry](/reference/filoz/synapse-core/utils/functions/datasetmetadataobjecttoentry/) | Convert a dataset metadata object to an array of metadata entries to be signed. |
| [dateToEpoch](/reference/filoz/synapse-core/utils/functions/datetoepoch/) | Convert a JavaScript Date to a Filecoin epoch |
| [decodeAddressCapability](/reference/filoz/synapse-core/utils/functions/decodeaddresscapability/) | Matches the behavior of `address(uint160(BigEndian.decode(values[i])))` |
| [decodePDPCapabilities](/reference/filoz/synapse-core/utils/functions/decodepdpcapabilities/) | Decode PDP capabilities from keys/values arrays into a PDPOffering object. Based on Curio's capabilitiesToOffering function. |
| [decodePDPError](/reference/filoz/synapse-core/utils/functions/decodepdperror/) | - |
| [decodePDPOffering](/reference/filoz/synapse-core/utils/functions/decodepdpoffering/) | Decode the PDP offering from the provider. |
| [dedupePieces](/reference/filoz/synapse-core/utils/functions/dedupepieces/) | Remove inputs whose PieceCID already appeared earlier in the array. |
| [encodePDPCapabilities](/reference/filoz/synapse-core/utils/functions/encodepdpcapabilities/) | Encode a [PDPOffering](/reference/filoz/synapse-core/sp-registry/interfaces/pdpoffering/) plus optional user-supplied extras into the `(keys[], values[])` tuple consumed by `registerProvider` / `addProduct` / `updateProduct` on `ServiceProviderRegistry`. |
| [epochsToDays](/reference/filoz/synapse-core/utils/functions/epochstodays/) | Convert a number of epochs to whole days (floor division). |
| [epochsToHours](/reference/filoz/synapse-core/utils/functions/epochstohours/) | Convert a number of epochs to whole hours (floor division). |
| [epochToDate](/reference/filoz/synapse-core/utils/functions/epochtodate/) | Convert a Filecoin epoch to a JavaScript Date |
| [estimateAddPiecesCalldataSize](/reference/filoz/synapse-core/utils/functions/estimateaddpiecescalldatasize/) | Estimated on-chain calldata size in bytes for the given piece list. |
| [fallbackRandIndex](/reference/filoz/synapse-core/utils/functions/fallbackrandindex/) | - |
| [fallbackRandU256](/reference/filoz/synapse-core/utils/functions/fallbackrandu256/) | - |
| [formatBalance](/reference/filoz/synapse-core/utils/functions/formatbalance/) | - |
| [formatFraction](/reference/filoz/synapse-core/utils/functions/formatfraction/) | - |
| [formatUnits](/reference/filoz/synapse-core/utils/functions/formatunits/) | - |
| [getPayerAddress](/reference/filoz/synapse-core/utils/functions/getpayeraddress/) | Get the address that pays for an account's storage: a session key signs as itself, but its root wallet pays. |
| [isAsyncIterable](/reference/filoz/synapse-core/utils/functions/isasynciterable/) | Type guard to check if a value is an AsyncIterable |
| [isNullRoundError](/reference/filoz/synapse-core/utils/functions/isnullrounderror/) | Whether the error is Lotus refusing to return a block for a Filecoin null round. |
| [isProviderExistsRevert](/reference/filoz/synapse-core/utils/functions/isproviderexistsrevert/) | Check whether the given error is a revert emitted by the `ServiceProviderRegistry.providerExists` modifier (i.e. the provider ID is out of range or maps to an unoccupied storage slot). |
| [isReadableStream](/reference/filoz/synapse-core/utils/functions/isreadablestream/) | Type guard to check if a value is a ReadableStream |
| [isUint8Array](/reference/filoz/synapse-core/utils/functions/isuint8array/) | Check if value is Uint8Array |
| [isViemError](/reference/filoz/synapse-core/utils/functions/isviemerror/) | Check if the error is a viem error |
| [leafCountToRawSize](/reference/filoz/synapse-core/utils/functions/leafcounttorawsize/) | Convert a PDP data-bearing leaf count to the byte size used by FWSS pricing. |
| [mapWithLimit](/reference/filoz/synapse-core/utils/functions/mapwithlimit/) | Like `Promise.all(items.map(fn))`, but runs at most `concurrency` calls at a time. |
| [maxFittingPieces](/reference/filoz/synapse-core/utils/functions/maxfittingpieces/) | Length of the longest prefix of `options.pieces` that fits in one addPieces / createAndAdd message. |
| [metadataArrayToObject](/reference/filoz/synapse-core/utils/functions/metadataarraytoobject/) | - |
| [parseUnits](/reference/filoz/synapse-core/utils/functions/parseunits/) | Parse a value to a bigint. |
| [pieceMetadataObjectToEntry](/reference/filoz/synapse-core/utils/functions/piecemetadataobjecttoentry/) | Convert a dataset metadata object to an array of metadata entries to be signed. |
| [pieceSizesToLeafCount](/reference/filoz/synapse-core/utils/functions/piecesizestoleafcount/) | Sum the PDP data-bearing leaves produced by known raw piece sizes. |
| [randIndex](/reference/filoz/synapse-core/utils/functions/randindex/) | Provides a random index into an array of supplied length (0 <= index < length) |
| [randU256](/reference/filoz/synapse-core/utils/functions/randu256/) | - |
| [rawSizeToLeafCount](/reference/filoz/synapse-core/utils/functions/rawsizetoleafcount/) | Calculate the PDP data-bearing leaf count for a known raw piece size. |
| [readableStreamFromAsyncIterable](/reference/filoz/synapse-core/utils/functions/readablestreamfromasynciterable/) | Wrap an AsyncIterable in a ReadableStream. The first chunk is read before returning, so the source's own errors reject here; cancelling the stream calls the iterator's `return`. |
| [readAllBytes](/reference/filoz/synapse-core/utils/functions/readallbytes/) | Read a byte stream to the end and concatenate its chunks. |
| [stringErrorEquals](/reference/filoz/synapse-core/utils/functions/stringerrorequals/) | Check if the error message equals the expected string |
| [supportsStreamingFetchBody](/reference/filoz/synapse-core/utils/functions/supportsstreamingfetchbody/) | Detect whether the current environment supports ReadableStream as a fetch request body. Firefox stringifies the stream to "[object ReadableStream]" instead of consuming it, and Safari silently ignores the stream body and the duplex option. This check catches both: a browser that stringifies will set Content-Type to text/plain (string body), and one that ignores duplex will fail the duplexAccessed gate. The try/catch handles any future browser that throws on the Request constructor. |
| [throwIfAborted](/reference/filoz/synapse-core/utils/functions/throwifaborted/) | Throw iso-web's AbortError if the signal has already been aborted. |
| [timeUntilEpoch](/reference/filoz/synapse-core/utils/functions/timeuntilepoch/) | Calculate the time until a future epoch |
| [transportFromTransportConfig](/reference/filoz/synapse-core/utils/functions/transportfromtransportconfig/) | Create a Viem public client from a transport configuration |
| [validatePieceSizes](/reference/filoz/synapse-core/utils/functions/validatepiecesizes/) | Validate known raw piece sizes. |
| [waitForTransactionReceipt](/reference/filoz/synapse-core/utils/functions/waitfortransactionreceipt/) | viem's `waitForTransactionReceipt`, tolerant of Filecoin null rounds. |
| [~~withSignal~~](/reference/filoz/synapse-core/utils/functions/withsignal/) | Wrap a viem transport so its requests reject with iso-web's AbortError once the signal aborts. |