@lunarislab/state-sync 1.1.1 → 1.1.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.
@@ -1,6 +1,5 @@
1
1
  import { PublicClient, WalletClient } from "viem";
2
2
  import { IClientConfig, IStateSyncEvents } from "../types/Client";
3
- import { Watcher } from "./Watcher";
4
3
  import { EventSignature } from "abitype/dist/types/human-readable/types/signatures";
5
4
  import { Syncer } from "./Syncer";
6
5
  import { ISyncerConfig } from "../types/Syncer";
@@ -14,8 +13,6 @@ export declare class StateSync extends SimpleEmitter<IStateSyncEvents> {
14
13
  wallet: WalletClient;
15
14
  level: LevelDBConstructor;
16
15
  levelPath: string;
17
- timeout: number;
18
- protected watchers: Map<string, Watcher>;
19
16
  constructor(config: IClientConfig);
20
17
  createSyncer<signature extends EventSignature>(config: ISyncerConfig<signature>): Promise<Syncer<signature>>;
21
18
  }
package/classes/Client.js CHANGED
@@ -9,12 +9,10 @@ const SimpleEmitter_1 = require("./SimpleEmitter");
9
9
  class StateSync extends SimpleEmitter_1.SimpleEmitter {
10
10
  constructor(config) {
11
11
  super();
12
- this.watchers = new Map();
13
12
  this.public = config.public;
14
13
  this.wallet = config.wallet;
15
14
  this.level = config.level;
16
15
  this.levelPath = config.levelPath || "";
17
- this.timeout = config.timeout || 50;
18
16
  this.public.watchBlocks({
19
17
  onBlock: (block) => {
20
18
  this.emit("newBlock", { block });
@@ -10,7 +10,7 @@ export declare class Syncer<signature extends EventSignature> extends SimpleEmit
10
10
  protected client: StateSync;
11
11
  protected logs: Log<signature>[];
12
12
  protected lastPush: number;
13
- protected watcher: Watcher;
13
+ protected watcher: Watcher<signature>;
14
14
  protected saver: Saver<signature>;
15
15
  protected commitInterval: number;
16
16
  constructor(client: StateSync, config: ISyncerConfig<signature>);
@@ -7,14 +7,15 @@ import { IWatcherConfig, IWatcherEvents } from "../types/Watcher";
7
7
  * Watcher class is a reader class that watch events from the blockchain and send them as events via event emitter.
8
8
  * Events can then be received by the "Saver" class witch will save them in a local DB.
9
9
  */
10
- export declare class Watcher {
10
+ export declare class Watcher<signature extends EventSignature> {
11
11
  protected client: StateSync;
12
12
  protected emitter: EventEmitter;
13
13
  protected currentBlockNum: bigint;
14
14
  protected contracts: Address[];
15
15
  protected event: EventSignature;
16
+ protected requestInterval: number;
16
17
  protected started: boolean;
17
- constructor(client: StateSync, config: IWatcherConfig);
18
+ constructor(client: StateSync, config: IWatcherConfig<signature>);
18
19
  protected _getLogs(contracts: Address[], fromBlock?: bigint): Promise<void>;
19
20
  start(): void;
20
21
  addContracts(contracts: Address[]): Promise<void>;
@@ -14,6 +14,7 @@ class Watcher {
14
14
  this.started = false;
15
15
  this.emitter = new events_1.EventEmitter();
16
16
  this.contracts = config.contracts || [];
17
+ this.requestInterval = config.requestInterval || 50;
17
18
  this.event = config.event;
18
19
  this.client.on("newBlock", ({ block }) => this.currentBlockNum = block.number);
19
20
  }
@@ -22,6 +23,7 @@ class Watcher {
22
23
  let gap = this.currentBlockNum;
23
24
  let block = fromBlock;
24
25
  while (block < this.currentBlockNum) {
26
+ await new Promise(x => setTimeout(x, this.requestInterval));
25
27
  try {
26
28
  const logs = await this.client.public.getLogs({
27
29
  fromBlock: block,
package/package.json CHANGED
@@ -8,12 +8,13 @@
8
8
  "viem": "^2.21.58"
9
9
  },
10
10
  "name": "@lunarislab/state-sync",
11
- "version": "1.1.1",
11
+ "version": "1.1.2",
12
12
  "main": "index.js",
13
13
  "scripts": {
14
14
  "test": "ts-node ./test.ts",
15
15
  "build": "tsc && cp package.json dist/ && cp README.md dist/",
16
- "sendNpm": "cd ./dist && npm version patch && npm publish --access=public"
16
+ "sendNpm": "cd ./dist && npm version patch && npm publish --access=public",
17
+ "publish": "tsc && cp package.json dist/ && cp README.md dist/ && cd ./dist && npm publish --access=public && cd ../ && npm version patch"
17
18
  },
18
19
  "author": "",
19
20
  "license": "ISC",
package/types/Syncer.d.ts CHANGED
@@ -3,7 +3,7 @@ import { ISaverConfig } from "./Saver";
3
3
  import { EventSignature } from "abitype/dist/types/human-readable/types/signatures";
4
4
  import { IWatcherConfig } from "./Watcher";
5
5
  import { Log } from "./Log";
6
- export type ISyncerConfig<signature extends EventSignature> = ISaverConfig<signature> & IWatcherConfig & {
6
+ export type ISyncerConfig<signature extends EventSignature> = ISaverConfig<signature> & IWatcherConfig<signature> & {
7
7
  commitInterval?: number;
8
8
  };
9
9
  export interface ISyncerEvents<signature extends EventSignature> {
@@ -1,8 +1,9 @@
1
1
  import { EventSignature } from "abitype/dist/types/human-readable/types/signatures";
2
2
  import { Address } from "viem";
3
3
  import { Log } from "./Log";
4
- export interface IWatcherConfig {
5
- event: EventSignature;
4
+ export interface IWatcherConfig<signature extends EventSignature> {
5
+ event: signature;
6
+ requestInterval?: number;
6
7
  contracts?: Address[];
7
8
  }
8
9
  export interface IWatcherEvents {