-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBaseAbstractClient.ts
102 lines (92 loc) · 3.08 KB
/
BaseAbstractClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { providers } from "ethers";
import { CachingMechanismInterface } from "../interfaces";
import { EventSearchConfig, isDefined, MakeOptional } from "../utils";
import { SVMProvider } from "../arch/svm";
export enum UpdateFailureReason {
NotReady,
AlreadyUpdated,
BadRequest,
RPCError,
}
export function isUpdateFailureReason(x: EventSearchConfig | UpdateFailureReason): x is UpdateFailureReason {
return Number.isInteger(x);
}
/**
* Base class for all clients to extend.
*/
export abstract class BaseAbstractClient {
protected _isUpdated: boolean;
public firstHeightToSearch = 0;
public latestHeightSearched = 0;
/**
* Creates a new client.
* @param cachingMechanism The caching mechanism to use for this client. If not provided, the client will not rely on an external cache.
*/
constructor(
readonly eventSearchConfig: MakeOptional<EventSearchConfig, "to"> = { from: 0, maxLookBack: 0 },
protected cachingMechanism?: CachingMechanismInterface
) {
this._isUpdated = false;
}
/**
* Indicates whether the client has been updated since it was created.
* @returns Whether the client has been updated since it was created.
*/
public get isUpdated(): boolean {
return this._isUpdated;
}
/**
* Sets whether the client has been updated since it was created.
* @param value Whether the client has been updated since it was created.
* @throws Error if the client has already been updated and the value is false.
*/
public set isUpdated(value: boolean) {
if (this._isUpdated === true && !value) {
throw new Error("Cannot set isUpdated to false once it is true");
}
this._isUpdated = value;
}
/**
* Validates and updates the stored EventSearchConfig in advance of an update() call.
* Use isEventSearchConfig() to discriminate the result.
* @provider Ethers RPC provider instance.
* @returns An EventSearchConfig instance if valid, otherwise an UpdateFailureReason.
*/
public async updateSearchConfig(
provider: providers.Provider | SVMProvider
): Promise<EventSearchConfig | UpdateFailureReason> {
const from = this.firstHeightToSearch;
let { to } = this.eventSearchConfig;
if (isDefined(to)) {
if (from > to) {
throw new Error(`Invalid event search config from (${from}) > to (${to})`);
}
} else {
if (provider instanceof providers.Provider) {
to = await provider.getBlockNumber();
} else {
to = Number(await provider.getBlockHeight({ commitment: "confirmed" }).send());
}
if (to < from) {
return UpdateFailureReason.AlreadyUpdated;
}
}
const { maxLookBack } = this.eventSearchConfig;
return { from, to, maxLookBack };
}
/**
* Asserts that the client has been updated.
*/
protected assertUpdated(): void {
if (!this.isUpdated) {
throw new Error("Client not updated");
}
}
/**
* Determines if the client has an external cache.
* @returns Whether the client has an external cache.
*/
protected hasCachingMechanism(): boolean {
return isDefined(this.cachingMechanism);
}
}