Skip to content

Commit a92766e

Browse files
committed
Added safe and finalized provider events (#3921).
1 parent cf00331 commit a92766e

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src.ts/providers/abstract-provider.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
import { Network } from "./network.js";
3737
import { copyRequest, Block, FeeData, Log, TransactionReceipt, TransactionResponse } from "./provider.js";
3838
import {
39-
PollingBlockSubscriber, PollingEventSubscriber, PollingOrphanSubscriber, PollingTransactionSubscriber
39+
PollingBlockSubscriber, PollingBlockTagSubscriber, PollingEventSubscriber,
40+
PollingOrphanSubscriber, PollingTransactionSubscriber
4041
} from "./subscriber-polling.js";
4142

4243
import type { Addressable, AddressLike } from "../address/index.js";
@@ -127,7 +128,7 @@ export type DebugEventAbstractProvider = {
127128
* if they are modifying a low-level feature of how subscriptions operate.
128129
*/
129130
export type Subscription = {
130-
type: "block" | "close" | "debug" | "error" | "network" | "pending",
131+
type: "block" | "close" | "debug" | "error" | "finalized" | "network" | "pending" | "safe",
131132
tag: string
132133
} | {
133134
type: "transaction",
@@ -235,7 +236,13 @@ async function getSubscription(_event: ProviderEvent, provider: AbstractProvider
235236

236237
if (typeof(_event) === "string") {
237238
switch (_event) {
238-
case "block": case "pending": case "debug": case "error": case "network": {
239+
case "block":
240+
case "debug":
241+
case "error":
242+
case "finalized":
243+
case "network":
244+
case "pending":
245+
case "safe": {
239246
return { type: _event, tag: _event };
240247
}
241248
}
@@ -708,7 +715,10 @@ export class AbstractProvider implements Provider {
708715
switch (blockTag) {
709716
case "earliest":
710717
return "0x0";
711-
case "latest": case "pending": case "safe": case "finalized":
718+
case "finalized":
719+
case "latest":
720+
case "pending":
721+
case "safe":
712722
return blockTag;
713723
}
714724

@@ -1319,6 +1329,8 @@ export class AbstractProvider implements Provider {
13191329
subscriber.pollingInterval = this.pollingInterval;
13201330
return subscriber;
13211331
}
1332+
case "safe": case "finalized":
1333+
return new PollingBlockTagSubscriber(this, sub.type);
13221334
case "event":
13231335
return new PollingEventSubscriber(this, sub.filter);
13241336
case "transaction":

src.ts/providers/subscriber-polling.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export class PollingBlockSubscriber implements Subscriber {
113113
}
114114
}
115115

116+
116117
/**
117118
* An **OnBlockSubscriber** can be sub-classed, with a [[_poll]]
118119
* implmentation which will be called on every new block.
@@ -161,6 +162,35 @@ export class OnBlockSubscriber implements Subscriber {
161162
resume(): void { this.start(); }
162163
}
163164

165+
export class PollingBlockTagSubscriber extends OnBlockSubscriber {
166+
readonly #tag: string;
167+
#lastBlock: number;
168+
169+
constructor(provider: AbstractProvider, tag: string) {
170+
super(provider);
171+
this.#tag = tag;
172+
this.#lastBlock = -2;
173+
}
174+
175+
pause(dropWhilePaused?: boolean): void {
176+
if (dropWhilePaused) { this.#lastBlock = -2; }
177+
super.pause(dropWhilePaused);
178+
}
179+
180+
async _poll(blockNumber: number, provider: AbstractProvider): Promise<void> {
181+
const block = await provider.getBlock(this.#tag);
182+
if (block == null) { return; }
183+
184+
if (this.#lastBlock === -2) {
185+
this.#lastBlock = block.number;
186+
} else if (block.number > this.#lastBlock) {
187+
provider.emit(this.#tag, block.number);
188+
this.#lastBlock = block.number;
189+
}
190+
}
191+
}
192+
193+
164194
/**
165195
* @_ignore:
166196
*

0 commit comments

Comments
 (0)