@lunarislab/state-sync 1.1.17 → 1.1.19

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.
package/classes/Syncer.js CHANGED
@@ -4,6 +4,7 @@ exports.Syncer = void 0;
4
4
  const Watcher_1 = require("./Watcher");
5
5
  const Saver_1 = require("./Saver");
6
6
  const SimpleEmitter_1 = require("./SimpleEmitter");
7
+ const viem_1 = require("viem");
7
8
  class Syncer extends SimpleEmitter_1.SimpleEmitter {
8
9
  constructor(client, config) {
9
10
  super();
@@ -11,8 +12,9 @@ class Syncer extends SimpleEmitter_1.SimpleEmitter {
11
12
  this.logs = [];
12
13
  this.lastPush = 0;
13
14
  this.commitInterval = config.commitInterval || 100;
15
+ const checksumed = config.contracts.map(c => (0, viem_1.checksumAddress)(c));
14
16
  this.watcher = new Watcher_1.Watcher(client, {
15
- contracts: config.contracts,
17
+ contracts: checksumed,
16
18
  event: config.event
17
19
  });
18
20
  this.saver = new Saver_1.Saver(client, {
@@ -37,17 +39,21 @@ class Syncer extends SimpleEmitter_1.SimpleEmitter {
37
39
  this.emit('log', { log: log });
38
40
  this.logs.push(log);
39
41
  });
40
- await this.watcher.start();
42
+ const env = await this.saver.getEnv();
43
+ const fromBlock = BigInt(env.lastBlockNum);
44
+ await this.watcher.start(fromBlock);
41
45
  this._commit();
42
46
  }
43
47
  ;
44
48
  async addContracts(contracts) {
45
- await this.watcher.addContracts(contracts);
49
+ const checksumed = contracts.map(c => (0, viem_1.checksumAddress)(c));
50
+ await this.watcher.addContracts(checksumed);
46
51
  }
47
52
  ;
48
53
  async removeContracts(contracts) {
49
- await this.watcher.removeContracts(contracts);
50
- for (const contract of contracts) {
54
+ const checksumed = contracts.map(c => (0, viem_1.checksumAddress)(c));
55
+ await this.watcher.removeContracts(checksumed);
56
+ for (const contract of checksumed) {
51
57
  await this.saver.db.del(contract);
52
58
  }
53
59
  ;
@@ -11,13 +11,15 @@ export declare class Watcher<signature extends EventSignature> {
11
11
  protected client: StateSync;
12
12
  protected emitter: EventEmitter;
13
13
  protected currentBlockNum: bigint;
14
- protected contracts: Address[];
14
+ protected contracts: Set<Address>;
15
15
  protected event: EventSignature;
16
16
  protected requestInterval: number;
17
17
  protected started: boolean;
18
+ protected concurrency: number;
18
19
  constructor(client: StateSync, config: IWatcherConfig<signature>);
20
+ protected getContracts(): `0x${string}`[];
19
21
  protected _getLogs(contracts: Address[], fromBlock?: bigint): Promise<void>;
20
- start(): Promise<void>;
22
+ start(fromBlock?: bigint): Promise<void>;
21
23
  addContracts(contracts: Address[]): Promise<void>;
22
24
  removeContracts(contracts: Address[]): Promise<void>;
23
25
  on<Evt extends keyof IWatcherEvents>(event: Evt, cb: IWatcherEvents[Evt]): EventEmitter;
@@ -12,18 +12,23 @@ class Watcher {
12
12
  this.client = client;
13
13
  this.currentBlockNum = 0n;
14
14
  this.started = false;
15
+ this.concurrency = 0;
15
16
  this.emitter = new events_1.EventEmitter();
16
- this.contracts = config.contracts || [];
17
+ this.contracts = new Set(config.contracts) || new Set();
17
18
  this.requestInterval = config.requestInterval || 50;
18
19
  this.event = config.event;
19
20
  this.client.on("newBlock", ({ block }) => this.currentBlockNum = block.number);
20
21
  }
21
22
  ;
23
+ getContracts() {
24
+ return Array.from(this.contracts);
25
+ }
22
26
  async _getLogs(contracts, fromBlock = 0n) {
23
27
  let gap = this.currentBlockNum;
24
28
  let block = fromBlock;
29
+ this.concurrency++;
25
30
  while (block < this.currentBlockNum) {
26
- await new Promise(x => setTimeout(x, this.requestInterval));
31
+ await new Promise(x => setTimeout(x, this.requestInterval * this.concurrency));
27
32
  try {
28
33
  const logs = await this.client.public.getLogs({
29
34
  fromBlock: block,
@@ -42,18 +47,20 @@ class Watcher {
42
47
  continue;
43
48
  }
44
49
  }
50
+ ;
51
+ this.concurrency--;
45
52
  }
46
53
  ;
47
- async start() {
54
+ async start(fromBlock) {
48
55
  if (this.started)
49
56
  throw new Error("Already started");
50
57
  this.currentBlockNum = await this.client.public.getBlockNumber();
51
58
  this.started = true;
52
- this._getLogs(this.contracts).then(() => {
59
+ this._getLogs(this.getContracts(), fromBlock).then(() => {
53
60
  this.client.on("newBlock", async ({ block }) => {
54
61
  const logs = await this.client.public.getLogs({
55
62
  blockHash: block.hash,
56
- address: this.contracts,
63
+ address: this.getContracts(),
57
64
  event: (0, viem_1.parseAbiItem)(this.event),
58
65
  });
59
66
  logs.forEach(l => this.emitter.emit("log", l));
@@ -62,18 +69,13 @@ class Watcher {
62
69
  }
63
70
  ;
64
71
  async addContracts(contracts) {
65
- await this._getLogs(contracts);
66
- this.contracts.push(...contracts);
72
+ if (this.started)
73
+ await this._getLogs(contracts);
74
+ this.contracts.forEach(c => this.contracts.add(c));
67
75
  }
68
76
  ;
69
77
  async removeContracts(contracts) {
70
- for (const contract of contracts) {
71
- const index = this.contracts.indexOf(contract);
72
- if (index === -1)
73
- continue;
74
- this.contracts.splice(index, 1);
75
- }
76
- ;
78
+ contracts.forEach(c => this.contracts.delete(c));
77
79
  }
78
80
  ;
79
81
  on(event, cb) {
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "viem": "^2.21.58"
9
9
  },
10
10
  "name": "@lunarislab/state-sync",
11
- "version": "1.1.17",
11
+ "version": "1.1.19",
12
12
  "main": "index.js",
13
13
  "scripts": {
14
14
  "test": "ts-node ./test.ts",
@@ -4,7 +4,7 @@ import { Log } from "./Log";
4
4
  export interface IWatcherConfig<signature extends EventSignature> {
5
5
  event: signature;
6
6
  requestInterval?: number;
7
- contracts?: Address[];
7
+ contracts: Address[];
8
8
  }
9
9
  export interface IWatcherEvents {
10
10
  log: (log: Log<any>) => void;