github.com/ethereum/go-ethereum@v1.16.1/accounts/abi/abigen/source2.go.tpl (about)

     1  // Code generated via abigen V2 - DO NOT EDIT.
     2  // This file is a generated binding and any manual changes will be lost.
     3  
     4  package {{.Package}}
     5  
     6  import (
     7  	"bytes"
     8  	"math/big"
     9  	"errors"
    10  
    11  	"github.com/ethereum/go-ethereum/accounts/abi"
    12  	"github.com/ethereum/go-ethereum/accounts/abi/bind/v2"
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethereum/go-ethereum/core/types"
    15  )
    16  
    17  // Reference imports to suppress errors if they are not otherwise used.
    18  var (
    19  	_ = bytes.Equal
    20  	_ = errors.New
    21  	_ = big.NewInt
    22  	_ = common.Big1
    23  	_ = types.BloomLookup
    24  	_ = abi.ConvertType
    25  )
    26  
    27  {{$structs := .Structs}}
    28  {{range $structs}}
    29  	// {{.Name}} is an auto generated low-level Go binding around an user-defined struct.
    30  	type {{.Name}} struct {
    31  	{{range $field := .Fields}}
    32  	{{capitalise $field.Name}} {{$field.Type}}{{end}}
    33  	}
    34  {{end}}
    35  
    36  {{range $contract := .Contracts}}
    37  	// {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract.
    38  	var {{.Type}}MetaData = bind.MetaData{
    39  		ABI: "{{.InputABI}}",
    40  		{{if (index $.Libraries .Type) -}}
    41  		ID: "{{index $.Libraries .Type}}",
    42  		{{ else -}}
    43  		ID: "{{.Type}}",
    44  		{{end -}}
    45  		{{if .InputBin -}}
    46  		Bin: "0x{{.InputBin}}",
    47  		{{end -}}
    48  		{{if .Libraries -}}
    49  		Deps: []*bind.MetaData{
    50  		{{- range $name, $pattern := .Libraries}}
    51  			&{{$name}}MetaData,
    52  		{{- end}}
    53  		},
    54  		{{end}}
    55  	}
    56  
    57  	// {{.Type}} is an auto generated Go binding around an Ethereum contract.
    58  	type {{.Type}} struct {
    59  		abi abi.ABI
    60  	}
    61  
    62  	// New{{.Type}} creates a new instance of {{.Type}}.
    63  	func New{{.Type}}() *{{.Type}} {
    64  		parsed, err := {{.Type}}MetaData.ParseABI()
    65  		if err != nil {
    66  			panic(errors.New("invalid ABI: " + err.Error()))
    67  		}
    68  		return &{{.Type}}{abi: *parsed}
    69  	}
    70  
    71  	// Instance creates a wrapper for a deployed contract instance at the given address.
    72  	// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
    73  	func (c *{{.Type}}) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
    74  		 return bind.NewBoundContract(addr, c.abi, backend, backend, backend)
    75  	}
    76  
    77  	{{ if .Constructor.Inputs }}
    78  	// PackConstructor is the Go binding used to pack the parameters required for
    79  	// contract deployment.
    80  	//
    81  	// Solidity: {{.Constructor.String}}
    82  	func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) PackConstructor({{range .Constructor.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) []byte {
    83  		enc, err := {{ decapitalise $contract.Type}}.abi.Pack("" {{range .Constructor.Inputs}}, {{.Name}}{{end}})
    84  		if err != nil {
    85  		   panic(err)
    86  		}
    87  		return enc
    88  	}
    89  	{{ end }}
    90  
    91  	{{range .Calls}}
    92  		// Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling
    93  		// the contract method with ID 0x{{printf "%x" .Original.ID}}.  This method will panic if any
    94  		// invalid/nil inputs are passed.
    95  		//
    96  		// Solidity: {{.Original.String}}
    97  		func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Pack{{.Normalized.Name}}({{range .Normalized.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) []byte {
    98  			enc, err := {{ decapitalise $contract.Type}}.abi.Pack("{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
    99  			if err != nil {
   100  				panic(err)
   101  			}
   102  			return enc
   103  		}
   104  
   105  		// TryPack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling
   106  		// the contract method with ID 0x{{printf "%x" .Original.ID}}.  This method will return an error
   107  		// if any inputs are invalid/nil.
   108  		//
   109  		// Solidity: {{.Original.String}}
   110  		func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) TryPack{{.Normalized.Name}}({{range .Normalized.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) ([]byte, error) {
   111  			return {{ decapitalise $contract.Type}}.abi.Pack("{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
   112  		}
   113  
   114  		{{/* Unpack method is needed only when there are return args */}}
   115  		{{if .Normalized.Outputs }}
   116  			{{ if .Structured }}
   117  			// {{.Normalized.Name}}Output serves as a container for the return parameters of contract
   118  			// method {{ .Normalized.Name }}.
   119  			type {{.Normalized.Name}}Output struct {
   120  			  {{range .Normalized.Outputs}}
   121  			  {{capitalise .Name}} {{bindtype .Type $structs}}{{end}}
   122  			}
   123  			{{ end }}
   124  
   125  			// Unpack{{.Normalized.Name}} is the Go binding that unpacks the parameters returned
   126  			// from invoking the contract method with ID 0x{{printf "%x" .Original.ID}}.
   127  			//
   128  			// Solidity: {{.Original.String}}
   129  			func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}(data []byte) (
   130  				{{- if .Structured}} {{.Normalized.Name}}Output,{{else}}
   131  				{{- range .Normalized.Outputs}} {{bindtype .Type $structs}},{{- end }}
   132  				{{- end }} error) {
   133  				out, err := {{ decapitalise $contract.Type}}.abi.Unpack("{{.Original.Name}}", data)
   134  				{{- if .Structured}}
   135  				outstruct := new({{.Normalized.Name}}Output)
   136  				if err != nil {
   137  					return *outstruct, err
   138  				}
   139  				{{- range $i, $t := .Normalized.Outputs}}
   140  				{{- if ispointertype .Type}}
   141  					outstruct.{{capitalise .Name}} = abi.ConvertType(out[{{$i}}], new({{underlyingbindtype .Type }})).({{bindtype .Type $structs}})
   142  				{{- else }}
   143  					outstruct.{{capitalise .Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
   144  				{{- end }}
   145  				{{- end }}
   146  				return *outstruct, nil{{else}}
   147  				if err != nil {
   148  					return {{range $i, $_ := .Normalized.Outputs}}{{if ispointertype .Type}}new({{underlyingbindtype .Type }}), {{else}}*new({{bindtype .Type $structs}}), {{end}}{{end}} err
   149  				}
   150  				{{- range $i, $t := .Normalized.Outputs}}
   151  				{{- if ispointertype .Type }}
   152  				out{{$i}} := abi.ConvertType(out[{{$i}}], new({{underlyingbindtype .Type}})).({{bindtype .Type $structs}})
   153  				{{- else }}
   154  				out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
   155  				{{- end }}
   156  				{{- end}}
   157  				return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} nil
   158  				{{- end}}
   159  			}
   160  		{{end}}
   161  	{{end}}
   162  
   163  	{{range .Events}}
   164  		// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Original.Name}} event raised by the {{$contract.Type}} contract.
   165  		type {{$contract.Type}}{{.Normalized.Name}} struct {
   166  			{{- range .Normalized.Inputs}}
   167  				{{ capitalise .Name}}
   168  				{{- if .Indexed}} {{ bindtopictype .Type $structs}}{{- else}} {{ bindtype .Type $structs}}{{ end }}
   169  			{{- end}}
   170  			Raw *types.Log // Blockchain specific contextual infos
   171  		}
   172  
   173  		const {{$contract.Type}}{{.Normalized.Name}}EventName = "{{.Original.Name}}"
   174  
   175  		// ContractEventName returns the user-defined event name.
   176  		func ({{$contract.Type}}{{.Normalized.Name}}) ContractEventName() string {
   177  			return {{$contract.Type}}{{.Normalized.Name}}EventName
   178  		}
   179  
   180  		// Unpack{{.Normalized.Name}}Event is the Go binding that unpacks the event data emitted
   181  		// by contract.
   182  		//
   183  		// Solidity: {{.Original.String}}
   184  		func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}Event(log *types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
   185  			event := "{{.Original.Name}}"
   186  			if log.Topics[0] != {{ decapitalise $contract.Type}}.abi.Events[event].ID {
   187  				return nil, errors.New("event signature mismatch")
   188  			}
   189  			out := new({{$contract.Type}}{{.Normalized.Name}})
   190  			if len(log.Data) > 0 {
   191  				if err := {{ decapitalise $contract.Type}}.abi.UnpackIntoInterface(out, event, log.Data); err != nil {
   192  					return nil, err
   193  				}
   194  			}
   195  			var indexed abi.Arguments
   196  			for _, arg := range {{ decapitalise $contract.Type}}.abi.Events[event].Inputs {
   197  				if arg.Indexed {
   198  					indexed = append(indexed, arg)
   199  				}
   200  			}
   201  			if err := abi.ParseTopics(out, indexed, log.Topics[1:]); err != nil {
   202  				return nil, err
   203  			}
   204  			out.Raw = log
   205  			return out, nil
   206  		}
   207  	{{end}}
   208  
   209  	{{ if .Errors }}
   210  	// UnpackError attempts to decode the provided error data using user-defined
   211  	// error definitions.
   212  	func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) UnpackError(raw []byte) (any, error) {
   213  		{{- range $k, $v := .Errors}}
   214  		if bytes.Equal(raw[:4], {{ decapitalise $contract.Type}}.abi.Errors["{{.Normalized.Name}}"].ID.Bytes()[:4]) {
   215  			return {{ decapitalise $contract.Type}}.Unpack{{.Normalized.Name}}Error(raw[4:])
   216  		}
   217  		{{- end }}
   218  		return nil, errors.New("Unknown error")
   219  	}
   220  	{{ end }}
   221  
   222  	{{range .Errors}}
   223  		// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Original.Name}} error raised by the {{$contract.Type}} contract.
   224  		type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}
   225  			{{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}}
   226  		}
   227  
   228  		// ErrorID returns the hash of canonical representation of the error's signature.
   229  		//
   230  		// Solidity: {{.Original.String}}
   231  		func {{$contract.Type}}{{.Normalized.Name}}ErrorID() common.Hash {
   232  			return common.HexToHash("{{.Original.ID}}")
   233  		}
   234  
   235  		// Unpack{{.Normalized.Name}}Error is the Go binding used to decode the provided
   236  		// error data into the corresponding Go error struct.
   237  		//
   238  		// Solidity: {{.Original.String}}
   239  		func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}Error(raw []byte) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
   240  			out := new({{$contract.Type}}{{.Normalized.Name}})
   241  			if err := {{ decapitalise $contract.Type}}.abi.UnpackIntoInterface(out, "{{.Normalized.Name}}", raw); err != nil {
   242  				return nil, err
   243  			}
   244  			return out, nil
   245  		}
   246  	{{end}}
   247  {{end}}