@lunarislab/state-sync 1.1.16 → 1.1.18
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 +16 -14
- package/classes/Watcher.d.ts +3 -2
- package/classes/Watcher.js +9 -12
- package/package.json +1 -1
- package/types/Watcher.d.ts +1 -1
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:
|
17
|
+
contracts: checksumed,
|
16
18
|
event: config.event
|
17
19
|
});
|
18
20
|
this.saver = new Saver_1.Saver(client, {
|
@@ -23,35 +25,35 @@ class Syncer extends SimpleEmitter_1.SimpleEmitter {
|
|
23
25
|
}
|
24
26
|
;
|
25
27
|
async _commit() {
|
26
|
-
const date = Date.now();
|
27
|
-
// If the last push was less than "commitInterval" ago, don't commit
|
28
|
-
if (this.lastPush > date - this.commitInterval)
|
29
|
-
return;
|
30
|
-
this.lastPush = date;
|
31
|
-
// Wait for commit interval to avoid letting some logs in the queue
|
32
|
-
await new Promise(x => setTimeout(x, this.commitInterval));
|
33
|
-
// Clear queue and save logs
|
34
28
|
const logs = this.logs.splice(0, this.logs.length);
|
35
29
|
const commits = await this.saver.saveLogs(logs);
|
36
30
|
this.emit("commit", { commits });
|
31
|
+
// start the recursive commit process
|
32
|
+
await new Promise(x => setTimeout(x, this.commitInterval));
|
33
|
+
this._commit();
|
37
34
|
}
|
35
|
+
;
|
38
36
|
async start() {
|
39
37
|
await this.saver.init();
|
40
38
|
this.watcher.on('log', async (log) => {
|
41
39
|
this.emit('log', { log: log });
|
42
40
|
this.logs.push(log);
|
43
|
-
await this._commit();
|
44
41
|
});
|
45
|
-
await this.
|
42
|
+
const env = await this.saver.getEnv();
|
43
|
+
const fromBlock = BigInt(env.lastBlockNum);
|
44
|
+
await this.watcher.start(fromBlock);
|
45
|
+
this._commit();
|
46
46
|
}
|
47
47
|
;
|
48
48
|
async addContracts(contracts) {
|
49
|
-
|
49
|
+
const checksumed = contracts.map(c => (0, viem_1.checksumAddress)(c));
|
50
|
+
await this.watcher.addContracts(checksumed);
|
50
51
|
}
|
51
52
|
;
|
52
53
|
async removeContracts(contracts) {
|
53
|
-
|
54
|
-
|
54
|
+
const checksumed = contracts.map(c => (0, viem_1.checksumAddress)(c));
|
55
|
+
await this.watcher.removeContracts(checksumed);
|
56
|
+
for (const contract of checksumed) {
|
55
57
|
await this.saver.db.del(contract);
|
56
58
|
}
|
57
59
|
;
|
package/classes/Watcher.d.ts
CHANGED
@@ -11,13 +11,14 @@ 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
18
|
constructor(client: StateSync, config: IWatcherConfig<signature>);
|
19
|
+
protected getContracts(): `0x${string}`[];
|
19
20
|
protected _getLogs(contracts: Address[], fromBlock?: bigint): Promise<void>;
|
20
|
-
start(): Promise<void>;
|
21
|
+
start(fromBlock?: bigint): Promise<void>;
|
21
22
|
addContracts(contracts: Address[]): Promise<void>;
|
22
23
|
removeContracts(contracts: Address[]): Promise<void>;
|
23
24
|
on<Evt extends keyof IWatcherEvents>(event: Evt, cb: IWatcherEvents[Evt]): EventEmitter;
|
package/classes/Watcher.js
CHANGED
@@ -13,12 +13,15 @@ class Watcher {
|
|
13
13
|
this.currentBlockNum = 0n;
|
14
14
|
this.started = false;
|
15
15
|
this.emitter = new events_1.EventEmitter();
|
16
|
-
this.contracts = config.contracts ||
|
16
|
+
this.contracts = new Set(config.contracts) || new Set();
|
17
17
|
this.requestInterval = config.requestInterval || 50;
|
18
18
|
this.event = config.event;
|
19
19
|
this.client.on("newBlock", ({ block }) => this.currentBlockNum = block.number);
|
20
20
|
}
|
21
21
|
;
|
22
|
+
getContracts() {
|
23
|
+
return Array.from(this.contracts);
|
24
|
+
}
|
22
25
|
async _getLogs(contracts, fromBlock = 0n) {
|
23
26
|
let gap = this.currentBlockNum;
|
24
27
|
let block = fromBlock;
|
@@ -44,16 +47,16 @@ class Watcher {
|
|
44
47
|
}
|
45
48
|
}
|
46
49
|
;
|
47
|
-
async start() {
|
50
|
+
async start(fromBlock) {
|
48
51
|
if (this.started)
|
49
52
|
throw new Error("Already started");
|
50
53
|
this.currentBlockNum = await this.client.public.getBlockNumber();
|
51
54
|
this.started = true;
|
52
|
-
this._getLogs(this.
|
55
|
+
this._getLogs(this.getContracts(), fromBlock).then(() => {
|
53
56
|
this.client.on("newBlock", async ({ block }) => {
|
54
57
|
const logs = await this.client.public.getLogs({
|
55
58
|
blockHash: block.hash,
|
56
|
-
address: this.
|
59
|
+
address: this.getContracts(),
|
57
60
|
event: (0, viem_1.parseAbiItem)(this.event),
|
58
61
|
});
|
59
62
|
logs.forEach(l => this.emitter.emit("log", l));
|
@@ -63,17 +66,11 @@ class Watcher {
|
|
63
66
|
;
|
64
67
|
async addContracts(contracts) {
|
65
68
|
await this._getLogs(contracts);
|
66
|
-
this.contracts.
|
69
|
+
this.contracts.forEach(c => this.contracts.add(c));
|
67
70
|
}
|
68
71
|
;
|
69
72
|
async removeContracts(contracts) {
|
70
|
-
|
71
|
-
const index = this.contracts.indexOf(contract);
|
72
|
-
if (index === -1)
|
73
|
-
continue;
|
74
|
-
this.contracts.splice(index, 1);
|
75
|
-
}
|
76
|
-
;
|
73
|
+
contracts.forEach(c => this.contracts.delete(c));
|
77
74
|
}
|
78
75
|
;
|
79
76
|
on(event, cb) {
|
package/package.json
CHANGED
package/types/Watcher.d.ts
CHANGED
@@ -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
|
7
|
+
contracts: Address[];
|
8
8
|
}
|
9
9
|
export interface IWatcherEvents {
|
10
10
|
log: (log: Log<any>) => void;
|