Skip to content

prepareCreateDataSetAndAddPieces

prepareCreateDataSetAndAddPieces(client, options): Promise<CreateDataSetAndAddPiecesCommit>

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

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

Checks the pieces and data set metadata 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 create the data set twice: FWSS rejects a reused client data set id.

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

Promise<CreateDataSetAndAddPiecesCommit>

The signed commit prepareCreateDataSetAndAddPieces.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.prepareCreateDataSetAndAddPieces(client, {
  payee: '0x1234567890123456789012345678901234567890', // the provider's service address
  metadata: { project: 'photos' },
  pieces: [{ pieceCid }],
})

// 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 { dataSetId, pieceIds } = await SP.waitForCreateDataSetAddPieces({ client, ...result })
console.log(dataSetId, pieceIds)

Errors prepareCreateDataSetAndAddPieces.ErrorType