@bosonprotocol/core-sdk 1.6.0-alpha.8 → 1.6.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.
Files changed (88) hide show
  1. package/dist/cjs/accounts/subgraph.d.ts +9 -5
  2. package/dist/cjs/accounts/subgraph.d.ts.map +1 -1
  3. package/dist/cjs/accounts/subgraph.js +26 -22
  4. package/dist/cjs/accounts/subgraph.js.map +1 -1
  5. package/dist/cjs/core-sdk.d.ts +14 -11
  6. package/dist/cjs/core-sdk.d.ts.map +1 -1
  7. package/dist/cjs/core-sdk.js +61 -19
  8. package/dist/cjs/core-sdk.js.map +1 -1
  9. package/dist/cjs/exchanges/handler.d.ts +18 -0
  10. package/dist/cjs/exchanges/handler.d.ts.map +1 -1
  11. package/dist/cjs/exchanges/handler.js +82 -6
  12. package/dist/cjs/exchanges/handler.js.map +1 -1
  13. package/dist/cjs/exchanges/interface.d.ts +5 -0
  14. package/dist/cjs/exchanges/interface.d.ts.map +1 -1
  15. package/dist/cjs/exchanges/interface.js +31 -1
  16. package/dist/cjs/exchanges/interface.js.map +1 -1
  17. package/dist/cjs/exchanges/subgraph.d.ts +4 -4
  18. package/dist/cjs/exchanges/subgraph.d.ts.map +1 -1
  19. package/dist/cjs/exchanges/subgraph.js +9 -11
  20. package/dist/cjs/exchanges/subgraph.js.map +1 -1
  21. package/dist/cjs/funds/subgraph.d.ts +4 -2
  22. package/dist/cjs/funds/subgraph.d.ts.map +1 -1
  23. package/dist/cjs/funds/subgraph.js +13 -7
  24. package/dist/cjs/funds/subgraph.js.map +1 -1
  25. package/dist/cjs/offers/interface.d.ts.map +1 -1
  26. package/dist/cjs/offers/interface.js +11 -4
  27. package/dist/cjs/offers/interface.js.map +1 -1
  28. package/dist/cjs/offers/subgraph.d.ts +4 -4
  29. package/dist/cjs/offers/subgraph.d.ts.map +1 -1
  30. package/dist/cjs/offers/subgraph.js +7 -12
  31. package/dist/cjs/offers/subgraph.js.map +1 -1
  32. package/dist/cjs/subgraph.d.ts +531 -121
  33. package/dist/cjs/subgraph.d.ts.map +1 -1
  34. package/dist/cjs/subgraph.js +229 -101
  35. package/dist/cjs/subgraph.js.map +1 -1
  36. package/dist/cjs/utils/graphql.d.ts +82 -29
  37. package/dist/cjs/utils/graphql.d.ts.map +1 -1
  38. package/dist/esm/accounts/subgraph.d.ts +9 -5
  39. package/dist/esm/accounts/subgraph.d.ts.map +1 -1
  40. package/dist/esm/accounts/subgraph.js +37 -18
  41. package/dist/esm/accounts/subgraph.js.map +1 -1
  42. package/dist/esm/core-sdk.d.ts +14 -11
  43. package/dist/esm/core-sdk.d.ts.map +1 -1
  44. package/dist/esm/core-sdk.js +49 -19
  45. package/dist/esm/core-sdk.js.map +1 -1
  46. package/dist/esm/exchanges/handler.d.ts +18 -0
  47. package/dist/esm/exchanges/handler.d.ts.map +1 -1
  48. package/dist/esm/exchanges/handler.js +73 -6
  49. package/dist/esm/exchanges/handler.js.map +1 -1
  50. package/dist/esm/exchanges/interface.d.ts +5 -0
  51. package/dist/esm/exchanges/interface.d.ts.map +1 -1
  52. package/dist/esm/exchanges/interface.js +25 -0
  53. package/dist/esm/exchanges/interface.js.map +1 -1
  54. package/dist/esm/exchanges/subgraph.d.ts +4 -4
  55. package/dist/esm/exchanges/subgraph.d.ts.map +1 -1
  56. package/dist/esm/exchanges/subgraph.js +8 -10
  57. package/dist/esm/exchanges/subgraph.js.map +1 -1
  58. package/dist/esm/funds/subgraph.d.ts +4 -2
  59. package/dist/esm/funds/subgraph.d.ts.map +1 -1
  60. package/dist/esm/funds/subgraph.js +11 -5
  61. package/dist/esm/funds/subgraph.js.map +1 -1
  62. package/dist/esm/offers/interface.d.ts.map +1 -1
  63. package/dist/esm/offers/interface.js +11 -4
  64. package/dist/esm/offers/interface.js.map +1 -1
  65. package/dist/esm/offers/subgraph.d.ts +4 -4
  66. package/dist/esm/offers/subgraph.d.ts.map +1 -1
  67. package/dist/esm/offers/subgraph.js +7 -10
  68. package/dist/esm/offers/subgraph.js.map +1 -1
  69. package/dist/esm/subgraph.d.ts +531 -121
  70. package/dist/esm/subgraph.d.ts.map +1 -1
  71. package/dist/esm/subgraph.js +234 -100
  72. package/dist/esm/subgraph.js.map +1 -1
  73. package/dist/esm/utils/graphql.d.ts +82 -29
  74. package/dist/esm/utils/graphql.d.ts.map +1 -1
  75. package/package.json +3 -3
  76. package/src/accounts/queries.graphql +89 -17
  77. package/src/accounts/subgraph.ts +58 -23
  78. package/src/core-sdk.ts +82 -35
  79. package/src/exchanges/handler.ts +123 -6
  80. package/src/exchanges/interface.ts +28 -0
  81. package/src/exchanges/queries.graphql +23 -23
  82. package/src/exchanges/subgraph.ts +21 -20
  83. package/src/funds/queries.graphql +20 -2
  84. package/src/funds/subgraph.ts +27 -8
  85. package/src/offers/interface.ts +16 -6
  86. package/src/offers/queries.graphql +43 -22
  87. package/src/offers/subgraph.ts +18 -20
  88. package/src/subgraph.ts +807 -262
