1
1
import type { AbstractExecutionService } from '@metamask/snaps-controllers' ;
2
2
import type { SnapId } from '@metamask/snaps-sdk' ;
3
- import type { InstallSnapOptions } from '@metamask/snaps-simulation' ;
4
- import {
5
- JsonRpcMockOptionsStruct ,
6
- SignatureOptionsStruct ,
7
- handleRequest ,
8
- TransactionOptionsStruct ,
9
- addJsonRpcMock ,
10
- removeJsonRpcMock ,
11
- SnapResponseWithInterfaceStruct ,
12
- } from '@metamask/snaps-simulation' ;
13
- import { HandlerType , logInfo } from '@metamask/snaps-utils' ;
14
- import { create } from '@metamask/superstruct' ;
15
- import { assertStruct , createModuleLogger } from '@metamask/utils' ;
3
+ import type { InstallSnapOptions , Snap } from '@metamask/snaps-simulation' ;
4
+ import { logInfo } from '@metamask/snaps-utils' ;
5
+ import { createModuleLogger } from '@metamask/utils' ;
16
6
17
7
import { rootLogger , getEnvironment } from './internals' ;
18
- import type {
19
- SnapResponseWithInterface ,
20
- CronjobOptions ,
21
- JsonRpcMockOptions ,
22
- Snap ,
23
- SnapResponse ,
24
- TransactionOptions ,
25
- } from './types' ;
26
8
27
9
const log = createModuleLogger ( rootLogger , 'helpers' ) ;
28
10
@@ -48,17 +30,6 @@ function getOptions<
48
30
return [ snapId , options ] ;
49
31
}
50
32
51
- /**
52
- * Ensure that the actual response contains `getInterface`.
53
- *
54
- * @param response - The response of the handler.
55
- */
56
- function assertIsResponseWithInterface (
57
- response : SnapResponse ,
58
- ) : asserts response is SnapResponseWithInterface {
59
- assertStruct ( response , SnapResponseWithInterfaceStruct ) ;
60
- }
61
-
62
33
/**
63
34
* Load a snap into the environment. This is the main entry point for testing
64
35
* snaps: It returns a {@link Snap} object that can be used to interact with the
@@ -200,154 +171,33 @@ export async function installSnap<
200
171
) : Promise < Snap > {
201
172
const resolvedOptions = getOptions ( snapId , options ) ;
202
173
const {
203
- snapId : installedSnapId ,
204
- store,
205
- executionService,
206
- runSaga,
207
- controllerMessenger,
174
+ request,
175
+ onTransaction,
176
+ sendTransaction,
177
+ onSignature,
178
+ onCronjob,
179
+ runCronjob,
180
+ onHomePage,
181
+ mockJsonRpc,
182
+ close,
208
183
} = await getEnvironment ( ) . installSnap ( ...resolvedOptions ) ;
209
184
210
- const onTransaction = async (
211
- request : TransactionOptions ,
212
- ) : Promise < SnapResponseWithInterface > => {
213
- log ( 'Sending transaction %o.' , request ) ;
214
-
215
- const {
216
- origin : transactionOrigin ,
217
- chainId,
218
- ...transaction
219
- } = create ( request , TransactionOptionsStruct ) ;
220
-
221
- const response = await handleRequest ( {
222
- snapId : installedSnapId ,
223
- store,
224
- executionService,
225
- runSaga,
226
- controllerMessenger,
227
- handler : HandlerType . OnTransaction ,
228
- request : {
229
- method : '' ,
230
- params : {
231
- chainId,
232
- transaction,
233
- transactionOrigin,
234
- } ,
235
- } ,
236
- } ) ;
237
-
238
- assertIsResponseWithInterface ( response ) ;
239
-
240
- return response ;
241
- } ;
242
-
243
- const onCronjob = ( request : CronjobOptions ) => {
244
- log ( 'Running cronjob %o.' , options ) ;
245
-
246
- return handleRequest ( {
247
- snapId : installedSnapId ,
248
- store,
249
- executionService,
250
- controllerMessenger,
251
- runSaga,
252
- handler : HandlerType . OnCronjob ,
253
- request,
254
- } ) ;
255
- } ;
256
-
257
185
return {
258
- request : ( request ) => {
259
- log ( 'Sending request %o.' , request ) ;
260
-
261
- return handleRequest ( {
262
- snapId : installedSnapId ,
263
- store,
264
- executionService,
265
- controllerMessenger,
266
- runSaga,
267
- handler : HandlerType . OnRpcRequest ,
268
- request,
269
- } ) ;
270
- } ,
271
-
186
+ request,
272
187
onTransaction,
273
- sendTransaction : onTransaction ,
274
-
275
- onSignature : async (
276
- request : unknown ,
277
- ) : Promise < SnapResponseWithInterface > => {
278
- log ( 'Requesting signature %o.' , request ) ;
279
-
280
- const { origin : signatureOrigin , ...signature } = create (
281
- request ,
282
- SignatureOptionsStruct ,
283
- ) ;
284
-
285
- const response = await handleRequest ( {
286
- snapId : installedSnapId ,
287
- store,
288
- executionService,
289
- controllerMessenger,
290
- runSaga,
291
- handler : HandlerType . OnSignature ,
292
- request : {
293
- method : '' ,
294
- params : {
295
- signature,
296
- signatureOrigin,
297
- } ,
298
- } ,
299
- } ) ;
300
-
301
- assertIsResponseWithInterface ( response ) ;
302
-
303
- return response ;
304
- } ,
305
-
188
+ sendTransaction,
189
+ onSignature,
306
190
onCronjob,
307
- runCronjob : onCronjob ,
308
-
309
- onHomePage : async ( ) : Promise < SnapResponseWithInterface > => {
310
- log ( 'Rendering home page.' ) ;
311
-
312
- const response = await handleRequest ( {
313
- snapId : installedSnapId ,
314
- store,
315
- executionService,
316
- controllerMessenger,
317
- runSaga,
318
- handler : HandlerType . OnHomePage ,
319
- request : {
320
- method : '' ,
321
- } ,
322
- } ) ;
323
-
324
- assertIsResponseWithInterface ( response ) ;
325
-
326
- return response ;
327
- } ,
328
-
329
- mockJsonRpc ( mock : JsonRpcMockOptions ) {
330
- log ( 'Mocking JSON-RPC request %o.' , mock ) ;
331
-
332
- const { method, result } = create ( mock , JsonRpcMockOptionsStruct ) ;
333
- store . dispatch ( addJsonRpcMock ( { method, result } ) ) ;
334
-
335
- return {
336
- unmock ( ) {
337
- log ( 'Unmocking JSON-RPC request %o.' , mock ) ;
338
-
339
- store . dispatch ( removeJsonRpcMock ( method ) ) ;
340
- } ,
341
- } ;
342
- } ,
343
-
191
+ runCronjob,
192
+ onHomePage,
193
+ mockJsonRpc,
344
194
close : async ( ) => {
345
195
log ( 'Closing execution service.' ) ;
346
196
logInfo (
347
197
'Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.' ,
348
198
) ;
349
199
350
- await executionService . terminateAllSnaps ( ) ;
200
+ await close ( ) ;
351
201
} ,
352
202
} ;
353
203
}
0 commit comments