dkg.js 6.3.0 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,254 @@
1
+ const jsonld = require('jsonld');
2
+ const DKG = require('../index.js');
3
+
4
+ const ENVIRONMENT = 'development';
5
+ const OT_NODE_HOSTNAME = 'http://localhost';
6
+ const OT_NODE_PORT = '8900';
7
+ const PUBLIC_KEY = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';
8
+ const PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
9
+
10
+ const DkgClient = new DKG({
11
+ environment: ENVIRONMENT,
12
+ endpoint: OT_NODE_HOSTNAME,
13
+ port: OT_NODE_PORT,
14
+ blockchain: {
15
+ name: 'hardhat2',
16
+ publicKey: PUBLIC_KEY,
17
+ privateKey: PRIVATE_KEY,
18
+ },
19
+ maxNumberOfRetries: 30,
20
+ frequency: 2,
21
+ contentType: 'all',
22
+ });
23
+
24
+ function divider() {
25
+ console.log('==================================================');
26
+ console.log('==================================================');
27
+ console.log('==================================================');
28
+ }
29
+
30
+ (async () => {
31
+
32
+ divider();
33
+
34
+ const nodeInfo = await DkgClient.node.info();
35
+ console.log('======================== NODE INFO RECEIVED');
36
+ console.log(nodeInfo);
37
+
38
+ divider();
39
+
40
+ let content = {
41
+ public: {
42
+ '@context': ['https://schema.org'],
43
+ '@id': 'uuid:1',
44
+ company: 'ParanetOperatorsDAO',
45
+ user: {
46
+ '@id': 'uuid:user:1',
47
+ },
48
+ city: {
49
+ '@id': 'uuid:Belgrade',
50
+ },
51
+ }
52
+ };
53
+
54
+ divider();
55
+
56
+ const paranetAssetResult = await DkgClient.asset.create(content, { epochsNum: 2 });
57
+ console.log('======================== PARANET KNOWLEDGE ASSET CREATED');
58
+ console.log(paranetAssetResult);
59
+
60
+ divider();
61
+
62
+ const paranetOptions = {
63
+ paranetName: 'FirstParanet',
64
+ paranetDescription: 'First ever paranet on DKG!',
65
+ tracToNeuroEmissionMultiplier: 5,
66
+ incentivizationProposalVotersRewardPercentage: 12.00,
67
+ operatorRewardPercentage: 10.00,
68
+ };
69
+ const paranetRegistered = await DkgClient.paranet.create(paranetAssetResult.UAL, paranetOptions);
70
+ console.log('======================== PARANET REGISTERED');
71
+ console.log(paranetRegistered);
72
+ divider();
73
+
74
+ const paranetDeployed = await DkgClient.paranet.deployIncentivesContract(paranetAssetResult.UAL, 'Neuroweb', paranetOptions);
75
+ console.log('======================== PARANET INCENTIVES POOL DEPLOYED');
76
+ console.log(paranetDeployed);
77
+ divider();
78
+
79
+ content = {
80
+ public: {
81
+ '@context': ['https://schema.org'],
82
+ '@id': 'uuid:6',
83
+ company: 'ServiceExample',
84
+ user: {
85
+ '@id': 'uuid:user:6',
86
+ },
87
+ city: {
88
+ '@id': 'uuid:Ljubljana',
89
+ },
90
+ }
91
+ };
92
+ const createServiceKAResult = await DkgClient.asset.create(content, { epochsNum: 2 });
93
+ console.log('======================== SERVICE KA CREATED');
94
+ console.log(createServiceKAResult);
95
+ divider();
96
+
97
+ const paranetServiceUAL = await DkgClient.paranet.createService(createServiceKAResult.UAL, {
98
+ paranetServiceName: 'FKPS',
99
+ paranetServiceDescription: 'Fast Knowledge Processing Service',
100
+ paranetServiceAddresses: [],
101
+ });
102
+ console.log('======================== SERVICE KA CREATED');
103
+ console.log(paranetServiceUAL);
104
+ divider();
105
+
106
+ await DkgClient.paranet.addServices(paranetAssetResult.UAL, [ createServiceKAResult.UAL]);
107
+ console.log('======================== SERVICE ADDED TO PARANET');
108
+ divider();
109
+
110
+ content = {
111
+ public: {
112
+ '@context': ['https://schema.org'],
113
+ '@id': 'uuid:2',
114
+ company: 'KA-Company',
115
+ user: {
116
+ '@id': 'uuid:user:2',
117
+ },
118
+ city: {
119
+ '@id': 'uuid:budapest',
120
+ },
121
+ }
122
+ };
123
+ const createAssetResult = await DkgClient.asset.create(content, { epochsNum: 2, paranetUAL: paranetAssetResult.UAL });
124
+ console.log('======================== KNOWLEDGE ASSET CREATED TO PARANET');
125
+ console.log(createAssetResult);
126
+ divider();
127
+
128
+ content = {
129
+ public: {
130
+ '@context': ['https://schema.org'],
131
+ '@id': 'uuid:3',
132
+ company: 'KA1-Company',
133
+ user: {
134
+ '@id': 'uuid:user:3',
135
+ },
136
+ city: {
137
+ '@id': 'uuid:Belgrade',
138
+ },
139
+ }
140
+ };
141
+ const createSecondAssetResult = await DkgClient.asset.create(content, { epochsNum: 2 });
142
+ console.log('======================== SECOND KNOWLEDGE ASSET CREATED');
143
+ console.log(createSecondAssetResult);
144
+ divider();
145
+
146
+ const submitResult = await DkgClient.asset.submitToParanet(createSecondAssetResult.UAL, paranetAssetResult.UAL);
147
+ console.log('======================== SECOND KA ADDED TO PARANET');
148
+ console.log(submitResult);
149
+ divider();
150
+
151
+ console.log('======================== IS MINER : ', await DkgClient.paranet.isKnowledgeMiner(paranetAssetResult.UAL, { roleAddress: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' }));
152
+ console.log('======================== IS OPERATOR : ', await DkgClient.paranet.isParanetOperator(paranetAssetResult.UAL));
153
+ console.log('======================== IS VOTER : ', await DkgClient.paranet.isProposalVoter(paranetAssetResult.UAL, { roleAddress: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' }));
154
+ divider();
155
+
156
+ let claimable = await DkgClient.paranet.getClaimableMinerReward(paranetAssetResult.UAL);
157
+ console.log('======================== KA MINER REWARD TO CLAIM')
158
+ console.log(claimable);
159
+ divider();
160
+
161
+ claimable = await DkgClient.paranet.getClaimableOperatorReward(paranetAssetResult.UAL);
162
+ console.log('======================== OPERATOR REWARD TO CLAIM');
163
+ console.log(claimable);
164
+ divider();
165
+
166
+ let claimedResult = await DkgClient.paranet.claimMinerReward(paranetAssetResult.UAL);
167
+ console.log('======================== KA MINER REWARD CLAIMED');
168
+ console.log(claimedResult);
169
+ divider();
170
+
171
+ claimedResult = await DkgClient.paranet.claimOperatorReward(paranetAssetResult.UAL);
172
+ console.log('======================== OPERATOR REWARD CLAIMED');
173
+ console.log(claimedResult);
174
+ divider();
175
+
176
+ claimable = await DkgClient.paranet.getClaimableMinerReward(paranetAssetResult.UAL);
177
+ console.log('======================== KA MINER REWARD TO CLAIM');
178
+ console.log(claimable);
179
+ divider();
180
+
181
+ claimable = await DkgClient.paranet.getClaimableOperatorReward(paranetAssetResult.UAL);
182
+ console.log('======================== OPERATOR REWARD TO CLAIM');
183
+ console.log(claimable);
184
+ divider();
185
+
186
+ const updateAssetResult = await DkgClient.asset.update(createSecondAssetResult.UAL, {
187
+ public: {
188
+ '@context': ['https://schema.org'],
189
+ '@id': 'uuid:1',
190
+ company: 'TL',
191
+ user: {
192
+ '@id': 'uuid:user:2',
193
+ },
194
+ city: {
195
+ '@id': 'uuid:Madrid',
196
+ },
197
+ },
198
+ });
199
+ console.log('========================KNOWLEDGE ASSET UPDATED');
200
+ console.log(updateAssetResult);
201
+ divider();
202
+
203
+ await DkgClient.asset.waitFinalization(createSecondAssetResult.UAL);
204
+ console.log('======================== FINALIZATION COMPLETED');
205
+ divider();
206
+
207
+ await DkgClient.paranet.updateClaimableRewards(paranetAssetResult.UAL);
208
+ console.log('======================== CLAIMABLE REWARDS UPDATED');
209
+ divider();
210
+
211
+ const queryWhereMadrid = `PREFIX schema: <http://schema.org/>
212
+ SELECT DISTINCT ?graphName
213
+ WHERE {
214
+ GRAPH ?graphName {
215
+ ?s schema:city <uuid:uzice> .
216
+ }
217
+ }`;
218
+
219
+ let queryResult = await DkgClient.graph.query(
220
+ queryWhereMadrid,
221
+ 'SELECT',
222
+ { paranetUAL: paranetAssetResult.UAL },
223
+ );
224
+ console.log('======================== QUERY PARANET REPO RESULT');
225
+ console.log(queryResult.data);
226
+ divider();
227
+
228
+ const federatedQuery = `
229
+ PREFIX schema: <http://schema.org/>
230
+ SELECT DISTINCT ?s ?city1 ?user1 ?s2 ?city2 ?user2 ?company1
231
+ WHERE {
232
+ ?s schema:city ?city1 .
233
+ ?s schema:company ?company1 .
234
+ ?s schema:user ?user1;
235
+
236
+ SERVICE <${createSecondAssetResult.UAL}> {
237
+ ?s2 schema:city <uuid:Belgrade> .
238
+ ?s2 schema:city ?city2 .
239
+ ?s2 schema:user ?user2;
240
+ }
241
+
242
+ filter(contains(str(?city2), "Belgrade"))
243
+ }
244
+ `;
245
+
246
+ queryResult = await DkgClient.graph.query(
247
+ federatedQuery,
248
+ 'SELECT',
249
+ { graphLocation: paranetAssetResult.UAL },
250
+ );
251
+ console.log('======================== FEDERATED QUERY RESULT');
252
+ console.log(queryResult.data);
253
+ divider();
254
+ })();
package/index.js CHANGED
@@ -5,6 +5,7 @@ const BlockchainOperationsManager = require('./managers/blockchain-operations-ma
5
5
  const GraphOperationsManager = require('./managers/graph-operations-manager.js');
6
6
  const NetworkOperationsManager = require('./managers/network-operations-manager.js');
7
7
  const NodeOperationsManager = require('./managers/node-operations-manager.js');
8
+ const ParanetOperationsManager = require('./managers/paranet-operations-manager.js');
8
9
 
9
10
  const BaseServiceManager = require('./services/base-service-manager.js');
10
11
 
@@ -19,6 +20,7 @@ class DkgClient {
19
20
  this.node = new NodeOperationsManager(services);
20
21
  this.graph = new GraphOperationsManager(services);
21
22
  this.network = new NetworkOperationsManager(services);
23
+ this.paranet = new ParanetOperationsManager(services);
22
24
  }
23
25
  }
24
26
  module.exports = DkgClient;
@@ -280,6 +280,7 @@ class AssetOperationsManager {
280
280
  immutable,
281
281
  tokenAmount,
282
282
  authToken,
283
+ paranetUAL,
283
284
  } = this.inputService.getAssetCreateArguments(options);
284
285
 
285
286
  this.validationService.validateAssetCreate(
@@ -295,6 +296,7 @@ class AssetOperationsManager {
295
296
  immutable,
296
297
  tokenAmount,
297
298
  authToken,
299
+ paranetUAL
298
300
  );
299
301
 
300
302
  const { public: publicAssertion, private: privateAssertion } = await formatGraph(
@@ -330,20 +332,43 @@ class AssetOperationsManager {
330
332
  hashFunctionId,
331
333
  ));
332
334
 
333
- const tokenId = await this.blockchainService.createAsset(
334
- {
335
- publicAssertionId,
336
- assertionSize: publicAssertionSizeInBytes,
337
- triplesNumber: assertionMetadata.getAssertionTriplesNumber(publicAssertion),
338
- chunksNumber: assertionMetadata.getAssertionChunksNumber(publicAssertion),
339
- epochsNum,
340
- tokenAmount: tokenAmountInWei,
341
- scoreFunctionId: scoreFunctionId ?? 1,
342
- immutable_: immutable,
343
- },
344
- blockchain,
345
- stepHooks,
346
- );
335
+ let tokenId;
336
+ if(paranetUAL == null) {
337
+ tokenId = await this.blockchainService.createAsset(
338
+ {
339
+ publicAssertionId,
340
+ assertionSize: publicAssertionSizeInBytes,
341
+ triplesNumber: assertionMetadata.getAssertionTriplesNumber(publicAssertion),
342
+ chunksNumber: assertionMetadata.getAssertionChunksNumber(publicAssertion),
343
+ epochsNum,
344
+ tokenAmount: tokenAmountInWei,
345
+ scoreFunctionId: scoreFunctionId ?? 1,
346
+ immutable_: immutable,
347
+ },
348
+ null,
349
+ null,
350
+ blockchain,
351
+ stepHooks,
352
+ );
353
+ } else {
354
+ const { contract: paranetKaContract, tokenId: paranetTokenId } = resolveUAL(paranetUAL);
355
+ tokenId = await this.blockchainService.createAsset(
356
+ {
357
+ publicAssertionId,
358
+ assertionSize: publicAssertionSizeInBytes,
359
+ triplesNumber: assertionMetadata.getAssertionTriplesNumber(publicAssertion),
360
+ chunksNumber: assertionMetadata.getAssertionChunksNumber(publicAssertion),
361
+ epochsNum,
362
+ tokenAmount: tokenAmountInWei,
363
+ scoreFunctionId: scoreFunctionId ?? 1,
364
+ immutable_: immutable,
365
+ },
366
+ paranetKaContract,
367
+ paranetTokenId,
368
+ blockchain,
369
+ stepHooks,
370
+ );
371
+ }
347
372
 
348
373
  const resolvedUAL = {
349
374
  blockchain: blockchain.name,
@@ -1349,6 +1374,41 @@ class AssetOperationsManager {
1349
1374
 
1350
1375
  return tokenAmountInWei > 0 ? tokenAmountInWei : 0;
1351
1376
  }
1377
+
1378
+ /**
1379
+ * Add knowledge asset to a paranet.
1380
+ * @async
1381
+ * @param {string} UAL - The Universal Asset Locator of the knowledge asset.
1382
+ * @param {string} paranetUAL - The Universal Asset Locator of the Paranet.
1383
+ * @param {Object} [options={}] - Additional options for adding tokens.
1384
+ * @returns {Object} An object containing the UAL and operation status.
1385
+ */
1386
+ async submitToParanet(UAL, paranetUAL, options = {}) {
1387
+ const blockchain = this.inputService.getBlockchain(options);
1388
+
1389
+ this.validationService.validateSubmitToParanet(
1390
+ UAL,
1391
+ paranetUAL,
1392
+ blockchain,
1393
+ );
1394
+
1395
+ const { contract, tokenId } = resolveUAL(UAL);
1396
+ const { contract: paranetContract, tokenId: paranetTokenId } = resolveUAL(paranetUAL);
1397
+
1398
+ await this.blockchainService.submitToParanet({
1399
+ paranetContract,
1400
+ paranetTokenId,
1401
+ contract,
1402
+ tokenId,
1403
+ },
1404
+ blockchain
1405
+ );
1406
+
1407
+ return {
1408
+ UAL,
1409
+ operation: getOperationStatusObject({ status: 'COMPLETED' }, null),
1410
+ };
1411
+ }
1352
1412
  }
1353
1413
 
1354
1414
  module.exports = AssetOperationsManager;