github.com/ava-labs/subnet-evm@v0.6.4/accounts/abi/bind/precompilebind/precompile_module_template.go (about)

     1  // (c) 2019-2022, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  package precompilebind
     4  
     5  // tmplSourcePrecompileModuleGo is the Go precompiled module source template.
     6  const tmplSourcePrecompileModuleGo = `
     7  // Code generated
     8  // This file is a generated precompile contract config with stubbed abstract functions.
     9  // The file is generated by a template. Please inspect every code and comment in this file before use.
    10  
    11  package {{.Package}}
    12  
    13  import (
    14  	"fmt"
    15  
    16  	"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
    17  	"github.com/ava-labs/subnet-evm/precompile/contract"
    18  	"github.com/ava-labs/subnet-evm/precompile/modules"
    19  
    20  	"github.com/ethereum/go-ethereum/common"
    21  )
    22  
    23  var _ contract.Configurator = &configurator{}
    24  
    25  // ConfigKey is the key used in json config files to specify this precompile config.
    26  // must be unique across all precompiles.
    27  const ConfigKey = "{{decapitalise .Contract.Type}}Config"
    28  
    29  // ContractAddress is the defined address of the precompile contract.
    30  // This should be unique across all precompile contracts.
    31  // See precompile/registry/registry.go for registered precompile contracts and more information.
    32  var ContractAddress = common.HexToAddress("{ASUITABLEHEXADDRESS}") // SET A SUITABLE HEX ADDRESS HERE
    33  
    34  // Module is the precompile module. It is used to register the precompile contract.
    35  var Module = modules.Module{
    36  	ConfigKey:    ConfigKey,
    37  	Address:      ContractAddress,
    38  	Contract:     {{.Contract.Type}}Precompile,
    39  	Configurator: &configurator{},
    40  }
    41  
    42  type configurator struct{}
    43  
    44  func init() {
    45  	// Register the precompile module.
    46  	// Each precompile contract registers itself through [RegisterModule] function.
    47  	if err := modules.RegisterModule(Module); err != nil {
    48  		panic(err)
    49  	}
    50  }
    51  
    52  // MakeConfig returns a new precompile config instance.
    53  // This is required to Marshal/Unmarshal the precompile config.
    54  func (*configurator) MakeConfig() precompileconfig.Config {
    55  	return new(Config)
    56  }
    57  
    58  // Configure configures [state] with the given [cfg] precompileconfig.
    59  // This function is called by the EVM once per precompile contract activation.
    60  // You can use this function to set up your precompile contract's initial state,
    61  // by using the [cfg] config and [state] stateDB.
    62  func (*configurator) Configure(chainConfig precompileconfig.ChainConfig, cfg precompileconfig.Config, state contract.StateDB, blockContext contract.ConfigurationBlockContext) error {
    63  	// CUSTOM CODE STARTS HERE
    64  	{{- if .Contract.AllowList}}
    65  	config, ok := cfg.(*Config)
    66  	if !ok {
    67  		return fmt.Errorf("expected config type %T, got %T: %v", &Config{}, cfg, cfg)
    68  	}
    69  
    70  	// AllowList is activated for this precompile. Configuring allowlist addresses here.
    71  	return config.AllowListConfig.Configure(chainConfig, ContractAddress, state, blockContext)
    72  	{{- else}}
    73  	if _, ok := cfg.(*Config); !ok {
    74  		return fmt.Errorf("expected config type %T, got %T: %v", &Config{}, cfg, cfg)
    75  	}
    76  
    77  	return nil
    78  	{{- end}}
    79  }
    80  `