1
1
import { toBech32 } from '@cosmjs/encoding' ;
2
+ import { Map } from 'immutable' ;
3
+ import { Err , Ok , Result } from 'ts-results' ;
2
4
import type { CWSimulateApp } from '../../CWSimulateApp' ;
3
-
5
+ import { NEVER_IMMUTIFY , Transactional , TransactionalLens } from '../../store/transactional' ;
4
6
import {
5
7
AppResponse ,
6
8
Binary ,
@@ -11,19 +13,18 @@ import {
11
13
ContractResponse ,
12
14
Event ,
13
15
ExecuteEnv ,
16
+ ExecuteTraceLog ,
14
17
ReplyMsg ,
15
18
ReplyOn ,
19
+ ReplyTraceLog ,
16
20
SubMsg ,
17
21
TraceLog ,
18
22
DebugLog ,
19
23
Snapshot ,
20
24
} from '../../types' ;
21
- import { Map } from 'immutable' ;
22
- import { Err , Ok , Result } from 'ts-results' ;
23
25
import { fromBinary , toBinary } from '../../util' ;
24
- import { NEVER_IMMUTIFY , Transactional , TransactionalLens } from '../../store/transactional' ;
25
- import { buildAppResponse , buildContractAddress } from './wasm-util' ;
26
26
import Contract from './contract' ;
27
+ import { buildAppResponse , buildContractAddress } from './wasm-util' ;
27
28
28
29
type WasmData = {
29
30
lastCodeId : number ;
@@ -209,16 +210,31 @@ export class WasmModule {
209
210
trace : TraceLog [ ] = [ ]
210
211
) : Promise < Result < AppResponse , string > > {
211
212
return await this . chain . pushBlock ( async ( ) => {
212
- const snapshot = this . store . db . data ;
213
-
214
213
// first register the contract instance
215
214
const contractAddress = this . registerContractInstance ( sender , codeId , label ) . unwrap ( ) ;
216
215
let logs = [ ] as DebugLog [ ] ;
217
216
218
217
const contract = await this . getContract ( contractAddress ) . init ( ) ;
218
+ const tracebase : Omit < ExecuteTraceLog , 'response' | 'result' > = {
219
+ [ NEVER_IMMUTIFY ] : true ,
220
+ type : 'instantiate' ,
221
+ contractAddress,
222
+ msg : instantiateMsg ,
223
+ info : { sender, funds } ,
224
+ logs,
225
+ env : contract . getExecutionEnv ( ) ,
226
+ storeSnapshot : this . store . db . data ,
227
+ }
219
228
220
229
const send = this . chain . bank . send ( sender , contract . address , funds ) ;
221
- if ( send . err ) return send ;
230
+ if ( send . err ) {
231
+ trace . push ( {
232
+ ...tracebase ,
233
+ response : send ,
234
+ result : send ,
235
+ } ) ;
236
+ return send ;
237
+ }
222
238
223
239
// then call instantiate
224
240
let response = contract . instantiate (
@@ -229,23 +245,12 @@ export class WasmModule {
229
245
) ;
230
246
231
247
if ( response . err ) {
232
- let result = Err ( response . val ) ;
233
248
trace . push ( {
234
- [ NEVER_IMMUTIFY ] : true ,
235
- type : 'instantiate' as 'instantiate' ,
236
- contractAddress,
237
- msg : instantiateMsg ,
249
+ ...tracebase ,
238
250
response,
239
- info : {
240
- sender,
241
- funds,
242
- } ,
243
- env : this . getExecutionEnv ( contractAddress ) ,
244
- logs,
245
- storeSnapshot : snapshot ,
246
- result,
251
+ result : response ,
247
252
} ) ;
248
- return result ;
253
+ return response ;
249
254
}
250
255
else {
251
256
let customEvent : Event = {
@@ -271,20 +276,11 @@ export class WasmModule {
271
276
) ;
272
277
273
278
trace . push ( {
274
- [ NEVER_IMMUTIFY ] : true ,
275
- type : 'instantiate' as 'instantiate' ,
276
- contractAddress,
277
- msg : instantiateMsg ,
279
+ ...tracebase ,
278
280
response,
279
- info : {
280
- sender,
281
- funds,
282
- } ,
283
- env : this . getExecutionEnv ( contractAddress ) ,
284
- logs,
281
+ result,
285
282
trace : subtrace ,
286
283
storeSnapshot : this . store . db . data ,
287
- result,
288
284
} ) ;
289
285
290
286
return result ;
@@ -301,13 +297,29 @@ export class WasmModule {
301
297
trace : TraceLog [ ] = [ ]
302
298
) : Promise < Result < AppResponse , string > > {
303
299
return await this . chain . pushBlock ( async ( ) => {
304
- const snapshot = this . store . db . data ;
305
300
const contract = await this . getContract ( contractAddress ) . init ( ) ;
306
- const env = contract . getExecutionEnv ( ) ;
307
301
const logs : DebugLog [ ] = [ ] ;
302
+
303
+ const tracebase : Omit < ExecuteTraceLog , 'response' | 'result' > = {
304
+ [ NEVER_IMMUTIFY ] : true ,
305
+ type : 'execute' ,
306
+ contractAddress,
307
+ msg : executeMsg ,
308
+ logs,
309
+ env : contract . getExecutionEnv ( ) ,
310
+ info : { sender, funds } ,
311
+ storeSnapshot : this . store . db . data ,
312
+ }
308
313
309
314
const send = this . chain . bank . send ( sender , contractAddress , funds ) ;
310
- if ( send . err ) return send ;
315
+ if ( send . err ) {
316
+ trace . push ( {
317
+ ...tracebase ,
318
+ response : send ,
319
+ result : send ,
320
+ } ) ;
321
+ return send ;
322
+ }
311
323
312
324
const response = contract . execute (
313
325
sender ,
@@ -317,25 +329,12 @@ export class WasmModule {
317
329
) ;
318
330
319
331
if ( response . err ) {
320
- const result = Err ( response . val ) ;
321
-
322
332
trace . push ( {
323
- [ NEVER_IMMUTIFY ] : true ,
324
- type : 'execute' as 'execute' ,
325
- contractAddress,
326
- msg : executeMsg ,
333
+ ...tracebase ,
327
334
response,
328
- env,
329
- info : {
330
- sender,
331
- funds,
332
- } ,
333
- logs,
334
- storeSnapshot : snapshot ,
335
- result,
335
+ result : response ,
336
336
} ) ;
337
-
338
- return result ;
337
+ return response ;
339
338
}
340
339
else {
341
340
let customEvent = {
@@ -363,20 +362,11 @@ export class WasmModule {
363
362
) ;
364
363
365
364
trace . push ( {
366
- [ NEVER_IMMUTIFY ] : true ,
367
- type : 'execute' as 'execute' ,
368
- contractAddress,
369
- msg : executeMsg ,
365
+ ...tracebase ,
370
366
response,
371
- info : {
372
- sender,
373
- funds,
374
- } ,
375
- env,
367
+ result,
376
368
trace : subtrace ,
377
- logs,
378
369
storeSnapshot : this . store . db . data ,
379
- result,
380
370
} ) ;
381
371
382
372
return result ;
@@ -490,25 +480,25 @@ export class WasmModule {
490
480
) : Promise < Result < AppResponse , string > > {
491
481
const logs : DebugLog [ ] = [ ] ;
492
482
const contract = this . getContract ( contractAddress ) ;
493
- const env = contract . getExecutionEnv ( ) ;
494
483
const response = contract . reply ( replyMsg , logs ) ;
495
484
485
+ const tracebase : Omit < ReplyTraceLog , 'response' | 'result' > = {
486
+ [ NEVER_IMMUTIFY ] : true ,
487
+ type : 'reply' ,
488
+ contractAddress,
489
+ msg : replyMsg ,
490
+ env : contract . getExecutionEnv ( ) ,
491
+ logs,
492
+ storeSnapshot : this . store . db . data ,
493
+ }
494
+
496
495
if ( response . err ) {
497
- const result = Err ( response . val ) ;
498
-
499
496
trace . push ( {
500
- [ NEVER_IMMUTIFY ] : true ,
501
- type : 'reply' as 'reply' ,
502
- contractAddress,
503
- env,
504
- msg : replyMsg ,
497
+ ...tracebase ,
505
498
response,
506
- logs,
507
- storeSnapshot : this . store . db . data ,
508
- result,
499
+ result : response ,
509
500
} ) ;
510
-
511
- return result ;
501
+ return response ;
512
502
}
513
503
else {
514
504
const customEvent = {
@@ -541,16 +531,10 @@ export class WasmModule {
541
531
) ;
542
532
543
533
trace . push ( {
544
- [ NEVER_IMMUTIFY ] : true ,
545
- type : 'reply' as 'reply' ,
546
- contractAddress,
547
- msg : replyMsg ,
548
- env,
534
+ ...tracebase ,
549
535
response,
550
- trace : subtrace ,
551
- logs,
552
- storeSnapshot : this . store . db . data ,
553
536
result,
537
+ storeSnapshot : this . store . db . data ,
554
538
} ) ;
555
539
556
540
return result ;
@@ -659,6 +643,15 @@ export class WasmModule {
659
643
return storage ? lensFromSnapshot ( storage ) : this . store ;
660
644
}
661
645
646
+ protected pushTrace ( traces : TraceLog [ ] , details : Omit < TraceLog , typeof NEVER_IMMUTIFY | 'env' > ) {
647
+ //@ts -ignore
648
+ traces . push ( {
649
+ [ NEVER_IMMUTIFY ] : true ,
650
+ ...details ,
651
+ env : this . getExecutionEnv ( details . contractAddress ) ,
652
+ } ) ;
653
+ }
654
+
662
655
get lastCodeId ( ) { return this . store . get ( 'lastCodeId' ) }
663
656
get lastInstanceId ( ) { return this . store . get ( 'lastInstanceId' ) }
664
657
}
0 commit comments