@@ -6,6 +6,8 @@ import { SecurityAnalyzer } from "../analyzers/SecurityAnalyzer.js";
66import { ProxyDetector } from "../analyzers/ProxyDetector.js" ;
77import { ScanHistory } from "../services/ScanHistory.js" ;
88import { AdvancedSimulator } from "../analyzers/AdvancedSimulator.js" ;
9+ import { OpcodeTracer } from "../analyzers/OpcodeTracer.js" ;
10+ import { ExplanationEngine } from "../analyzers/ExplanationEngine.js" ;
911
1012export class EvmExecutor {
1113 constructor ( ) { }
@@ -114,16 +116,26 @@ export class EvmExecutor {
114116 let sstoreCount = 0 ;
115117 let callCount = 0 ;
116118
119+ const tracer = new OpcodeTracer ( ) ;
120+
117121 evm . events . on ( 'step' , ( data : any ) => {
118122 if ( instructionCount === 0 ) console . log ( "[Fork] First opcode executed:" , data . opcode . name ) ;
119123 instructionCount ++ ;
120124 if ( data . opcode . name === 'SSTORE' ) sstoreCount ++ ;
121125 if ( [ 'CALL' , 'DELEGATECALL' , 'STATICCALL' , 'CALLCODE' ] . includes ( data . opcode . name ) ) callCount ++ ;
126+
127+ // [PHASE 3] Dynamic Capability Tracing
128+ tracer . handleStep ( data ) ;
122129 } ) ;
123130
124131 console . log ( `Executing Call: from=${ sender . toString ( ) } value=${ txParams . value } ` ) ;
125132 const { status, result } = await this . executeCall ( evm , txParams , sender ) ;
126133
134+ // [PHASE 3] Generate Mechanism Story
135+ const traceResult = tracer . getTrace ( ) ;
136+ const mechanismStory = ExplanationEngine . generateExplanation ( traceResult , status ) ;
137+ console . log ( "[Phase3] Mechanism Story:" , mechanismStory . story ) ;
138+
127139 console . log ( "EVM Execution Complete." ) ;
128140 console . log ( `Analysis: ${ instructionCount } steps, ${ sstoreCount } sstores` ) ;
129141
@@ -176,6 +188,10 @@ export class EvmExecutor {
176188 console . log ( "Running Security Checks on:" , addressToAnalyze . toString ( ) ) ;
177189 securityReport = await SecurityAnalyzer . analyze ( evm , addressToAnalyze , { status } , activeProvider ) ;
178190
191+ // [PHASE 3] Attach Detective Insights
192+ securityReport . mechanismStory = mechanismStory ;
193+ securityReport . tracingEvents = traceResult . events ;
194+
179195 if ( proxyInfo ) {
180196 securityReport . proxyInfo = proxyInfo ;
181197 if ( proxyInfo . isProxy ) {
@@ -237,6 +253,33 @@ export class EvmExecutor {
237253 console . warn ( "[Phase2] Advanced simulation failed:" , advErr . message ) ;
238254 }
239255
256+ // [PHASE 3 Refinement] Reconcile Phase 2 and Phase 3
257+ // If Phase 2 detected a scam (Revert/Honeypot) but Phase 3 trace (local) was Safe, it's likely due to missing storage.
258+ // We trust Phase 2 (RPC-based) more for outcomes.
259+ if ( advancedAnalysis && advancedAnalysis . isScam && securityReport . mechanismStory . severity === 'Safe' ) {
260+ console . log ( "[Phase3] Reconciling: Overwriting Safe story with Phase 2 detection." ) ;
261+
262+ if ( advancedAnalysis . counterfactual . hasOwnerPrivileges ) {
263+ securityReport . mechanismStory = {
264+ title : "Privilege Abuse Detected" ,
265+ story : "🕵️ The Detective noticed a discrepancy: Expected safe execution, but real-world simulation confirms only the OWNER can trade. This is a clear Honeypot." ,
266+ severity : "High"
267+ } ;
268+ } else if ( advancedAnalysis . timeTravel . isTimeSensitive ) {
269+ securityReport . mechanismStory = {
270+ title : "Hidden Time-Lock" ,
271+ story : "🕵️ The Detective found that while code looks clean, it relies on time checks (likely uninitialized in scan) that strictly block trading." ,
272+ severity : "High"
273+ } ;
274+ } else {
275+ securityReport . mechanismStory = {
276+ title : "Hidden Revert Mechanism" ,
277+ story : "🕵️ The execution path is misleading. Deep simulation confirms this transaction WILL fail for you, likely due to hidden storage dependencies." ,
278+ severity : "High"
279+ } ;
280+ }
281+ }
282+
240283 console . log ( "Security Report:" , securityReport ) ;
241284
242285 const chainIdNum = typeof chainId === 'string' && chainId . includes ( ':' ) ? parseInt ( chainId . split ( ':' ) [ 1 ] ) : Number ( chainId ) ;
0 commit comments