11import { toBech32 } from '@cosmjs/encoding' ;
2+ import { Map } from 'immutable' ;
3+ import { Err , Ok , Result } from 'ts-results' ;
24import type { CWSimulateApp } from '../../CWSimulateApp' ;
3-
5+ import { NEVER_IMMUTIFY , Transactional , TransactionalLens } from '../../store/transactional' ;
46import {
57 AppResponse ,
68 Binary ,
@@ -11,19 +13,18 @@ import {
1113 ContractResponse ,
1214 Event ,
1315 ExecuteEnv ,
16+ ExecuteTraceLog ,
1417 ReplyMsg ,
1518 ReplyOn ,
19+ ReplyTraceLog ,
1620 SubMsg ,
1721 TraceLog ,
1822 DebugLog ,
1923 Snapshot ,
2024} from '../../types' ;
21- import { Map } from 'immutable' ;
22- import { Err , Ok , Result } from 'ts-results' ;
2325import { fromBinary , toBinary } from '../../util' ;
24- import { NEVER_IMMUTIFY , Transactional , TransactionalLens } from '../../store/transactional' ;
25- import { buildAppResponse , buildContractAddress } from './wasm-util' ;
2626import Contract from './contract' ;
27+ import { buildAppResponse , buildContractAddress } from './wasm-util' ;
2728
2829type WasmData = {
2930 lastCodeId : number ;
@@ -209,16 +210,31 @@ export class WasmModule {
209210 trace : TraceLog [ ] = [ ]
210211 ) : Promise < Result < AppResponse , string > > {
211212 return await this . chain . pushBlock ( async ( ) => {
212- const snapshot = this . store . db . data ;
213-
214213 // first register the contract instance
215214 const contractAddress = this . registerContractInstance ( sender , codeId , label ) . unwrap ( ) ;
216215 let logs = [ ] as DebugLog [ ] ;
217216
218217 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+ }
219228
220229 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+ }
222238
223239 // then call instantiate
224240 let response = contract . instantiate (
@@ -229,23 +245,12 @@ export class WasmModule {
229245 ) ;
230246
231247 if ( response . err ) {
232- let result = Err ( response . val ) ;
233248 trace . push ( {
234- [ NEVER_IMMUTIFY ] : true ,
235- type : 'instantiate' as 'instantiate' ,
236- contractAddress,
237- msg : instantiateMsg ,
249+ ...tracebase ,
238250 response,
239- info : {
240- sender,
241- funds,
242- } ,
243- env : this . getExecutionEnv ( contractAddress ) ,
244- logs,
245- storeSnapshot : snapshot ,
246- result,
251+ result : response ,
247252 } ) ;
248- return result ;
253+ return response ;
249254 }
250255 else {
251256 let customEvent : Event = {
@@ -271,20 +276,11 @@ export class WasmModule {
271276 ) ;
272277
273278 trace . push ( {
274- [ NEVER_IMMUTIFY ] : true ,
275- type : 'instantiate' as 'instantiate' ,
276- contractAddress,
277- msg : instantiateMsg ,
279+ ...tracebase ,
278280 response,
279- info : {
280- sender,
281- funds,
282- } ,
283- env : this . getExecutionEnv ( contractAddress ) ,
284- logs,
281+ result,
285282 trace : subtrace ,
286283 storeSnapshot : this . store . db . data ,
287- result,
288284 } ) ;
289285
290286 return result ;
@@ -301,13 +297,29 @@ export class WasmModule {
301297 trace : TraceLog [ ] = [ ]
302298 ) : Promise < Result < AppResponse , string > > {
303299 return await this . chain . pushBlock ( async ( ) => {
304- const snapshot = this . store . db . data ;
305300 const contract = await this . getContract ( contractAddress ) . init ( ) ;
306- const env = contract . getExecutionEnv ( ) ;
307301 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+ }
308313
309314 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+ }
311323
312324 const response = contract . execute (
313325 sender ,
@@ -317,25 +329,12 @@ export class WasmModule {
317329 ) ;
318330
319331 if ( response . err ) {
320- const result = Err ( response . val ) ;
321-
322332 trace . push ( {
323- [ NEVER_IMMUTIFY ] : true ,
324- type : 'execute' as 'execute' ,
325- contractAddress,
326- msg : executeMsg ,
333+ ...tracebase ,
327334 response,
328- env,
329- info : {
330- sender,
331- funds,
332- } ,
333- logs,
334- storeSnapshot : snapshot ,
335- result,
335+ result : response ,
336336 } ) ;
337-
338- return result ;
337+ return response ;
339338 }
340339 else {
341340 let customEvent = {
@@ -363,20 +362,11 @@ export class WasmModule {
363362 ) ;
364363
365364 trace . push ( {
366- [ NEVER_IMMUTIFY ] : true ,
367- type : 'execute' as 'execute' ,
368- contractAddress,
369- msg : executeMsg ,
365+ ...tracebase ,
370366 response,
371- info : {
372- sender,
373- funds,
374- } ,
375- env,
367+ result,
376368 trace : subtrace ,
377- logs,
378369 storeSnapshot : this . store . db . data ,
379- result,
380370 } ) ;
381371
382372 return result ;
@@ -490,25 +480,25 @@ export class WasmModule {
490480 ) : Promise < Result < AppResponse , string > > {
491481 const logs : DebugLog [ ] = [ ] ;
492482 const contract = this . getContract ( contractAddress ) ;
493- const env = contract . getExecutionEnv ( ) ;
494483 const response = contract . reply ( replyMsg , logs ) ;
495484
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+
496495 if ( response . err ) {
497- const result = Err ( response . val ) ;
498-
499496 trace . push ( {
500- [ NEVER_IMMUTIFY ] : true ,
501- type : 'reply' as 'reply' ,
502- contractAddress,
503- env,
504- msg : replyMsg ,
497+ ...tracebase ,
505498 response,
506- logs,
507- storeSnapshot : this . store . db . data ,
508- result,
499+ result : response ,
509500 } ) ;
510-
511- return result ;
501+ return response ;
512502 }
513503 else {
514504 const customEvent = {
@@ -541,16 +531,10 @@ export class WasmModule {
541531 ) ;
542532
543533 trace . push ( {
544- [ NEVER_IMMUTIFY ] : true ,
545- type : 'reply' as 'reply' ,
546- contractAddress,
547- msg : replyMsg ,
548- env,
534+ ...tracebase ,
549535 response,
550- trace : subtrace ,
551- logs,
552- storeSnapshot : this . store . db . data ,
553536 result,
537+ storeSnapshot : this . store . db . data ,
554538 } ) ;
555539
556540 return result ;
@@ -659,6 +643,15 @@ export class WasmModule {
659643 return storage ? lensFromSnapshot ( storage ) : this . store ;
660644 }
661645
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+
662655 get lastCodeId ( ) { return this . store . get ( 'lastCodeId' ) }
663656 get lastInstanceId ( ) { return this . store . get ( 'lastInstanceId' ) }
664657}
0 commit comments