@bosonprotocol/core-sdk 1.6.0-alpha.13 → 1.6.0-alpha.2

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 (175) hide show
  1. package/dist/cjs/accounts/subgraph.d.ts +9 -9
  2. package/dist/cjs/accounts/subgraph.d.ts.map +1 -1
  3. package/dist/cjs/accounts/subgraph.js +50 -26
  4. package/dist/cjs/accounts/subgraph.js.map +1 -1
  5. package/dist/cjs/accounts/types.d.ts +8 -0
  6. package/dist/cjs/accounts/types.d.ts.map +1 -1
  7. package/dist/cjs/core-sdk.d.ts +10 -15
  8. package/dist/cjs/core-sdk.d.ts.map +1 -1
  9. package/dist/cjs/core-sdk.js +15 -67
  10. package/dist/cjs/core-sdk.js.map +1 -1
  11. package/dist/cjs/exchanges/handler.d.ts +0 -18
  12. package/dist/cjs/exchanges/handler.d.ts.map +1 -1
  13. package/dist/cjs/exchanges/handler.js +6 -82
  14. package/dist/cjs/exchanges/handler.js.map +1 -1
  15. package/dist/cjs/exchanges/index.d.ts +0 -1
  16. package/dist/cjs/exchanges/index.d.ts.map +1 -1
  17. package/dist/cjs/exchanges/index.js +1 -2
  18. package/dist/cjs/exchanges/index.js.map +1 -1
  19. package/dist/cjs/exchanges/interface.d.ts +0 -5
  20. package/dist/cjs/exchanges/interface.d.ts.map +1 -1
  21. package/dist/cjs/exchanges/interface.js +1 -31
  22. package/dist/cjs/exchanges/interface.js.map +1 -1
  23. package/dist/cjs/funds/index.d.ts +1 -0
  24. package/dist/cjs/funds/index.d.ts.map +1 -1
  25. package/dist/cjs/funds/index.js +4 -0
  26. package/dist/cjs/funds/index.js.map +1 -1
  27. package/dist/cjs/funds/subgraph.d.ts +4 -4
  28. package/dist/cjs/funds/subgraph.d.ts.map +1 -1
  29. package/dist/cjs/funds/subgraph.js +28 -14
  30. package/dist/cjs/funds/subgraph.js.map +1 -1
  31. package/dist/cjs/funds/types.d.ts +12 -0
  32. package/dist/cjs/funds/types.d.ts.map +1 -0
  33. package/dist/cjs/funds/types.js +3 -0
  34. package/dist/cjs/funds/types.js.map +1 -0
  35. package/dist/cjs/index.d.ts +0 -1
  36. package/dist/cjs/index.d.ts.map +1 -1
  37. package/dist/cjs/index.js +1 -2
  38. package/dist/cjs/index.js.map +1 -1
  39. package/dist/cjs/offers/index.d.ts +0 -1
  40. package/dist/cjs/offers/index.d.ts.map +1 -1
  41. package/dist/cjs/offers/index.js +0 -1
  42. package/dist/cjs/offers/index.js.map +1 -1
  43. package/dist/cjs/offers/interface.d.ts.map +1 -1
  44. package/dist/cjs/offers/interface.js +4 -11
  45. package/dist/cjs/offers/interface.js.map +1 -1
  46. package/dist/cjs/offers/subgraph.d.ts +8 -4
  47. package/dist/cjs/offers/subgraph.d.ts.map +1 -1
  48. package/dist/cjs/offers/subgraph.js +100 -10
  49. package/dist/cjs/offers/subgraph.js.map +1 -1
  50. package/dist/cjs/offers/types.d.ts +50 -0
  51. package/dist/cjs/offers/types.d.ts.map +1 -1
  52. package/dist/cjs/utils/errors.d.ts +10 -0
  53. package/dist/cjs/utils/errors.d.ts.map +1 -0
  54. package/dist/cjs/utils/errors.js +12 -0
  55. package/dist/cjs/utils/errors.js.map +1 -0
  56. package/dist/cjs/utils/subgraph.d.ts +8 -0
  57. package/dist/cjs/utils/subgraph.d.ts.map +1 -0
  58. package/dist/cjs/utils/subgraph.js +46 -0
  59. package/dist/cjs/utils/subgraph.js.map +1 -0
  60. package/dist/esm/accounts/subgraph.d.ts +9 -9
  61. package/dist/esm/accounts/subgraph.d.ts.map +1 -1
  62. package/dist/esm/accounts/subgraph.js +51 -42
  63. package/dist/esm/accounts/subgraph.js.map +1 -1
  64. package/dist/esm/accounts/types.d.ts +8 -0
  65. package/dist/esm/accounts/types.d.ts.map +1 -1
  66. package/dist/esm/core-sdk.d.ts +10 -15
  67. package/dist/esm/core-sdk.d.ts.map +1 -1
  68. package/dist/esm/core-sdk.js +15 -51
  69. package/dist/esm/core-sdk.js.map +1 -1
  70. package/dist/esm/exchanges/handler.d.ts +0 -18
  71. package/dist/esm/exchanges/handler.d.ts.map +1 -1
  72. package/dist/esm/exchanges/handler.js +6 -73
  73. package/dist/esm/exchanges/handler.js.map +1 -1
  74. package/dist/esm/exchanges/index.d.ts +0 -1
  75. package/dist/esm/exchanges/index.d.ts.map +1 -1
  76. package/dist/esm/exchanges/index.js +0 -1
  77. package/dist/esm/exchanges/index.js.map +1 -1
  78. package/dist/esm/exchanges/interface.d.ts +0 -5
  79. package/dist/esm/exchanges/interface.d.ts.map +1 -1
  80. package/dist/esm/exchanges/interface.js +0 -25
  81. package/dist/esm/exchanges/interface.js.map +1 -1
  82. package/dist/esm/funds/index.d.ts +1 -0
  83. package/dist/esm/funds/index.d.ts.map +1 -1
  84. package/dist/esm/funds/index.js +1 -0
  85. package/dist/esm/funds/index.js.map +1 -1
  86. package/dist/esm/funds/subgraph.d.ts +4 -4
  87. package/dist/esm/funds/subgraph.d.ts.map +1 -1
  88. package/dist/esm/funds/subgraph.js +26 -12
  89. package/dist/esm/funds/subgraph.js.map +1 -1
  90. package/dist/esm/funds/types.d.ts +12 -0
  91. package/dist/esm/funds/types.d.ts.map +1 -0
  92. package/dist/esm/funds/types.js +2 -0
  93. package/dist/esm/funds/types.js.map +1 -0
  94. package/dist/esm/index.d.ts +0 -1
  95. package/dist/esm/index.d.ts.map +1 -1
  96. package/dist/esm/index.js +0 -1
  97. package/dist/esm/index.js.map +1 -1
  98. package/dist/esm/offers/index.d.ts +0 -1
  99. package/dist/esm/offers/index.d.ts.map +1 -1
  100. package/dist/esm/offers/index.js +0 -1
  101. package/dist/esm/offers/index.js.map +1 -1
  102. package/dist/esm/offers/interface.d.ts.map +1 -1
  103. package/dist/esm/offers/interface.js +4 -11
  104. package/dist/esm/offers/interface.js.map +1 -1
  105. package/dist/esm/offers/subgraph.d.ts +8 -4
  106. package/dist/esm/offers/subgraph.d.ts.map +1 -1
  107. package/dist/esm/offers/subgraph.js +104 -12
  108. package/dist/esm/offers/subgraph.js.map +1 -1
  109. package/dist/esm/offers/types.d.ts +50 -0
  110. package/dist/esm/offers/types.d.ts.map +1 -1
  111. package/dist/esm/utils/errors.d.ts +10 -0
  112. package/dist/esm/utils/errors.d.ts.map +1 -0
  113. package/dist/esm/utils/errors.js +10 -0
  114. package/dist/esm/utils/errors.js.map +1 -0
  115. package/dist/esm/utils/subgraph.d.ts +8 -0
  116. package/dist/esm/utils/subgraph.d.ts.map +1 -0
  117. package/dist/esm/utils/subgraph.js +28 -0
  118. package/dist/esm/utils/subgraph.js.map +1 -0
  119. package/package.json +4 -7
  120. package/src/accounts/subgraph.ts +65 -66
  121. package/src/accounts/types.ts +9 -0
  122. package/src/core-sdk.ts +32 -112
  123. package/src/exchanges/handler.ts +6 -123
  124. package/src/exchanges/index.ts +0 -1
  125. package/src/exchanges/interface.ts +0 -28
  126. package/src/funds/index.ts +1 -0
  127. package/src/funds/subgraph.ts +31 -27
  128. package/src/funds/types.ts +11 -0
  129. package/src/index.ts +0 -1
  130. package/src/offers/index.ts +0 -1
  131. package/src/offers/interface.ts +6 -16
  132. package/src/offers/subgraph.ts +120 -26
  133. package/src/offers/types.ts +51 -0
  134. package/src/utils/errors.ts +14 -0
  135. package/src/utils/subgraph.ts +43 -0
  136. package/dist/cjs/exchanges/subgraph.d.ts +0 -6
  137. package/dist/cjs/exchanges/subgraph.d.ts.map +0 -1
  138. package/dist/cjs/exchanges/subgraph.js +0 -30
  139. package/dist/cjs/exchanges/subgraph.js.map +0 -1
  140. package/dist/cjs/offers/getOfferStatus.d.ts +0 -9
  141. package/dist/cjs/offers/getOfferStatus.d.ts.map +0 -1
  142. package/dist/cjs/offers/getOfferStatus.js +0 -26
  143. package/dist/cjs/offers/getOfferStatus.js.map +0 -1
  144. package/dist/cjs/subgraph.d.ts +0 -3148
  145. package/dist/cjs/subgraph.d.ts.map +0 -1
  146. package/dist/cjs/subgraph.js +0 -539
  147. package/dist/cjs/subgraph.js.map +0 -1
  148. package/dist/cjs/utils/graphql.d.ts +0 -91
  149. package/dist/cjs/utils/graphql.d.ts.map +0 -1
  150. package/dist/cjs/utils/graphql.js +0 -11
  151. package/dist/cjs/utils/graphql.js.map +0 -1
  152. package/dist/esm/exchanges/subgraph.d.ts +0 -6
  153. package/dist/esm/exchanges/subgraph.d.ts.map +0 -1
  154. package/dist/esm/exchanges/subgraph.js +0 -15
  155. package/dist/esm/exchanges/subgraph.js.map +0 -1
  156. package/dist/esm/offers/getOfferStatus.d.ts +0 -9
  157. package/dist/esm/offers/getOfferStatus.d.ts.map +0 -1
  158. package/dist/esm/offers/getOfferStatus.js +0 -22
  159. package/dist/esm/offers/getOfferStatus.js.map +0 -1
  160. package/dist/esm/subgraph.d.ts +0 -3148
  161. package/dist/esm/subgraph.d.ts.map +0 -1
  162. package/dist/esm/subgraph.js +0 -541
  163. package/dist/esm/subgraph.js.map +0 -1
  164. package/dist/esm/utils/graphql.d.ts +0 -91
  165. package/dist/esm/utils/graphql.d.ts.map +0 -1
  166. package/dist/esm/utils/graphql.js +0 -7
  167. package/dist/esm/utils/graphql.js.map +0 -1
  168. package/src/accounts/queries.graphql +0 -107
  169. package/src/exchanges/queries.graphql +0 -49
  170. package/src/exchanges/subgraph.ts +0 -35
  171. package/src/funds/queries.graphql +0 -35
  172. package/src/offers/getOfferStatus.ts +0 -27
  173. package/src/offers/queries.graphql +0 -88
  174. package/src/subgraph.ts +0 -3731
  175. package/src/utils/graphql.ts +0 -7
