@peeramid-labs/sdk 3.10.4 → 3.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.
Files changed (152) hide show
  1. package/cli/cli/commands/blockchain/mine.js +1 -0
  2. package/cli/cli/commands/blockchain/mine.js.map +1 -1
  3. package/cli/cli/commands/distributions/add.js +7 -6
  4. package/cli/cli/commands/distributions/add.js.map +1 -1
  5. package/cli/cli/commands/distributions/list.js +7 -0
  6. package/cli/cli/commands/distributions/list.js.map +1 -1
  7. package/cli/cli/commands/distributions/remove.js +5 -0
  8. package/cli/cli/commands/distributions/remove.js.map +1 -1
  9. package/cli/cli/commands/distributions/state.js +5 -0
  10. package/cli/cli/commands/distributions/state.js.map +1 -1
  11. package/cli/cli/commands/fellowship/create.js +7 -1
  12. package/cli/cli/commands/fellowship/create.js.map +1 -1
  13. package/cli/cli/commands/fellowship/eip712.js +5 -0
  14. package/cli/cli/commands/fellowship/eip712.js.map +1 -1
  15. package/cli/cli/commands/fellowship/game/cancel.js +6 -0
  16. package/cli/cli/commands/fellowship/game/cancel.js.map +1 -1
  17. package/cli/cli/commands/fellowship/game/create.js +8 -1
  18. package/cli/cli/commands/fellowship/game/create.js.map +1 -1
  19. package/cli/cli/commands/fellowship/game/end-turn.js +12 -5
  20. package/cli/cli/commands/fellowship/game/end-turn.js.map +1 -1
  21. package/cli/cli/commands/fellowship/game/join.js +9 -1
  22. package/cli/cli/commands/fellowship/game/join.js.map +1 -1
  23. package/cli/cli/commands/fellowship/game/list.js +6 -0
  24. package/cli/cli/commands/fellowship/game/list.js.map +1 -1
  25. package/cli/cli/commands/fellowship/game/propose.js +10 -1
  26. package/cli/cli/commands/fellowship/game/propose.js.map +1 -1
  27. package/cli/cli/commands/fellowship/game/start.js +10 -1
  28. package/cli/cli/commands/fellowship/game/start.js.map +1 -1
  29. package/cli/cli/commands/fellowship/game/vote.js +10 -1
  30. package/cli/cli/commands/fellowship/game/vote.js.map +1 -1
  31. package/cli/cli/commands/fellowship/list.js +5 -0
  32. package/cli/cli/commands/fellowship/list.js.map +1 -1
  33. package/cli/cli/commands/fellowship/params.js +5 -0
  34. package/cli/cli/commands/fellowship/params.js.map +1 -1
  35. package/cli/cli/commands/instances/list.js +5 -0
  36. package/cli/cli/commands/instances/list.js.map +1 -1
  37. package/cli/cli/commands/rankify/games.js +5 -0
  38. package/cli/cli/commands/rankify/games.js.map +1 -1
  39. package/cli/cli/utils.js +3 -3
  40. package/cli/cli/utils.js.map +1 -1
  41. package/cli/eds/Distributor.js +18 -29
  42. package/cli/eds/Distributor.js.map +1 -1
  43. package/cli/rankify/GameMaster.js +42 -39
  44. package/cli/rankify/GameMaster.js.map +1 -1
  45. package/cli/rankify/InstanceBase.js +99 -127
  46. package/cli/rankify/InstanceBase.js.map +1 -1
  47. package/cli/rankify/MAODistributor.js +45 -54
  48. package/cli/rankify/MAODistributor.js.map +1 -1
  49. package/cli/rankify/Player.js +4 -6
  50. package/cli/rankify/Player.js.map +1 -1
  51. package/cli/utils/EnvioGraphQLClient.js +728 -0
  52. package/cli/utils/EnvioGraphQLClient.js.map +1 -0
  53. package/cli/utils/index.js.map +1 -1
  54. package/cli/utils/logger.js +12 -0
  55. package/cli/utils/logger.js.map +1 -0
  56. package/docs/classes/EnvioGraphQLClient.md +309 -0
  57. package/docs/classes/GameMaster.md +8 -0
  58. package/docs/classes/InstanceBase.md +16 -8
  59. package/docs/classes/InstancePlayer.md +22 -10
  60. package/docs/classes/MAODistributorClient.md +36 -27
  61. package/docs/docs/classes/EnvioGraphQLClient.md +309 -0
  62. package/docs/docs/classes/GameMaster.md +8 -0
  63. package/docs/docs/classes/InstanceBase.md +16 -8
  64. package/docs/docs/classes/InstancePlayer.md +22 -10
  65. package/docs/docs/classes/MAODistributorClient.md +36 -27
  66. package/docs/docs/index.md +5 -2
  67. package/docs/docs/interfaces/EnvioGraphQLClientConfig.md +41 -0
  68. package/docs/docs/interfaces/MAOInstanceArgs.md +29 -0
  69. package/docs/index.md +5 -2
  70. package/docs/interfaces/EnvioGraphQLClientConfig.md +41 -0
  71. package/docs/interfaces/MAOInstanceArgs.md +29 -0
  72. package/lib.commonjs/eds/Distributor.d.ts +5 -3
  73. package/lib.commonjs/eds/Distributor.d.ts.map +1 -1
  74. package/lib.commonjs/eds/Distributor.js +19 -29
  75. package/lib.commonjs/eds/Distributor.js.map +1 -1
  76. package/lib.commonjs/index.d.ts +1 -0
  77. package/lib.commonjs/index.d.ts.map +1 -1
  78. package/lib.commonjs/index.js +4 -1
  79. package/lib.commonjs/index.js.map +1 -1
  80. package/lib.commonjs/rankify/GameMaster.d.ts +6 -2
  81. package/lib.commonjs/rankify/GameMaster.d.ts.map +1 -1
  82. package/lib.commonjs/rankify/GameMaster.js +43 -39
  83. package/lib.commonjs/rankify/GameMaster.js.map +1 -1
  84. package/lib.commonjs/rankify/InstanceBase.d.ts +46 -9442
  85. package/lib.commonjs/rankify/InstanceBase.d.ts.map +1 -1
  86. package/lib.commonjs/rankify/InstanceBase.js +100 -127
  87. package/lib.commonjs/rankify/InstanceBase.js.map +1 -1
  88. package/lib.commonjs/rankify/MAODistributor.d.ts +24 -16
  89. package/lib.commonjs/rankify/MAODistributor.d.ts.map +1 -1
  90. package/lib.commonjs/rankify/MAODistributor.js +45 -54
  91. package/lib.commonjs/rankify/MAODistributor.js.map +1 -1
  92. package/lib.commonjs/rankify/Player.d.ts +4 -2
  93. package/lib.commonjs/rankify/Player.d.ts.map +1 -1
  94. package/lib.commonjs/rankify/Player.js +4 -6
  95. package/lib.commonjs/rankify/Player.js.map +1 -1
  96. package/lib.commonjs/utils/EnvioGraphQLClient.d.ts +273 -0
  97. package/lib.commonjs/utils/EnvioGraphQLClient.d.ts.map +1 -0
  98. package/lib.commonjs/utils/EnvioGraphQLClient.js +730 -0
  99. package/lib.commonjs/utils/EnvioGraphQLClient.js.map +1 -0
  100. package/lib.commonjs/utils/index.d.ts +1 -1
  101. package/lib.commonjs/utils/index.d.ts.map +1 -1
  102. package/lib.commonjs/utils/index.js.map +1 -1
  103. package/lib.commonjs/utils/logger.d.ts +12 -0
  104. package/lib.commonjs/utils/logger.d.ts.map +1 -0
  105. package/lib.commonjs/utils/logger.js +12 -0
  106. package/lib.commonjs/utils/logger.js.map +1 -0
  107. package/lib.commonjs/utils/mockUtils.d.ts +39 -0
  108. package/lib.commonjs/utils/mockUtils.d.ts.map +1 -0
  109. package/lib.commonjs/utils/mockUtils.js +103 -0
  110. package/lib.commonjs/utils/mockUtils.js.map +1 -0
  111. package/lib.esm/eds/Distributor.d.ts +5 -3
  112. package/lib.esm/eds/Distributor.d.ts.map +1 -1
  113. package/lib.esm/eds/Distributor.js +20 -30
  114. package/lib.esm/eds/Distributor.js.map +1 -1
  115. package/lib.esm/index.d.ts +1 -0
  116. package/lib.esm/index.d.ts.map +1 -1
  117. package/lib.esm/index.js +2 -0
  118. package/lib.esm/index.js.map +1 -1
  119. package/lib.esm/rankify/GameMaster.d.ts +6 -2
  120. package/lib.esm/rankify/GameMaster.d.ts.map +1 -1
  121. package/lib.esm/rankify/GameMaster.js +43 -39
  122. package/lib.esm/rankify/GameMaster.js.map +1 -1
  123. package/lib.esm/rankify/InstanceBase.d.ts +46 -9442
  124. package/lib.esm/rankify/InstanceBase.d.ts.map +1 -1
  125. package/lib.esm/rankify/InstanceBase.js +100 -127
  126. package/lib.esm/rankify/InstanceBase.js.map +1 -1
  127. package/lib.esm/rankify/MAODistributor.d.ts +24 -16
  128. package/lib.esm/rankify/MAODistributor.d.ts.map +1 -1
  129. package/lib.esm/rankify/MAODistributor.js +37 -46
  130. package/lib.esm/rankify/MAODistributor.js.map +1 -1
  131. package/lib.esm/rankify/Player.d.ts +4 -2
  132. package/lib.esm/rankify/Player.d.ts.map +1 -1
  133. package/lib.esm/rankify/Player.js +5 -7
  134. package/lib.esm/rankify/Player.js.map +1 -1
  135. package/lib.esm/utils/EnvioGraphQLClient.d.ts +273 -0
  136. package/lib.esm/utils/EnvioGraphQLClient.d.ts.map +1 -0
  137. package/lib.esm/utils/EnvioGraphQLClient.js +726 -0
  138. package/lib.esm/utils/EnvioGraphQLClient.js.map +1 -0
  139. package/lib.esm/utils/index.d.ts +1 -1
  140. package/lib.esm/utils/index.d.ts.map +1 -1
  141. package/lib.esm/utils/index.js.map +1 -1
  142. package/lib.esm/utils/logger.d.ts +12 -0
  143. package/lib.esm/utils/logger.d.ts.map +1 -0
  144. package/lib.esm/utils/logger.js +12 -0
  145. package/lib.esm/utils/logger.js.map +1 -0
  146. package/lib.esm/utils/mockUtils.d.ts +39 -0
  147. package/lib.esm/utils/mockUtils.d.ts.map +1 -0
  148. package/lib.esm/utils/mockUtils.js +96 -0
  149. package/lib.esm/utils/mockUtils.js.map +1 -0
  150. package/package.json +3 -1
  151. package/cli/cli/helpers.js +0 -64
  152. package/cli/cli/helpers.js.map +0 -1
