@waku/rln 0.1.5-aaa7a0c.0 → 0.1.5-bc093d3.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.
@@ -17,10 +17,7 @@ const RATE_LIMIT_TIERS = {
17
17
  // Global rate limit parameters
18
18
  const RATE_LIMIT_PARAMS = {
19
19
  MIN_RATE: RATE_LIMIT_TIERS.LOW,
20
- MAX_RATE: RATE_LIMIT_TIERS.HIGH,
21
- MAX_TOTAL_RATE: 160_000, // Maximum total rate limit across all memberships
22
- EPOCH_LENGTH: 600 // Epoch length in seconds (10 minutes)
23
- };
20
+ MAX_RATE: RATE_LIMIT_TIERS.HIGH};
24
21
  const DEFAULT_RATE_LIMIT = RATE_LIMIT_PARAMS.MAX_RATE;
25
22
 
26
23
  export { DEFAULT_RATE_LIMIT, LINEA_CONTRACT, RATE_LIMIT_PARAMS, RATE_LIMIT_TIERS };
@@ -19,6 +19,7 @@ import { MembershipState } from './types.js';
19
19
  import { Contract } from '../../../../node_modules/@ethersproject/contracts/lib.esm/index.js';
20
20
  import { BigNumber } from '../../../../node_modules/@ethersproject/bignumber/lib.esm/bignumber.js';
21
21
 
22
+ /* eslint-disable no-console */
22
23
  const log = new Logger("waku:rln:contract:base");
