Skip to content

prepareAddPieces

prepareAddPieces(client, options): Promise<AddPiecesCommit>

Defined in: packages/synapse-core/src/sp/commit.ts:192

Sign an add pieces commit and return it as JSON-safe data, without sending it.

Checks the pieces fit in one message first, so a commit that can’t be sent fails before it is saved. Save the commit, then send it with submitCommit. Resubmitting the same commit can never add the pieces twice: FWSS rejects a reused nonce.

Parameter Type Description
client Client<Transport, Chain, Account> The client that signs the commit.
options OptionsType prepareAddPieces.OptionsType

Promise<AddPiecesCommit>

The signed commit prepareAddPieces.OutputType

import * as SP from '@filoz/synapse-core/sp'
import * as Piece from '@filoz/synapse-core/piece'
import { calibration } from '@filoz/synapse-core/chains'
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

const client = createWalletClient({
  account: privateKeyToAccount('0x...'),
  chain: calibration,
  transport: http(),
})
const serviceURL = 'https://pdp.example.com'
const pieceCid = Piece.from('bafkzcibcaabffs4jcd4iheeo5wisbmurjb7l4xgpmzgyzrenebvjjhsbwgx4smy')

const commit = await SP.prepareAddPieces(client, {
  dataSetId: 1n,
  clientDataSetId: 1n,
  pieces: [{ pieceCid, metadata: { name: 'photo.jpg' } }],
})

// Save the commit before sending it, so it can be resubmitted after a crash.
const saved = JSON.stringify(commit)

const result = await SP.submitCommit(client, { serviceURL, commit: JSON.parse(saved) })
const { pieceIds } = await SP.waitForAddPieces({ client, payer: commit.payer, ...result })
console.log(pieceIds)

Errors prepareAddPieces.ErrorType