-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdata-sets.ts
More file actions
340 lines (311 loc) · 10.2 KB
/
data-sets.ts
File metadata and controls
340 lines (311 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from 'abitype'
import {
type Account,
type Address,
type Chain,
type Client,
encodeAbiParameters,
isAddressEqual,
type Transport,
} from 'viem'
import { multicall, readContract, simulateContract, writeContract } from 'viem/actions'
import type * as Abis from '../abis/index.ts'
import { getChain } from '../chains.ts'
import { DataSetNotFoundError } from '../errors/warm-storage.ts'
import type { PieceCID } from '../piece.ts'
import * as SP from '../sp.ts'
import { signAddPieces } from '../typed-data/sign-add-pieces.ts'
import { signCreateDataSet } from '../typed-data/sign-create-dataset.ts'
import { capabilitiesListToObject } from '../utils/capabilities.ts'
import {
datasetMetadataObjectToEntry,
type MetadataObject,
metadataArrayToObject,
pieceMetadataObjectToEntry,
} from '../utils/metadata.ts'
import { decodePDPCapabilities } from '../utils/pdp-capabilities.ts'
import { randU256 } from '../utils/rand.ts'
import type { PDPOffering } from './providers.ts'
/**
* ABI function to get the client data sets
*/
export type getClientDataSetsType = ExtractAbiFunction<typeof Abis.storageView, 'getClientDataSets'>
/**
* ABI Client data set
*/
export type ClientDataSet = AbiParametersToPrimitiveTypes<getClientDataSetsType['outputs']>[0][0]
/**
* Data set type
*/
export interface DataSet extends ClientDataSet {
live: boolean
managed: boolean
cdn: boolean
metadata: MetadataObject
pdp: PDPOffering
}
export interface GetDataSetsOptions {
address: Address
}
/**
* Get all data sets for a client
*
* @param client
* @param options
*/
export async function getDataSets(client: Client<Transport, Chain>, options: GetDataSetsOptions): Promise<DataSet[]> {
const chain = getChain(client.chain.id)
const address = options.address
const data = await readContract(client, {
address: chain.contracts.storageView.address,
abi: chain.contracts.storageView.abi,
functionName: 'getClientDataSets',
args: [address],
})
const promises = data.map(async (dataSet) => {
const [live, listener, metadata, pdpOffering] = await multicall(client, {
allowFailure: false,
contracts: [
{
abi: chain.contracts.pdp.abi,
address: chain.contracts.pdp.address,
functionName: 'dataSetLive',
args: [dataSet.dataSetId],
},
{
abi: chain.contracts.pdp.abi,
address: chain.contracts.pdp.address,
functionName: 'getDataSetListener',
args: [dataSet.dataSetId],
},
{
address: chain.contracts.storageView.address,
abi: chain.contracts.storageView.abi,
functionName: 'getAllDataSetMetadata',
args: [dataSet.dataSetId],
},
{
address: chain.contracts.serviceProviderRegistry.address,
abi: chain.contracts.serviceProviderRegistry.abi,
functionName: 'getProviderWithProduct',
args: [dataSet.providerId, 0], // 0 = PDP product type
},
],
})
// getProviderWithProduct returns {providerId, providerInfo, product, productCapabilityValues}
const pdpCaps = await decodePDPCapabilities(
dataSet.providerId,
client.chain.id,
capabilitiesListToObject(pdpOffering.product.capabilityKeys, pdpOffering.productCapabilityValues)
)
return {
...dataSet,
live,
managed: isAddressEqual(listener, chain.contracts.storage.address),
cdn: dataSet.cdnRailId !== 0n,
metadata: metadataArrayToObject(metadata),
pdp: pdpCaps,
}
})
const proofs = await Promise.all(promises)
return proofs
}
export type GetDataSetOptions = {
/**
* The ID of the data set to get.
*/
dataSetId: bigint
}
/**
* Get a data set by ID
*
* @param client - The client to use to get the data set.
* @param options - The options for the get data set.
* @param options.dataSetId - The ID of the data set to get.
* @throws - {@link DataSetNotFoundError} if the data set is not found.
* @returns The data set
*/
export async function getDataSet(client: Client<Transport, Chain>, options: GetDataSetOptions): Promise<DataSet> {
const chain = getChain(client.chain.id)
const dataSet = await readContract(client, {
address: chain.contracts.storageView.address,
abi: chain.contracts.storageView.abi,
functionName: 'getDataSet',
args: [options.dataSetId],
})
if (dataSet.pdpRailId === 0n) {
throw new DataSetNotFoundError(options.dataSetId)
}
const [live, listener, metadata, pdpOffering] = await multicall(client, {
allowFailure: false,
contracts: [
{
abi: chain.contracts.pdp.abi,
address: chain.contracts.pdp.address,
functionName: 'dataSetLive',
args: [options.dataSetId],
},
{
abi: chain.contracts.pdp.abi,
address: chain.contracts.pdp.address,
functionName: 'getDataSetListener',
args: [options.dataSetId],
},
{
address: chain.contracts.storageView.address,
abi: chain.contracts.storageView.abi,
functionName: 'getAllDataSetMetadata',
args: [options.dataSetId],
},
{
address: chain.contracts.serviceProviderRegistry.address,
abi: chain.contracts.serviceProviderRegistry.abi,
functionName: 'getProviderWithProduct',
args: [dataSet.providerId, 0], // 0 = PDP product type
},
],
})
// getProviderWithProduct returns {providerId, providerInfo, product, productCapabilityValues}
const pdpCaps = await decodePDPCapabilities(
dataSet.providerId,
client.chain.id,
capabilitiesListToObject(pdpOffering.product.capabilityKeys, pdpOffering.productCapabilityValues)
)
return {
...dataSet,
live,
managed: isAddressEqual(listener, chain.contracts.storage.address),
cdn: dataSet.cdnRailId !== 0n,
metadata: metadataArrayToObject(metadata),
pdp: pdpCaps,
}
}
/**
* Get the metadata for a data set
*
* @param client
* @param dataSetId
* @returns
*/
export async function getDataSetMetadata(client: Client<Transport, Chain>, dataSetId: bigint) {
const chain = getChain(client.chain.id)
const metadata = await readContract(client, {
address: chain.contracts.storageView.address,
abi: chain.contracts.storageView.abi,
functionName: 'getAllDataSetMetadata',
args: [dataSetId],
})
return metadataArrayToObject(metadata)
}
export type CreateDataSetOptions = {
cdn: boolean
payee: Address
/**
* If client is from a session key this should be set to the actual payer address
*/
payer?: Address
endpoint: string
metadata?: MetadataObject
}
/**
* Create a data set
*
* @param client - The client to use to create the data set.
* @param options - The options for the create data set.
* @param options.payee - The address that will receive payments (service provider).
* @param options.payer - The address that will pay for the storage (client).
* @param options.endpoint - The endpoint of the PDP API.
* @param options.cdn - Whether the data set should use CDN.
* @param options.metadata - The metadata for the data set.
* @returns The response from the create data set on PDP API.
*/
export async function createDataSet(client: Client<Transport, Chain, Account>, options: CreateDataSetOptions) {
const chain = getChain(client.chain.id)
const nonce = randU256()
// Sign and encode the create data set message
const extraData = await signCreateDataSet(client, {
clientDataSetId: nonce,
payee: options.payee,
payer: options.payer,
metadata: datasetMetadataObjectToEntry(options.metadata, {
cdn: options.cdn,
}),
})
return SP.createDataSet({
endpoint: options.endpoint,
recordKeeper: chain.contracts.storage.address,
extraData,
})
}
export type CreateDataSetAndAddPiecesOptions = {
/**
* If client is from a session key this should be set to the actual payer address
*/
payer?: Address
endpoint: string
payee: Address
cdn: boolean
metadata?: MetadataObject
pieces: { pieceCid: PieceCID; metadata?: MetadataObject }[]
}
/**
* Create a data set and add pieces to it
*
* @param client - The client to use to create the data set.
* @param options - The options for the create data set.
* @param options.payer - The address that will pay for the storage (client).
* @param options.endpoint - The endpoint of the PDP API.
* @param options.payee - The address that will receive payments (service provider).
* @param options.cdn - Whether the data set should use CDN.
* @param options.metadata - The metadata for the data set.
* @returns The response from the create data set on PDP API.
*/
export async function createDataSetAndAddPieces(
client: Client<Transport, Chain, Account>,
options: CreateDataSetAndAddPiecesOptions
) {
const chain = getChain(client.chain.id)
const clientDataSetId = randU256()
// Sign and encode the create data set message
const dataSetExtraData = await signCreateDataSet(client, {
clientDataSetId,
payee: options.payee,
payer: options.payer,
metadata: datasetMetadataObjectToEntry(options.metadata, {
cdn: options.cdn,
}),
})
// Sign and encode the add pieces message
const addPiecesExtraData = await signAddPieces(client, {
clientDataSetId,
nonce: randU256(),
pieces: options.pieces.map((piece) => ({
pieceCid: piece.pieceCid,
metadata: pieceMetadataObjectToEntry(piece.metadata),
})),
})
const extraData = encodeAbiParameters([{ type: 'bytes' }, { type: 'bytes' }], [dataSetExtraData, addPiecesExtraData])
return SP.createDataSetAndAddPieces({
endpoint: options.endpoint,
recordKeeper: chain.contracts.storage.address,
extraData,
pieces: options.pieces.map((piece) => piece.pieceCid),
})
}
export type TerminateDataSetOptions = {
/**
* The ID of the data set to terminate.
*/
dataSetId: bigint
}
export async function terminateDataSet(client: Client<Transport, Chain, Account>, options: TerminateDataSetOptions) {
const chain = getChain(client.chain.id)
const { request } = await simulateContract(client, {
address: chain.contracts.storage.address,
abi: chain.contracts.storage.abi,
functionName: 'terminateService',
args: [options.dataSetId],
})
const tx = await writeContract(client, request)
return tx
}