@@ -1,92 +1,91 @@
1
- import { getSubgraphSdk } from "../utils/graphql";
2
- import {
3
- SellerFieldsFragment,
4
- GetSellersQueryQueryVariables,
5
- GetSellerByIdQueryQueryVariables
6
- } from "../subgraph";
7
- import { BigNumberish } from "@ethersproject/bignumber";
1
+ import { fetchSubgraph } from "../utils/subgraph";
2
+ import { RawSellerFromSubgraph } from "./types";
8
3
 
9
- export type SingleSellerQueryVariables = Omit<
10
- GetSellerByIdQueryQueryVariables,
11
- "sellerId"
12
- >;
13
-
14
- export async function getSellerById(
15
- subgraphUrl: string,
16
- sellerId: BigNumberish,
17
- queryVars: SingleSellerQueryVariables = {}
18
- ): Promise<SellerFieldsFragment> {
19
- const sdk = getSubgraphSdk(subgraphUrl);
20
- const { seller } = await sdk.getSellerByIdQuery({
21
- sellerId: sellerId.toString(),
22
- ...queryVars
23
- });
24
- return seller;
4
+ export const sellerFieldsFragment = `
5
+ fragment sellerFields on Seller {
6
+ id
7
+ operator
8
+ admin
9
+ clerk
10
+ treasury
11
+ active
25
12
  }
13
+ `;
26
14
 
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;
15
+ export const getSellerByOperatorQuery = `
16
+ query GetSellersByOperator($operator: String!) {
17
+ sellers(where: {
18
+ operator: $operator
19
+ }) {
20
+ ...sellerFields
21
+ }
34
22
  }
23
+ ${sellerFieldsFragment}
24
+ `;
35
25
 
36
26
  export async function getSellerByOperator(
37
27
  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
- });
28
+ operatorAddress: string
29
+ ): Promise<RawSellerFromSubgraph> {
30
+ const { sellers = [] } = await fetchSubgraph<{
31
+ sellers: RawSellerFromSubgraph[];
32
+ }>(subgraphUrl, getSellerByOperatorQuery, { operator: operatorAddress });
33
+
48
34
  return sellers[0];
49
35
  }
