github.com/ethereum/go-ethereum@v1.16.1/accounts/abi/abigen/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 abigen
    18  
    19  import (
    20  	_ "embed"
    21  	"strings"
    22  	"unicode"
    23  
    24  	"github.com/ethereum/go-ethereum/accounts/abi"
    25  )
    26  
    27  // tmplData is the data structure required to fill the binding template.
    28  type tmplData struct {
    29  	Package   string                   // Name of the package to place the generated file in
    30  	Contracts map[string]*tmplContract // List of contracts to generate into this file
    31  	Libraries map[string]string        // Map the bytecode's link pattern to the library name
    32  	Structs   map[string]*tmplStruct   // Contract struct type definitions
    33  }
    34  
    35  // tmplContract contains the data needed to generate an individual contract binding.
    36  type tmplContract struct {
    37  	Type        string                 // Type name of the main contract binding
    38  	InputABI    string                 // JSON ABI used as the input to generate the binding from
    39  	InputBin    string                 // Optional EVM bytecode used to generate deploy code from
    40  	FuncSigs    map[string]string      // Optional map: string signature -> 4-byte signature
    41  	Constructor abi.Method             // Contract constructor for deploy parametrization
    42  	Calls       map[string]*tmplMethod // Contract calls that only read state data
    43  	Transacts   map[string]*tmplMethod // Contract calls that write state data
    44  	Fallback    *tmplMethod            // Additional special fallback function
    45  	Receive     *tmplMethod            // Additional special receive function
    46  	Events      map[string]*tmplEvent  // Contract events accessors
    47  	Libraries   map[string]string      // Same as tmplData, but filtered to only keep direct deps that the contract needs
    48  	Library     bool                   // Indicator whether the contract is a library
    49  }
    50  
    51  type tmplContractV2 struct {
    52  	Type        string                 // Type name of the main contract binding
    53  	InputABI    string                 // JSON ABI used as the input to generate the binding from
    54  	InputBin    string                 // Optional EVM bytecode used to generate deploy code from
    55  	Constructor abi.Method             // Contract constructor for deploy parametrization
    56  	Calls       map[string]*tmplMethod // All contract methods (excluding fallback, receive)
    57  	Events      map[string]*tmplEvent  // Contract events accessors
    58  	Libraries   map[string]string      // all direct library dependencies
    59  	Errors      map[string]*tmplError  // all errors defined
    60  }
    61  
    62  func newTmplContractV2(typ string, abiStr string, bytecode string, constructor abi.Method, cb *contractBinder) *tmplContractV2 {
    63  	// Strip any whitespace from the JSON ABI
    64  	strippedABI := strings.Map(func(r rune) rune {
    65  		if unicode.IsSpace(r) {
    66  			return -1
    67  		}
    68  		return r
    69  	}, abiStr)
    70  	return &tmplContractV2{
    71  		abi.ToCamelCase(typ),
    72  		strings.ReplaceAll(strippedABI, "\"", "\\\""),
    73  		strings.TrimPrefix(strings.TrimSpace(bytecode), "0x"),
    74  		constructor,
    75  		cb.calls,
    76  		cb.events,
    77  		make(map[string]string),
    78  		cb.errors,
    79  	}
    80  }
    81  
    82  type tmplDataV2 struct {
    83  	Package   string                     // Name of the package to use for the generated bindings
    84  	Contracts map[string]*tmplContractV2 // Contracts that will be emitted in the bindings (keyed by contract name)
    85  	Libraries map[string]string          // Map of the contract's name to link pattern
    86  	Structs   map[string]*tmplStruct     // Contract struct type definitions
    87  }
    88  
    89  // tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
    90  // and cached data fields.
    91  type tmplMethod struct {
    92  	Original   abi.Method // Original method as parsed by the abi package
    93  	Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
    94  	Structured bool       // Whether the returns should be accumulated into a struct
    95  }
    96  
    97  // tmplEvent is a wrapper around an abi.Event that contains a few preprocessed
    98  // and cached data fields.
    99  type tmplEvent struct {
   100  	Original   abi.Event // Original event as parsed by the abi package
   101  	Normalized abi.Event // Normalized version of the parsed fields
   102  }
   103  
   104  // tmplError is a wrapper around an abi.Error that contains a few preprocessed
   105  // and cached data fields.
   106  type tmplError struct {
   107  	Original   abi.Error
   108  	Normalized abi.Error
   109  }
   110  
   111  // tmplField is a wrapper around a struct field with binding language
   112  // struct type definition and relative filed name.
   113  type tmplField struct {
   114  	Type    string   // Field type representation depends on target binding language
   115  	Name    string   // Field name converted from the raw user-defined field name
   116  	SolKind abi.Type // Raw abi type information
   117  }
   118  
   119  // tmplStruct is a wrapper around an abi.tuple and contains an auto-generated
   120  // struct name.
   121  type tmplStruct struct {
   122  	Name   string       // Auto-generated struct name(before solidity v0.5.11) or raw name.
   123  	Fields []*tmplField // Struct fields definition depends on the binding language.
   124  }
   125  
   126  // tmplSource is the Go source template that the generated Go contract binding
   127  // is based on.
   128  //
   129  //go:embed source.go.tpl
   130  var tmplSource string
   131  
   132  // tmplSourceV2 is the Go source template that the generated Go contract binding
   133  // for abigen v2 is based on.
   134  //
   135  //go:embed source2.go.tpl
   136  var tmplSourceV2 string