Skip to content

Commit

Permalink
Added safe and finalized provider events (#3921).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Nov 11, 2023
1 parent cf00331 commit a92766e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src.ts/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
import { Network } from "./network.js";
import { copyRequest, Block, FeeData, Log, TransactionReceipt, TransactionResponse } from "./provider.js";
import {
PollingBlockSubscriber, PollingEventSubscriber, PollingOrphanSubscriber, PollingTransactionSubscriber
PollingBlockSubscriber, PollingBlockTagSubscriber, PollingEventSubscriber,
PollingOrphanSubscriber, PollingTransactionSubscriber
} from "./subscriber-polling.js";

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

if (typeof(_event) === "string") {
switch (_event) {
case "block": case "pending": case "debug": case "error": case "network": {
case "block":
case "debug":
case "error":
case "finalized":
case "network":
case "pending":
case "safe": {
return { type: _event, tag: _event };
}
}
Expand Down Expand Up @@ -708,7 +715,10 @@ export class AbstractProvider implements Provider {
switch (blockTag) {
case "earliest":
return "0x0";
case "latest": case "pending": case "safe": case "finalized":
case "finalized":
case "latest":
case "pending":
case "safe":
return blockTag;
}

Expand Down Expand Up @@ -1319,6 +1329,8 @@ export class AbstractProvider implements Provider {
subscriber.pollingInterval = this.pollingInterval;
return subscriber;
}
case "safe": case "finalized":
return new PollingBlockTagSubscriber(this, sub.type);
case "event":
return new PollingEventSubscriber(this, sub.filter);
case "transaction":
Expand Down
30 changes: 30 additions & 0 deletions src.ts/providers/subscriber-polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class PollingBlockSubscriber implements Subscriber {
}
}


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

export class PollingBlockTagSubscriber extends OnBlockSubscriber {
readonly #tag: string;
#lastBlock: number;

constructor(provider: AbstractProvider, tag: string) {
super(provider);
this.#tag = tag;
this.#lastBlock = -2;
}

pause(dropWhilePaused?: boolean): void {
if (dropWhilePaused) { this.#lastBlock = -2; }
super.pause(dropWhilePaused);
}

async _poll(blockNumber: number, provider: AbstractProvider): Promise<void> {
const block = await provider.getBlock(this.#tag);
if (block == null) { return; }

if (this.#lastBlock === -2) {
this.#lastBlock = block.number;
} else if (block.number > this.#lastBlock) {
provider.emit(this.#tag, block.number);
this.#lastBlock = block.number;
}
}
}


/**
* @_ignore:
*
Expand Down

0 comments on commit a92766e

Please sign in to comment.