@maci-protocol/coordinator 0.0.0-ci.da13fbb → 0.0.0-ci.dc5eaa4

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 (147) hide show
  1. package/README.md +4 -4
  2. package/build/hardhat.config.cjs +2 -3
  3. package/build/hardhat.config.cjs.map +1 -1
  4. package/build/hardhat.config.d.cts +2 -4
  5. package/build/scripts/generateKeypair.js +2 -2
  6. package/build/scripts/generateKeypair.js.map +1 -1
  7. package/build/tests/e2e.deploy.test.js +30 -19
  8. package/build/tests/e2e.deploy.test.js.map +1 -1
  9. package/build/tests/e2e.redis.test.d.ts +2 -0
  10. package/build/tests/e2e.redis.test.d.ts.map +1 -0
  11. package/build/tests/e2e.redis.test.js +119 -0
  12. package/build/tests/e2e.redis.test.js.map +1 -0
  13. package/build/tests/utils.d.ts.map +1 -1
  14. package/build/tests/utils.js +2 -2
  15. package/build/tests/utils.js.map +1 -1
  16. package/build/ts/app.module.d.ts.map +1 -1
  17. package/build/ts/app.module.js +6 -0
  18. package/build/ts/app.module.js.map +1 -1
  19. package/build/ts/common/__tests__/common.test.js +8 -18
  20. package/build/ts/common/__tests__/common.test.js.map +1 -1
  21. package/build/ts/common/accountAbstraction.d.ts +1 -1
  22. package/build/ts/common/accountAbstraction.d.ts.map +1 -1
  23. package/build/ts/common/accountAbstraction.js +4 -5
  24. package/build/ts/common/accountAbstraction.js.map +1 -1
  25. package/build/ts/common/chain.d.ts +2 -2
  26. package/build/ts/common/chain.d.ts.map +1 -1
  27. package/build/ts/common/chain.js +10 -9
  28. package/build/ts/common/chain.js.map +1 -1
  29. package/build/ts/common/errors.d.ts +15 -11
  30. package/build/ts/common/errors.d.ts.map +1 -1
  31. package/build/ts/common/errors.js +15 -11
  32. package/build/ts/common/errors.js.map +1 -1
  33. package/build/ts/deployer/__tests__/deployer.controller.test.js +17 -20
  34. package/build/ts/deployer/__tests__/deployer.controller.test.js.map +1 -1
  35. package/build/ts/deployer/__tests__/deployer.service.test.js +161 -79
  36. package/build/ts/deployer/__tests__/deployer.service.test.js.map +1 -1
  37. package/build/ts/deployer/__tests__/utils.d.ts +0 -1
  38. package/build/ts/deployer/__tests__/utils.d.ts.map +1 -1
  39. package/build/ts/deployer/__tests__/utils.js +4 -4
  40. package/build/ts/deployer/__tests__/utils.js.map +1 -1
  41. package/build/ts/deployer/deployer.service.d.ts +17 -7
  42. package/build/ts/deployer/deployer.service.d.ts.map +1 -1
  43. package/build/ts/deployer/deployer.service.js +209 -56
  44. package/build/ts/deployer/deployer.service.js.map +1 -1
  45. package/build/ts/deployer/types.d.ts +18 -11
  46. package/build/ts/deployer/types.d.ts.map +1 -1
  47. package/build/ts/file/file.service.d.ts.map +1 -1
  48. package/build/ts/file/file.service.js +4 -3
  49. package/build/ts/file/file.service.js.map +1 -1
  50. package/build/ts/health/__tests__/health.controller.test.d.ts +2 -0
  51. package/build/ts/health/__tests__/health.controller.test.d.ts.map +1 -0
  52. package/build/ts/health/__tests__/health.controller.test.js +52 -0
  53. package/build/ts/health/__tests__/health.controller.test.js.map +1 -0
  54. package/build/ts/health/__tests__/health.service.test.d.ts +2 -0
  55. package/build/ts/health/__tests__/health.service.test.d.ts.map +1 -0
  56. package/build/ts/health/__tests__/health.service.test.js +101 -0
  57. package/build/ts/health/__tests__/health.service.test.js.map +1 -0
  58. package/build/ts/health/health.controller.d.ts +16 -0
  59. package/build/ts/health/health.controller.d.ts.map +1 -0
  60. package/build/ts/health/health.controller.js +43 -0
  61. package/build/ts/health/health.controller.js.map +1 -0
  62. package/build/ts/health/health.module.d.ts +3 -0
  63. package/build/ts/health/health.module.d.ts.map +1 -0
  64. package/build/ts/health/health.module.js +22 -0
  65. package/build/ts/health/health.module.js.map +1 -0
  66. package/build/ts/health/health.service.d.ts +42 -0
  67. package/build/ts/health/health.service.d.ts.map +1 -0
  68. package/build/ts/health/health.service.js +176 -0
  69. package/build/ts/health/health.service.js.map +1 -0
  70. package/build/ts/health/types.d.ts +87 -0
  71. package/build/ts/health/types.d.ts.map +1 -0
  72. package/build/ts/health/types.js +2 -0
  73. package/build/ts/health/types.js.map +1 -0
  74. package/build/ts/proof/__tests__/proof.service.test.js +23 -54
  75. package/build/ts/proof/__tests__/proof.service.test.js.map +1 -1
  76. package/build/ts/proof/proof.service.d.ts.map +1 -1
  77. package/build/ts/proof/proof.service.js.map +1 -1
  78. package/build/ts/redis/__tests__/redis.service.test.d.ts +2 -0
  79. package/build/ts/redis/__tests__/redis.service.test.d.ts.map +1 -0
  80. package/build/ts/redis/__tests__/redis.service.test.js +149 -0
  81. package/build/ts/redis/__tests__/redis.service.test.js.map +1 -0
  82. package/build/ts/redis/redis.module.d.ts +3 -0
  83. package/build/ts/redis/redis.module.d.ts.map +1 -0
  84. package/build/ts/redis/redis.module.js +18 -0
  85. package/build/ts/redis/redis.module.js.map +1 -0
  86. package/build/ts/redis/redis.service.d.ts +52 -0
  87. package/build/ts/redis/redis.service.d.ts.map +1 -0
  88. package/build/ts/redis/redis.service.js +97 -0
  89. package/build/ts/redis/redis.service.js.map +1 -0
  90. package/build/ts/redis/types.d.ts +54 -0
  91. package/build/ts/redis/types.d.ts.map +1 -0
  92. package/build/ts/redis/types.js +2 -0
  93. package/build/ts/redis/types.js.map +1 -0
  94. package/build/ts/redis/utils.d.ts +20 -0
  95. package/build/ts/redis/utils.d.ts.map +1 -0
  96. package/build/ts/redis/utils.js +27 -0
  97. package/build/ts/redis/utils.js.map +1 -0
  98. package/build/ts/scheduler/__tests__/scheduler.controller.test.d.ts +2 -0
  99. package/build/ts/scheduler/__tests__/scheduler.controller.test.d.ts.map +1 -0
  100. package/build/ts/scheduler/__tests__/scheduler.controller.test.js +63 -0
  101. package/build/ts/scheduler/__tests__/scheduler.controller.test.js.map +1 -0
  102. package/build/ts/scheduler/__tests__/scheduler.service.test.d.ts +2 -0
  103. package/build/ts/scheduler/__tests__/scheduler.service.test.d.ts.map +1 -0
  104. package/build/ts/scheduler/__tests__/scheduler.service.test.js +264 -0
  105. package/build/ts/scheduler/__tests__/scheduler.service.test.js.map +1 -0
  106. package/build/ts/scheduler/dto.d.ts +42 -0
  107. package/build/ts/scheduler/dto.d.ts.map +1 -0
  108. package/build/ts/scheduler/dto.js +116 -0
  109. package/build/ts/scheduler/dto.js.map +1 -0
  110. package/build/ts/scheduler/scheduler.controller.d.ts +32 -0
  111. package/build/ts/scheduler/scheduler.controller.d.ts.map +1 -0
  112. package/build/ts/scheduler/scheduler.controller.js +111 -0
  113. package/build/ts/scheduler/scheduler.controller.js.map +1 -0
  114. package/build/ts/scheduler/scheduler.module.d.ts +3 -0
  115. package/build/ts/scheduler/scheduler.module.d.ts.map +1 -0
  116. package/build/ts/scheduler/scheduler.module.js +23 -0
  117. package/build/ts/scheduler/scheduler.module.js.map +1 -0
  118. package/build/ts/scheduler/scheduler.service.d.ts +63 -0
  119. package/build/ts/scheduler/scheduler.service.d.ts.map +1 -0
  120. package/build/ts/scheduler/scheduler.service.js +239 -0
  121. package/build/ts/scheduler/scheduler.service.js.map +1 -0
  122. package/build/ts/scheduler/types.d.ts +61 -0
  123. package/build/ts/scheduler/types.d.ts.map +1 -0
  124. package/build/ts/scheduler/types.js +2 -0
  125. package/build/ts/scheduler/types.js.map +1 -0
  126. package/build/ts/sessionKeys/__tests__/sessionKeys.controller.test.js +2 -2
  127. package/build/ts/sessionKeys/__tests__/sessionKeys.controller.test.js.map +1 -1
  128. package/build/ts/sessionKeys/__tests__/sessionKeys.service.test.js +30 -16
  129. package/build/ts/sessionKeys/__tests__/sessionKeys.service.test.js.map +1 -1
  130. package/build/ts/sessionKeys/__tests__/utils.d.ts.map +1 -1
  131. package/build/ts/sessionKeys/__tests__/utils.js +4 -5
  132. package/build/ts/sessionKeys/__tests__/utils.js.map +1 -1
  133. package/build/ts/sessionKeys/sessionKeys.service.d.ts.map +1 -1
  134. package/build/ts/sessionKeys/sessionKeys.service.js +1 -2
  135. package/build/ts/sessionKeys/sessionKeys.service.js.map +1 -1
  136. package/build/ts/subgraph/__tests__/subgraph.service.test.js +2 -3
  137. package/build/ts/subgraph/__tests__/subgraph.service.test.js.map +1 -1
  138. package/build/ts/subgraph/subgraph.service.d.ts +4 -0
  139. package/build/ts/subgraph/subgraph.service.d.ts.map +1 -1
  140. package/build/ts/subgraph/subgraph.service.js +16 -5
  141. package/build/ts/subgraph/subgraph.service.js.map +1 -1
  142. package/build/tsconfig.build.tsbuildinfo +1 -1
  143. package/package.json +34 -30
  144. package/build/tests/e2e.aa.test.d.ts +0 -2
  145. package/build/tests/e2e.aa.test.d.ts.map +0 -1
  146. package/build/tests/e2e.aa.test.js +0 -96
  147. package/build/tests/e2e.aa.test.js.map +0 -1