50
36
 
37
+ export const getSellerByAdminQuery = `
38
+ query GetSellersByAdmin($admin: String!) {
39
+ sellers(where: {
40
+ admin: $admin
41
+ }) {
42
+ ...sellerFields
43
+ }
44
+ }
45
+ ${sellerFieldsFragment}
46
+ `;
47
+
51
48
  export async function getSellerByAdmin(
52
49
  subgraphUrl: string,
53
- adminAddress: string,
54
- queryVars: GetSellersQueryQueryVariables = {}
55
- ): Promise<SellerFieldsFragment> {
56
- const sellers = await getSellers(subgraphUrl, {
57
- sellersFilter: {
58
- ...queryVars.sellersFilter,
59
- admin: adminAddress.toLowerCase()
60
- },
61
- ...queryVars
62
- });
50
+ adminAddress: string
51
+ ): Promise<RawSellerFromSubgraph> {
52
+ const { sellers = [] } = await fetchSubgraph<{
53
+ sellers: RawSellerFromSubgraph[];
54
+ }>(subgraphUrl, getSellerByAdminQuery, { admin: adminAddress });
55
+
63
56
  return sellers[0];
64
57
  }
65
58
 
59
+ export const getSellerByClerkQuery = `
60
+ query GetSellersByOperator($clerk: String!) {
61
+ sellers(where: {
62
+ clerk: $clerk
63
+ }) {
64
+ ...sellerFields
65
+ }
66
+ }
67
+ ${sellerFieldsFragment}
68
+ `;
69
+
66
70
  export async function getSellerByClerk(
67
71
  subgraphUrl: string,
68
- clerkAddress: string,
69
- queryVars: GetSellersQueryQueryVariables = {}
70
- ): Promise<SellerFieldsFragment> {
71
- const sellers = await getSellers(subgraphUrl, {
72
- sellersFilter: {
73
- ...queryVars.sellersFilter,
74
- clerk: clerkAddress.toLowerCase()
75
- },
76
- ...queryVars
77
- });
72
+ clerkAddress: string
73
+ ): Promise<RawSellerFromSubgraph> {
74
+ const { sellers = [] } = await fetchSubgraph<{
75
+ sellers: RawSellerFromSubgraph[];
76
+ }>(subgraphUrl, getSellerByClerkQuery, { clerk: clerkAddress });
77
+
78
78
  return sellers[0];
79
79
  }