@@ -1,35 +1,107 @@
1
- query getSellerByOperatorQuery($operator: Bytes!, $fundsTokenAddress: Bytes) {
2
- sellers(where: { operator: $operator }) {
1
+ query getSellerByIdQuery(
2
+ $sellerId: ID!
3
+ $fundsSkip: Int
4
+ $fundsFirst: Int
5
+ $fundsOrderBy: FundsEntity_orderBy
6
+ $fundsOrderDirection: OrderDirection
7
+ $fundsFilter: FundsEntity_filter
8
+ $offersSkip: Int
9
+ $offersFirst: Int
10
+ $offersOrderBy: Offer_orderBy
11
+ $offersOrderDirection: OrderDirection
12
+ $offersFilter: Offer_filter
13
+ $exchangesSkip: Int
14
+ $exchangesFirst: Int
15
+ $exchangesOrderBy: Exchange_orderBy
16
+ $exchangesOrderDirection: OrderDirection
17
+ $exchangesFilter: Exchange_filter
18
+ $includeExchanges: Boolean = false
19
+ $includeOffers: Boolean = false
20
+ $includeFunds: Boolean = false
21
+ ) {
22
+ seller(id: $sellerId) {
3
23
  ...SellerFields
4
24
  }
5
25
  }
6
26
 
7
- query getSellerByAdminQuery($admin: Bytes!, $fundsTokenAddress: Bytes) {
8
- sellers(where: { admin: $admin }) {
27
+ query getSellersQuery(
28
+ $sellersSkip: Int
29
+ $sellersFirst: Int
30
+ $sellersOrderBy: Seller_orderBy
31
+ $sellersOrderDirection: OrderDirection
32
+ $sellersFilter: Seller_filter
33
+ $fundsSkip: Int
34
+ $fundsFirst: Int
35
+ $fundsOrderBy: FundsEntity_orderBy
36
+ $fundsOrderDirection: OrderDirection
37
+ $fundsFilter: FundsEntity_filter
38
+ $offersSkip: Int
39
+ $offersFirst: Int
40
+ $offersOrderBy: Offer_orderBy
41
+ $offersOrderDirection: OrderDirection
42
+ $offersFilter: Offer_filter
43
+ $exchangesSkip: Int
44
+ $exchangesFirst: Int
45
+ $exchangesOrderBy: Exchange_orderBy
46
+ $exchangesOrderDirection: OrderDirection
47
+ $exchangesFilter: Exchange_filter
48
+ $includeExchanges: Boolean = false
49
+ $includeOffers: Boolean = false
50
+ $includeFunds: Boolean = false
51
+ ) {
52
+ sellers(
53
+ skip: $sellersSkip
54
+ first: $sellersFirst
55
+ orderBy: $sellersOrderBy
56
+ orderDirection: $sellersOrderDirection
57
+ where: $sellersFilter
58
+ ) {
9
59
  ...SellerFields
10
60
  }
11
61
  }
12
62
 
13
- query getSellerByClerkQuery($clerk: Bytes!, $fundsTokenAddress: Bytes) {
14
- sellers(where: { clerk: $clerk }) {
15
- ...SellerFields
63
+ fragment SellerFields on Seller {
64
+ ...BaseSellerFields
65
+ funds(
66
+ skip: $fundsSkip
67
+ first: $fundsFirst
68
+ orderBy: $fundsOrderBy
69
+ orderDirection: $fundsOrderDirection
70
+ where: $fundsFilter
71
+ ) @include(if: $includeFunds) {
72
+ ...FundsEntityFields
73
+ }
74
+ offers(
75
+ skip: $offersSkip
76
+ first: $offersFirst
77
+ orderBy: $offersOrderBy
78
+ orderDirection: $offersOrderDirection
79
+ where: $offersFilter
80
+ ) @include(if: $includeOffers) {
81
+ ...BaseOfferFields
82
+ }
83
+ exchanges(
84
+ skip: $exchangesSkip
85
+ first: $exchangesFirst
86
+ orderBy: $exchangesOrderBy
87
+ orderDirection: $exchangesOrderDirection
88
+ where: $exchangesFilter
89
+ ) @include(if: $includeExchanges) {
90
+ ...BaseExchangeFields
16
91
  }
17
92
  }
18
93
 
19
- fragment SellerFields on Seller {
94
+ fragment BaseSellerFields on Seller {
20
95
  id
21
96
  operator
22
97
  admin
23
98
  clerk
24
99
  treasury
25
100
  active
26
- funds(where: { tokenAddress: $fundsTokenAddress }) {
27
- availableAmount
28
- token {
29
- address
30
- decimals
31
- name
32
- symbol
33
- }
34
- }
101
+ }
102
+
103
+ fragment BaseBuyerFields on Buyer {
104
+ id
105
+ wallet
106
+ active
35
107
  }
@@ -1,57 +1,92 @@
1
1
  import { getSubgraphSdk } from "../utils/graphql";
2
- import { SellerFieldsFragment } from "../subgraph";
2
+ import {
3
+ SellerFieldsFragment,
4
+ GetSellersQueryQueryVariables,
5
+ GetSellerByIdQueryQueryVariables
6
+ } from "../subgraph";
7
+ import { BigNumberish } from "@ethersproject/bignumber";
3
8
 
4
- export async function getSellerByOperator(
9
+ export type SingleSellerQueryVariables = Omit<
10
+ GetSellerByIdQueryQueryVariables,
11
+ "sellerId"
12
+ >;
13
+
14
+ export async function getSellerById(
5
15
  subgraphUrl: string,
6
- operatorAddress: string,
7
- fundsTokenAddress?: string
16
+ sellerId: BigNumberish,
17
+ queryVars: SingleSellerQueryVariables = {}
8
18
  ): Promise<SellerFieldsFragment> {
9
19
  const sdk = getSubgraphSdk(subgraphUrl);
10
- const { sellers = [] } = await sdk.getSellerByOperatorQuery({
11
- operator: operatorAddress,
12
- fundsTokenAddress
20
+ const { seller } = await sdk.getSellerByIdQuery({
21
+ sellerId: sellerId.toString(),
22
+ ...queryVars
13
23
  });
24
+ return seller;
25
+ }
26
+
27
+ export async function getSellers(
28
+ subgraphUrl: string,
29
+ queryVars: GetSellersQueryQueryVariables = {}
30
+ ): Promise<SellerFieldsFragment[]> {
31
+ const sdk = getSubgraphSdk(subgraphUrl);
32
+ const { sellers = [] } = await sdk.getSellersQuery(queryVars);
33
+ return sellers;
34
+ }
14
35
 
36
+ export async function getSellerByOperator(
37
+ subgraphUrl: string,
38
+ operatorAddress: string,
39
+ queryVars: GetSellersQueryQueryVariables = {}
40
+ ): Promise<SellerFieldsFragment | undefined> {
41
+ const sellers = await getSellers(subgraphUrl, {
42
+ sellersFilter: {
43
+ ...queryVars.sellersFilter,
44
+ operator: operatorAddress.toLowerCase()
45
+ },
46
+ ...queryVars
47
+ });
15
48
  return sellers[0];
16
49
  }
17
50
 
18
51
  export async function getSellerByAdmin(
19
52
  subgraphUrl: string,
20
53
  adminAddress: string,
21
- fundsTokenAddress?: string
54
+ queryVars: GetSellersQueryQueryVariables = {}
22
55
  ): Promise<SellerFieldsFragment> {
23
- const sdk = getSubgraphSdk(subgraphUrl);
24
- const { sellers = [] } = await sdk.getSellerByAdminQuery({
25
- admin: adminAddress,
26
- fundsTokenAddress
56
+ const sellers = await getSellers(subgraphUrl, {
57
+ sellersFilter: {
58
+ ...queryVars.sellersFilter,
59
+ admin: adminAddress.toLowerCase()
60
+ },
61
+ ...queryVars
27
62
  });
28
-
29
63
  return sellers[0];
30
64
  }
31
65
 
32
66
  export async function getSellerByClerk(
33
67
  subgraphUrl: string,
34
68
  clerkAddress: string,
35
- fundsTokenAddress?: string
69
+ queryVars: GetSellersQueryQueryVariables = {}
36
70
  ): Promise<SellerFieldsFragment> {
37
- const sdk = getSubgraphSdk(subgraphUrl);
38
- const { sellers = [] } = await sdk.getSellerByClerkQuery({
39
- clerk: clerkAddress,
40
- fundsTokenAddress
71
+ const sellers = await getSellers(subgraphUrl, {
72
+ sellersFilter: {
73
+ ...queryVars.sellersFilter,
74
+ clerk: clerkAddress.toLowerCase()
75
+ },
76
+ ...queryVars
41
77
  });
42
-
43
78
  return sellers[0];
44
79
  }
45
80
 
46
81
  export async function getSellerByAddress(
47
82
  subgraphUrl: string,
48
83
  address: string,
49
- fundsTokenAddress?: string
84
+ queryVars: GetSellersQueryQueryVariables = {}
50
85
  ): Promise<SellerFieldsFragment> {
51
86
  const [operator, admin, clerk] = await Promise.all([
52
- getSellerByOperator(subgraphUrl, address, fundsTokenAddress),
53
- getSellerByAdmin(subgraphUrl, address, fundsTokenAddress),
54
- getSellerByClerk(subgraphUrl, address, fundsTokenAddress)
87
+ getSellerByOperator(subgraphUrl, address, queryVars),
88
+ getSellerByAdmin(subgraphUrl, address, queryVars),
89
+ getSellerByClerk(subgraphUrl, address, queryVars)
55
90
  ]);
56
91
 
57
92
  return operator || admin || clerk;
package/src/core-sdk.ts CHANGED
@@ -78,39 +78,56 @@ export class CoreSDK {
78
78
  return this._metadataStorage.getMetadata(metadataHashOrUri);
79
79
  }
80
80
 
81
+ public async getSellerById(
82
+ sellerId: BigNumberish,
83
+ queryVars?: accounts.subgraph.SingleSellerQueryVariables
84
+ ): Promise<subgraph.SellerFieldsFragment> {
85
+ return accounts.subgraph.getSellerById(
86
+ this._subgraphUrl,
87
+ sellerId,
88
+ queryVars
89
+ );
90
+ }
91
+
81
92
  public async getSellerByOperator(
82
93
  operator: string,
83
- fundsTokenAddress?: string
94
+ queryVars?: subgraph.GetSellersQueryQueryVariables
84
95
  ): Promise<subgraph.SellerFieldsFragment> {
85
96
  return accounts.subgraph.getSellerByOperator(
86
97
  this._subgraphUrl,
87
98
  operator,
88
- fundsTokenAddress
99
+ queryVars
89
100
  );
90
101
  }
91
102
 
92
103
  public async getSellerByClerk(
93
104
  clerk: string,
94
- fundsTokenAddress?: string
105
+ queryVars?: subgraph.GetSellersQueryQueryVariables
95
106
  ): Promise<subgraph.SellerFieldsFragment> {
96
107
  return accounts.subgraph.getSellerByClerk(
97
108
  this._subgraphUrl,
98
109
  clerk,
99
- fundsTokenAddress
110
+ queryVars
100
111
  );
101
112
  }
102
113
 
103
114
  public async getSellerByAddress(
104
115
  address: string,
105
- fundsTokenAddress?: string
116
+ queryVars?: subgraph.GetSellersQueryQueryVariables
106
117
  ): Promise<subgraph.SellerFieldsFragment> {
107
118
  return accounts.subgraph.getSellerByAddress(
108
119
  this._subgraphUrl,
109
120
  address,
110
- fundsTokenAddress
121
+ queryVars
111
122
  );
112
123
  }
113
124
 
125
+ public async getSellers(
126
+ queryVars?: subgraph.GetSellersQueryQueryVariables
127
+ ): Promise<subgraph.SellerFieldsFragment[]> {
128
+ return accounts.subgraph.getSellers(this._subgraphUrl, queryVars);
129
+ }
130
+
114
131
  public async createSellerAndOffer(
115
132
  sellerToCreate: accounts.CreateSellerArgs,
116
133
  offerToCreate: offers.CreateOfferArgs,
@@ -177,26 +194,16 @@ export class CoreSDK {
177
194
  }
178
195
 
179
196
  public async getOfferById(
180
- offerId: BigNumberish
197
+ offerId: BigNumberish,
198
+ queryVars?: offers.subgraph.SingleOfferQueryVariables
181
199
  ): Promise<subgraph.OfferFieldsFragment> {
182
- return offers.subgraph.getOfferById(this._subgraphUrl, offerId);
200
+ return offers.subgraph.getOfferById(this._subgraphUrl, offerId, queryVars);
183
201
  }
184
202
 
185
- public async getAllOffersOfSeller(
186
- sellerFilter: {
187
- operatorAddress: string;
188
- // TODO: add support for sellerId, adminAddress, clerkAddress, treasuryAddress
189
- },
190
- opts?: offers.subgraph.AllOffersQueryOpts
203
+ public async getOffers(
204
+ queryVars?: subgraph.GetOffersQueryQueryVariables
191
205
  ): Promise<subgraph.OfferFieldsFragment[]> {
192
- if (sellerFilter.operatorAddress) {
193
- return offers.subgraph.getAllOffersOfOperator(
194
- this._subgraphUrl,
195
- sellerFilter.operatorAddress,
196
- opts
197
- );
198
- }
199
- return [];
206
+ return offers.subgraph.getOffers(this._subgraphUrl, queryVars);
200
207
  }
201
208
 
202
209
  public async commitToOffer(
@@ -285,26 +292,66 @@ export class CoreSDK {
285
292
  });
286
293
  }
287
294
 
288
- public async getFundsByAccountId(
289
- accountId: BigNumberish
295
+ public async getFundsById(
296
+ fundsId: BigNumberish,
297
+ queryVars?: subgraph.GetFundsByIdQueryVariables
298
+ ): Promise<subgraph.FundsEntityFieldsFragment> {
299
+ return funds.subgraph.getFundsById(this._subgraphUrl, fundsId, queryVars);
300
+ }
301
+
302
+ public async getFunds(
303
+ queryVars?: subgraph.GetFundsQueryVariables
290
304
  ): Promise<subgraph.FundsEntityFieldsFragment[]> {
291
- return funds.subgraph.getFundsByAccountId(this._subgraphUrl, accountId);
305
+ return funds.subgraph.getFunds(this._subgraphUrl, queryVars);
292
306
  }
293
307
 
294
308
  public async getExchangeById(
295
- exchangeId: BigNumberish
309
+ exchangeId: BigNumberish,
310
+ queryVars?: subgraph.GetExchangeByIdQueryQueryVariables
296
311
  ): Promise<subgraph.ExchangeFieldsFragment> {
297
- return exchanges.subgraph.getExchangeById(this._subgraphUrl, exchangeId);
312
+ return exchanges.subgraph.getExchangeById(
313
+ this._subgraphUrl,
314
+ exchangeId,
315
+ queryVars
316
+ );
298
317
  }
299
318
 
300
- public async getExchangesByOfferId(
301
- offerId: BigNumberish,
302
- opts?: exchanges.subgraph.ExchangesQueryOpts
319
+ public async getExchanges(
320
+ queryVars?: subgraph.GetExchangesQueryQueryVariables
303
321
  ): Promise<subgraph.ExchangeFieldsFragment[]> {
304
- return exchanges.subgraph.getExchangesByOfferId(
305
- this._subgraphUrl,
306
- offerId,
307
- opts
308
- );
322
+ return exchanges.subgraph.getExchanges(this._subgraphUrl, queryVars);
323
+ }
324
+
325
+ public async revokeVoucher(
326
+ exchangeId: BigNumberish
327
+ ): Promise<TransactionResponse> {
328
+ return exchanges.handler.revokeVoucher({
329
+ web3Lib: this._web3Lib,
330
+ contractAddress: this._protocolDiamond,
331
+ exchangeId,
332
+ subgraphUrl: this._subgraphUrl
333
+ });
334
+ }
335
+
336
+ public async cancelVoucher(
337
+ exchangeId: BigNumberish
338
+ ): Promise<TransactionResponse> {
339
+ return exchanges.handler.cancelVoucher({
340
+ web3Lib: this._web3Lib,
341
+ contractAddress: this._protocolDiamond,
342
+ exchangeId,
343
+ subgraphUrl: this._subgraphUrl
344
+ });
345
+ }
346
+
347
+ public async redeemVoucher(
348
+ exchangeId: BigNumberish
349
+ ): Promise<TransactionResponse> {
350
+ return exchanges.handler.redeemVoucher({
351
+ web3Lib: this._web3Lib,
352
+ contractAddress: this._protocolDiamond,
353
+ exchangeId,
354
+ subgraphUrl: this._subgraphUrl
355
+ });
309
356
  }
310
357
  }
@@ -1,7 +1,14 @@
1
1
  import { BigNumberish } from "@ethersproject/bignumber";
2
2
  import { Web3LibAdapter, TransactionResponse } from "@bosonprotocol/common";
3
- import { encodeCommitToOffer } from "./interface";
3
+ import {
4
+ encodeCancelVoucher,
5
+ encodeCommitToOffer,
6
+ encodeRevokeVoucher,
7
+ encodeRedeemVoucher
8
+ } from "./interface";
4
9
  import { getOfferById } from "../offers/subgraph";
10
+ import { getExchangeById } from "../exchanges/subgraph";
11
+ import { ExchangeFieldsFragment, ExchangeState } from "../subgraph";
5
12
 
6
13
  export async function commitToOffer(args: {
7
14
  buyer: string;
@@ -13,23 +20,23 @@ export async function commitToOffer(args: {
13
20
  const offer = await getOfferById(args.subgraphUrl, args.offerId);
14
21
 
15
22
  if (!offer) {
16
- throw new Error(`Offer with id "${args.offerId}" does not exist`);
23
+ throw new Error(`Offer with id ${args.offerId} does not exist`);
17
24
  }
18
25
 
19
26
  if (offer.voidedAt) {
20
- throw new Error(`Offer with id "${args.offerId}" has been voided`);
27
+ throw new Error(`Offer with id ${args.offerId} has been voided`);
21
28
  }
22
29
 
23
30
  if (Date.now() < Number(offer.validFromDate) * 1000) {
24
- throw new Error(`Offer with id "${args.offerId}" is not valid yet`);
31
+ throw new Error(`Offer with id ${args.offerId} is not valid yet`);
25
32
  }
26
33
 
27
34
  if (Date.now() >= Number(offer.validUntilDate) * 1000) {
28
- throw new Error(`Offer with id "${args.offerId}" is not valid anymore`);
35
+ throw new Error(`Offer with id ${args.offerId} is not valid anymore`);
29
36
  }
30
37
 
31
38
  if (Number(offer.quantityAvailable) === 0) {
32
- throw new Error(`Offer with id "${args.offerId}" is sold out`);
39
+ throw new Error(`Offer with id ${args.offerId} is sold out`);
33
40
  }
34
41
 
35
42
  return args.web3Lib.sendTransaction({
@@ -38,3 +45,113 @@ export async function commitToOffer(args: {
38
45
  value: offer.price
39
46
  });
40
47
  }
48
+
49
+ export async function revokeVoucher(args: {
50
+ exchangeId: BigNumberish;
51
+ contractAddress: string;
52
+ subgraphUrl: string;
53
+ web3Lib: Web3LibAdapter;
54
+ }): Promise<TransactionResponse> {
55
+ const [exchange, signerAddress] = await Promise.all([
56
+ getExchangeById(args.subgraphUrl, args.exchangeId),
57
+ args.web3Lib.getSignerAddress()
58
+ ]);
59
+
60
+ assertExchange(args.exchangeId, exchange);
61
+ assertExchangeState(exchange, ExchangeState.Committed);
62
+ assertSignerIsOperator(signerAddress, exchange);
63
+
64
+ return args.web3Lib.sendTransaction({
65
+ to: args.contractAddress,
66
+ data: encodeRevokeVoucher(args.exchangeId)
67
+ });
68
+ }
69
+
70
+ export async function cancelVoucher(args: {
71
+ exchangeId: BigNumberish;
72
+ contractAddress: string;
73
+ subgraphUrl: string;
74
+ web3Lib: Web3LibAdapter;
75
+ }): Promise<TransactionResponse> {
76
+ const [exchange, signerAddress] = await Promise.all([
77
+ getExchangeById(args.subgraphUrl, args.exchangeId),
78
+ args.web3Lib.getSignerAddress()
79
+ ]);
80
+
81
+ assertExchange(args.exchangeId, exchange);
82
+ assertExchangeState(exchange, ExchangeState.Committed);
83
+ assertSignerIsBuyer(signerAddress, exchange);
84
+
85
+ return args.web3Lib.sendTransaction({
86
+ to: args.contractAddress,
87
+ data: encodeCancelVoucher(args.exchangeId)
88
+ });
89
+ }
90
+
91
+ export async function redeemVoucher(args: {
92
+ exchangeId: BigNumberish;
93
+ contractAddress: string;
94
+ subgraphUrl: string;
95
+ web3Lib: Web3LibAdapter;
96
+ }): Promise<TransactionResponse> {
97
+ const [exchange, signerAddress] = await Promise.all([
98
+ getExchangeById(args.subgraphUrl, args.exchangeId),
99
+ args.web3Lib.getSignerAddress()
100
+ ]);
101
+
102
+ assertExchange(args.exchangeId, exchange);
103
+ assertExchangeState(exchange, ExchangeState.Committed);
104
+ assertSignerIsBuyer(signerAddress, exchange);
105
+
106
+ if (Date.now() < Number(exchange.offer.voucherRedeemableFromDate) * 1000) {
107
+ throw new Error(`Voucher not redeemable yet`);
108
+ }
109
+
110
+ if (Date.now() > Number(exchange.validUntilDate) * 1000) {
111
+ throw new Error(`Voucher can not be redeemed anymore`);
112
+ }
113
+
114
+ return args.web3Lib.sendTransaction({
115
+ to: args.contractAddress,
116
+ data: encodeRedeemVoucher(args.exchangeId)
117
+ });
118
+ }
119
+
120
+ function assertExchange(
121
+ exchangeId: BigNumberish,
122
+ exchange?: ExchangeFieldsFragment
123
+ ) {
124
+ if (!exchange) {
125
+ throw new Error(`Exchange with id ${exchangeId} does not exist`);
126
+ }
127
+ }
128
+
129
+ function assertExchangeState(
130
+ exchange: ExchangeFieldsFragment,
131
+ requiredState: ExchangeState
132
+ ) {
133
+ if (exchange.state !== requiredState) {
134
+ throw new Error(
135
+ `Exchange with id ${exchange.id} not in required state: ${requiredState}, actual state: ${exchange.state}`
136
+ );
137
+ }
138
+ }
139
+
140
+ function assertSignerIsOperator(
141
+ signer: string,
142
+ exchange: ExchangeFieldsFragment
143
+ ) {
144
+ if (exchange.seller.operator.toLowerCase() !== signer.toLowerCase()) {
145
+ throw new Error(
146
+ `Signer ${signer} is not the operator ${exchange.seller.operator}`
147
+ );
148
+ }
149
+ }
150
+
151
+ function assertSignerIsBuyer(signer: string, exchange: ExchangeFieldsFragment) {
152
+ if (exchange.buyer.wallet.toLowerCase() !== signer.toLowerCase()) {
153
+ throw new Error(
154
+ `Signer ${signer} is not the buyer ${exchange.buyer.wallet}`
155
+ );
156
+ }
157
+ }
@@ -12,3 +12,31 @@ export function encodeCommitToOffer(buyer: string, offerId: BigNumberish) {
12
12
  offerId
13
13
  ]);
14
14
  }
15
+
16
+ export function encodeCompleteExchange(exchangeId: BigNumberish) {
17
+ return bosonExchangeHandlerIface.encodeFunctionData("completeExchange", [
18
+ exchangeId
19
+ ]);
20
+ }
21
+
22
+ export function encodeRevokeVoucher(exchangeId: BigNumberish) {
23
+ return bosonExchangeHandlerIface.encodeFunctionData("revokeVoucher", [
24
+ exchangeId
25
+ ]);
26
+ }
27
+
28
+ export function encodeCancelVoucher(exchangeId: BigNumberish) {
29
+ return bosonExchangeHandlerIface.encodeFunctionData("cancelVoucher", [
30
+ exchangeId
31
+ ]);
32
+ }
33
+ export function encodeExpireVoucher(exchangeId: BigNumberish) {
34
+ return bosonExchangeHandlerIface.encodeFunctionData("expireVoucher", [
35
+ exchangeId
36
+ ]);
37
+ }
38
+ export function encodeRedeemVoucher(exchangeId: BigNumberish) {
39
+ return bosonExchangeHandlerIface.encodeFunctionData("redeemVoucher", [
40
+ exchangeId
41
+ ]);
42
+ }
@@ -4,46 +4,46 @@ query getExchangeByIdQuery($exchangeId: ID!) {
4
4
  }
5
5
  }
6
6
 
7
- query getExchangesByOfferId(
8
- $offerId: String!
9
- $first: Int
10
- $skip: Int
11
- $orderBy: Exchange_orderBy
12
- $orderDirection: OrderDirection
7
+ query getExchangesQuery(
8
+ $exchangesSkip: Int
9
+ $exchangesFirst: Int
10
+ $exchangesOrderBy: Exchange_orderBy
11
+ $exchangesOrderDirection: OrderDirection
12
+ $exchangesFilter: Exchange_filter
13
13
  ) {
14
14
  exchanges(
15
- where: { offer: $offerId }
16
- first: $first
17
- skip: $skip
18
- orderBy: $orderBy
19
- orderDirection: $orderDirection
15
+ skip: $exchangesSkip
16
+ first: $exchangesFirst
17
+ orderBy: $exchangesOrderBy
18
+ orderDirection: $exchangesOrderDirection
19
+ where: $exchangesFilter
20
20
  ) {
21
21
  ...ExchangeFields
22
22
  }
23
23
  }
24
24
 
25
25
  fragment ExchangeFields on Exchange {
26
- id
26
+ ...BaseExchangeFields
27
27
  buyer {
28
- id
29
- wallet
30
- active
28
+ ...BaseBuyerFields
31
29
  }
32
30
  seller {
33
- operator
34
- admin
35
- clerk
36
- treasury
37
- active
31
+ ...BaseSellerFields
32
+ }
33
+ offer {
34
+ ...BaseOfferFields
38
35
  }
36
+ }
37
+
38
+ fragment BaseExchangeFields on Exchange {
39
+ id
39
40
  disputed
40
41
  state
41
42
  committedDate
42
43
  finalizedDate
43
44
  validUntilDate
44
45
  redeemedDate
46
+ revokedDate
47
+ cancelledDate
45
48
  expired
46
- offer {
47
- ...OfferFields
48
- }
49
49
  }