Files
evm/accounts/abi/bind/precompilebind/precompile_module_template.go

81 lines
2.8 KiB
Go

// (c) 2020-2022, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package precompilebind
// tmplSourcePrecompileModuleGo is the Go precompiled module source template.
const tmplSourcePrecompileModuleGo = `
// Code generated
// This file is a generated precompile contract config with stubbed abstract functions.
// The file is generated by a template. Please inspect every code and comment in this file before use.
package {{.Package}}
import (
"fmt"
"github.com/luxfi/evm/precompile/precompileconfig"
"github.com/luxfi/evm/precompile/contract"
"github.com/luxfi/evm/precompile/modules"
"github.com/luxfi/geth/common"
)
var _ contract.Configurator = &configurator{}
// ConfigKey is the key used in json config files to specify this precompile config.
// must be unique across all precompiles.
const ConfigKey = "{{decapitalise .Contract.Type}}Config"
// ContractAddress is the defined address of the precompile contract.
// This should be unique across all precompile contracts.
// See precompile/registry/registry.go for registered precompile contracts and more information.
var ContractAddress = common.HexToAddress("{ASUITABLEHEXADDRESS}") // SET A SUITABLE HEX ADDRESS HERE
// Module is the precompile module. It is used to register the precompile contract.
var Module = modules.Module{
ConfigKey: ConfigKey,
Address: ContractAddress,
Contract: {{.Contract.Type}}Precompile,
Configurator: &configurator{},
}
type configurator struct{}
func init() {
// Register the precompile module.
// Each precompile contract registers itself through [RegisterModule] function.
if err := modules.RegisterModule(Module); err != nil {
panic(err)
}
}
// MakeConfig returns a new precompile config instance.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
// Configure configures [state] with the given [cfg] precompileconfig.
// This function is called by the EVM once per precompile contract activation.
// You can use this function to set up your precompile contract's initial state,
// by using the [cfg] config and [state] stateDB.
func (*configurator) Configure(chainConfig precompileconfig.ChainConfig, cfg precompileconfig.Config, state contract.StateDB, blockContext contract.ConfigurationBlockContext) error {
// CUSTOM CODE STARTS HERE
{{- if .Contract.AllowList}}
config, ok := cfg.(*Config)
if !ok {
return fmt.Errorf("expected config type %T, got %T: %v", &Config{}, cfg, cfg)
}
// AllowList is activated for this precompile. Configuring allowlist addresses here.
return config.AllowListConfig.Configure(chainConfig, ContractAddress, state, blockContext)
{{- else}}
if _, ok := cfg.(*Config); !ok {
return fmt.Errorf("expected config type %T, got %T: %v", &Config{}, cfg, cfg)
}
return nil
{{- end}}
}
`