80
80
 
81
81
  export async function getSellerByAddress(
82
82
  subgraphUrl: string,
83
- address: string,
84
- queryVars: GetSellersQueryQueryVariables = {}
85
- ): Promise<SellerFieldsFragment> {
83
+ address: string
84
+ ): Promise<RawSellerFromSubgraph> {
86
85
  const [operator, admin, clerk] = await Promise.all([
87
- getSellerByOperator(subgraphUrl, address, queryVars),
88
- getSellerByAdmin(subgraphUrl, address, queryVars),
89
- getSellerByClerk(subgraphUrl, address, queryVars)
86
+ getSellerByOperator(subgraphUrl, address),
87
+ getSellerByAdmin(subgraphUrl, address),
88
+ getSellerByClerk(subgraphUrl, address)
90
89
  ]);
91
90
 
92
91
  return operator || admin || clerk;
@@ -1 +1,10 @@
1
1
  export { CreateSellerArgs } from "@bosonprotocol/common";
2
+
3
+ export type RawSellerFromSubgraph = {
4
+ id: string;
5
+ operator: string;
6
+ admin: string;
7
+ clerk: string;
8
+ treasury: string;
9
+ active: boolean;
10
+ };
package/src/core-sdk.ts CHANGED
@@ -15,9 +15,8 @@ import * as offers from "./offers";
15
15
  import * as orchestration from "./orchestration";
16
16
  import * as erc20 from "./erc20";
17
17
  import * as funds from "./funds";
18
- import * as subgraph from "./subgraph";
19
-
20
18
  import { getValueFromLogs } from "./utils/logs";
19
+ import { MultiQueryOpts } from "./utils/subgraph";
21
20
 
22
21
  export class CoreSDK {
23
22
  private _web3Lib: Web3LibAdapter;
@@ -78,54 +77,22 @@ export class CoreSDK {
78
77
  return this._metadataStorage.getMetadata(metadataHashOrUri);
79
78
  }
80
79
 
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
-
92
80
  public async getSellerByOperator(
93
- operator: string,
94
- queryVars?: subgraph.GetSellersQueryQueryVariables
95
- ): Promise<subgraph.SellerFieldsFragment> {
96
- return accounts.subgraph.getSellerByOperator(
97
- this._subgraphUrl,
98
- operator,
99
- queryVars
100
- );
81
+ operator: string
82
+ ): Promise<accounts.RawSellerFromSubgraph> {
83
+ return accounts.subgraph.getSellerByOperator(this._subgraphUrl, operator);
101
84
  }
102
85
 
103
86
  public async getSellerByClerk(
104
- clerk: string,
105
- queryVars?: subgraph.GetSellersQueryQueryVariables
106
- ): Promise<subgraph.SellerFieldsFragment> {
107
- return accounts.subgraph.getSellerByClerk(
108
- this._subgraphUrl,
109
- clerk,
110
- queryVars
111
- );
87
+ clerk: string
88
+ ): Promise<accounts.RawSellerFromSubgraph> {
89
+ return accounts.subgraph.getSellerByClerk(this._subgraphUrl, clerk);
112
90
  }
113
91
 
114
92
  public async getSellerByAddress(
115
- address: string,
116
- queryVars?: subgraph.GetSellersQueryQueryVariables
117
- ): Promise<subgraph.SellerFieldsFragment> {
118
- return accounts.subgraph.getSellerByAddress(
119
- this._subgraphUrl,
120
- address,
121
- queryVars
122
- );
123
- }
124
-
125
- public async getSellers(
126
- queryVars?: subgraph.GetSellersQueryQueryVariables
127
- ): Promise<subgraph.SellerFieldsFragment[]> {
128
- return accounts.subgraph.getSellers(this._subgraphUrl, queryVars);
93
+ address: string
94
+ ): Promise<accounts.RawSellerFromSubgraph> {
95
+ return accounts.subgraph.getSellerByAddress(this._subgraphUrl, address);
129
96
  }
130
97
 
131
98
  public async createSellerAndOffer(
@@ -194,16 +161,26 @@ export class CoreSDK {
194
161
  }
195
162
 
196
163
  public async getOfferById(
197
- offerId: BigNumberish,
198
- queryVars?: offers.subgraph.SingleOfferQueryVariables
199
- ): Promise<subgraph.OfferFieldsFragment> {
200
- return offers.subgraph.getOfferById(this._subgraphUrl, offerId, queryVars);
164
+ offerId: BigNumberish
165
+ ): Promise<offers.RawOfferFromSubgraph> {
166
+ return offers.subgraph.getOfferById(this._subgraphUrl, offerId);
201
167
  }
202
168
 
203
- public async getOffers(
204
- queryVars?: subgraph.GetOffersQueryQueryVariables
205
- ): Promise<subgraph.OfferFieldsFragment[]> {
206
- return offers.subgraph.getOffers(this._subgraphUrl, queryVars);
169
+ public async getAllOffersOfSeller(
170
+ sellerFilter: {
171
+ operatorAddress: string;
172
+ // TODO: add support for sellerId, adminAddress, clerkAddress, treasuryAddress
173
+ },
174
+ opts: MultiQueryOpts = {}
175
+ ): Promise<offers.RawOfferFromSubgraph[]> {
176
+ if (sellerFilter.operatorAddress) {
177
+ return offers.subgraph.getAllOffersOfOperator(
178
+ this._subgraphUrl,
179
+ sellerFilter.operatorAddress,
180
+ opts
181
+ );
182
+ }
183
+ return [];
207
184
  }
208
185
 
209
186
  public async commitToOffer(
@@ -292,66 +269,9 @@ export class CoreSDK {
292
269
  });
293
270
  }
294
271
 
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
304
- ): Promise<subgraph.FundsEntityFieldsFragment[]> {
305
- return funds.subgraph.getFunds(this._subgraphUrl, queryVars);
306
- }
307
-
308
- public async getExchangeById(
309
- exchangeId: BigNumberish,
310
- queryVars?: subgraph.GetExchangeByIdQueryQueryVariables
311
- ): Promise<subgraph.ExchangeFieldsFragment> {
312
- return exchanges.subgraph.getExchangeById(
313
- this._subgraphUrl,
314
- exchangeId,
315
- queryVars
316
- );
317
- }
318
-
319
- public async getExchanges(
320
- queryVars?: subgraph.GetExchangesQueryQueryVariables
321
- ): Promise<subgraph.ExchangeFieldsFragment[]> {
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
- });
272
+ public async getFundsByAccountId(
273
+ accountId: BigNumberish
274
+ ): Promise<funds.RawFundsEntityFromSubgraph[]> {
275
+ return funds.subgraph.getFundsByAccountId(this._subgraphUrl, accountId);
356
276
  }
357
277
  }
