github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/accounts/abi/bind/template.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package bind
    18  
    19  import "github.com/ethereumproject/go-ethereum/accounts/abi"
    20  
    21  // tmplData is the data structure required to fill the binding template.
    22  type tmplData struct {
    23  	Package   string                   // Name of the package to place the generated file in
    24  	Contracts map[string]*tmplContract // List of contracts to generate into this file
    25  }
    26  
    27  // tmplContract contains the data needed to generate an individual contract binding.
    28  type tmplContract struct {
    29  	Type        string                 // Type name of the main contract binding
    30  	InputABI    string                 // JSON ABI used as the input to generate the binding from
    31  	InputBin    string                 // Optional EVM bytecode used to denetare deploy code from
    32  	Constructor abi.Method             // Contract constructor for deploy parametrization
    33  	Calls       map[string]*tmplMethod // Contract calls that only read state data
    34  	Transacts   map[string]*tmplMethod // Contract calls that write state data
    35  }
    36  
    37  // tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
    38  // and cached data fields.
    39  type tmplMethod struct {
    40  	Original   abi.Method // Original method as parsed by the abi package
    41  	Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
    42  	Structured bool       // Whether the returns should be accumulated into a contract
    43  }
    44  
    45  // tmplSource is the Go source template use to generate the contract binding
    46  // based on.
    47  const tmplSource = `
    48  // This file is an automatically generated Go binding. Do not modify as any
    49  // change will likely be lost upon the next re-generation!
    50  
    51  package {{.Package}}
    52  
    53  {{range $contract := .Contracts}}
    54  	// {{.Type}}ABI is the input ABI used to generate the binding from.
    55  	const {{.Type}}ABI = ` + "`" + `{{.InputABI}}` + "`" + `
    56  
    57  	{{if .InputBin}}
    58  		// {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
    59  		const {{.Type}}Bin = ` + "`" + `{{.InputBin}}` + "`" + `
    60  
    61  		// Deploy{{.Type}} deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
    62  		func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) {
    63  		  parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
    64  		  if err != nil {
    65  		    return common.Address{}, nil, nil, err
    66  		  }
    67  		  address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
    68  		  if err != nil {
    69  		    return common.Address{}, nil, nil, err
    70  		  }
    71  		  return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract} }, nil
    72  		}
    73  	{{end}}
    74  
    75  	// {{.Type}} is an auto generated Go binding around an Ethereum contract.
    76  	type {{.Type}} struct {
    77  	  {{.Type}}Caller     // Read-only binding to the contract
    78  	  {{.Type}}Transactor // Write-only binding to the contract
    79  	}
    80  
    81  	// {{.Type}}Caller is an auto generated read-only Go binding around an Ethereum contract.
    82  	type {{.Type}}Caller struct {
    83  	  contract *bind.BoundContract // Generic contract wrapper for the low level calls
    84  	}
    85  
    86  	// {{.Type}}Transactor is an auto generated write-only Go binding around an Ethereum contract.
    87  	type {{.Type}}Transactor struct {
    88  	  contract *bind.BoundContract // Generic contract wrapper for the low level calls
    89  	}
    90  
    91  	// {{.Type}}Session is an auto generated Go binding around an Ethereum contract,
    92  	// with pre-set call and transact options.
    93  	type {{.Type}}Session struct {
    94  	  Contract     *{{.Type}}        // Generic contract binding to set the session for
    95  	  CallOpts     bind.CallOpts     // Call options to use throughout this session
    96  	  TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
    97  	}
    98  
    99  	// {{.Type}}CallerSession is an auto generated read-only Go binding around an Ethereum contract,
   100  	// with pre-set call options.
   101  	type {{.Type}}CallerSession struct {
   102  	  Contract *{{.Type}}Caller // Generic contract caller binding to set the session for
   103  	  CallOpts bind.CallOpts    // Call options to use throughout this session
   104  	}
   105  
   106  	// {{.Type}}TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
   107  	// with pre-set transact options.
   108  	type {{.Type}}TransactorSession struct {
   109  	  Contract     *{{.Type}}Transactor // Generic contract transactor binding to set the session for
   110  	  TransactOpts bind.TransactOpts    // Transaction auth options to use throughout this session
   111  	}
   112  
   113  	// {{.Type}}Raw is an auto generated low-level Go binding around an Ethereum contract.
   114  	type {{.Type}}Raw struct {
   115  	  Contract *{{.Type}} // Generic contract binding to access the raw methods on
   116  	}
   117  
   118  	// {{.Type}}CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
   119  	type {{.Type}}CallerRaw struct {
   120  		Contract *{{.Type}}Caller // Generic read-only contract binding to access the raw methods on
   121  	}
   122  
   123  	// {{.Type}}TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
   124  	type {{.Type}}TransactorRaw struct {
   125  		Contract *{{.Type}}Transactor // Generic write-only contract binding to access the raw methods on
   126  	}
   127  
   128  	// New{{.Type}} creates a new instance of {{.Type}}, bound to a specific deployed contract.
   129  	func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) {
   130  	  contract, err := bind{{.Type}}(address, backend.(bind.ContractCaller), backend.(bind.ContractTransactor))
   131  	  if err != nil {
   132  	    return nil, err
   133  	  }
   134  	  return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract} }, nil
   135  	}
   136  
   137  	// New{{.Type}}Caller creates a new read-only instance of {{.Type}}, bound to a specific deployed contract.
   138  	func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) {
   139  	  contract, err := bind{{.Type}}(address, caller, nil)
   140  	  if err != nil {
   141  	    return nil, err
   142  	  }
   143  	  return &{{.Type}}Caller{contract: contract}, nil
   144  	}
   145  
   146  	// New{{.Type}}Transactor creates a new write-only instance of {{.Type}}, bound to a specific deployed contract.
   147  	func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) {
   148  	  contract, err := bind{{.Type}}(address, nil, transactor)
   149  	  if err != nil {
   150  	    return nil, err
   151  	  }
   152  	  return &{{.Type}}Transactor{contract: contract}, nil
   153  	}
   154  
   155  	// bind{{.Type}} binds a generic wrapper to an already deployed contract.
   156  	func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) {
   157  	  parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
   158  	  if err != nil {
   159  	    return nil, err
   160  	  }
   161  	  return bind.NewBoundContract(address, parsed, caller, transactor), nil
   162  	}
   163  
   164  	// Call invokes the (constant) contract method with params as input values and
   165  	// sets the output to result. The result type might be a single field for simple
   166  	// returns, a slice of interfaces for anonymous returns and a struct for named
   167  	// returns.
   168  	func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
   169  		return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...)
   170  	}
   171  
   172  	// Transfer initiates a plain transaction to move funds to the contract, calling
   173  	// its default method if one is available.
   174  	func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
   175  		return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts)
   176  	}
   177  
   178  	// Transact invokes the (paid) contract method with params as input values.
   179  	func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
   180  		return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...)
   181  	}
   182  
   183  	// Call invokes the (constant) contract method with params as input values and
   184  	// sets the output to result. The result type might be a single field for simple
   185  	// returns, a slice of interfaces for anonymous returns and a struct for named
   186  	// returns.
   187  	func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
   188  		return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...)
   189  	}
   190  
   191  	// Transfer initiates a plain transaction to move funds to the contract, calling
   192  	// its default method if one is available.
   193  	func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
   194  		return _{{$contract.Type}}.Contract.contract.Transfer(opts)
   195  	}
   196  
   197  	// Transact invokes the (paid) contract method with params as input values.
   198  	func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
   199  		return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...)
   200  	}
   201  
   202  	{{range .Calls}}
   203  		// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
   204  		//
   205  		// Solidity: {{.Original.String}}
   206  		func (_{{$contract.Type}} *{{$contract.Type}}Caller) {{.Normalized.Name}}(opts *bind.CallOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}};{{end}} },{{else}}{{range .Normalized.Outputs}}{{bindtype .Type}},{{end}}{{end}} error) {
   207  			{{if .Structured}}ret := new(struct{
   208  				{{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}}
   209  				{{end}}
   210  			}){{else}}var (
   211  				{{range $i, $_ := .Normalized.Outputs}}ret{{$i}} = new({{bindtype .Type}})
   212  				{{end}}
   213  			){{end}}
   214  			out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}&[]interface{}{
   215  				{{range $i, $_ := .Normalized.Outputs}}ret{{$i}},
   216  				{{end}}
   217  			}{{end}}{{end}}
   218  			err := _{{$contract.Type}}.contract.Call(opts, out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
   219  			return {{if .Structured}}*ret,{{else}}{{range $i, $_ := .Normalized.Outputs}}*ret{{$i}},{{end}}{{end}} err
   220  		}
   221  
   222  		// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
   223  		//
   224  		// Solidity: {{.Original.String}}
   225  		func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type}},{{end}} {{end}} error) {
   226  		  return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
   227  		}
   228  
   229  		// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
   230  		//
   231  		// Solidity: {{.Original.String}}
   232  		func (_{{$contract.Type}} *{{$contract.Type}}CallerSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type}},{{end}} {{end}} error) {
   233  		  return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
   234  		}
   235  	{{end}}
   236  
   237  	{{range .Transacts}}
   238  		// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
   239  		//
   240  		// Solidity: {{.Original.String}}
   241  		func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
   242  			return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
   243  		}
   244  
   245  		// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
   246  		//
   247  		// Solidity: {{.Original.String}}
   248  		func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
   249  		  return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
   250  		}
   251  
   252  		// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
   253  		//
   254  		// Solidity: {{.Original.String}}
   255  		func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
   256  		  return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
   257  		}
   258  	{{end}}
   259  {{end}}
   260  `