@@ -0,0 +1,726 @@
1
+ import { GraphQLClient, gql } from "graphql-request";
2
+ import { logger } from "./logger";
3
+ /**
4
+ * Default Envio GraphQL client configuration
5
+ */
6
+ const DEFAULT_CONFIG = {
7
+ endpoint: process.env.INDEXER_URL || "http://localhost:8080/v1/graphql",
8
+ fallbackToRPC: true,
9
+ };
10
+ /**
11
+ * Client for interacting with Envio GraphQL API to retrieve indexed blockchain events
12
+ */
13
+ export class EnvioGraphQLClient {
14
+ config;
15
+ client;
16
+ /**
17
+ * Create a new Envio GraphQL client
18
+ * @param config - Optional configuration for the client
19
+ */
20
+ constructor(config = {}) {
21
+ this.config = { ...DEFAULT_CONFIG, ...config };
22
+ // Log the endpoint we're using
23
+ console.debug(`Initializing EnvioGraphQLClient with endpoint: ${this.config.endpoint}`);
24
+ if (this.config.endpoint === DEFAULT_CONFIG.endpoint) {
25
+ console.debug(`Using default endpoint ${DEFAULT_CONFIG.endpoint}. Set INDEXER_URL environment variable to override.`);
26
+ }
27
+ const headers = {
28
+ "Content-Type": "application/json",
29
+ };
30
+ if (this.config.apiKey) {
31
+ headers["Authorization"] = `Bearer ${this.config.apiKey}`;
32
+ }
33
+ this.client = new GraphQLClient(this.config.endpoint, {
34
+ headers,
35
+ });
36
+ }
37
+ /**
38
+ * Get GameCreated events for a specific creator or game ID
39
+ */
40
+ async getGameCreatedEvents({ creator, gameId, gm, contractAddress, limit = 10, offset = 0, }) {
41
+ try {
42
+ // Use direct string interpolation for the query - no variables for numeric fields
43
+ const gameIdStr = gameId !== undefined ? gameId.toString() : undefined;
44
+ // Build where conditions
45
+ const whereParts = [];
46
+ if (creator) {
47
+ whereParts.push(`creator: { _eq: "${creator}" }`);
48
+ }
49
+ if (gameIdStr !== undefined) {
50
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
51
+ }
52
+ if (gm) {
53
+ whereParts.push(`gm: { _eq: "${gm}" }`);
54
+ }
55
+ if (contractAddress) {
56
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
57
+ }
58
+ const whereClause = whereParts.length > 0 ? whereParts.join(", ") : "";
59
+ // Simpler query with direct string literals instead of variables
60
+ const query = gql `
61
+ query {
62
+ RankifyInstance_gameCreated(
63
+ where: {
64
+ ${whereClause}
65
+ }
66
+ limit: ${limit}
67
+ offset: ${offset}
68
+ order_by: { blockTimestamp: desc }
69
+ ) {
70
+ id
71
+ gameId
72
+ gm
73
+ creator
74
+ rank
75
+ blockNumber
76
+ blockTimestamp
77
+ srcAddress
78
+ }
79
+ }
80
+ `;
81
+ const result = await this.client.request(query);
82
+ return result.RankifyInstance_gameCreated.map((event) => ({
83
+ ...event,
84
+ gameId: BigInt(event.gameId),
85
+ rank: BigInt(event.rank),
86
+ blockNumber: BigInt(event.blockNumber),
87
+ contractAddress: event.srcAddress,
88
+ gm: event.gm,
89
+ creator: event.creator,
90
+ }));
91
+ }
92
+ catch (error) {
93
+ console.error("Error fetching game created events:", error);
94
+ throw error;
95
+ }
96
+ }
97
+ /**
98
+ * Get player joined events for a specific game and participant
99
+ */
100
+ async getPlayerJoinedEvents({ gameId, participant, contractAddress, }) {
101
+ try {
102
+ // Use direct string interpolation for the query - no variables for numeric fields
103
+ const gameIdStr = gameId.toString();
104
+ // Build where conditions
105
+ const whereParts = [];
106
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
107
+ if (participant) {
108
+ whereParts.push(`participant: { _eq: "${participant}" }`);
109
+ }
110
+ if (contractAddress) {
111
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
112
+ }
113
+ const whereClause = whereParts.join(", ");
114
+ // Simpler query with direct string literals instead of variables
115
+ const query = gql `
116
+ query {
117
+ RankifyInstance_PlayerJoined(
118
+ where: {
119
+ ${whereClause}
120
+ }
121
+ order_by: { blockTimestamp: asc }
122
+ ) {
123
+ id
124
+ gameId
125
+ participant
126
+ gmCommitment
127
+ voterPubKey
128
+ blockNumber
129
+ blockTimestamp
130
+ srcAddress
131
+ transactionIndex
132
+ logIndex
133
+ }
134
+ }
135
+ `;
136
+ const result = await this.client.request(query);
137
+ return result.RankifyInstance_PlayerJoined.map((event) => ({
138
+ ...event,
139
+ gameId: BigInt(event.gameId),
140
+ blockNumber: BigInt(event.blockNumber),
141
+ contractAddress: event.srcAddress,
142
+ participant: event.participant,
143
+ transactionIndex: event.transactionIndex,
144
+ logIndex: event.logIndex,
145
+ }));
146
+ }
147
+ catch (error) {
148
+ console.error("Error fetching player joined events:", error);
149
+ throw error;
150
+ }
151
+ }
152
+ /**
153
+ * Get proposal submitted events for a specific game and turn
154
+ */
155
+ async getProposalSubmittedEvents({ gameId, turn, proposer, contractAddress, }) {
156
+ try {
157
+ // Use direct string interpolation for the query - no variables for numeric fields
158
+ const gameIdStr = gameId.toString();
159
+ const turnStr = turn !== undefined ? turn.toString() : undefined;
160
+ // Build where conditions
161
+ const whereParts = [];
162
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
163
+ if (turnStr !== undefined) {
164
+ whereParts.push(`turn: { _eq: ${turnStr} }`);
165
+ }
166
+ if (proposer) {
167
+ whereParts.push(`proposer: { _eq: "${proposer}" }`);
168
+ }
169
+ if (contractAddress) {
170
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
171
+ }
172
+ const whereClause = whereParts.join(", ");
173
+ // Simpler query with direct string literals instead of variables
174
+ const query = gql `
175
+ query {
176
+ RankifyInstance_ProposalSubmitted(
177
+ where: {
178
+ ${whereClause}
179
+ }
180
+ order_by: { blockTimestamp: asc }
181
+ ) {
182
+ id
183
+ gameId
184
+ turn
185
+ proposer
186
+ commitment
187
+ encryptedProposal
188
+ gmSignature
189
+ proposerSignature
190
+ blockNumber
191
+ blockTimestamp
192
+ srcAddress
193
+ }
194
+ }
195
+ `;
196
+ const result = await this.client.request(query);
197
+ return result.RankifyInstance_ProposalSubmitted.map((event) => ({
198
+ ...event,
199
+ gameId: BigInt(event.gameId),
200
+ turn: BigInt(event.turn),
201
+ commitment: BigInt(event.commitment),
202
+ blockNumber: BigInt(event.blockNumber),
203
+ contractAddress: event.srcAddress,
204
+ proposer: event.proposer,
205
+ }));
206
+ }
207
+ catch (error) {
208
+ console.error("Error fetching proposal submitted events:", error);
209
+ throw error;
210
+ }
211
+ }
212
+ /**
213
+ * Get vote submitted events for a specific game and turn
214
+ */
215
+ async getVoteSubmittedEvents({ gameId, turn, player, contractAddress, }) {
216
+ try {
217
+ // Use direct string interpolation for the query - no variables for numeric fields
218
+ const gameIdStr = gameId.toString();
219
+ const turnStr = turn !== undefined ? turn.toString() : undefined;
220
+ // Build where conditions
221
+ const whereParts = [];
222
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
223
+ if (turnStr !== undefined) {
224
+ whereParts.push(`turn: { _eq: ${turnStr} }`);
225
+ }
226
+ if (player) {
227
+ whereParts.push(`player: { _eq: "${player}" }`);
228
+ }
229
+ if (contractAddress) {
230
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
231
+ }
232
+ const whereClause = whereParts.join(", ");
233
+ // Simpler query with direct string literals instead of variables
234
+ const query = gql `
235
+ query {
236
+ RankifyInstance_VoteSubmitted(
237
+ where: {
238
+ ${whereClause}
239
+ }
240
+ order_by: { blockTimestamp: asc }
241
+ ) {
242
+ id
243
+ gameId
244
+ turn
245
+ player
246
+ sealedBallotId
247
+ gmSignature
248
+ voterSignature
249
+ ballotHash
250
+ blockNumber
251
+ blockTimestamp
252
+ srcAddress
253
+ }
254
+ }
255
+ `;
256
+ const result = await this.client.request(query);
257
+ return result.RankifyInstance_VoteSubmitted.map((event) => ({
258
+ ...event,
259
+ gameId: BigInt(event.gameId),
260
+ turn: BigInt(event.turn),
261
+ blockNumber: BigInt(event.blockNumber),
262
+ contractAddress: event.srcAddress,
263
+ player: event.player,
264
+ }));
265
+ }
266
+ catch (error) {
267
+ console.error("Error fetching vote submitted events:", error);
268
+ throw error;
269
+ }
270
+ }
271
+ /**
272
+ * Get registration open events for a specific game
273
+ */
274
+ async getRegistrationOpenEvents({ gameId, contractAddress }) {
275
+ try {
276
+ // Use direct string interpolation for the query - no variables for numeric fields
277
+ const gameIdStr = gameId.toString();
278
+ // Build where conditions
279
+ const whereParts = [];
280
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
281
+ if (contractAddress) {
282
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
283
+ }
284
+ const whereClause = whereParts.join(", ");
285
+ // Simpler query with direct string literals instead of variables
286
+ const query = gql `
287
+ query {
288
+ RankifyInstance_RegistrationOpen(
289
+ where: {
290
+ ${whereClause}
291
+ },
292
+ limit: 1
293
+ ) {
294
+ id
295
+ gameId
296
+ blockNumber
297
+ blockTimestamp
298
+ srcAddress
299
+ }
300
+ }
301
+ `;
302
+ const result = await this.client.request(query);
303
+ return result.RankifyInstance_RegistrationOpen.map((event) => ({
304
+ ...event,
305
+ gameId: BigInt(event.gameId),
306
+ blockNumber: BigInt(event.blockNumber),
307
+ contractAddress: event.srcAddress,
308
+ }));
309
+ }
310
+ catch (error) {
311
+ console.error("Error fetching registration open events:", error);
312
+ throw error;
313
+ }
314
+ }
315
+ /**
316
+ * Get turn ended events for a specific game and turn
317
+ */
318
+ async getTurnEndedEvents({ gameId, turn, contractAddress, }) {
319
+ try {
320
+ // Use direct string interpolation for the query - no variables for numeric fields
321
+ const gameIdStr = gameId.toString();
322
+ const turnStr = turn !== undefined ? turn.toString() : undefined;
323
+ // Build where conditions
324
+ const whereParts = [];
325
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
326
+ if (turnStr !== undefined) {
327
+ whereParts.push(`turn: { _eq: ${turnStr} }`);
328
+ }
329
+ if (contractAddress) {
330
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
331
+ }
332
+ const whereClause = whereParts.join(", ");
333
+ // Simpler query with direct string literals instead of variables
334
+ const query = gql `
335
+ query {
336
+ RankifyInstance_TurnEnded(
337
+ where: {
338
+ ${whereClause}
339
+ }
340
+ order_by: { turn: desc }
341
+ limit: 1
342
+ ) {
343
+ id
344
+ gameId
345
+ turn
346
+ players
347
+ scores
348
+ newProposals
349
+ proposerIndices
350
+ votes
351
+ blockNumber
352
+ blockTimestamp
353
+ srcAddress
354
+ }
355
+ }
356
+ `;
357
+ const result = await this.client.request(query);
358
+ return result.RankifyInstance_TurnEnded.map((event) => ({
359
+ ...event,
360
+ gameId: BigInt(event.gameId),
361
+ turn: BigInt(event.turn),
362
+ scores: event.scores.map((score) => BigInt(score)),
363
+ proposerIndices: event.proposerIndices.map((index) => BigInt(index)),
364
+ votes: event.votes.map((row) => row.map((vote) => BigInt(vote))),
365
+ blockNumber: BigInt(event.blockNumber),
366
+ contractAddress: event.srcAddress,
367
+ players: event.players.map((player) => player),
368
+ }));
369
+ }
370
+ catch (error) {
371
+ console.error("Error fetching turn ended events:", error);
372
+ throw error;
373
+ }
374
+ }
375
+ /**
376
+ * Get proposal score events for a specific game and turn
377
+ */
378
+ async getProposalScoreEvents({ gameId, turn, contractAddress, }) {
379
+ try {
380
+ // Use direct string interpolation for the query - no variables for numeric fields
381
+ const gameIdStr = gameId.toString();
382
+ const turnStr = turn !== undefined ? turn.toString() : undefined;
383
+ // Build where conditions
384
+ const whereParts = [];
385
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
386
+ if (turnStr !== undefined) {
387
+ whereParts.push(`turn: { _eq: ${turnStr} }`);
388
+ }
389
+ if (contractAddress) {
390
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
391
+ }
392
+ const whereClause = whereParts.join(", ");
393
+ // Simpler query with direct string literals instead of variables
394
+ const query = gql `
395
+ query {
396
+ RankifyInstance_ProposalScore(
397
+ where: {
398
+ ${whereClause}
399
+ }
400
+ order_by: { blockTimestamp: desc }
401
+ ) {
402
+ id
403
+ gameId
404
+ turn
405
+ proposalHash
406
+ proposal
407
+ score
408
+ blockNumber
409
+ blockTimestamp
410
+ srcAddress
411
+ }
412
+ }
413
+ `;
414
+ const result = await this.client.request(query);
415
+ return result.RankifyInstance_ProposalScore.map((event) => ({
416
+ ...event,
417
+ gameId: BigInt(event.gameId),
418
+ turn: BigInt(event.turn),
419
+ score: BigInt(event.score),
420
+ blockNumber: BigInt(event.blockNumber),
421
+ contractAddress: event.srcAddress,
422
+ }));
423
+ }
424
+ catch (error) {
425
+ console.error("Error fetching proposal score events:", error);
426
+ throw error;
427
+ }
428
+ }
429
+ /**
430
+ * Get game over events for a specific game
431
+ */
432
+ async getGameOverEvents({ gameId, contractAddress }) {
433
+ try {
434
+ // Use direct string interpolation for the query - no variables for numeric fields
435
+ const gameIdStr = gameId.toString();
436
+ // Build where conditions
437
+ const whereParts = [];
438
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
439
+ if (contractAddress) {
440
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
441
+ }
442
+ const whereClause = whereParts.join(", ");
443
+ // Simpler query with direct string literals instead of variables
444
+ const query = gql `
445
+ query {
446
+ RankifyInstance_GameOver(
447
+ where: {
448
+ ${whereClause}
449
+ },
450
+ limit: 1
451
+ ) {
452
+ id
453
+ gameId
454
+ players
455
+ scores
456
+ blockNumber
457
+ blockTimestamp
458
+ srcAddress
459
+ }
460
+ }
461
+ `;
462
+ const result = await this.client.request(query);
463
+ return result.RankifyInstance_GameOver.map((event) => ({
464
+ ...event,
465
+ gameId: BigInt(event.gameId),
466
+ scores: event.scores.map((score) => BigInt(score)),
467
+ blockNumber: BigInt(event.blockNumber),
468
+ contractAddress: event.srcAddress,
469
+ players: event.players.map((player) => player),
470
+ }));
471
+ }
472
+ catch (error) {
473
+ console.error("Error fetching game over events:", error);
474
+ throw error;
475
+ }
476
+ }
477
+ /**
478
+ * Get game started events for a specific game
479
+ */
480
+ async getGameStartedEvents({ gameId, contractAddress }) {
481
+ try {
482
+ // Use direct string interpolation for the query - no variables for numeric fields
483
+ const gameIdStr = gameId.toString();
484
+ // Build where conditions
485
+ const whereParts = [];
486
+ whereParts.push(`gameId: { _eq: ${gameIdStr} }`);
487
+ if (contractAddress) {
488
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
489
+ }
490
+ const whereClause = whereParts.join(", ");
491
+ // Simpler query with direct string literals instead of variables
492
+ const query = gql `
493
+ query {
494
+ RankifyInstance_GameStarted(
495
+ where: {
496
+ ${whereClause}
497
+ },
498
+ limit: 1,
499
+ order_by: { blockTimestamp: desc }
500
+ ) {
501
+ id
502
+ gameId
503
+ blockNumber
504
+ blockTimestamp
505
+ srcAddress
506
+ }
507
+ }
508
+ `;
509
+ const result = await this.client.request(query);
510
+ return result.RankifyInstance_GameStarted.map((event) => ({
511
+ ...event,
512
+ gameId: BigInt(event.gameId),
513
+ blockNumber: BigInt(event.blockNumber),
514
+ contractAddress: event.srcAddress,
515
+ }));
516
+ }
517
+ catch (error) {
518
+ console.error("Error fetching game started events:", error);
519
+ throw error;
520
+ }
521
+ }
522
+ /**
523
+ * Query MAO instances from the Envio GraphQL API
524
+ * @param params Query parameters for filtering instances
525
+ * @returns Array of MAO instance data
526
+ */
527
+ async queryInstances(params) {
528
+ try {
529
+ // Convert parameters to strings
530
+ const instanceIdStr = params.instanceId ? params.instanceId.toString() : undefined;
531
+ const distributionIdStr = params.distributionId || undefined;
532
+ // Build where conditions using the proper Envio GraphQL syntax
533
+ const conditions = [];
534
+ // Add simple equality conditions using _eq operator
535
+ if (instanceIdStr) {
536
+ conditions.push(`newInstanceId: { _eq: "${instanceIdStr}" }`);
537
+ }
538
+ if (distributionIdStr) {
539
+ conditions.push(`distributionId: { _eq: "${distributionIdStr}" }`);
540
+ }
541
+ // For a simple query, let's just use these two main identifiers
542
+ // and filter the results client-side for other criteria
543
+ console.log("Querying DAODistributor_Instantiated with params:", {
544
+ instanceId: instanceIdStr,
545
+ distributionId: distributionIdStr,
546
+ });
547
+ const query = gql `
548
+ query {
549
+ DAODistributor_Instantiated(
550
+ where: {
551
+ ${conditions.length > 0 ? conditions.join(", ") : ""}
552
+ }
553
+ order_by: { blockTimestamp: desc }
554
+ limit: ${instanceIdStr ? 1 : 100}
555
+ ) {
556
+ id
557
+ distributionId
558
+ newInstanceId
559
+ instances
560
+ args
561
+ blockNumber
562
+ blockTimestamp
563
+ version
564
+ }
565
+ }
566
+ `;
567
+ // Log the query for debugging (as a string)
568
+ logger("GraphQL query:", 3);
569
+ logger(query, 3);
570
+ const result = await this.client.request(query);
571
+ if (!result.DAODistributor_Instantiated || result.DAODistributor_Instantiated.length === 0) {
572
+ logger("No instances found with the given parameters", 3);
573
+ return [];
574
+ }
575
+ logger(`Found ${result.DAODistributor_Instantiated.length} instance(s)`, 3);
576
+ // Filter results client-side based on additional criteria
577
+ let results = result.DAODistributor_Instantiated;
578
+ // Return the filtered results
579
+ return results.map((event) => ({
580
+ distributionId: event.distributionId,
581
+ newInstanceId: event.newInstanceId,
582
+ version: event.version,
583
+ instances: event.instances,
584
+ args: event.args,
585
+ blockNumber: event.blockNumber,
586
+ blockTimestamp: event.blockTimestamp,
587
+ }));
588
+ }
589
+ catch (error) {
590
+ console.error("Error querying instances:", error);
591
+ // Log more details about the error to help diagnose issues
592
+ if (error instanceof Error) {
593
+ console.error("Error message:", error.message);
594
+ console.error("Error stack:", error.stack);
595
+ }
596
+ if (typeof error === 'object' &&
597
+ error !== null &&
598
+ 'response' in error &&
599
+ typeof error.response === 'object' &&
600
+ error.response !== null &&
601
+ 'status' in error.response &&
602
+ error.response.status === 404) {
603
+ console.error("Server returned 404 - Check if the Envio indexer is running and accessible at", this.config.endpoint);
604
+ }
605
+ else if (typeof error === 'object' &&
606
+ error !== null &&
607
+ 'response' in error &&
608
+ typeof error.response === 'object' &&
609
+ error.response !== null &&
610
+ 'errors' in error.response) {
611
+ console.error("GraphQL errors:", error.response.errors);
612
+ }
613
+ throw error;
614
+ }
615
+ }
616
+ /**
617
+ * Get multiple game states with pagination
618
+ */
619
+ async getGameStates({ skip = 0, first = 10, orderDirection = "desc", contractAddress, }) {
620
+ const variables = {
621
+ limit: first,
622
+ offset: skip,
623
+ contractAddress: contractAddress,
624
+ };
625
+ const query = gql `
626
+ query GetGames($limit: Int, $offset: Int, $contractAddress: String) {
627
+ RankifyInstance_gameCreated(
628
+ where: {
629
+ srcAddress: { _eq: $contractAddress }
630
+ }
631
+ limit: $limit
632
+ offset: $offset
633
+ order_by: { blockTimestamp: ${orderDirection} }
634
+ ) {
635
+ gameId
636
+ gm
637
+ creator
638
+ rank
639
+ blockNumber
640
+ blockTimestamp
641
+ srcAddress
642
+ }
643
+ }
644
+ `;
645
+ try {
646
+ const result = await this.client.request(query, variables);
647
+ // Fetch additional state info for each game
648
+ const gameStates = await Promise.all(result.RankifyInstance_gameCreated.map(async (game) => {
649
+ const gameId = BigInt(game.gameId);
650
+ const [turnEndedEvents, gameOverEvents] = await Promise.all([
651
+ this.getTurnEndedEvents({ gameId, contractAddress }),
652
+ this.getGameOverEvents({ gameId, contractAddress }),
653
+ ]);
654
+ // Determine current state
655
+ const turn = turnEndedEvents.length > 0 ? turnEndedEvents[0].turn + 1n : 0n;
656
+ const hasStarted = turn > 0n;
657
+ const isLastTurn = gameOverEvents.length > 0;
658
+ const hasEnded = gameOverEvents.length > 0;
659
+ return {
660
+ gameId,
661
+ gm: game.gm,
662
+ creator: game.creator,
663
+ rank: BigInt(game.rank),
664
+ turn,
665
+ hasStarted,
666
+ isLastTurn,
667
+ hasEnded,
668
+ createdAt: game.blockTimestamp,
669
+ contractAddress,
670
+ };
671
+ }));
672
+ return gameStates;
673
+ }
674
+ catch (error) {
675
+ console.error("Error fetching game states:", error);
676
+ throw error;
677
+ }
678
+ }
679
+ /**
680
+ * Get MAO instances for a specific distribution
681
+ */
682
+ async getMAOInstances({ distributionId, contractAddress, }) {
683
+ try {
684
+ // Build where conditions
685
+ const whereParts = [];
686
+ whereParts.push(`distributionId: { _eq: "${distributionId}" }`);
687
+ if (contractAddress) {
688
+ whereParts.push(`srcAddress: { _eq: "${contractAddress}" }`);
689
+ }
690
+ const whereClause = whereParts.join(", ");
691
+ // Simpler query with direct string literals instead of variables
692
+ const query = gql `
693
+ query {
694
+ DAODistributor_Instantiated(
695
+ where: {
696
+ ${whereClause}
697
+ }
698
+ order_by: { blockTimestamp: desc }
699
+ ) {
700
+ id
701
+ distributionId
702
+ newInstanceId
703
+ instances
704
+ args
705
+ blockNumber
706
+ blockTimestamp
707
+ version
708
+ srcAddress
709
+ }
710
+ }
711
+ `;
712
+ const result = await this.client.request(query);
713
+ return result.DAODistributor_Instantiated.map((event) => ({
714
+ ...event,
715
+ blockNumber: BigInt(event.blockNumber),
716
+ contractAddress: event.srcAddress,
717
+ }));
718
+ }
719
+ catch (error) {
720
+ console.error("Error fetching MAO instances:", error);
721
+ throw error;
722
+ }
723
+ }
724
+ }
725
+ export default EnvioGraphQLClient;
726
+ //# sourceMappingURL=EnvioGraphQLClient.js.map