github.com/onflow/flow-go@v0.33.17/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  	evm "github.com/onflow/flow-go/fvm/evm/emulator"
     8  	"github.com/onflow/flow-go/fvm/evm/handler"
     9  	"github.com/onflow/flow-go/fvm/evm/stdlib"
    10  	"github.com/onflow/flow-go/fvm/evm/types"
    11  	"github.com/onflow/flow-go/fvm/systemcontracts"
    12  	"github.com/onflow/flow-go/model/flow"
    13  )
    14  
    15  func ContractAccountAddress(chainID flow.ChainID) (flow.Address, error) {
    16  	sc := systemcontracts.SystemContractsForChain(chainID)
    17  	return sc.EVMContract.Address, nil
    18  }
    19  
    20  func StorageAccountAddress(chainID flow.ChainID) (flow.Address, error) {
    21  	sc := systemcontracts.SystemContractsForChain(chainID)
    22  	return sc.EVMStorage.Address, nil
    23  }
    24  
    25  func SetupEnvironment(
    26  	chainID flow.ChainID,
    27  	backend types.Backend,
    28  	env runtime.Environment,
    29  	service flow.Address,
    30  	flowToken flow.Address,
    31  ) error {
    32  	evmStorageAccountAddress, err := StorageAccountAddress(chainID)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	evmContractAccountAddress, err := ContractAccountAddress(chainID)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	em := evm.NewEmulator(backend, evmStorageAccountAddress)
    43  
    44  	bs, err := handler.NewBlockStore(backend, evmStorageAccountAddress)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	aa, err := handler.NewAddressAllocator(backend, evmStorageAccountAddress)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	contractHandler := handler.NewContractHandler(common.Address(flowToken), bs, aa, backend, em)
    55  
    56  	stdlib.SetupEnvironment(env, contractHandler, evmContractAccountAddress)
    57  
    58  	return nil
    59  }