@@ -7,36 +7,37 @@ import (
77 "strings"
88
99 "github.com/0xsequence/ethkit/ethartifact"
10- "github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind "
10+ geth_abigen "github.com/0xsequence/ethkit/go-ethereum/accounts/abi/abigen "
1111 "github.com/spf13/cobra"
1212)
1313
1414func init () {
1515 abigen := & abigen {}
1616 cmd := & cobra.Command {
1717 Use : "abigen" ,
18- Short : "Generate contract Go client code from an abi or truffle artifacts file" ,
18+ Short : "Generate contract Go client code from an abi or contract artifact file" ,
1919 Run : abigen .Run ,
2020 }
2121
22- cmd .Flags ().String ("artifactsFile" , "" , "path to truffle contract artifacts file" )
23- cmd .Flags ().String ("abiFile" , "" , "path to abi json file" )
24- cmd .Flags ().String ("lang " , "" , "target language, supported: [go], default=go " )
25- cmd .Flags ().String ("pkg " , "" , "pkg (optional) " )
26- cmd .Flags ().String ("type " , "" , "type (optional)" )
27- cmd .Flags ().String ( "outFile " , "" , "outFile (optional), default=stdout " )
28- cmd .Flags ().Bool ("includeDeployed " , false , "include deployed bytecode on the generated file " )
22+ cmd .Flags ().String ("artifactsFile" , "" , "path to compiled contract artifact file" )
23+ cmd .Flags ().String ("abiFile" , "" , "path to abi json file (optional) " )
24+ cmd .Flags ().String ("pkg " , "" , "go package name " )
25+ cmd .Flags ().String ("type " , "" , "type name used in generated output " )
26+ cmd .Flags ().String ("outFile " , "" , "outFile (optional, default=stdout )" )
27+ cmd .Flags ().Bool ( "includeDeployedBin " , false , "include deployed bytecode in the generated file ( default=false) " )
28+ cmd .Flags ().Bool ("v2 " , false , "use go-ethereum abigen v2 (default=false) " )
2929
3030 rootCmd .AddCommand (cmd )
3131}
3232
3333type abigen struct {
34- fArtifactsFile string
35- fAbiFile string
36- fPkg string
37- fType string
38- fOutFile string
39- fIncludeDeployed bool
34+ fArtifactsFile string
35+ fAbiFile string
36+ fPkg string
37+ fType string
38+ fOutFile string
39+ fIncludeDeployedBin bool
40+ fUseV2 bool
4041}
4142
4243func (c * abigen ) Run (cmd * cobra.Command , args []string ) {
@@ -45,7 +46,8 @@ func (c *abigen) Run(cmd *cobra.Command, args []string) {
4546 c .fPkg , _ = cmd .Flags ().GetString ("pkg" )
4647 c .fType , _ = cmd .Flags ().GetString ("type" )
4748 c .fOutFile , _ = cmd .Flags ().GetString ("outFile" )
48- c .fIncludeDeployed , _ = cmd .Flags ().GetBool ("includeDeployed" )
49+ c .fIncludeDeployedBin , _ = cmd .Flags ().GetBool ("includeDeployedBin" )
50+ c .fUseV2 , _ = cmd .Flags ().GetBool ("v2" )
4951
5052 if c .fArtifactsFile == "" && c .fAbiFile == "" {
5153 fmt .Println ("error: please pass one of --artifactsFile or --abiFile" )
@@ -92,11 +94,9 @@ func (c *abigen) generateGo(artifact ethartifact.RawArtifact) error {
9294 var (
9395 abis []string
9496 bins []string
95- dbins []string
9697 types []string
9798 sigs []map [string ]string
9899 libs = make (map [string ]string )
99- lang = bind .LangGo
100100 )
101101
102102 if strings .Contains (string (artifact .Bytecode ), "//" ) {
@@ -122,21 +122,29 @@ func (c *abigen) generateGo(artifact ethartifact.RawArtifact) error {
122122 bins = append (bins , artifact .Bytecode )
123123 aliases := map [string ]string {}
124124
125- if c . fIncludeDeployed {
126- dbins = append ( dbins , artifact . DeployedBytecode )
125+ var code string
126+ var err error
127127
128- if strings .Contains (string (artifact .DeployedBytecode ), "//" ) {
129- log .Fatal ("Contract has additional library references, which is unsupported at this time." )
130- }
128+ // NOTE: "bytecode" in an artifact contains both the constructor + the runtime code of the contract,
129+ // the "bytecode" value is what we use to deploy a new contract.
130+ //
131+ // Whereas the "deployedBytecode" is the runtime code of the contract, and can be used to verify
132+ // the contract bytecode once its been deployed. For our purposes of generating a client, we only
133+ // need the constructor code, so we use the "bytecode" value.
134+
135+ if c .fUseV2 {
136+ code , err = geth_abigen .BindV2 (types , abis , bins , pkgName , libs , aliases )
131137 } else {
132- dbins = append ( dbins , "" )
138+ code , err = geth_abigen . Bind ( types , abis , bins , sigs , pkgName , libs , aliases )
133139 }
134-
135- code , err := bind .Bind (types , abis , bins , dbins , sigs , pkgName , lang , libs , aliases )
136140 if err != nil {
137141 return err
138142 }
139143
144+ if c .fIncludeDeployedBin {
145+ code = fmt .Sprintf ("%s\n // %sDeployedBin is the resulting bytecode of the created contract\n const %sDeployedBin = %q\n " , code , typeName , typeName , artifact .DeployedBytecode )
146+ }
147+
140148 if c .fOutFile == "" {
141149 fmt .Println (code )
142150 } else {
0 commit comments