Skip to content

Commit

Permalink
abigen support smbin flag and remove useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Jan 4, 2024
1 parent 43690dd commit 91361e0
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 712 deletions.
3 changes: 2 additions & 1 deletion v3/abi/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (
// to be used as is in client code, but rather as an intermediate struct which
// enforces compile time type safety and naming convention opposed to having to
// manually maintain hard coded strings that break on runtime.
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, smcrypto bool) (string, error) {
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, smcrypto bool, smBytecodes []string) (string, error) {
var (
// contracts is the map of each individual contract requested binding
contracts = make(map[string]*tmplContract)
Expand Down Expand Up @@ -170,6 +170,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
Type: capitalise(types[i]),
InputABI: strings.Replace(strippedABI, "\"", "\\\"", -1),
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
InputSMBin: strings.TrimPrefix(strings.TrimSpace(smBytecodes[i]), "0x"),
Constructor: evmABI.Constructor,
Calls: calls,
Transacts: transacts,
Expand Down
25 changes: 23 additions & 2 deletions v3/abi/bind/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type tmplContract struct {
Type string // Type name of the main contract binding
InputABI string // JSON ABI used as the input to generate the binding from
InputBin string // Optional EVM bytecode used to denetare deploy code from
InputSMBin string // Optional EVM bytecode used to denetare deploy code from
FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
Constructor abi.Method // Contract constructor for deploy parametrization
Calls map[string]*tmplMethod // Contract calls that only read state data
Expand Down Expand Up @@ -87,6 +88,7 @@ const tmplSourceGo = `
package {{.Package}}
import (
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -131,6 +133,7 @@ var (
{{if .InputBin}}
// {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
var {{.Type}}Bin = "0x{{.InputBin}}"
var {{.Type}}SMBin = "0x{{.InputSMBin}}"
// Deploy{{.Type}} deploys a new contract, binding an instance of {{.Type}} to it.
func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}}{{end}}) (common.Address, *types.Receipt, *{{.Type}}, error) {
Expand All @@ -142,7 +145,16 @@ var (
{{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend)
{{$contract.Type}}Bin = strings.Replace({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:], -1)
{{end}}
address, receipt, contract, err := bind.DeployContract(auth, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
var bytecode []byte
if backend.SMCrypto() {
bytecode = common.FromHex({{.Type}}SMBin)
} else {
bytecode = common.FromHex({{.Type}}Bin)
}
if len(bytecode) == 0 {
return common.Address{}, nil, nil, fmt.Errorf("cannot deploy empty bytecode")
}
address, receipt, contract, err := bind.DeployContract(auth, parsed, bytecode, backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
if err != nil {
return common.Address{}, nil, nil, err
}
Expand All @@ -158,7 +170,16 @@ var (
{{decapitalise $name}}Addr, _, _, _ := AsyncDeploy{{capitalise $name}}(auth, backend)
{{$contract.Type}}Bin = strings.Replace({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:], -1)
{{end}}
tx, err := bind.AsyncDeployContract(auth, handler, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
var bytecode []byte
if backend.SMCrypto() {
bytecode = common.FromHex({{.Type}}SMBin)
} else {
bytecode = common.FromHex({{.Type}}Bin)
}
if len(bytecode) == 0 {
return nil, fmt.Errorf("cannot deploy empty bytecode")
}
tx, err := bind.AsyncDeployContract(auth, handler, parsed, bytecode, backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
if err != nil {
return nil, err
}
Expand Down
23 changes: 20 additions & 3 deletions v3/cmd/abigen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ var (
// Flags needed by abigen
abiFlag = &cli.StringFlag{
Name: "abi",
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
Usage: "Path to the Solidity contract ABI json to bind, - for STDIN",
}
binFlag = &cli.StringFlag{
Name: "bin",
Usage: "Path to the Ethereum contract bytecode (generate deploy method)",
Usage: "Path to the Solidity contract bytecode (generate deploy method)",
}
smBinFlag = &cli.StringFlag{
Name: "smbin",
Usage: "Path to the Solidity contract sm crypto bytecode (generate deploy method)",
}
typeFlag = &cli.StringFlag{
Name: "type",
Expand Down Expand Up @@ -101,6 +105,7 @@ func init() {
langFlag,
aliasFlag,
cryptoFlag,
smBinFlag,
}
app.Action = abigen
}
Expand All @@ -127,6 +132,7 @@ func abigen(c *cli.Context) error {
var (
abis []string
bins []string
smBins []string
types []string
sigs []map[string]string
libs = make(map[string]string)
Expand Down Expand Up @@ -160,6 +166,17 @@ func abigen(c *cli.Context) error {
}
bins = append(bins, string(bin))

var smBin []byte
if smBinFile := c.String(smBinFlag.Name); smBinFile != "" {
if smBin, err = os.ReadFile(smBinFile); err != nil {
utils.Fatalf("Failed to read input sm crypto bytecode: %v", err)
}
if strings.Contains(string(smBin), "//") {
utils.Fatalf("Contract has additional library references, please use other mode(e.g. --combined-json) to catch library infos")
}
}
smBins = append(smBins, string(smBin))

kind := c.String(typeFlag.Name)
if kind == "" {
kind = c.String(pkgFlag.Name)
Expand Down Expand Up @@ -232,7 +249,7 @@ func abigen(c *cli.Context) error {
}
}
// Generate the contract binding
code, err := bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases, smcrypto)
code, err := bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases, smcrypto, smBins)
if err != nil {
utils.Fatalf("Failed to generate ABI binding: %v", err)
}
Expand Down
94 changes: 0 additions & 94 deletions v3/cmd/commandline/cns.go

This file was deleted.

6 changes: 3 additions & 3 deletions v3/cmd/commandline/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,9 @@ func init() {
// will be global for your application.

// rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is the project directory ./config.ini)")
rootCmd.Flags().StringVarP(&cfgFile, "privateKeyPath", "p", "", "private key file path of pem format")
rootCmd.Flags().BoolVarP(&smCrypto, "smCrypto", "s", false, "use smCrypto or not, default is false")
rootCmd.Flags().BoolVarP(&disableSsl, "disableSsl", "d", false, "switch off ssl or not, default use ssl")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "privateKeyPath", "p", "", "private key file path of pem format")
rootCmd.PersistentFlags().BoolVarP(&smCrypto, "smCrypto", "s", false, "use smCrypto or not, default is false")
rootCmd.PersistentFlags().BoolVarP(&disableSsl, "disableSsl", "d", false, "switch off ssl or not, default use ssl")
rootCmd.PersistentFlags().StringVarP(&groupID, "groupID", "g", "group0", "groupID of FISCO BCOS chain")
rootCmd.PersistentFlags().StringVarP(&nodeEndpoint, "nodeEndpoint", "n", "127.0.0.1:20200", "node endpoint, default is 127.0.0.1:20200")
rootCmd.PersistentFlags().StringVarP(&certPath, "certPath", "c", "./conf", "cert path, default is ./conf, should contain ca.crt, sdk.crt, sdk.key")
Expand Down
Loading

0 comments on commit 91361e0

Please sign in to comment.