github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/evm.go (about)

     1  package evm
     2  
     3  import (
     4  	"github.com/onflow/cadence/runtime"
     5  	"github.com/onflow/cadence/runtime/common"
     6  
     7  	"github.com/onflow/flow-go/fvm/environment"
     8  	"github.com/onflow/flow-go/fvm/evm/backends"
     9  	evm "github.com/onflow/flow-go/fvm/evm/emulator"
    10  	"github.com/onflow/flow-go/fvm/evm/handler"
    11  	"github.com/onflow/flow-go/fvm/evm/stdlib"
    12  	"github.com/onflow/flow-go/fvm/systemcontracts"
    13  	"github.com/onflow/flow-go/model/flow"
    14  )
    15  
    16  func ContractAccountAddress(chainID flow.ChainID) (flow.Address, error) {
    17  	sc := systemcontracts.SystemContractsForChain(chainID)
    18  	return sc.EVMContract.Address, nil
    19  }
    20  
    21  func StorageAccountAddress(chainID flow.ChainID) (flow.Address, error) {
    22  	sc := systemcontracts.SystemContractsForChain(chainID)
    23  	return sc.EVMStorage.Address, nil
    24  }
    25  
    26  func RandomBeaconAddress(chainID flow.ChainID) flow.Address {
    27  	return systemcontracts.SystemContractsForChain(chainID).RandomBeaconHistory.Address
    28  }
    29  
    30  func SetupEnvironment(
    31  	chainID flow.ChainID,
    32  	fvmEnv environment.Environment,
    33  	runtimeEnv runtime.Environment,
    34  	flowToken flow.Address,
    35  ) error {
    36  	evmStorageAccountAddress, err := StorageAccountAddress(chainID)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	evmContractAccountAddress, err := ContractAccountAddress(chainID)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	randomBeaconAddress := RandomBeaconAddress(chainID)
    47  
    48  	backend := backends.NewWrappedEnvironment(fvmEnv)
    49  
    50  	emulator := evm.NewEmulator(backend, evmStorageAccountAddress)
    51  
    52  	blockStore := handler.NewBlockStore(backend, evmStorageAccountAddress)
    53  
    54  	addressAllocator := handler.NewAddressAllocator()
    55  
    56  	contractHandler := handler.NewContractHandler(
    57  		chainID,
    58  		evmContractAccountAddress,
    59  		common.Address(flowToken),
    60  		randomBeaconAddress,
    61  		blockStore,
    62  		addressAllocator,
    63  		backend,
    64  		emulator,
    65  	)
    66  
    67  	stdlib.SetupEnvironment(
    68  		runtimeEnv,
    69  		contractHandler,
    70  		evmContractAccountAddress,
    71  	)
    72  
    73  	return nil
    74  }