This repository was archived by the owner on Oct 16, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathwithBlockTracker.ts
More file actions
182 lines (168 loc) · 5.46 KB
/
withBlockTracker.ts
File metadata and controls
182 lines (168 loc) · 5.46 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { providerFromEngine } from '@metamask/eth-json-rpc-provider';
import type {
// Eip1193Request,
SafeEventEmitterProvider,
} from '@metamask/eth-json-rpc-provider';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type { Json } from '@metamask/utils';
import util from 'util';
import type { PollingBlockTrackerOptions } from '../src';
import { PollingBlockTracker } from '../src';
type WithPollingBlockTrackerOptions = {
provider?: FakeProviderOptions;
blockTracker?: PollingBlockTrackerOptions;
};
type WithPollingBlockTrackerCallback = (args: {
provider: SafeEventEmitterProvider;
blockTracker: PollingBlockTracker;
}) => void | Promise<void>;
/**
* An object that allows specifying the behavior of a specific invocation of
* `request`. The `methodName` always identifies the stub, but the behavior
* may be specified multiple ways: `request` can either return a result
* or reject with an error.
*
* methodName - The RPC method to which this stub will be matched.
*
* result - Instructs `request` to return a result.
*
* implementation - Allows overriding `request` entirely. Useful if
* you want it to throw an error.
*
* error - Instructs `request` to return a promise that rejects with
* this error.
*/
type FakeProviderStub =
| {
methodName: string;
result: any;
}
| {
methodName: string;
implementation: () => void;
}
| {
methodName: string;
error: unknown;
};
/**
* The set of options that a new instance of FakeProvider takes.
*
* stubs - A set of objects that allow specifying the behavior
* of specific invocations of `request` matching a `methodName`.
*/
type FakeProviderOptions = {
stubs?: FakeProviderStub[];
};
/**
* Constructs a provider that returns fake responses for the various
* RPC methods that the provider supports can be supplied.
*
* @param options - The options.
* @param options.stubs - A set of objects that allow specifying the behavior
* of specific invocations of `request` matching a `methodName`.
* @returns The fake provider.
*/
function getFakeProvider({
stubs: initialStubs = [],
}: {
stubs?: FakeProviderStub[];
} = {}) {
const originalStubs = initialStubs.slice();
const stubs = initialStubs.slice();
if (!stubs.some((stub) => stub.methodName === 'eth_blockNumber')) {
stubs.push({
methodName: 'eth_blockNumber',
result: '0x0',
});
}
if (!stubs.some((stub) => stub.methodName === 'eth_subscribe')) {
stubs.push({
methodName: 'eth_subscribe',
result: '0x0',
});
}
if (!stubs.some((stub) => stub.methodName === 'eth_unsubscribe')) {
stubs.push({
methodName: 'eth_unsubscribe',
result: true,
});
}
const provider = providerFromEngine(new JsonRpcEngine());
jest
.spyOn(provider, 'request')
.mockImplementation(async (eip1193Request): Promise<Json> => {
const index = stubs.findIndex(
(stub) => stub.methodName === eip1193Request.method,
);
if (index !== -1) {
const stub = stubs[index];
stubs.splice(index, 1);
if ('implementation' in stub) {
stub.implementation();
} else if ('result' in stub) {
return stub.result;
} else if ('error' in stub) {
throw stub.error;
}
return null;
}
throw new Error(
`Could not find any stubs matching "${eip1193Request.method}". Perhaps they've already been called?\n\n` +
'The original set of stubs were:\n\n' +
`${util.inspect(originalStubs, { depth: null })}\n\n` +
'Current set of stubs:\n\n' +
`${util.inspect(stubs, { depth: null })}\n\n`,
);
});
return provider;
}
/**
* Calls the given function with a built-in PollingBlockTracker, ensuring that
* all listeners that are on the block tracker are removed and any timers or
* loops that are running within the block tracker are properly stopped.
*
* @param options - Options that allow configuring the block tracker or
* provider.
* @param callback - A callback which will be called with the built block
* tracker.
* @returns The provider and block tracker.
*/
export async function withPollingBlockTracker(
options: WithPollingBlockTrackerOptions,
callback: WithPollingBlockTrackerCallback,
): Promise<void>;
/**
* Calls the given function with a built-in PollingBlockTracker, ensuring that
* all listeners that are on the block tracker are removed and any timers or
* loops that are running within the block tracker are properly stopped.
*
* @param callback - A callback which will be called with the built block
* tracker.
* @returns The provider and block tracker.
*/
export async function withPollingBlockTracker(
callback: WithPollingBlockTrackerCallback,
): Promise<void>;
/* eslint-disable-next-line jsdoc/require-jsdoc */
export async function withPollingBlockTracker(
...args:
| [WithPollingBlockTrackerOptions, WithPollingBlockTrackerCallback]
| [WithPollingBlockTrackerCallback]
) {
const [options, callback] = args.length === 2 ? args : [{}, args[0]];
const provider =
options.provider === undefined
? getFakeProvider()
: getFakeProvider(options.provider);
const blockTrackerOptions =
options.blockTracker === undefined
? { provider }
: {
provider,
...options.blockTracker,
};
const blockTracker = new PollingBlockTracker(blockTrackerOptions);
const callbackArgs = { provider, blockTracker };
return await callback(callbackArgs);
}