@@ -1,14 +1,7 @@
1
1
  import { BigNumberish } from "@ethersproject/bignumber";
2
2
  import { Web3LibAdapter, TransactionResponse } from "@bosonprotocol/common";
3
- import {
4
- encodeCancelVoucher,
5
- encodeCommitToOffer,
6
- encodeRevokeVoucher,
7
- encodeRedeemVoucher
8
- } from "./interface";
3
+ import { encodeCommitToOffer } from "./interface";
9
4
  import { getOfferById } from "../offers/subgraph";
10
- import { getExchangeById } from "../exchanges/subgraph";
11
- import { ExchangeFieldsFragment, ExchangeState } from "../subgraph";
12
5
 
13
6
  export async function commitToOffer(args: {
14
7
  buyer: string;
@@ -20,23 +13,23 @@ export async function commitToOffer(args: {
20
13
  const offer = await getOfferById(args.subgraphUrl, args.offerId);
21
14
 
22
15
  if (!offer) {
23
- throw new Error(`Offer with id ${args.offerId} does not exist`);
16
+ throw new Error(`Offer with id "${args.offerId}" does not exist`);
24
17
  }
25
18
 
26
19
  if (offer.voidedAt) {
27
- throw new Error(`Offer with id ${args.offerId} has been voided`);
20
+ throw new Error(`Offer with id "${args.offerId}" has been voided`);
28
21
  }
29
22
 
30
23
  if (Date.now() < Number(offer.validFromDate) * 1000) {
31
- throw new Error(`Offer with id ${args.offerId} is not valid yet`);
24
+ throw new Error(`Offer with id "${args.offerId}" is not valid yet`);
32
25
  }
33
26
 
34
27
  if (Date.now() >= Number(offer.validUntilDate) * 1000) {
35
- throw new Error(`Offer with id ${args.offerId} is not valid anymore`);
28
+ throw new Error(`Offer with id "${args.offerId}" is not valid anymore`);
36
29
  }
37
30
 
38
31
  if (Number(offer.quantityAvailable) === 0) {
39
- throw new Error(`Offer with id ${args.offerId} is sold out`);
32
+ throw new Error(`Offer with id "${args.offerId}" is sold out`);
40
33
  }
41
34
 
42
35
  return args.web3Lib.sendTransaction({
@@ -45,113 +38,3 @@ export async function commitToOffer(args: {
45
38
  value: offer.price
46
39
  });
47
40
  }
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
- }
@@ -1,3 +1,2 @@
1
1
  export * as handler from "./handler";
2
2
  export * as iface from "./interface";
3
- export * as subgraph from "./subgraph";
@@ -12,31 +12,3 @@ 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
- }
@@ -1,3 +1,4 @@
1
1
  export * as iface from "./interface";
2
2
  export * as subgraph from "./subgraph";
3
3
  export * as handler from "./handler";
4
+ export * from "./types";
@@ -1,35 +1,39 @@
1
- import { getSubgraphSdk } from "../utils/graphql";
2
- import {
3
- FundsEntityFieldsFragment,
4
- GetFundsByIdQueryVariables,
5
- GetFundsQueryVariables
6
- } from "../subgraph";
7
1
  import { BigNumberish } from "@ethersproject/bignumber";
2
+ import { fetchSubgraph } from "../utils/subgraph";
3
+ import { RawFundsEntityFromSubgraph } from "./types";
8
4
 
9
- export type SingleFundsQueryVariables = Omit<
10
- GetFundsByIdQueryVariables,
11
- "fundsId"
12
- >;
5
+ export const fundsEntityFieldsFragment = `
6
+ fragment fundsEntityFields on FundsEntity {
7
+ id
8
+ availableAmount
9
+ token {
10
+ address
11
+ name
12
+ symbol
13
+ decimals
14
+ }
15
+ accountId
16
+ }
17
+ `;
13
18
 
14
- export async function getFunds(
15
- subgraphUrl: string,
16
- queryVars: GetFundsQueryVariables = {}
17
- ): Promise<FundsEntityFieldsFragment[]> {
18
- const sdk = getSubgraphSdk(subgraphUrl);
19
- const { fundsEntities = [] } = await sdk.getFunds(queryVars);
20
- return fundsEntities;
19
+ export const getFundsByAccountIdQuery = `
20
+ query GetFundsByAccountIdQuery($accountId: String!) {
21
+ fundsEntities(where: {
22
+ accountId: $accountId
23
+ }) {
24
+ ...fundsEntityFields
25
+ }
21
26
  }
27
+ ${fundsEntityFieldsFragment}
28
+ `;
22
29
 
23
- export async function getFundsById(
30
+ export async function getFundsByAccountId(
24
31
  subgraphUrl: string,
25
- fundsId: BigNumberish,
26
- queryVars: SingleFundsQueryVariables = {}
27
- ): Promise<FundsEntityFieldsFragment> {
28
- const sdk = getSubgraphSdk(subgraphUrl);
29
- const { fundsEntity } = await sdk.getFundsById({
30
- fundsId: fundsId.toString(),
31
- ...queryVars
32
- });
32
+ accountId: BigNumberish
33
+ ): Promise<RawFundsEntityFromSubgraph[]> {
34
+ const { fundsEntities = [] } = await fetchSubgraph<{
35
+ fundsEntities: RawFundsEntityFromSubgraph[];
36
+ }>(subgraphUrl, getFundsByAccountIdQuery, { accountId });
33
37
 
34
- return fundsEntity;
38
+ return fundsEntities;
35
39
  }
@@ -0,0 +1,11 @@
1
+ export type RawFundsEntityFromSubgraph = {
2
+ id: string;
3
+ availableAmount: string;
4
+ token: {
5
+ address: string;
6
+ name: string;
7
+ symbol: string;
8
+ decimals: number;
9
+ };
10
+ accountId: string;
11
+ };
package/src/index.ts CHANGED
@@ -6,7 +6,6 @@ export * as exchanges from "./exchanges";
6
6
  export * as orchestration from "./orchestration";
7
7
  export * as erc20 from "./erc20";
8
8
  export * as funds from "./funds";
9
- export * as subgraph from "./subgraph";
10
9
 
11
10
  export { defaultConfigs, getDefaultConfig } from "@bosonprotocol/common";
12
11
 
@@ -5,7 +5,6 @@ export * as subgraph from "./subgraph";
5
5
  export * as iface from "./interface";
6
6
  export * as storage from "./storage";
7
7
  export * from "./types";
8
- export * from "./getOfferStatus";
9
8
 
10
9
  export const validation = {
11
10
  createOfferArgsSchema: utils.validation.createOfferArgsSchema