diff --git a/fvm/script.go b/fvm/script.go index 9d173f886cd..038f4d03ebd 100644 --- a/fvm/script.go +++ b/fvm/script.go @@ -202,6 +202,11 @@ func (executor *scriptExecutor) executeScript() error { chainID := executor.ctx.Chain.ChainID() + // Setup InternalEVM in both environments because: + // - Scripts execute in ScriptRuntimeEnv + // - But system contract invocations (e.g., getAccount().balance) use TxRuntimeEnv + + // Setup InternalEVM in ScriptRuntimeEnv (for script execution) err := evm.SetupEnvironment( chainID, executor.env, @@ -211,6 +216,21 @@ func (executor *scriptExecutor) executeScript() error { return err } + // Setup InternalEVM in TxRuntimeEnv (for system contract invocations) + // This solves the problem where FlowServiceAccount (which imports EVM) needs + // InternalEVM to be available during dependency checking when invoked via + // system contracts (e.g., getAccount().balance). Without this, type checking + // fails with "cannot find variable in this scope: `InternalEVM`" because + // system contract invocations use TxRuntimeEnv, not ScriptRuntimeEnv. + err = evm.SetupEnvironment( + chainID, + executor.env, + rt.TxRuntimeEnv, + ) + if err != nil { + return err + } + value, err := rt.ExecuteScript( runtime.Script{ Source: executor.proc.Script,