23
24
  class RLNBaseContract {
24
25
  contract;
@@ -33,6 +34,22 @@ class RLNBaseContract {
33
34
  * Allows injecting a mocked contract for testing purposes.
34
35
  */
35
36
  constructor(options) {
37
+ const { address, signer, rateLimit = DEFAULT_RATE_LIMIT, contract } = options;
38
+ log.info("Initializing RLNBaseContract", { address, rateLimit });
39
+ this.contract = contract || new Contract(address, RLN_ABI, signer);
40
+ this.rateLimit = rateLimit;
41
+ try {
42
+ log.info("Setting up event filters");
43
+ // Initialize event filters
44
+ this._membersFilter = this.contract.filters.MembershipRegistered();
45
+ this._membershipErasedFilter = this.contract.filters.MembershipErased();
46
+ this._membersExpiredFilter = this.contract.filters.MembershipExpired();
47
+ log.info("Event filters initialized successfully");
48
+ }
49
+ catch (error) {
50
+ log.error("Failed to initialize event filters", { error });
51
+ throw new Error("Failed to initialize event filters: " + error.message);
52
+ }
36
53
  // Initialize members and subscriptions
37
54
  this.fetchMembers()
38
55
  .then(() => {
@@ -41,14 +58,10 @@ class RLNBaseContract {
41
58
  .catch((error) => {
42
59
  log.error("Failed to initialize members", { error });
43
60
  });
44
- const { address, signer, rateLimit = DEFAULT_RATE_LIMIT, contract } = options;
45
- this.validateRateLimit(rateLimit);
46
- this.contract = contract || new Contract(address, RLN_ABI, signer);
47
- this.rateLimit = rateLimit;
48
- // Initialize event filters
49
- this._membersFilter = this.contract.filters.MembershipRegistered();
50
- this._membershipErasedFilter = this.contract.filters.MembershipErased();
51
- this._membersExpiredFilter = this.contract.filters.MembershipExpired();
61
+ // Validate rate limit asynchronously
62
+ this.validateRateLimit(rateLimit).catch((error) => {
63
+ log.error("Failed to validate initial rate limit", { error });
64
+ });
52
65
  }
53
66
  /**
54
67
  * Gets the current rate limit for this contract instance
@@ -116,7 +129,7 @@ class RLNBaseContract {
116
129
  * @param newRateLimit The new rate limit to use
117
130
  */
118
131
  async setRateLimit(newRateLimit) {
119
- this.validateRateLimit(newRateLimit);
132
+ await this.validateRateLimit(newRateLimit);
120
133
  this.rateLimit = newRateLimit;
121
134
  }
122
135
  get members() {
@@ -247,53 +260,42 @@ class RLNBaseContract {
247
260
  this.processEvents([event]);
248
261
  });
249
262
  }
250
- /**
251
- * Helper method to get remaining messages in current epoch
252
- * @param membershipId The ID of the membership to check
253
- * @returns number of remaining messages allowed in current epoch
254
- */
255
- async getRemainingMessages(membershipId) {
263
+ async getMembershipInfo(idCommitmentBigInt) {
256
264
  try {
257
- const [startTime, , rateLimit] = await this.contract.getMembershipInfo(membershipId);
258
- // Calculate current epoch
259
- const currentTime = Math.floor(Date.now() / 1000);
260
- const epochsPassed = Math.floor((currentTime - startTime) / RATE_LIMIT_PARAMS.EPOCH_LENGTH);
261
- const currentEpochStart = startTime + epochsPassed * RATE_LIMIT_PARAMS.EPOCH_LENGTH;
262
- // Get message count in current epoch using contract's function
263
- const messageCount = await this.contract.getMessageCount(membershipId, currentEpochStart);
264
- return Math.max(0, BigNumber.from(rateLimit)
265
- .sub(BigNumber.from(messageCount))
266
- .toNumber());
267
- }
268
- catch (error) {
269
- log.error(`Error getting remaining messages: ${error.message}`);
270
- return 0; // Fail safe: assume no messages remaining on error
271
- }
272
- }
273
- async getMembershipInfo(idCommitment) {
274
- try {
275
- const [startBlock, endBlock, rateLimit] = await this.contract.getMembershipInfo(idCommitment);
265
+ console.log("idCommitmentBigInt", idCommitmentBigInt);
266
+ const membershipData = await this.contract.memberships(idCommitmentBigInt);
276
267
  const currentBlock = await this.contract.provider.getBlockNumber();
268
+ console.log("membershipData", membershipData);
277
269
  let state;
278
- if (currentBlock < startBlock) {
270
+ const gracePeriodEnd = membershipData.gracePeriodStartTimestamp.add(membershipData.gracePeriodDuration);
271
+ console.log("gracePeriodEnd", gracePeriodEnd);
272
+ if (currentBlock < membershipData.gracePeriodStartTimestamp) {
279
273
  state = MembershipState.Active;
280
274
  }
281
- else if (currentBlock < endBlock) {
275
+ else if (currentBlock < gracePeriodEnd) {
282
276
  state = MembershipState.GracePeriod;
283
277
  }
284
278
  else {
285
279
  state = MembershipState.Expired;
286
280
  }
287
- const index = await this.getMemberIndex(idCommitment);
288
- if (!index)
289
- return undefined;
281
+ console.log("state", state);
282
+ console.log("membershipData.index", membershipData.index);
283
+ console.log("membershipData.idCommitment", membershipData.idCommitment);
284
+ console.log("membershipData.rateLimit", membershipData.rateLimit);
285
+ console.log("membershipData.gracePeriodStartTimestamp", membershipData.gracePeriodStartTimestamp);
286
+ console.log("gracePeriodEnd", gracePeriodEnd);
290
287
  return {
291
- index,
292
- idCommitment,
293
- rateLimit: rateLimit.toNumber(),
294
- startBlock: startBlock.toNumber(),
295
- endBlock: endBlock.toNumber(),
296
- state
288
+ index: membershipData.index,
289
+ idCommitment: membershipData.idCommitment,
290
+ rateLimit: membershipData.rateLimit.toNumber(),
291
+ startBlock: membershipData.gracePeriodStartTimestamp.toNumber(),
292
+ endBlock: gracePeriodEnd.toNumber(),
293
+ state,
294
+ depositAmount: membershipData.depositAmount,
295
+ activeDuration: membershipData.activeDuration,
296
+ gracePeriodDuration: membershipData.gracePeriodDuration,
297
+ holder: membershipData.holder,
298
+ token: membershipData.token
297
299
  };
298
300
  }
299
301
  catch (error) {
@@ -313,9 +315,9 @@ class RLNBaseContract {
313
315
  }
314
316
  return this.contract.register(idCommitment, rateLimit, []);
315
317
  }
316
- async withdraw(token, holder) {
318
+ async withdraw(token) {
317
319
  try {
318
- const tx = await this.contract.withdraw(token, { from: holder });
320
+ const tx = await this.contract.withdraw(token);
319
321
  await tx.wait();
320
322
  }
321
323
  catch (error) {
@@ -435,10 +437,15 @@ class RLNBaseContract {
435
437
  * Validates that the rate limit is within the allowed range
436
438
  * @throws Error if the rate limit is outside the allowed range
437
439
  */
438
- validateRateLimit(rateLimit) {
439
- if (rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
440
- rateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
441
- throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE} messages per epoch`);
440
+ async validateRateLimit(rateLimit) {
441
+ const [minRate, maxRate] = await Promise.all([
442
+ this.contract.minMembershipRateLimit(),
443
+ this.contract.maxMembershipRateLimit()
444
+ ]);
445
+ const minRateNum = BigNumber.from(minRate).toNumber();
446
+ const maxRateNum = BigNumber.from(maxRate).toNumber();
447
+ if (rateLimit < minRateNum || rateLimit > maxRateNum) {
448
+ throw new Error(`Rate limit must be between ${minRateNum} and ${maxRateNum} messages per epoch`);
442
449
  }
443
450
  }
444
451
  get membersFilter() {
@@ -177,7 +177,7 @@ class Keystore {
177
177
  treeIndex: _.get(obj, "treeIndex"),
178
178
  chainId: _.get(obj, "membershipContract.chainId"),
179
179
  address: _.get(obj, "membershipContract.address"),
180
- rateLimit: _.get(obj, "membershipContract.rateLimit")
180
+ rateLimit: _.get(obj, "userMessageLimit")
181
181
  }
182
182
  };
183
183
  }
@@ -219,7 +219,8 @@ class Keystore {
219
219
  membershipContract: {
220
220
  chainId: options.membership.chainId,
221
221
  address: options.membership.address
222
- }
222
+ },
223
+ userMessageLimit: options.membership.rateLimit
223
224
  }));
224
225
  }
225
226
  }