-
Notifications
You must be signed in to change notification settings - Fork 3
/
ReadContractStub.ts
289 lines (268 loc) · 9.04 KB
/
ReadContractStub.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { Abi } from 'abitype';
import stringify from 'fast-safe-stringify';
import { SinonStub, stub } from 'sinon';
import {
ContractDecodeFunctionDataArgs,
ContractEncodeFunctionDataArgs,
ContractGetEventsArgs,
ContractGetEventsOptions,
ContractReadArgs,
ContractReadOptions,
ContractWriteArgs,
ContractWriteOptions,
ReadContract,
} from 'src/contract/types/Contract';
import { Event, EventName } from 'src/contract/types/Event';
import {
DecodedFunctionData,
FunctionArgs,
FunctionName,
FunctionReturn,
} from 'src/contract/types/Function';
/**
* A mock implementation of a `ReadContract` designed to facilitate unit
* testing. The `ReadContractStub` provides a way to stub out specific
* contract read, write, and event-fetching behaviors, allowing tests to focus
* on the business logic of the SDK.
*
* @example
* const contract = new ReadContractStub(ERC20ABI);
* contract.stubRead("baseToken", "0x123abc");
*
* const value = await contract.read("baseToken", []); // "0x123abc"
*
*/
export class ReadContractStub<TAbi extends Abi = Abi>
implements ReadContract<TAbi>
{
abi;
address = '0x0000000000000000000000000000000000000000' as const;
// Maps to store stubs for different contract methods based on their name.
protected readStubMap = new Map<
FunctionName<TAbi>,
ReadStub<TAbi, FunctionName<TAbi>>
>();
protected eventsStubMap = new Map<
EventName<TAbi>,
EventsStub<TAbi, EventName<TAbi>>
>();
protected simulateWriteStubMap = new Map<
FunctionName<TAbi, 'nonpayable' | 'payable'>,
SimulateWriteStub<TAbi, FunctionName<TAbi, 'nonpayable' | 'payable'>>
>();
constructor(abi: TAbi = [] as any) {
this.abi = abi;
}
/**
* Simulates a contract read operation for a given function. If the function
* is not previously stubbed using `stubRead`, an error will be thrown.
*/
async read<TFunctionName extends FunctionName<TAbi>>(
...[functionName, args, options]: ContractReadArgs<TAbi, TFunctionName>
): Promise<FunctionReturn<TAbi, TFunctionName>> {
const stub = this.getReadStub(functionName);
if (!stub) {
throw new Error(
`Called read for ${functionName} on a stubbed contract without a return value. The function must be stubbed first:\n\tcontract.stubRead("${functionName}", value)`,
);
}
return stub(args, options);
}
/**
* Simulates a contract write operation for a given function. If the function
* is not previously stubbed using `stubWrite`, an error will be thrown.
*/
async simulateWrite<
TFunctionName extends FunctionName<TAbi, 'nonpayable' | 'payable'>,
>(
...[functionName, args, options]: ContractWriteArgs<TAbi, TFunctionName>
): Promise<FunctionReturn<TAbi, TFunctionName>> {
const stub = this.getSimulateWriteStub(functionName);
if (!stub) {
throw new Error(
`Called simulateWrite for ${functionName} on a stubbed contract without a return value. The function must be stubbed first:\n\tcontract.stubWrite("${functionName}", value)`,
);
}
return stub(args, options);
}
/**
* Simulates fetching events for a given event name from the contract. If the
* event name is not previously stubbed using `stubEvents`, an error will be
* thrown.
*/
async getEvents<TEventName extends EventName<TAbi>>(
...[eventName, options]: ContractGetEventsArgs<TAbi, TEventName>
): Promise<Event<TAbi, TEventName>[]> {
const stub = this.getEventsStub(eventName, options);
if (!stub) {
throw new Error(
`Called getEvents for ${eventName} on a stubbed contract without a return value. The function must be stubbed first:\n\tcontract.stubEvents("${eventName}", value)`,
);
}
return stub(options);
}
/**
* Stubs the return value for a given function when `read` is called with that
* function name. This method overrides any previously stubbed values for the
* same function.
*/
stubRead<TFunctionName extends FunctionName<TAbi>>({
functionName,
args,
value,
options,
}: {
functionName: TFunctionName;
args?: FunctionArgs<TAbi, TFunctionName>;
value: FunctionReturn<TAbi, TFunctionName>;
options?: ContractReadOptions;
}): void {
let readStub = this.readStubMap.get(functionName);
if (!readStub) {
readStub = stub();
this.readStubMap.set(functionName, readStub);
}
// Account for dynamic args if provided
if (args || options) {
// The stub returned from the map doesn't have a strong FunctionName type
// so we have to cast to avoid contravariance errors with the args.
(readStub as ReadStub<TAbi, TFunctionName>)
.withArgs(args, options)
.resolves(value);
return;
}
readStub.resolves(value);
}
/**
* Stubs the return value for a given function when `simulateWrite` is called
* with that function name. This method overrides any previously stubbed
* values for the same function.
*
* *Note: The stub doesn't account for dynamic values based on provided
* arguments/options.*
*/
stubSimulateWrite<
TFunctionName extends FunctionName<TAbi, 'nonpayable' | 'payable'>,
>(
functionName: TFunctionName,
value: FunctionReturn<TAbi, TFunctionName>,
): void {
let simulateWriteStub = this.simulateWriteStubMap.get(functionName);
if (!simulateWriteStub) {
simulateWriteStub = stub();
this.simulateWriteStubMap.set(functionName, simulateWriteStub);
}
simulateWriteStub.resolves(value);
}
/**
* Stubs the return value for a given event name when `getEvents` is called
* with that event name. This method overrides any previously stubbed values
* for the same event.
*/
stubEvents<TEventName extends EventName<TAbi>>(
eventName: TEventName,
args: ContractGetEventsOptions<TAbi, TEventName> | undefined,
value: Event<TAbi, TEventName>[],
): void {
const stubKey = stableStringify({ eventName, args });
if (this.eventsStubMap.has(stubKey)) {
this.getEventsStub(eventName, args)!.resolves(value as any);
} else {
this.eventsStubMap.set(stubKey, stub().resolves(value) as any);
}
}
/**
* Retrieves the stub associated with a read function name.
* Useful for assertions in testing, such as checking call counts.
*/
getReadStub<TFunctionName extends FunctionName<TAbi>>(
functionName: TFunctionName,
): ReadStub<TAbi, TFunctionName> | undefined {
return this.readStubMap.get(functionName) as
| ReadStub<TAbi, TFunctionName>
| undefined;
}
/**
* Retrieves the stub associated with a write function name.
* Useful for assertions in testing, such as checking call counts.
*/
getSimulateWriteStub<
TFunctionName extends FunctionName<TAbi, 'nonpayable' | 'payable'>,
>(
functionName: TFunctionName,
): SimulateWriteStub<TAbi, TFunctionName> | undefined {
return this.simulateWriteStubMap.get(functionName) as
| SimulateWriteStub<TAbi, TFunctionName>
| undefined;
}
/**
* Retrieves the stub associated with an event name.
* Useful for assertions in testing, such as checking call counts.
*/
getEventsStub<TEventName extends EventName<TAbi>>(
eventName: TEventName,
args?: ContractGetEventsOptions<TAbi, TEventName> | undefined,
): EventsStub<TAbi, TEventName> | undefined {
const stubKey = stableStringify({ eventName, args });
return this.eventsStubMap.get(stubKey) as
| EventsStub<TAbi, TEventName>
| undefined;
}
// TODO:
decodeFunctionData<
TFunctionName extends FunctionName<TAbi> = FunctionName<TAbi>,
>(
...args: ContractDecodeFunctionDataArgs
): DecodedFunctionData<TAbi, TFunctionName> {
throw new Error('Method not implemented.');
}
// TODO:
encodeFunctionData<
TFunctionName extends FunctionName<TAbi> = FunctionName<TAbi>,
>(
...args: ContractEncodeFunctionDataArgs<TAbi, TFunctionName>
): `0x${string}` {
throw new Error('Method not implemented.');
}
}
/**
* Type representing a stub for the "read" function of a contract.
*/
type ReadStub<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi>,
> = SinonStub<
[args?: FunctionArgs<TAbi, TFunctionName>, options?: ContractReadOptions],
Promise<FunctionReturn<TAbi, TFunctionName>>
>;
/**
* Type representing a stub for the "getEvents" function of a contract.
*/
type EventsStub<
TAbi extends Abi,
TEventName extends EventName<TAbi>,
> = SinonStub<
[options?: ContractGetEventsOptions<TAbi, TEventName>],
Promise<Event<TAbi, TEventName>[]>
>;
/**
* Type representing a stub for the "write" and "simulateWrite" functions of a
* contract.
*/
type SimulateWriteStub<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi, 'nonpayable' | 'payable'>,
> = SinonStub<
[
args?: FunctionArgs<TAbi, TFunctionName> | undefined,
options?: ContractWriteOptions,
],
Promise<FunctionReturn<TAbi, TFunctionName>>
>;
function stableStringify(obj: Record<any, any>) {
// simple non-recursive stringify replacer for bigints
function replacer(_: any, v: any) {
return typeof v === 'bigint' ? v.toString() : v;
}
return stringify.stableStringify(obj, replacer);
}