-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
349 lines (321 loc) · 11.8 KB
/
index.ts
File metadata and controls
349 lines (321 loc) · 11.8 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
341
342
343
344
345
346
347
348
349
/*******************************************************************************
* (c) 2023 dataswap
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/
import {
withCallMethod,
withSendMethod,
EvmOutput,
EvmTransactionOptions,
} from "@unipackage/net"
import { Message, ContractMessageDecoder } from "@unipackage/filecoin"
import { DataswapMessage } from "../../../../../message/types"
import { DatasetMetadata, DatasetTimeoutParameters } from "../../types"
import { EvmEx } from "../../../../../shared/types/evmEngineType"
import { DatasetState } from "../../../../../shared/types/datasetType"
import { BasicStatisticsInfo } from "../../../../../shared/types/statisticsType"
import { ethAddressFromDelegated } from "@glif/filecoin-address"
interface DatasetMetadataCallEvm {
/**
* Retrieves an overview of count statistics.
* @returns A promise that resolves with the count overview.
*/
getCountOverview(): Promise<EvmOutput<BasicStatisticsInfo>>
/**
* Retrieves an overview of size statistics.
* @returns A promise that resolves with the size overview.
*/
getSizeOverview(): Promise<EvmOutput<BasicStatisticsInfo>>
/**
* Get dataset metadata
* @param datasetId The ID of the dataset to get metadata.
* @returns The metadata of dataset.
*/
getDatasetMetadata(datasetId: number): Promise<EvmOutput<DatasetMetadata>>
/**
* Get submitter of dataset's metadata
* @param datasetId The ID of the dataset to get submitter of dataset'metadata.
* @returns The address of submitter.
*/
getDatasetMetadataSubmitter(datasetId: number): Promise<EvmOutput<string>>
/**
* Get client of dataset.
* @param datasetId The ID of the dataset to get client of dataset'metadata.
* @returns The actor id of client in filecoin network.
*/
getDatasetMetadataClient(datasetId: number): Promise<EvmOutput<number>>
/**
* Retrieves the timeout parameters for a dataset.
* @param datasetId The ID of the dataset for which to retrieve the timeout parameters.
* @returns A promise that resolves with the proof and audit block count timeout parameters.
*/
getDatasetTimeoutParameters(
datasetId: number
): Promise<EvmOutput<DatasetTimeoutParameters>>
/**
* Get dataset state
* @param datasetId The ID of the dataset to get state of dataset.
* @returns The state of dataset.
*/
getDatasetState(datasetId: number): Promise<EvmOutput<DatasetState>>
/**
* Retrieves the associated dataset ID for a given dataset ID.
* @param datasetId The dataset ID for which to retrieve the associated dataset ID.
* @returns A promise that resolves with the associated dataset ID.
*/
getAssociatedDatasetId(datasetId: number): Promise<EvmOutput<number>>
/**
* Retrieves the dataset ID associated with the specified access method.
* @param accessMethod The method of accessing the dataset.
* @returns A promise that resolves with the dataset ID corresponding to the access method.
*/
getDatasetIdForAccessMethod(
accessMethod: string
): Promise<EvmOutput<number>>
/**
* Check if a dataset has metadata
* @param accessMethod Method of accessing the dataset to check.
* @returns When the access method exists in a dataset, return true; otherwise, return false.
*/
hasDatasetMetadata(accessMethod: string): Promise<EvmOutput<boolean>>
/**
* Default getter functions for public variables
* @returns The total number of datasets in the current datasets contract.
*/
datasetsCount(): Promise<EvmOutput<number>>
/**
* Retrieves the roles associated with the current user.
* @returns A promise that resolves with the roles of the current user.
*/
roles(): Promise<EvmOutput<string>>
/**
* Get governance address
* @return The address of governance
*/
governanceAddress(): Promise<EvmOutput<string>>
}
interface DatasetMetadataSendEvm {
/**
* Submit metadata for a dataset
* @param client The client id of the dataset.
* @param title The title of the dataset.
* @param industry The industry of the dataset.
* @param name The name of the dataset.
* @param description The description of the dataset.
* @param source The source of the dataset.
* @param accessMethod The access method of the dataset.
* @param sizeInBytes The size of the dataset in bytes.
* @param isPublic A boolean indicating if the dataset is public.
* @param version The version of the dataset.
* @param options The options of transaction.
*/
submitDatasetMetadata(
client: number,
title: string,
industry: string,
name: string,
description: string,
source: string,
accessMethod: string,
sizeInBytes: bigint,
isPublic: boolean,
version: bigint,
options?: EvmTransactionOptions
): Promise<EvmOutput<any>>
/**
* Reports a dataset workflow timeout.
* @param datasetId The ID of the dataset for which to report the timeout.
* @returns A promise that resolves with the result of reporting the dataset workflow timeout.
*/
reportDatasetWorkflowTimeout(datasetId: number): Promise<EvmOutput<any>>
/**
* Updates the timeout parameters for a dataset.
* @param datasetId The ID of the dataset for which to update the timeout parameters.
* @param proofBlockCount The new proof block count timeout parameter.
* @param auditBlockCount The new audit block count timeout parameter.
* @returns A promise that resolves with the result of updating the dataset timeout parameters.
*/
updateDatasetTimeoutParameters(
datasetId: number,
proofBlockCount: bigint,
auditBlockCount: bigint
): Promise<EvmOutput<any>>
}
/**
* Combined interface for EVM calls and transactions related to Datasets contract.
*/
export interface DatasetMetadataOriginEvm
extends DatasetMetadataCallEvm,
DatasetMetadataSendEvm {}
/**
* Implementation of DatasetMetadataOriginEvm with specific EVM methods.
*/
@withCallMethod([
"getCountOverview",
"getSizeOverview",
"getDatasetMetadata",
"getDatasetMetadataSubmitter",
"getDatasetMetadataClient",
"getDatasetTimeoutParameters",
"getDatasetState",
"getAssociatedDatasetId",
"getDatasetIdForAccessMethod",
"hasDatasetMetadata",
"datasetsCount",
"roles",
"governanceAddress",
])
@withSendMethod([
"submitDatasetMetadata",
"reportDatasetWorkflowTimeout",
"updateDatasetTimeoutParameters",
])
export class DatasetMetadataOriginEvm extends EvmEx {}
/**
* Extended class for DatasetMetadataOriginEvm with additional message decoding.
*/
export class DatasetMetadataEvm extends DatasetMetadataOriginEvm {
/**
* Retrieves an overview of count statistics.
* @returns A promise that resolves with the count overview.
*/
async getCountOverview(): Promise<EvmOutput<BasicStatisticsInfo>> {
const res = await super.getCountOverview()
if (res.ok && res.data) {
return {
ok: true,
data: new BasicStatisticsInfo({
...res.data,
}),
}
}
return res
}
/**
* Retrieves an overview of size statistics.
* @returns A promise that resolves with the size overview.
*/
async getSizeOverview(): Promise<EvmOutput<BasicStatisticsInfo>> {
const res = await super.getSizeOverview()
if (res.ok && res.data) {
return {
ok: true,
data: new BasicStatisticsInfo({
...res.data,
}),
}
}
return res
}
async getDatasetMetadata(
datasetId: number
): Promise<EvmOutput<DatasetMetadata>> {
const metaRes = await super.getDatasetMetadata(datasetId)
if (metaRes.ok && metaRes.data) {
return {
ok: true,
data: new DatasetMetadata({
...metaRes.data,
datasetId: datasetId,
}),
}
}
return metaRes
}
async getDatasetTimeoutParameters(
datasetId: number
): Promise<EvmOutput<DatasetTimeoutParameters>> {
const res = await super.getDatasetTimeoutParameters(datasetId)
if (res.ok && res.data) {
return {
ok: true,
data: new DatasetTimeoutParameters({
...res.data,
datasetId: datasetId,
}),
}
}
return res
}
async getDatasetState(datasetId: number): Promise<EvmOutput<DatasetState>> {
const metaRes = await super.getDatasetState(datasetId)
if (metaRes.ok && metaRes.data) {
return {
ok: true,
data: Number(metaRes.data) as DatasetState,
}
}
return metaRes
}
async getAssociatedDatasetId(
datasetId: number
): Promise<EvmOutput<number>> {
const res = await super.getAssociatedDatasetId(datasetId)
if (res.ok && res.data) {
return {
ok: true,
data: Number(res.data),
}
}
return res
}
decodeMessage(msg: Message): EvmOutput<DataswapMessage> {
const decoder = new ContractMessageDecoder(this)
const decodeRes = decoder.decode(msg)
if (!decodeRes.ok && !decodeRes.data) {
return { ok: false, error: decodeRes.error }
}
let result: DataswapMessage =
decodeRes.data!.values() as DataswapMessage
switch (decodeRes.data!.method) {
case "submitDatasetMetadata":
result.datasetId = Number(result.return)
/*params match the DatasetMetadata struct*/
result.params.submitter = ethAddressFromDelegated(msg.Msg.From)
result.params.createdBlockNumber = result.height
result.params.datasetId = result.datasetId
result.params.status = DatasetState.MetadataSubmitted
break
case "updateDatasetTimeoutParameters":
result.datasetId = Number(result.params.datasetId)
result.params.datasetId = result.datasetId
result.params.proofBlockCount = BigInt(
result.params.proofBlockCount
)
result.params.auditBlockCount = BigInt(
result.params.auditBlockCount
)
break
case "reportDatasetWorkflowTimeout":
result.datasetId = Number(result.params.datasetId)
result.params.datasetId = result.datasetId
break
case "upgradeTo":
break
default:
return {
ok: false,
error: "Not support method!",
}
}
return {
ok: true,
data: result,
}
}
}