@@ -0,0 +1,18 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Module } from "@nestjs/common";
8
+ import { RedisService } from "./redis.service";
9
+ let RedisModule = class RedisModule {
10
+ };
11
+ RedisModule = __decorate([
12
+ Module({
13
+ exports: [RedisService],
14
+ providers: [RedisService],
15
+ })
16
+ ], RedisModule);
17
+ export { RedisModule };
18
+ //# sourceMappingURL=redis.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.module.js","sourceRoot":"","sources":["../../../ts/redis/redis.module.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAMxC,IAAM,WAAW,GAAjB,MAAM,WAAW;CAAG,CAAA;AAAd,WAAW;IAJvB,MAAM,CAAC;QACN,OAAO,EAAE,CAAC,YAAY,CAAC;QACvB,SAAS,EAAE,CAAC,YAAY,CAAC;KAC1B,CAAC;GACW,WAAW,CAAG"}
@@ -0,0 +1,52 @@
1
+ import { OnModuleInit } from "@nestjs/common";
2
+ /**
3
+ * Redis service to interact with the Redis database.
4
+ */
5
+ export declare class RedisService implements OnModuleInit {
6
+ /**
7
+ * Client instance used to interact with the Redis database.
8
+ */
9
+ private client;
10
+ /**
11
+ * Create a new instance of the RedisService.
12
+ * The Redis configuration is set up here
13
+ */
14
+ constructor();
15
+ /**
16
+ * Initializes the Redis client connection when the module has been uploaded.
17
+ * @dev it is a lifecycle hook provided by NestJS that is called after all dependencies
18
+ * are injected and the module is fully initialized.
19
+ * This ensures your service is ready and all dependencies are available.
20
+ */
21
+ onModuleInit(): Promise<void>;
22
+ /**
23
+ * Saves a new key-value pair in Redis.
24
+ * @param key key for the Redis entry
25
+ * @param value value to be stored in Redis
26
+ */
27
+ set(key: string, value: string): Promise<void>;
28
+ /**
29
+ * Retrieves a value from Redis by its key.
30
+ * @param key key for the Redis entry
31
+ * @returns the value stored as string in Redis or null if the key does not exist
32
+ */
33
+ get(key: string): Promise<string | null>;
34
+ /**
35
+ * Retrieves all keys from Redis that match a specific pattern.
36
+ * @param match optional pattern to match keys in Redis
37
+ * @dev if no match is provided, it will return all keys
38
+ * @returns the array of string values stored in Redis that match the pattern
39
+ */
40
+ getAll(match?: string): Promise<string[]>;
41
+ /**
42
+ * Deletes a key from Redis.
43
+ * @param key key to be deleted from Redis
44
+ */
45
+ delete(key: string): Promise<number>;
46
+ /**
47
+ * Check if the connection to Redis is open
48
+ * @return true if the connection is open, false otherwise
49
+ */
50
+ isOpen(): boolean;
51
+ }
52
+ //# sourceMappingURL=redis.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.service.d.ts","sourceRoot":"","sources":["../../../ts/redis/redis.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG1D;;GAEG;AACH,qBACa,YAAa,YAAW,YAAY;IAC/C;;OAEG;IACH,OAAO,CAAC,MAAM,CAAkB;IAEhC;;;OAGG;;IAQH;;;;;OAKG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAInC;;;;OAIG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMpD;;;;OAIG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAI9C;;;;;OAKG;IACG,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAmB/C;;;OAGG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAI1C;;;OAGG;IACH,MAAM,IAAI,OAAO;CAGlB"}
@@ -0,0 +1,97 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { Injectable } from "@nestjs/common";
11
+ import { createClient } from "@redis/client";
12
+ /**
13
+ * Redis service to interact with the Redis database.
14
+ */
15
+ let RedisService = class RedisService {
16
+ /**
17
+ * Client instance used to interact with the Redis database.
18
+ */
19
+ client;
20
+ /**
21
+ * Create a new instance of the RedisService.
22
+ * The Redis configuration is set up here
23
+ */
24
+ constructor() {
25
+ this.client = createClient({
26
+ url: `redis://${process.env.COORDINATOR_REDIS_HOST}:${process.env.COORDINATOR_REDIS_PORT}`,
27
+ disableOfflineQueue: true,
28
+ });
29
+ }
30
+ /**
31
+ * Initializes the Redis client connection when the module has been uploaded.
32
+ * @dev it is a lifecycle hook provided by NestJS that is called after all dependencies
33
+ * are injected and the module is fully initialized.
34
+ * This ensures your service is ready and all dependencies are available.
35
+ */
36
+ async onModuleInit() {
37
+ await this.client.connect();
38
+ }
39
+ /**
40
+ * Saves a new key-value pair in Redis.
41
+ * @param key key for the Redis entry
42
+ * @param value value to be stored in Redis
43
+ */
44
+ async set(key, value) {
45
+ // we are not using @redis/json because we dont need complex search capabilities
46
+ // we store a string (lightweight) and parse it to use the object
47
+ await this.client.set(key, value);
48
+ }
49
+ /**
50
+ * Retrieves a value from Redis by its key.
51
+ * @param key key for the Redis entry
52
+ * @returns the value stored as string in Redis or null if the key does not exist
53
+ */
54
+ async get(key) {
55
+ return this.client.get(key);
56
+ }
57
+ /**
58
+ * Retrieves all keys from Redis that match a specific pattern.
59
+ * @param match optional pattern to match keys in Redis
60
+ * @dev if no match is provided, it will return all keys
61
+ * @returns the array of string values stored in Redis that match the pattern
62
+ */
63
+ async getAll(match) {
64
+ let result = [];
65
+ let currentCursor = "0";
66
+ do {
67
+ // using a cursor is recommended to be use in production environment
68
+ // eslint-disable-next-line no-await-in-loop
69
+ const { cursor, keys } = await this.client.scan(currentCursor, { MATCH: match || "*", COUNT: 100 });
70
+ // eslint-disable-next-line no-await-in-loop
71
+ const values = await Promise.all(keys.map((key) => this.get(key)));
72
+ result = result.concat(values.filter((value) => value !== null));
73
+ currentCursor = cursor;
74
+ } while (currentCursor !== "0");
75
+ return result;
76
+ }
77
+ /**
78
+ * Deletes a key from Redis.
79
+ * @param key key to be deleted from Redis
80
+ */
81
+ async delete(key) {
82
+ return this.client.del(key);
83
+ }
84
+ /**
85
+ * Check if the connection to Redis is open
86
+ * @return true if the connection is open, false otherwise
87
+ */
88
+ isOpen() {
89
+ return this.client.isOpen;
90
+ }
91
+ };
92
+ RedisService = __decorate([
93
+ Injectable(),
94
+ __metadata("design:paramtypes", [])
95
+ ], RedisService);
96
+ export { RedisService };
97
+ //# sourceMappingURL=redis.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.service.js","sourceRoot":"","sources":["../../../ts/redis/redis.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAgB,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAmB,MAAM,eAAe,CAAC;AAE9D;;GAEG;AAEI,IAAM,YAAY,GAAlB,MAAM,YAAY;IACvB;;OAEG;IACK,MAAM,CAAkB;IAEhC;;;OAGG;IACH;QACE,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC;YACzB,GAAG,EAAE,WAAW,OAAO,CAAC,GAAG,CAAC,sBAAsB,IAAI,OAAO,CAAC,GAAG,CAAC,sBAAsB,EAAE;YAC1F,mBAAmB,EAAE,IAAI;SAC1B,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,YAAY;QAChB,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAa;QAClC,gFAAgF;QAChF,iEAAiE;QACjE,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,GAAG,CAAC,GAAW;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,MAAM,CAAC,KAAc;QACzB,IAAI,MAAM,GAAa,EAAE,CAAC;QAC1B,IAAI,aAAa,GAAG,GAAG,CAAC;QAExB,GAAG,CAAC;YACF,oEAAoE;YACpE,4CAA4C;YAC5C,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,EAAE,EAAE,KAAK,EAAE,KAAK,IAAI,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;YAEpG,4CAA4C;YAC5C,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACnE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC;YAEjE,aAAa,GAAG,MAAM,CAAC;QACzB,CAAC,QAAQ,aAAa,KAAK,GAAG,EAAE;QAEhC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,MAAM,CAAC,GAAW;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC5B,CAAC;CACF,CAAA;AAvFY,YAAY;IADxB,UAAU,EAAE;;GACA,YAAY,CAuFxB"}
@@ -0,0 +1,54 @@
1
+ import type { ESupportedNetworks } from "../common";
2
+ import type { EMode } from "@maci-protocol/sdk";
3
+ /**
4
+ * Interface of the minimal properties to identify a scheduled poll
5
+ */
6
+ export interface IIdentityScheduledPoll {
7
+ /**
8
+ * Maci contract address
9
+ */
10
+ maciAddress: string;
11
+ /**
12
+ * Poll id (unique identifier)
13
+ */
14
+ pollId: string;
15
+ /**
16
+ * Chain in which the poll is deployed
17
+ */
18
+ chain: ESupportedNetworks;
19
+ }
20
+ /**
21
+ * Interface for scheduled polls stored in Redis
22
+ */
23
+ export interface IScheduledPoll extends IIdentityScheduledPoll {
24
+ /**
25
+ * Deployment block number
26
+ */
27
+ deploymentBlockNumber: number;
28
+ /**
29
+ * Voting mode
30
+ */
31
+ mode: EMode;
32
+ /**
33
+ * End date in seconds
34
+ */
35
+ endDate: number;
36
+ /**
37
+ * Whether the MACI contract's state root has been merged
38
+ */
39
+ merged: boolean;
40
+ /**
41
+ * Whether the proofs has been generated
42
+ */
43
+ proofsGenerated: boolean;
44
+ }
45
+ /**
46
+ * getPollKeyForRedis parameters
47
+ */
48
+ export interface IGetPollKeyForRedisParams extends IIdentityScheduledPoll {
49
+ /**
50
+ * Test environment flag (optional)
51
+ */
52
+ test?: boolean;
53
+ }
54
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../ts/redis/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,KAAK,EAAE,kBAAkB,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,cAAe,SAAQ,sBAAsB;IAC5D;;OAEG;IACH,qBAAqB,EAAE,MAAM,CAAC;IAE9B;;OAEG;IACH,IAAI,EAAE,KAAK,CAAC;IAEZ;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,eAAe,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,sBAAsB;IACvE;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../ts/redis/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ import type { IGetPollKeyForRedisParams, IIdentityScheduledPoll } from "./types";
2
+ /**
3
+ * Generates a Redis key for a poll based on its attributes
4
+ *
5
+ * @param chain - chain where the poll is deployed
6
+ * @param maciAddress - maci contract where the poll was created
7
+ * @param pollId - poll ID
8
+ * @param test - Optional flag to indicate if this is a test environment
9
+ * @returns key for Redis
10
+ */
11
+ export declare const getPollKeyForRedis: ({ chain, maciAddress, pollId, test }: IGetPollKeyForRedisParams) => string;
12
+ /**
13
+ * Generates a Redis key for a poll object
14
+ *
15
+ * @param scheduledPoll - poll object to generate the key from
16
+ * @param test - Optional flag to indicate if this is a test environment
17
+ * @returns key for Redis
18
+ */
19
+ export declare const getPollKeyFromObject: (scheduledPoll: IIdentityScheduledPoll, test?: boolean) => string;
20
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../ts/redis/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEjF;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,sCAA8C,yBAAyB,KAAG,MAG5G,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,eAAe,sBAAsB,EAAE,OAAO,OAAO,KAAG,MAMzF,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Generates a Redis key for a poll based on its attributes
3
+ *
4
+ * @param chain - chain where the poll is deployed
5
+ * @param maciAddress - maci contract where the poll was created
6
+ * @param pollId - poll ID
7
+ * @param test - Optional flag to indicate if this is a test environment
8
+ * @returns key for Redis
9
+ */
10
+ export const getPollKeyForRedis = ({ chain, maciAddress, pollId, test = false }) => {
11
+ const isTest = test || process.env.NODE_ENV === "test";
12
+ return `${chain}-${maciAddress}-poll-${pollId}${isTest ? `-test` : ""}`;
13
+ };
14
+ /**
15
+ * Generates a Redis key for a poll object
16
+ *
17
+ * @param scheduledPoll - poll object to generate the key from
18
+ * @param test - Optional flag to indicate if this is a test environment
19
+ * @returns key for Redis
20
+ */
21
+ export const getPollKeyFromObject = (scheduledPoll, test) => getPollKeyForRedis({
22
+ chain: scheduledPoll.chain,
23
+ maciAddress: scheduledPoll.maciAddress,
24
+ pollId: scheduledPoll.pollId,
25
+ test,
26
+ });
27
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../ts/redis/utils.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,IAAI,GAAG,KAAK,EAA6B,EAAU,EAAE;IACpH,MAAM,MAAM,GAAG,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC;IACvD,OAAO,GAAG,KAAK,IAAI,WAAW,SAAS,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;AAC1E,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,aAAqC,EAAE,IAAc,EAAU,EAAE,CACpG,kBAAkB,CAAC;IACjB,KAAK,EAAE,aAAa,CAAC,KAAK;IAC1B,WAAW,EAAE,aAAa,CAAC,WAAW;IACtC,MAAM,EAAE,aAAa,CAAC,MAAM;IAC5B,IAAI;CACL,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=scheduler.controller.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.controller.test.d.ts","sourceRoot":"","sources":["../../../../ts/scheduler/__tests__/scheduler.controller.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,63 @@
1
+ import { EMode } from "@maci-protocol/sdk";
2
+ import { HttpException, HttpStatus } from "@nestjs/common";
3
+ import { ESupportedNetworks } from "../../common";
4
+ import { SchedulerController } from "../scheduler.controller";
5
+ const scheduledPoll = {
6
+ maciAddress: "0x0",
7
+ pollId: 5,
8
+ mode: EMode.NON_QV,
9
+ chain: ESupportedNetworks.OPTIMISM_SEPOLIA,
10
+ deploymentBlockNumber: 1,
11
+ };
12
+ const identityScheduledPoll = {
13
+ maciAddress: scheduledPoll.maciAddress,
14
+ pollId: scheduledPoll.pollId,
15
+ chain: scheduledPoll.chain,
16
+ };
17
+ describe("SchedulerController", () => {
18
+ let service;
19
+ let controller;
20
+ beforeEach(() => {
21
+ service = {
22
+ registerPoll: jest.fn().mockResolvedValue({ isScheduled: true }),
23
+ isPollScheduled: jest.fn().mockResolvedValue({ isScheduled: true }),
24
+ deleteScheduledPoll: jest.fn().mockResolvedValue({ isScheduled: false }),
25
+ };
26
+ controller = new SchedulerController(service);
27
+ });
28
+ describe("register", () => {
29
+ test("should register a poll for finalization", async () => {
30
+ const { isScheduled } = await controller.register(scheduledPoll);
31
+ expect(isScheduled).toBe(true);
32
+ });
33
+ test("should throw an error if registration fails", async () => {
34
+ const errorMessage = "Registration failed";
35
+ service.registerPoll.mockRejectedValueOnce(new Error(errorMessage));
36
+ await expect(controller.register(scheduledPoll)).rejects.toThrow(new HttpException(errorMessage, HttpStatus.BAD_REQUEST));
37
+ });
38
+ });
39
+ describe("status", () => {
40
+ test("should return scheduled poll status", async () => {
41
+ const { isScheduled } = await controller.status(identityScheduledPoll);
42
+ expect(isScheduled).toBe(true);
43
+ });
44
+ test("should throw an error if checking status fails", async () => {
45
+ const errorMessage = "checking status failed";
46
+ service.isPollScheduled.mockRejectedValueOnce(new Error(errorMessage));
47
+ await expect(controller.status(scheduledPoll)).rejects.toThrow(new HttpException(errorMessage, HttpStatus.BAD_REQUEST));
48
+ });
49
+ });
50
+ describe("delete", () => {
51
+ test("should delete a scheduled poll", async () => {
52
+ await controller.register(scheduledPoll);
53
+ const { isScheduled } = await controller.delete(identityScheduledPoll);
54
+ expect(isScheduled).toBe(false);
55
+ });
56
+ test("should throw an error if checking status fails", async () => {
57
+ const errorMessage = "Delete failed";
58
+ service.deleteScheduledPoll.mockRejectedValueOnce(new Error(errorMessage));
59
+ await expect(controller.delete(scheduledPoll)).rejects.toThrow(new HttpException(errorMessage, HttpStatus.BAD_REQUEST));
60
+ });
61
+ });
62
+ });
63
+ //# sourceMappingURL=scheduler.controller.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.controller.test.js","sourceRoot":"","sources":["../../../../ts/scheduler/__tests__/scheduler.controller.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAG9D,MAAM,aAAa,GAA8B;IAC/C,WAAW,EAAE,KAAK;IAClB,MAAM,EAAE,CAAC;IACT,IAAI,EAAE,KAAK,CAAC,MAAM;IAClB,KAAK,EAAE,kBAAkB,CAAC,gBAAgB;IAC1C,qBAAqB,EAAE,CAAC;CACzB,CAAC;AAEF,MAAM,qBAAqB,GAAG;IAC5B,WAAW,EAAE,aAAa,CAAC,WAAW;IACtC,MAAM,EAAE,aAAa,CAAC,MAAM;IAC5B,KAAK,EAAE,aAAa,CAAC,KAAK;CACC,CAAC;AAE9B,QAAQ,CAAC,qBAAqB,EAAE,GAAG,EAAE;IACnC,IAAI,OAAsC,CAAC;IAE3C,IAAI,UAA+B,CAAC;IAEpC,UAAU,CAAC,GAAG,EAAE;QACd,OAAO,GAAG;YACR,YAAY,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;YAChE,eAAe,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;YACnE,mBAAmB,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;SAC7B,CAAC;QAE9C,UAAU,GAAG,IAAI,mBAAmB,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;QACxB,IAAI,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;YACzD,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;YAEjE,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,6CAA6C,EAAE,KAAK,IAAI,EAAE;YAC7D,MAAM,YAAY,GAAG,qBAAqB,CAAC;YAC3C,OAAO,CAAC,YAAY,CAAC,qBAAqB,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;YAEpE,MAAM,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAC9D,IAAI,aAAa,CAAC,YAAY,EAAE,UAAU,CAAC,WAAW,CAAC,CACxD,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;QACtB,IAAI,CAAC,qCAAqC,EAAE,KAAK,IAAI,EAAE;YACrD,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC;YAEvE,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,gDAAgD,EAAE,KAAK,IAAI,EAAE;YAChE,MAAM,YAAY,GAAG,wBAAwB,CAAC;YAC9C,OAAO,CAAC,eAAe,CAAC,qBAAqB,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;YAEvE,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAC5D,IAAI,aAAa,CAAC,YAAY,EAAE,UAAU,CAAC,WAAW,CAAC,CACxD,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;QACtB,IAAI,CAAC,gCAAgC,EAAE,KAAK,IAAI,EAAE;YAChD,MAAM,UAAU,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;YAEzC,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC;YAEvE,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,gDAAgD,EAAE,KAAK,IAAI,EAAE;YAChE,MAAM,YAAY,GAAG,eAAe,CAAC;YACrC,OAAO,CAAC,mBAAmB,CAAC,qBAAqB,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;YAE3E,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAC5D,IAAI,aAAa,CAAC,YAAY,EAAE,UAAU,CAAC,WAAW,CAAC,CACxD,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=scheduler.service.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.service.test.d.ts","sourceRoot":"","sources":["../../../../ts/scheduler/__tests__/scheduler.service.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,264 @@
1
+ import { EMode, getPoll, isTallied } from "@maci-protocol/sdk";
2
+ import { SchedulerRegistry } from "@nestjs/schedule";
3
+ import { ErrorCodes, ESupportedNetworks } from "../../common";
4
+ import { FileService } from "../../file/file.service";
5
+ import { getPollKeyFromObject } from "../../redis/utils";
6
+ import { SessionKeysService } from "../../sessionKeys/sessionKeys.service";
7
+ import { SchedulerService } from "../scheduler.service";
8
+ const scheduledPoll = {
9
+ maciAddress: "0x0",
10
+ pollId: "5",
11
+ mode: EMode.NON_QV,
12
+ chain: ESupportedNetworks.OPTIMISM_SEPOLIA,
13
+ endDate: 1752534000,
14
+ deploymentBlockNumber: 1,
15
+ merged: false,
16
+ proofsGenerated: false,
17
+ };
18
+ jest.mock("@maci-protocol/sdk", () => ({
19
+ ...jest.requireActual("@maci-protocol/sdk"),
20
+ getPoll: jest.fn().mockResolvedValue({
21
+ address: "0x123",
22
+ endDate: 1752534000,
23
+ isMerged: false,
24
+ }),
25
+ isTallied: jest.fn().mockResolvedValue(false),
26
+ }));
27
+ describe("SchedulerService", () => {
28
+ let fileService;
29
+ let sessionKeysService;
30
+ let redisService;
31
+ let schedulerRegistry;
32
+ let service;
33
+ beforeAll(async () => {
34
+ fileService = new FileService();
35
+ sessionKeysService = new SessionKeysService(fileService);
36
+ // Mock RedisService methods as needed
37
+ redisService = {
38
+ get: jest.fn().mockResolvedValue(JSON.stringify(scheduledPoll)),
39
+ set: jest.fn(),
40
+ delete: jest.fn(),
41
+ getAll: jest.fn().mockResolvedValue([]),
42
+ };
43
+ schedulerRegistry = new SchedulerRegistry();
44
+ service = new SchedulerService(sessionKeysService, redisService, schedulerRegistry);
45
+ await service.onModuleInit();
46
+ });
47
+ afterAll(() => {
48
+ jest.clearAllMocks();
49
+ });
50
+ afterEach(() => {
51
+ jest.clearAllMocks();
52
+ const timeouts = schedulerRegistry.getTimeouts();
53
+ // Clear all timeouts in the mock scheduler registry
54
+ timeouts.forEach((timeout) => {
55
+ if (timeout.includes("-test")) {
56
+ schedulerRegistry.deleteTimeout(timeout);
57
+ }
58
+ });
59
+ });
60
+ describe(`isPollScheduled`, () => {
61
+ test("should return true if poll is saved, scheduled and not tallied", async () => {
62
+ await service.registerPoll(scheduledPoll);
63
+ const isPollScheduled = await service.isPollScheduled(scheduledPoll);
64
+ expect(isPollScheduled.isScheduled).toBe(true);
65
+ });
66
+ test("should return false if poll is not saved", async () => {
67
+ redisService.get.mockResolvedValueOnce(null);
68
+ const isPollScheduled = await service.isPollScheduled(scheduledPoll);
69
+ expect(isPollScheduled.isScheduled).toBe(false);
70
+ });
71
+ test("should return false if poll is saved and timeout not scheduled", async () => {
72
+ await service.registerPoll(scheduledPoll);
73
+ schedulerRegistry.deleteTimeout(getPollKeyFromObject(scheduledPoll));
74
+ const isPollScheduled = await service.isPollScheduled(scheduledPoll);
75
+ expect(isPollScheduled.isScheduled).toBe(false);
76
+ });
77
+ test("should return false if poll is scheduled but not saved", async () => {
78
+ redisService.get.mockResolvedValueOnce(null);
79
+ const isPollScheduled = await service.isPollScheduled(scheduledPoll);
80
+ expect(isPollScheduled.isScheduled).toBe(false);
81
+ });
82
+ test("should throw an error if poll is already tallied", async () => {
83
+ await service.registerPoll(scheduledPoll);
84
+ getPoll.mockResolvedValueOnce({
85
+ isMerged: true,
86
+ });
87
+ isTallied.mockResolvedValueOnce(true);
88
+ await expect(service.isPollScheduled(scheduledPoll)).rejects.toThrow(ErrorCodes.POLL_ALREADY_TALLIED.toString());
89
+ });
90
+ });
91
+ describe("getPollFinalizationData", () => {
92
+ test("should return the end date and tallied status of a poll", async () => {
93
+ getPoll.mockResolvedValueOnce({
94
+ isMerged: false,
95
+ endDate: scheduledPoll.endDate,
96
+ });
97
+ isTallied.mockResolvedValueOnce(false);
98
+ const { endDate, isPollTallied } = await service.getPollFinalizationData({
99
+ maciAddress: scheduledPoll.maciAddress,
100
+ pollId: scheduledPoll.pollId,
101
+ chain: scheduledPoll.chain,
102
+ });
103
+ expect(endDate).toBe(scheduledPoll.endDate);
104
+ expect(isPollTallied).toBe(false);
105
+ });
106
+ test("should return isPollTallied as false if poll is not merged and is not tallied", async () => {
107
+ getPoll.mockResolvedValueOnce({
108
+ isMerged: false,
109
+ });
110
+ isTallied.mockResolvedValueOnce(false);
111
+ const { isPollTallied } = await service.getPollFinalizationData({
112
+ maciAddress: scheduledPoll.maciAddress,
113
+ pollId: scheduledPoll.pollId,
114
+ chain: scheduledPoll.chain,
115
+ });
116
+ expect(isPollTallied).toBe(false);
117
+ });
118
+ test("should return isPollTallied as false if poll is merged and is not tallied", async () => {
119
+ getPoll.mockResolvedValueOnce({
120
+ isMerged: true,
121
+ });
122
+ isTallied.mockResolvedValueOnce(false);
123
+ const { isPollTallied } = await service.getPollFinalizationData({
124
+ maciAddress: scheduledPoll.maciAddress,
125
+ pollId: scheduledPoll.pollId,
126
+ chain: scheduledPoll.chain,
127
+ });
128
+ expect(isPollTallied).toBe(false);
129
+ });
130
+ test("should return isPollTallied as false if poll is not merged and is tallied (it is impossible)", async () => {
131
+ getPoll.mockResolvedValueOnce({
132
+ isMerged: false,
133
+ });
134
+ isTallied.mockResolvedValueOnce(true);
135
+ const { isPollTallied } = await service.getPollFinalizationData({
136
+ maciAddress: scheduledPoll.maciAddress,
137
+ pollId: scheduledPoll.pollId,
138
+ chain: scheduledPoll.chain,
139
+ });
140
+ expect(isPollTallied).toBe(false);
141
+ });
142
+ });
143
+ describe("registerPoll", () => {
144
+ test("should register a poll for finalization", async () => {
145
+ const { isScheduled } = await service.registerPoll(scheduledPoll);
146
+ const isPollScheduled = await service.isPollScheduled(scheduledPoll);
147
+ const isPollTimeoutScheduled = schedulerRegistry.doesExist("timeout", getPollKeyFromObject(scheduledPoll));
148
+ expect(isScheduled).toBe(true);
149
+ expect(isPollScheduled.isScheduled).toBe(true);
150
+ expect(isPollTimeoutScheduled).toBe(true);
151
+ });
152
+ test("should register 2 polls and schedule their finalization", async () => {
153
+ const pollOne = { ...scheduledPoll, pollId: "1" };
154
+ const pollTwo = { ...scheduledPoll, pollId: "2" };
155
+ const { isScheduled: isScheduledOne } = await service.registerPoll(pollOne);
156
+ const { isScheduled: isScheduledTwo } = await service.registerPoll(pollTwo);
157
+ const [isPollScheduledOne, isPollScheduledTwo] = await Promise.all([
158
+ service.isPollScheduled(pollOne),
159
+ service.isPollScheduled(pollTwo),
160
+ ]);
161
+ const isPollOneTimeoutScheduled = schedulerRegistry.doesExist("timeout", getPollKeyFromObject(pollOne));
162
+ const isPollTwoTimeoutScheduled = schedulerRegistry.doesExist("timeout", getPollKeyFromObject(pollTwo));
163
+ expect(isScheduledOne).toBe(true);
164
+ expect(isScheduledTwo).toBe(true);
165
+ expect(isPollScheduledOne.isScheduled).toBe(true);
166
+ expect(isPollScheduledTwo.isScheduled).toBe(true);
167
+ expect(isPollOneTimeoutScheduled).toBe(true);
168
+ expect(isPollTwoTimeoutScheduled).toBe(true);
169
+ });
170
+ test("should schedule finalization after the poll has ended", async () => {
171
+ const endedPoll = { ...scheduledPoll, endDate: 1 };
172
+ const { isScheduled } = await service.registerPoll(endedPoll);
173
+ const isPollScheduled = await service.isPollScheduled(endedPoll);
174
+ const isPollTimeoutScheduled = schedulerRegistry.doesExist("timeout", getPollKeyFromObject(endedPoll));
175
+ expect(isScheduled).toBe(true);
176
+ expect(isPollScheduled.isScheduled).toBe(true);
177
+ expect(isPollTimeoutScheduled).toBe(true);
178
+ });
179
+ test("should throw an error if poll is already tallied", async () => {
180
+ getPoll.mockResolvedValueOnce({
181
+ isMerged: true,
182
+ });
183
+ isTallied.mockResolvedValueOnce(true);
184
+ await expect(service.registerPoll(scheduledPoll)).rejects.toThrow(ErrorCodes.POLL_ALREADY_TALLIED.toString());
185
+ });
186
+ test("should throw an error if poll is already scheduled", async () => {
187
+ await service.registerPoll(scheduledPoll);
188
+ await expect(service.registerPoll(scheduledPoll)).rejects.toThrow(ErrorCodes.POLL_ALREADY_SCHEDULED.toString());
189
+ });
190
+ });
191
+ describe("deleteScheduledPoll", () => {
192
+ test("should delete a scheduled poll", async () => {
193
+ await service.registerPoll(scheduledPoll);
194
+ const isPollScheduled = await service.isPollScheduled(scheduledPoll);
195
+ const { isScheduled } = await service.deleteScheduledPoll(scheduledPoll);
196
+ const isPollDeleted = await service.isPollScheduled(scheduledPoll);
197
+ expect(isPollScheduled.isScheduled).toBe(true);
198
+ expect(isScheduled).toBe(false);
199
+ expect(isPollDeleted.isScheduled).toBe(false);
200
+ });
201
+ test("should return isPollScheduled as false if poll was not scheduled", async () => {
202
+ const { isScheduled } = await service.deleteScheduledPoll(scheduledPoll);
203
+ expect(isScheduled).toBe(false);
204
+ });
205
+ });
206
+ describe("restoreTimeouts", () => {
207
+ const polls = [
208
+ { ...scheduledPoll, pollId: "1" },
209
+ { ...scheduledPoll, pollId: "2" },
210
+ { ...scheduledPoll, pollId: "3" },
211
+ ];
212
+ beforeEach(async () => {
213
+ await Promise.all([
214
+ service.registerPoll(polls[0]),
215
+ service.registerPoll(polls[1]),
216
+ service.registerPoll(polls[2]),
217
+ ]);
218
+ schedulerRegistry.deleteTimeout(getPollKeyFromObject(polls[0]));
219
+ schedulerRegistry.deleteTimeout(getPollKeyFromObject(polls[1]));
220
+ schedulerRegistry.deleteTimeout(getPollKeyFromObject(polls[2]));
221
+ redisService.getAll.mockResolvedValue(polls.map((poll) => getPollKeyFromObject(poll)));
222
+ redisService.get.mockImplementation((key) => {
223
+ const poll = polls.find((p) => getPollKeyFromObject(p) === key);
224
+ const result = poll ? JSON.stringify(poll) : null;
225
+ return Promise.resolve(result);
226
+ });
227
+ });
228
+ afterEach(() => {
229
+ jest.clearAllMocks();
230
+ getPoll.mockReset().mockResolvedValue({
231
+ address: "0x123",
232
+ endDate: 1752534000,
233
+ isMerged: false,
234
+ });
235
+ isTallied.mockReset().mockResolvedValue(false);
236
+ });
237
+ test("should restore scheduled polls from Redis", async () => {
238
+ await service.onModuleInit();
239
+ const timeouts = schedulerRegistry.getTimeouts();
240
+ timeouts.forEach((timeout, i) => {
241
+ expect(timeout).toBe(getPollKeyFromObject(polls[i]));
242
+ });
243
+ expect(timeouts.length).toBe(polls.length);
244
+ });
245
+ test("should not restore timeouts for tallied polls", async () => {
246
+ getPoll.mockImplementation(({ pollId }) => Promise.resolve({ isMerged: pollId === 2n }));
247
+ isTallied.mockImplementation(({ pollId }) => Promise.resolve(pollId === "2"));
248
+ await service.onModuleInit();
249
+ const timeouts = schedulerRegistry.getTimeouts();
250
+ expect(timeouts.length).toBe(polls.length - 1);
251
+ });
252
+ test("should not throw if Redis returns null for a key and should delete register", async () => {
253
+ redisService.get.mockImplementation((key) => {
254
+ const poll = polls.find((p) => getPollKeyFromObject(p) === key && key !== getPollKeyFromObject(polls[1])); // null to simulate missing poll 2 key
255
+ const result = poll ? JSON.stringify(poll) : null;
256
+ return Promise.resolve(result);
257
+ });
258
+ await service.onModuleInit();
259
+ const timeouts = schedulerRegistry.getTimeouts();
260
+ expect(timeouts.length).toBe(polls.length - 1);
261
+ });
262
+ });
263
+ });
264
+ //# sourceMappingURL=scheduler.service.test.js.map