dkg.js 6.0.2 → 6.0.4

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.
@@ -32,6 +32,76 @@ class AssetOperationsManager {
32
32
  this.inputService = services.inputService;
33
33
  }
34
34
 
35
+ /**
36
+ * Increases allowance for a set quantity of tokens.
37
+ * @async
38
+ * @param {number} tokenAmount - The amount of tokens to increase the allowance for.
39
+ * @param {Object} [options={}] - Additional options for increasing allowance - currently only blockchain option expected.
40
+ * @returns {Object} Object containing hash of blockchain transaction and status.
41
+ */
42
+ async increaseAllowance(tokenAmount, options = {}) {
43
+ const blockchain = this.inputService.getBlockchain(options);
44
+
45
+ this.validationService.validateIncreaseAllowance(blockchain);
46
+
47
+ const serviceAgreementV1Address = await this.blockchainService.getContractAddress(
48
+ 'ServiceAgreementV1',
49
+ blockchain,
50
+ );
51
+
52
+ const receipt = await this.blockchainService.executeContractFunction(
53
+ 'Token',
54
+ 'increaseAllowance',
55
+ [serviceAgreementV1Address, tokenAmount],
56
+ blockchain,
57
+ );
58
+
59
+ return {
60
+ transactionHash: receipt.transactionHash,
61
+ status: receipt.status,
62
+ };
63
+ }
64
+
65
+ /**
66
+ * Decreases allowance for a set quantity of tokens.
67
+ * @async
68
+ * @param {number} tokenAmount - The amount of tokens to decrease the allowance for.
69
+ * @param {Object} [options={}] - Additional options for decreasing allowance - currently only blockchain option expected.
70
+ * @returns {Object} Object containing hash of blockchain transaction and status.
71
+ */
72
+ async decreaseAllowance(tokenAmount, options = {}) {
73
+ const blockchain = this.inputService.getBlockchain(options);
74
+
75
+ this.validationService.validateDecreaseAllowance(blockchain);
76
+
77
+ const serviceAgreementV1Address = await this.blockchainService.getContractAddress(
78
+ 'ServiceAgreementV1',
79
+ blockchain,
80
+ );
81
+
82
+ const allowance = await this.blockchainService.callContractFunction(
83
+ 'Token',
84
+ 'allowance',
85
+ [blockchain.publicKey, serviceAgreementV1Address],
86
+ blockchain,
87
+ );
88
+
89
+ const receipt = await this.blockchainService.executeContractFunction(
90
+ 'Token',
91
+ 'decreaseAllowance',
92
+ [
93
+ serviceAgreementV1Address,
94
+ BigInt(tokenAmount) > BigInt(allowance) ? allowance : tokenAmount,
95
+ ], // So Error 'ERC20: decreased allowance below zero' is not emitted
96
+ blockchain,
97
+ );
98
+
99
+ return {
100
+ transactionHash: receipt.transactionHash,
101
+ status: receipt.status,
102
+ };
103
+ }
104
+
35
105
  /**
36
106
  * Creates a new asset.
37
107
  * @async
@@ -293,10 +363,10 @@ class AssetOperationsManager {
293
363
  getPublicOperationId,
294
364
  );
295
365
 
296
- if(!getPublicOperationResult.data.assertion) {
366
+ if (!getPublicOperationResult.data.assertion) {
297
367
  return {
298
368
  errorType: 'DKG_CLIENT_ERROR',
299
- errorMessage: "Unable to find assertion on the network!",
369
+ errorMessage: 'Unable to find assertion on the network!',
300
370
  };
301
371
  }
302
372
 
@@ -378,7 +448,7 @@ class AssetOperationsManager {
378
448
  authToken,
379
449
  queryString,
380
450
  QUERY_TYPES.CONSTRUCT,
381
- repository
451
+ repository,
382
452
  );
383
453
 
384
454
  queryPrivateOperationResult = await this.nodeApiService.getOperationResult(
@@ -611,6 +681,7 @@ class AssetOperationsManager {
611
681
  );
612
682
  return {
613
683
  UAL,
684
+ publicAssertionId: publicAssertionId,
614
685
  operation: getOperationStatusObject(operationResult, operationId),
615
686
  };
616
687
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dkg.js",
3
- "version": "6.0.2",
3
+ "version": "6.0.4",
4
4
  "description": "Javascript library for interaction with the OriginTrail Decentralized Knowledge Graph",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -143,16 +143,27 @@ class BlockchainServiceBase {
143
143
  blockchain,
144
144
  );
145
145
 
146
- await this.executeContractFunction(
146
+ const allowance = await this.callContractFunction(
147
147
  'Token',
148
- 'increaseAllowance',
149
- [serviceAgreementV1Address, requestData.tokenAmount],
148
+ 'allowance',
149
+ [blockchain.publicKey, serviceAgreementV1Address],
150
150
  blockchain,
151
151
  );
152
152
 
153
- stepHooks.afterHook({
154
- status: OPERATIONS_STEP_STATUS.INCREASE_ALLOWANCE_COMPLETED,
155
- });
153
+ let tokensNeeded = BigInt(requestData.tokenAmount) - BigInt(allowance);
154
+
155
+ if (tokensNeeded > 0) {
156
+ await this.executeContractFunction(
157
+ 'Token',
158
+ 'increaseAllowance',
159
+ [serviceAgreementV1Address, tokensNeeded],
160
+ blockchain,
161
+ );
162
+
163
+ stepHooks.afterHook({
164
+ status: OPERATIONS_STEP_STATUS.INCREASE_ALLOWANCE_COMPLETED,
165
+ });
166
+ }
156
167
 
157
168
  try {
158
169
  const receipt = await this.executeContractFunction(
@@ -173,12 +184,14 @@ class BlockchainServiceBase {
173
184
 
174
185
  return tokenId;
175
186
  } catch (e) {
176
- await this.executeContractFunction(
177
- 'Token',
178
- 'decreaseAllowance',
179
- [serviceAgreementV1Address, requestData.tokenAmount],
180
- blockchain,
181
- );
187
+ if (tokensNeeded > 0) {
188
+ await this.executeContractFunction(
189
+ 'Token',
190
+ 'decreaseAllowance',
191
+ [serviceAgreementV1Address, tokensNeeded],
192
+ blockchain,
193
+ );
194
+ }
182
195
  throw e;
183
196
  }
184
197
  }
@@ -197,13 +210,24 @@ class BlockchainServiceBase {
197
210
  blockchain,
198
211
  );
199
212
 
200
- await this.executeContractFunction(
213
+ const allowance = await this.callContractFunction(
201
214
  'Token',
202
- 'increaseAllowance',
203
- [serviceAgreementV1Address, tokenAmount],
215
+ 'allowance',
216
+ [blockchain.publicKey, serviceAgreementV1Address],
204
217
  blockchain,
205
218
  );
206
219
 
220
+ let tokensNeeded = BigInt(tokenAmount) - BigInt(allowance);
221
+
222
+ if (tokensNeeded > 0) {
223
+ await this.executeContractFunction(
224
+ 'Token',
225
+ 'increaseAllowance',
226
+ [serviceAgreementV1Address, tokensNeeded],
227
+ blockchain,
228
+ );
229
+ }
230
+
207
231
  try {
208
232
  return this.executeContractFunction(
209
233
  'ContentAsset',
@@ -219,12 +243,14 @@ class BlockchainServiceBase {
219
243
  blockchain,
220
244
  );
221
245
  } catch (e) {
222
- await this.executeContractFunction(
223
- 'Token',
224
- 'decreaseAllowance',
225
- [serviceAgreementV1Address, tokenAmount],
226
- blockchain,
227
- );
246
+ if (tokensNeeded > 0) {
247
+ await this.executeContractFunction(
248
+ 'Token',
249
+ 'decreaseAllowance',
250
+ [serviceAgreementV1Address, tokensNeeded],
251
+ blockchain,
252
+ );
253
+ }
228
254
  throw e;
229
255
  }
230
256
  }
@@ -1,4 +1,13 @@
1
- const { ASSET_STATES, CONTENT_TYPES, GRAPH_LOCATIONS, GRAPH_STATES, MAX_FILE_SIZE, OPERATIONS, GET_OUTPUT_FORMATS, QUERY_TYPES } = require('../constants.js');
1
+ const {
2
+ ASSET_STATES,
3
+ CONTENT_TYPES,
4
+ GRAPH_LOCATIONS,
5
+ GRAPH_STATES,
6
+ MAX_FILE_SIZE,
7
+ OPERATIONS,
8
+ GET_OUTPUT_FORMATS,
9
+ QUERY_TYPES,
10
+ } = require('../constants.js');
2
11
  const { nodeSupported } = require('./utilities.js');
3
12
 
4
13
  class ValidationService {
@@ -30,6 +39,14 @@ class ValidationService {
30
39
  this.validateAuthToken(authToken);
31
40
  }
32
41
 
42
+ validateIncreaseAllowance(blockchain) {
43
+ this.validateBlockchain(blockchain);
44
+ }
45
+
46
+ validateDecreaseAllowance(blockchain) {
47
+ this.validateBlockchain(blockchain);
48
+ }
49
+
33
50
  validateAssetCreate(
34
51
  content,
35
52
  blockchain,
@@ -44,7 +61,7 @@ class ValidationService {
44
61
  tokenAmount,
45
62
  authToken,
46
63
  ) {
47
- this.validateContent(content)
64
+ this.validateContent(content);
48
65
  this.validateBlockchain(blockchain, OPERATIONS.PUBLISH);
49
66
  this.validateEndpoint(endpoint);
50
67
  this.validatePort(port);
@@ -98,7 +115,7 @@ class ValidationService {
98
115
  tokenAmount,
99
116
  authToken,
100
117
  ) {
101
- this.validateContent(content)
118
+ this.validateContent(content);
102
119
  this.validateBlockchain(blockchain, OPERATIONS.UPDATE);
103
120
  this.validateEndpoint(endpoint);
104
121
  this.validatePort(port);
@@ -257,7 +274,7 @@ class ValidationService {
257
274
 
258
275
  const validContentTypes = Object.values(CONTENT_TYPES);
259
276
  if (!validContentTypes.includes(contentType))
260
- throw Error(`Invalid content visibility! Available parameters: ${validContentTypes}`)
277
+ throw Error(`Invalid content visibility! Available parameters: ${validContentTypes}`);
261
278
  }
262
279
 
263
280
  validateEpochsNum(epochsNum) {