filecoin-pin
Version:
Bridge IPFS content to Filecoin Onchain Cloud using familiar tools
67 lines • 2.79 kB
JavaScript
import { getErrorMessage } from '../utils/errors.js';
/**
* Number of block confirmations to wait for when waitForConfirmation=true
*/
const WAIT_CONFIRMATIONS = 1;
/**
* Timeout in milliseconds for waiting for transaction confirmation
* Set to 2 minutes - generous default for Calibration network finality
*/
const WAIT_TIMEOUT_MS = 2 * 60 * 1000;
/**
* Remove a piece from a Data Set
*
* @example
* ```typescript
* const txHash = await removePiece('baga...', storageContext, {
* synapse,
* onProgress: (event) => console.log(event.type),
* waitForConfirmation: true
* })
* ```
*
* Process:
* 1. Submit the transaction via storageContext.deletePiece
* 2. Optionally wait for confirmation using Synapse provider
* 3. Emit progress events for each stage
*
* @param pieceCid - Piece CID to remove
* @param storageContext - Storage context bound to a Data Set
* @param options - Callbacks and confirmation settings (synapse required if waiting)
* @returns Transaction hash of the removal
*/
export async function removePiece(pieceCid, storageContext, options) {
const { onProgress, waitForConfirmation } = options;
const dataSetId = storageContext.dataSetId;
if (dataSetId == null) {
throw new Error('Storage context must be bound to a Data Set before removing pieces. Use createStorageContext with dataset.useExisting to bind to a Data Set.');
}
if (waitForConfirmation === true && !isWaitForConfirmationOptions(options)) {
throw new Error('A Synapse instance is required when waitForConfirmation is true');
}
onProgress?.({ type: 'remove-piece:submitting', data: { pieceCid, dataSetId } });
const txHash = await storageContext.deletePiece(pieceCid);
onProgress?.({ type: 'remove-piece:submitted', data: { pieceCid, dataSetId, txHash } });
let isConfirmed = false;
if (isWaitForConfirmationOptions(options)) {
const { synapse } = options;
onProgress?.({ type: 'remove-piece:confirming', data: { pieceCid, dataSetId, txHash } });
try {
await synapse.getProvider().waitForTransaction(txHash, WAIT_CONFIRMATIONS, WAIT_TIMEOUT_MS);
isConfirmed = true;
}
catch (error) {
// Confirmation timeout is non-fatal - transaction may still succeed
onProgress?.({
type: 'remove-piece:confirmation-failed',
data: { pieceCid, dataSetId, txHash, message: getErrorMessage(error) },
});
}
}
onProgress?.({ type: 'remove-piece:complete', data: { txHash, confirmed: isConfirmed } });
return txHash;
}
function isWaitForConfirmationOptions(options) {
return options.waitForConfirmation === true && options.synapse != null;
}
//# sourceMappingURL=remove-piece.js.map