@bosonprotocol/core-sdk 1.11.0-alpha.8 → 1.11.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.
package/src/core-sdk.ts CHANGED
@@ -29,6 +29,10 @@ export class CoreSDK {
29
29
  private _subgraphUrl: string;
30
30
  private _protocolDiamond: string;
31
31
 
32
+ /**
33
+ * Creates an instance of `CoreSDK`
34
+ * @param args - Constructor args
35
+ */
32
36
  constructor(opts: {
33
37
  web3Lib: Web3LibAdapter;
34
38
  subgraphUrl: string;
@@ -43,6 +47,22 @@ export class CoreSDK {
43
47
  this._theGraphStorage = opts.theGraphStorage;
44
48
  }
45
49
 
50
+ /**
51
+ * Creates an instance of `CoreSDK` by using default values derived either from
52
+ * `args.envName` or `args.chainId`.
53
+ *
54
+ * @example
55
+ * Instance which uses the default contract address and subgraph url of mainnet:
56
+ * ```ts
57
+ * const coreSdk = CoreSDK.fromDefaultConfig({
58
+ * ...otherArgs,
59
+ * chainId: 137
60
+ * })
61
+ * ```
62
+ *
63
+ * @param args - Constructor args.
64
+ * @returns CoreSDK instance with default values.
65
+ */
46
66
  static fromDefaultConfig(args: {
47
67
  web3Lib: Web3LibAdapter;
48
68
  envName?: string;
@@ -64,6 +84,16 @@ export class CoreSDK {
64
84
  });
65
85
  }
66
86
 
87
+ /* -------------------------------------------------------------------------- */
88
+ /* Metadata related methods */
89
+ /* -------------------------------------------------------------------------- */
90
+
91
+ /**
92
+ * Stores supported offer metadata via the MetadataStorage instance which was passed in
93
+ * at construction.
94
+ * @param metadata - Offer metadata of type `BASE` or `PRODUCT_V1`.
95
+ * @returns Metadata hash / identifier.
96
+ */
67
97
  public async storeMetadata(metadata: AnyMetadata): Promise<string> {
68
98
  if (!this._metadataStorage) {
69
99
  throw new Error("No metadata storage set");
@@ -72,6 +102,12 @@ export class CoreSDK {
72
102
  return this._metadataStorage.storeMetadata(metadata);
73
103
  }
74
104
 
105
+ /**
106
+ * Returns supported offer metadata from passed in `MetadataStorage` instance.
107
+ * @param metadataHashOrUri - Metadata hash or uri that can be handled by the
108
+ * storage instance.
109
+ * @returns Metadata hash / identifier.
110
+ */
75
111
  public async getMetadata(metadataHashOrUri: string): Promise<AnyMetadata> {
76
112
  if (!this._metadataStorage) {
77
113
  throw new Error("No metadata storage set");
@@ -80,6 +116,11 @@ export class CoreSDK {
80
116
  return this._metadataStorage.getMetadata(metadataHashOrUri);
81
117
  }
82
118
 
119
+ /**
120
+ * Returns `BASE` type offer metadata entities from subgraph.
121
+ * @param queryVars - Optional query variables to skip, order or filter.
122
+ * @returns BaseMetadataEntities from subgraph.
123
+ */
83
124
  public async getBaseMetadataEntities(
84
125
  queryVars?: subgraph.GetBaseMetadataEntitiesQueryQueryVariables
85
126
  ): Promise<subgraph.BaseMetadataEntityFieldsFragment[]> {
@@ -89,6 +130,11 @@ export class CoreSDK {
89
130
  );
90
131
  }
91
132
 
133
+ /**
134
+ * Returns `PRODUCT_V1` type offer metadata entities from subgraph.
135
+ * @param queryVars - Optional query variables to skip, order or filter.
136
+ * @returns ProductV1MetadataEntities from subgraph.
137
+ */
92
138
  public async getProductV1MetadataEntities(
93
139
  queryVars?: subgraph.GetProductV1MetadataEntitiesQueryQueryVariables
94
140
  ): Promise<subgraph.ProductV1MetadataEntityFieldsFragment[]> {
@@ -98,6 +144,18 @@ export class CoreSDK {
98
144
  );
99
145
  }
100
146
 
147
+ /* -------------------------------------------------------------------------- */
148
+ /* Account related methods */
149
+ /* -------------------------------------------------------------------------- */
150
+
151
+ /* --------------------------------- Seller --------------------------------- */
152
+
153
+ /**
154
+ * Returns seller entity from subgraph.
155
+ * @param sellerId - ID of seller entity to query for.
156
+ * @param queryVars - Optional query variables to skip, order or filter.
157
+ * @returns Seller entity from subgraph.
158
+ */
101
159
  public async getSellerById(
102
160
  sellerId: BigNumberish,
103
161
  queryVars?: accounts.subgraph.SingleSellerQueryVariables
@@ -109,6 +167,12 @@ export class CoreSDK {
109
167
  );
110
168
  }
111
169
 
170
+ /**
171
+ * Returns seller entity from subgraph.
172
+ * @param operator - Operator address of seller entity to query for.
173
+ * @param queryVars - Optional query variables to skip, order or filter.
174
+ * @returns Seller entity from subgraph.
175
+ */
112
176
  public async getSellerByOperator(
113
177
  operator: string,
114
178
  queryVars?: subgraph.GetSellersQueryQueryVariables
@@ -120,6 +184,12 @@ export class CoreSDK {
120
184
  );
121
185
  }
122
186
 
187
+ /**
188
+ * Returns seller entity from subgraph.
189
+ * @param clerk - Clerk address of seller entity to query for.
190
+ * @param queryVars - Optional query variables to skip, order or filter.
191
+ * @returns Seller entity from subgraph.
192
+ */
123
193
  public async getSellerByClerk(
124
194
  clerk: string,
125
195
  queryVars?: subgraph.GetSellersQueryQueryVariables
@@ -131,6 +201,12 @@ export class CoreSDK {
131
201
  );
132
202
  }
133
203
 
204
+ /**
205
+ * Returns seller entity from subgraph. Matches `operator`, `clerk`, `admin` or `treasury`.
206
+ * @param address - Address of seller entity to query for.
207
+ * @param queryVars - Optional query variables to skip, order or filter.
208
+ * @returns Seller entity from subgraph.
209
+ */
134
210
  public async getSellerByAddress(
135
211
  address: string,
136
212
  queryVars?: subgraph.GetSellersQueryQueryVariables
@@ -142,12 +218,23 @@ export class CoreSDK {
142
218
  );
143
219
  }
144
220
 
221
+ /**
222
+ * Returns seller entities from subgraph.
223
+ * @param queryVars - Optional query variables to skip, order or filter.
224
+ * @returns Seller entities from subgraph.
225
+ */
145
226
  public async getSellers(
146
227
  queryVars?: subgraph.GetSellersQueryQueryVariables
147
228
  ): Promise<subgraph.SellerFieldsFragment[]> {
148
229
  return accounts.subgraph.getSellers(this._subgraphUrl, queryVars);
149
230
  }
150
231
 
232
+ /**
233
+ * Creates seller account by calling the `AccountHandlerFacet` contract.
234
+ * @param sellerToCreate - Addresses to set in the seller account.
235
+ * @param overrides - Optional overrides.
236
+ * @returns Transaction response.
237
+ */
151
238
  public async createSeller(
152
239
  sellerToCreate: accounts.CreateSellerArgs,
153
240
  overrides: Partial<{
@@ -161,6 +248,14 @@ export class CoreSDK {
161
248
  });
162
249
  }
163
250
 
251
+ /**
252
+ * Creates seller account and offer by calling the `OrchestrationHandlerFacet` contract.
253
+ * This transaction only succeeds if there is no existing seller account for the connected signer.
254
+ * @param sellerToCreate - Addresses to set in the seller account.
255
+ * @param offerToCreate - Offer arguments.
256
+ * @param overrides - Optional overrides.
257
+ * @returns Transaction response.
258
+ */
164
259
  public async createSellerAndOffer(
165
260
  sellerToCreate: accounts.CreateSellerArgs,
166
261
  offerToCreate: offers.CreateOfferArgs,
@@ -178,6 +273,47 @@ export class CoreSDK {
178
273
  });
179
274
  }
180
275
 
276
+ /* ---------------------------------- Buyer --------------------------------- */
277
+
278
+ /**
279
+ * Returns buyer entity from subgraph.
280
+ * @param buyerId - ID of buyer entity to query for.
281
+ * @param queryVars - Optional query variables to skip, order or filter.
282
+ * @returns Buyer entity from subgraph.
283
+ */
284
+ public async getBuyerById(
285
+ buyerId: BigNumberish,
286
+ queryVars?: accounts.subgraph.SingleBuyerQueryVariables
287
+ ): Promise<subgraph.BuyerFieldsFragment> {
288
+ return accounts.subgraph.getBuyerById(
289
+ this._subgraphUrl,
290
+ buyerId,
291
+ queryVars
292
+ );
293
+ }
294
+
295
+ /**
296
+ * Returns buyer entities from subgraph.
297
+ * @param queryVars - Optional query variables to skip, order or filter.
298
+ * @returns Seller entities from subgraph.
299
+ */
300
+ public async getBuyers(
301
+ queryVars?: subgraph.GetBuyersQueryQueryVariables
302
+ ): Promise<subgraph.BuyerFieldsFragment[]> {
303
+ return accounts.subgraph.getBuyers(this._subgraphUrl, queryVars);
304
+ }
305
+
306
+ /* -------------------------------------------------------------------------- */
307
+ /* Offer related methods */
308
+ /* -------------------------------------------------------------------------- */
309
+
310
+ /**
311
+ * Creates offer by calling the `OfferHandlerFacet` contract.
312
+ * This transaction only succeeds if there is an existing seller account for connected signer.
313
+ * @param offerToCreate - Offer arguments.
314
+ * @param overrides - Optional overrides.
315
+ * @returns Transaction response.
316
+ */
181
317
  public async createOffer(
182
318
  offerToCreate: offers.CreateOfferArgs,
183
319
  overrides: Partial<{
@@ -193,6 +329,12 @@ export class CoreSDK {
193
329
  });
194
330
  }
195
331
 
332
+ /**
333
+ * Utility method to retrieve the created `offerId` from logs after calling `createOffer`
334
+ * or `createOfferAndSeller`.
335
+ * @param logs - Logs to search in.
336
+ * @returns Created offer id.
337
+ */
196
338
  public getCreatedOfferIdFromLogs(logs: Log[]): string | null {
197
339
  const offerId = getValueFromLogs({
198
340
  iface: offers.iface.bosonOfferHandlerIface,
@@ -212,6 +354,13 @@ export class CoreSDK {
212
354
  );
213
355
  }
214
356
 
357
+ /**
358
+ * Voids an existing offer by calling the `OfferHandlerFacet` contract.
359
+ * This transaction only succeeds if the connected signer is the `operator`.
360
+ * @param offerId - ID of offer to void.
361
+ * @param overrides - Optional overrides.
362
+ * @returns Transaction response.
363
+ */
215
364
  public async voidOffer(
216
365
  offerId: BigNumberish,
217
366
  overrides: Partial<{
@@ -226,6 +375,12 @@ export class CoreSDK {
226
375
  });
227
376
  }
228
377
 
378
+ /**
379
+ * Returns offer from subgraph.
380
+ * @param offerId - ID of offer.
381
+ * @param queryVars - Optional query variables to skip, order or filter.
382
+ * @returns Offer entity from subgraph.
383
+ */
229
384
  public async getOfferById(
230
385
  offerId: BigNumberish,
231
386
  queryVars?: offers.subgraph.SingleOfferQueryVariables
@@ -233,36 +388,27 @@ export class CoreSDK {
233
388
  return offers.subgraph.getOfferById(this._subgraphUrl, offerId, queryVars);
234
389
  }
235
390
 
391
+ /**
392
+ * Returns offers from subgraph.
393
+ * @param queryVars - Optional query variables to skip, order or filter.
394
+ * @returns Offer entities from subgraph.
395
+ */
236
396
  public async getOffers(
237
397
  queryVars?: subgraph.GetOffersQueryQueryVariables
238
398
  ): Promise<subgraph.OfferFieldsFragment[]> {
239
399
  return offers.subgraph.getOffers(this._subgraphUrl, queryVars);
240
400
  }
241
401
 
242
- public async commitToOffer(
243
- offerId: BigNumberish,
244
- overrides: Partial<{
245
- buyer: string;
246
- }> = {}
247
- ): Promise<TransactionResponse> {
248
- return exchanges.handler.commitToOffer({
249
- buyer: overrides.buyer || (await this._web3Lib.getSignerAddress()),
250
- offerId,
251
- web3Lib: this._web3Lib,
252
- subgraphUrl: this._subgraphUrl,
253
- contractAddress: this._protocolDiamond
254
- });
255
- }
256
-
257
- public getCommittedExchangeIdFromLogs(logs: Log[]): string | null {
258
- return getValueFromLogs({
259
- iface: exchanges.iface.bosonExchangeHandlerIface,
260
- logs,
261
- eventArgsKey: "exchangeId",
262
- eventName: "BuyerCommitted"
263
- });
264
- }
402
+ /* -------------------------------------------------------------------------- */
403
+ /* ERC20 / Exchange Token related methods */
404
+ /* -------------------------------------------------------------------------- */
265
405
 
406
+ /**
407
+ * Returns the current allowance of the given token by calling the contract.
408
+ * @param exchangeToken - Address of exchange token.
409
+ * @param overrides - Optional overrides.
410
+ * @returns Allowance for given signer.
411
+ */
266
412
  public async getExchangeTokenAllowance(
267
413
  exchangeToken: string,
268
414
  overrides: Partial<{
@@ -278,6 +424,11 @@ export class CoreSDK {
278
424
  });
279
425
  }
280
426
 
427
+ /**
428
+ * Returns `name`, `decimals` and `symbol` of the given token by calling the contract.
429
+ * @param exchangeToken - Address exchange token.
430
+ * @returns Decimals, name and symbol.
431
+ */
281
432
  public async getExchangeTokenInfo(exchangeToken: string): Promise<{
282
433
  name: string;
283
434
  decimals: number;
@@ -296,6 +447,13 @@ export class CoreSDK {
296
447
  return { decimals, name, symbol };
297
448
  }
298
449
 
450
+ /**
451
+ * Approves the given amount for the main protocol contract.
452
+ * @param exchangeToken - Address of token to approve.
453
+ * @param value - Amount of allowance.
454
+ * @param overrides - Optional overrides.
455
+ * @returns Transaction response.
456
+ */
299
457
  public async approveExchangeToken(
300
458
  exchangeToken: string,
301
459
  value: BigNumberish,
@@ -311,6 +469,17 @@ export class CoreSDK {
311
469
  });
312
470
  }
313
471
 
472
+ /* -------------------------------------------------------------------------- */
473
+ /* Funds related methods */
474
+ /* -------------------------------------------------------------------------- */
475
+
476
+ /**
477
+ * Deposit funds by calling the `FundsHandlerFacet` contract.
478
+ * @param sellerId - ID of seller account to deposit funds for.
479
+ * @param fundsAmount - Amount of funds.
480
+ * @param fundsTokenAddress - Address of funds token.
481
+ * @returns Transaction response.
482
+ */
314
483
  public async depositFunds(
315
484
  sellerId: BigNumberish,
316
485
  fundsAmount: BigNumberish,
@@ -325,6 +494,12 @@ export class CoreSDK {
325
494
  });
326
495
  }
327
496
 
497
+ /**
498
+ * Returns funds entity from subgraph.
499
+ * @param fundsId - ID of funds entity.
500
+ * @param queryVars - Optional query variables to skip, order or filter.
501
+ * @returns Funds entity from subgraph.
502
+ */
328
503
  public async getFundsById(
329
504
  fundsId: BigNumberish,
330
505
  queryVars?: subgraph.GetFundsByIdQueryVariables
@@ -332,12 +507,24 @@ export class CoreSDK {
332
507
  return funds.subgraph.getFundsById(this._subgraphUrl, fundsId, queryVars);
333
508
  }
334
509
 
510
+ /**
511
+ * Returns funds entities from subgraph.
512
+ * @param queryVars - Optional query variables to skip, order or filter.
513
+ * @returns Funds entities from subgraph.
514
+ */
335
515
  public async getFunds(
336
516
  queryVars?: subgraph.GetFundsQueryVariables
337
517
  ): Promise<subgraph.FundsEntityFieldsFragment[]> {
338
518
  return funds.subgraph.getFunds(this._subgraphUrl, queryVars);
339
519
  }
340
520
 
521
+ /**
522
+ * Withdraw selected funds by calling the `FundsHandlerFacet` contract.
523
+ * @param sellerId - ID of seller account to withdraw funds for.
524
+ * @param tokensToWithdraw - Addresses of funds tokens to withdraw.
525
+ * @param amountsToWithdraw - Amounts of funds token to withdraw.
526
+ * @returns Transaction response.
527
+ */
341
528
  public async withdrawFunds(
342
529
  sellerId: BigNumberish,
343
530
  tokensToWithdraw: Array<string>,
@@ -352,6 +539,11 @@ export class CoreSDK {
352
539
  });
353
540
  }
354
541
 
542
+ /**
543
+ * Withdraw all available funds by calling the `FundsHandlerFacet` contract.
544
+ * @param sellerId - ID of seller account to withdraw funds for.
545
+ * @returns Transaction response.
546
+ */
355
547
  public async withdrawAllAvailableFunds(
356
548
  sellerId: BigNumberish
357
549
  ): Promise<TransactionResponse> {
@@ -363,6 +555,16 @@ export class CoreSDK {
363
555
  });
364
556
  }
365
557
 
558
+ /* -------------------------------------------------------------------------- */
559
+ /* Exchange related methods */
560
+ /* -------------------------------------------------------------------------- */
561
+
562
+ /**
563
+ * Returns exchange entity from subgraph.
564
+ * @param exchangeId - ID of exchange entity.
565
+ * @param queryVars - Optional query variables to skip, order or filter.
566
+ * @returns Exchange entity from subgraph.
567
+ */
366
568
  public async getExchangeById(
367
569
  exchangeId: BigNumberish,
368
570
  queryVars?: subgraph.GetExchangeByIdQueryQueryVariables
@@ -374,12 +576,59 @@ export class CoreSDK {
374
576
  );
375
577
  }
376
578
 
579
+ /**
580
+ * Returns exchange entities from subgraph.
581
+ * @param queryVars - Optional query variables to skip, order or filter.
582
+ * @returns Exchange entities from subgraph.
583
+ */
377
584
  public async getExchanges(
378
585
  queryVars?: subgraph.GetExchangesQueryQueryVariables
379
586
  ): Promise<subgraph.ExchangeFieldsFragment[]> {
380
587
  return exchanges.subgraph.getExchanges(this._subgraphUrl, queryVars);
381
588
  }
382
589
 
590
+ /**
591
+ * Commits to an offer by calling the `ExchangeHandlerContract`.
592
+ * This transaction only succeeds if the seller has deposited funds.
593
+ * @param offerId - ID of offer to commit to.
594
+ * @param overrides - Optional overrides.
595
+ * @returns Transaction response.
596
+ */
597
+ public async commitToOffer(
598
+ offerId: BigNumberish,
599
+ overrides: Partial<{
600
+ buyer: string;
601
+ }> = {}
602
+ ): Promise<TransactionResponse> {
603
+ return exchanges.handler.commitToOffer({
604
+ buyer: overrides.buyer || (await this._web3Lib.getSignerAddress()),
605
+ offerId,
606
+ web3Lib: this._web3Lib,
607
+ subgraphUrl: this._subgraphUrl,
608
+ contractAddress: this._protocolDiamond
609
+ });
610
+ }
611
+
612
+ /**
613
+ * Utility method to retrieve the created `exchangeId` from logs after calling `commitToOffer`.
614
+ * @param logs - Logs to search in.
615
+ * @returns Created exchange id.
616
+ */
617
+ public getCommittedExchangeIdFromLogs(logs: Log[]): string | null {
618
+ return getValueFromLogs({
619
+ iface: exchanges.iface.bosonExchangeHandlerIface,
620
+ logs,
621
+ eventArgsKey: "exchangeId",
622
+ eventName: "BuyerCommitted"
623
+ });
624
+ }
625
+
626
+ /**
627
+ * Revokes an existing voucher by calling the `ExchangeHandlerContract`.
628
+ * Callable by seller `operator`.
629
+ * @param exchangeId - ID of exchange to revoke.
630
+ * @returns Transaction response.
631
+ */
383
632
  public async revokeVoucher(
384
633
  exchangeId: BigNumberish
385
634
  ): Promise<TransactionResponse> {
@@ -391,6 +640,12 @@ export class CoreSDK {
391
640
  });
392
641
  }
393
642
 
643
+ /**
644
+ * Cancels an existing voucher by calling the `ExchangeHandlerContract`.
645
+ * Callable by buyer.
646
+ * @param exchangeId - ID of exchange to cancel.
647
+ * @returns Transaction response.
648
+ */
394
649
  public async cancelVoucher(
395
650
  exchangeId: BigNumberish
396
651
  ): Promise<TransactionResponse> {
@@ -402,6 +657,12 @@ export class CoreSDK {
402
657
  });
403
658
  }
404
659
 
660
+ /**
661
+ * Redeems an existing voucher by calling the `ExchangeHandlerContract`.
662
+ * Callable by buyer.
663
+ * @param exchangeId - ID of exchange to redeem.
664
+ * @returns Transaction response.
665
+ */
405
666
  public async redeemVoucher(
406
667
  exchangeId: BigNumberish
407
668
  ): Promise<TransactionResponse> {
@@ -413,6 +674,12 @@ export class CoreSDK {
413
674
  });
414
675
  }
415
676
 
677
+ /**
678
+ * Completes an existing voucher by calling the `ExchangeHandlerContract`.
679
+ * Callable by buyer or seller operator.
680
+ * @param exchangeId - ID of exchange to complete.
681
+ * @returns Transaction response.
682
+ */
416
683
  public async completeExchange(
417
684
  exchangeId: BigNumberish
418
685
  ): Promise<TransactionResponse> {
@@ -424,6 +691,11 @@ export class CoreSDK {
424
691
  });
425
692
  }
426
693
 
694
+ /**
695
+ * Expires an existing voucher by calling the `ExchangeHandlerContract`.
696
+ * @param exchangeId - ID of exchange to expire.
697
+ * @returns Transaction response.
698
+ */
427
699
  public async expireVoucher(
428
700
  exchangeId: BigNumberish
429
701
  ): Promise<TransactionResponse> {
@@ -435,6 +707,15 @@ export class CoreSDK {
435
707
  });
436
708
  }
437
709
 
710
+ /* -------------------------------------------------------------------------- */
711
+ /* Meta Tx related methods */
712
+ /* -------------------------------------------------------------------------- */
713
+
714
+ /**
715
+ * Encodes and signs a meta transaction that can be relayed.
716
+ * @param args - Meta transaction args.
717
+ * @returns Signature.
718
+ */
438
719
  public async signExecuteMetaTx(
439
720
  args: Omit<
440
721
  Parameters<typeof metaTx.handler.signExecuteMetaTx>[0],