2021-10-25 11:48:21 +00:00
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-10-27 19:35:06 +00:00
|
|
|
"go/token"
|
2021-10-25 11:48:21 +00:00
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const srcTmpl = `
|
|
|
|
{{- define "METHOD" -}}
|
|
|
|
// {{.Name}} {{.Comment}}
|
|
|
|
func {{.Name}}({{range $index, $arg := .Arguments -}}
|
|
|
|
{{- if ne $index 0}}, {{end}}
|
|
|
|
{{- .Name}} {{.Type}}
|
|
|
|
{{- end}}) {{if .ReturnType }}{{ .ReturnType }} {
|
|
|
|
return neogointernal.CallWithToken(Hash, "{{ .NameABI }}", int(contract.{{ .CallFlag }})
|
|
|
|
{{- range $arg := .Arguments -}}, {{.Name}}{{end}}).({{ .ReturnType }})
|
|
|
|
{{- else -}} {
|
|
|
|
neogointernal.CallWithTokenNoRet(Hash, "{{ .NameABI }}", int(contract.{{ .CallFlag }})
|
|
|
|
{{- range $arg := .Arguments -}}, {{.Name}}{{end}})
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
{{- end -}}
|
|
|
|
// Package {{.PackageName}} contains wrappers for {{.ContractName}} contract.
|
|
|
|
package {{.PackageName}}
|
|
|
|
|
|
|
|
import (
|
|
|
|
{{range $m := .Imports}} "{{ $m }}"
|
|
|
|
{{end}})
|
|
|
|
|
|
|
|
// Hash contains contract hash in big-endian form.
|
|
|
|
const Hash = "{{ .Hash }}"
|
|
|
|
{{range $m := .Methods}}
|
|
|
|
{{template "METHOD" $m }}
|
|
|
|
{{end}}`
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Config contains parameter for the generated binding.
|
|
|
|
Config struct {
|
|
|
|
Package string `yaml:"package,omitempty"`
|
|
|
|
Manifest *manifest.Manifest `yaml:"-"`
|
|
|
|
Hash util.Uint160 `yaml:"hash,omitempty"`
|
|
|
|
Overrides map[string]Override `yaml:"overrides,omitempty"`
|
|
|
|
CallFlags map[string]callflag.CallFlag `yaml:"callflags,omitempty"`
|
|
|
|
Output io.Writer `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
ContractTmpl struct {
|
2021-10-25 11:48:21 +00:00
|
|
|
PackageName string
|
|
|
|
ContractName string
|
|
|
|
Imports []string
|
|
|
|
Hash string
|
2022-10-26 09:43:48 +00:00
|
|
|
Methods []MethodTmpl
|
2021-10-25 11:48:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
MethodTmpl struct {
|
2021-10-25 11:48:21 +00:00
|
|
|
Name string
|
|
|
|
NameABI string
|
|
|
|
CallFlag string
|
|
|
|
Comment string
|
2022-10-26 09:43:48 +00:00
|
|
|
Arguments []ParamTmpl
|
2021-10-25 11:48:21 +00:00
|
|
|
ReturnType string
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
ParamTmpl struct {
|
2021-10-25 11:48:21 +00:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewConfig initializes and returns a new config instance.
|
2021-10-25 11:48:21 +00:00
|
|
|
func NewConfig() Config {
|
|
|
|
return Config{
|
|
|
|
Overrides: make(map[string]Override),
|
|
|
|
CallFlags: make(map[string]callflag.CallFlag),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate writes Go file containing smartcontract bindings to the `cfg.Output`.
|
|
|
|
func Generate(cfg Config) error {
|
2022-10-26 09:43:48 +00:00
|
|
|
ctr, err := TemplateFromManifest(cfg, scTypeToGo)
|
2021-10-25 11:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 09:40:59 +00:00
|
|
|
ctr.Imports = append(ctr.Imports, "github.com/nspcc-dev/neo-go/pkg/interop/contract")
|
|
|
|
ctr.Imports = append(ctr.Imports, "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal")
|
|
|
|
sort.Strings(ctr.Imports)
|
2021-10-25 11:48:21 +00:00
|
|
|
|
2022-10-26 09:47:39 +00:00
|
|
|
tmp, err := template.New("generate").Parse(srcTmpl)
|
2021-10-25 11:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp.Execute(cfg.Output, ctr)
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
func scTypeToGo(name string, typ smartcontract.ParamType, overrides map[string]Override) (string, string) {
|
|
|
|
if over, ok := overrides[name]; ok {
|
|
|
|
return over.TypeName, over.Package
|
|
|
|
}
|
|
|
|
|
2021-10-25 11:48:21 +00:00
|
|
|
switch typ {
|
|
|
|
case smartcontract.AnyType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "interface{}", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.BoolType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "bool", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.IntegerType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "int", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.ByteArrayType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "[]byte", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.StringType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "string", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.Hash160Type:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "interop.Hash160", "github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.Hash256Type:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "interop.Hash256", "github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.PublicKeyType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "interop.PublicKey", "github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.SignatureType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "interop.Signature", "github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.ArrayType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "[]interface{}", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.MapType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "map[string]interface{}", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.InteropInterfaceType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "interface{}", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
case smartcontract.VoidType:
|
2022-10-26 09:43:48 +00:00
|
|
|
return "", ""
|
2021-10-25 11:48:21 +00:00
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
// TemplateFromManifest create a contract template using the given configuration
|
|
|
|
// and type conversion function.
|
|
|
|
func TemplateFromManifest(cfg Config, scTypeConverter func(string, smartcontract.ParamType, map[string]Override) (string, string)) (ContractTmpl, error) {
|
2021-10-25 11:48:21 +00:00
|
|
|
hStr := ""
|
|
|
|
for _, b := range cfg.Hash.BytesBE() {
|
|
|
|
hStr += fmt.Sprintf("\\x%02x", b)
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
ctr := ContractTmpl{
|
2021-10-25 11:48:21 +00:00
|
|
|
PackageName: cfg.Package,
|
|
|
|
ContractName: cfg.Manifest.Name,
|
|
|
|
Hash: hStr,
|
|
|
|
}
|
|
|
|
if ctr.PackageName == "" {
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, len(cfg.Manifest.Name)))
|
|
|
|
for _, r := range cfg.Manifest.Name {
|
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
buf.WriteRune(unicode.ToLower(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctr.PackageName = buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
imports := make(map[string]struct{})
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
for _, m := range cfg.Manifest.ABI.Methods {
|
|
|
|
seen[m.Name] = false
|
|
|
|
}
|
|
|
|
for _, m := range cfg.Manifest.ABI.Methods {
|
|
|
|
if m.Name[0] == '_' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consider `perform(a)` and `perform(a, b)` methods.
|
|
|
|
// First, try to export the second method with `Perform2` name.
|
|
|
|
// If `perform2` is already in the manifest, use `perform_2` with as many underscores
|
|
|
|
// as needed to eliminate name conflicts. It will produce long names in certain circumstances,
|
|
|
|
// but if the manifest contains lots of similar names with trailing underscores, delicate naming
|
|
|
|
// was probably not the goal.
|
|
|
|
name := m.Name
|
|
|
|
if v, ok := seen[name]; !ok || v {
|
|
|
|
suffix := strconv.Itoa(len(m.Parameters))
|
|
|
|
for ; seen[name]; name = m.Name + suffix {
|
|
|
|
suffix = "_" + suffix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seen[name] = true
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
mtd := MethodTmpl{
|
2021-10-25 11:48:21 +00:00
|
|
|
Name: upperFirst(name),
|
|
|
|
NameABI: m.Name,
|
|
|
|
CallFlag: callflag.All.String(),
|
|
|
|
Comment: fmt.Sprintf("invokes `%s` method of contract.", m.Name),
|
|
|
|
}
|
|
|
|
if f, ok := cfg.CallFlags[m.Name]; ok {
|
|
|
|
mtd.CallFlag = f.String()
|
|
|
|
} else if m.Safe {
|
|
|
|
mtd.CallFlag = callflag.ReadOnly.String()
|
|
|
|
}
|
2022-10-27 19:35:06 +00:00
|
|
|
|
|
|
|
var varnames = make(map[string]bool)
|
2021-10-25 11:48:21 +00:00
|
|
|
for i := range m.Parameters {
|
|
|
|
name := m.Parameters[i].Name
|
|
|
|
if name == "" {
|
2022-08-22 10:50:47 +00:00
|
|
|
return ctr, fmt.Errorf("manifest ABI method %q/%d: parameter #%d is unnamed", m.Name, len(m.Parameters), i)
|
2021-10-25 11:48:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
typeStr, pkg := scTypeConverter(m.Name+"."+name, m.Parameters[i].Type, cfg.Overrides)
|
|
|
|
if pkg != "" {
|
|
|
|
imports[pkg] = struct{}{}
|
2021-10-25 11:48:21 +00:00
|
|
|
}
|
2022-10-27 19:35:06 +00:00
|
|
|
if token.IsKeyword(name) {
|
|
|
|
name = name + "v"
|
|
|
|
}
|
|
|
|
for varnames[name] {
|
|
|
|
name = name + "_"
|
|
|
|
}
|
|
|
|
varnames[name] = true
|
2022-10-26 09:43:48 +00:00
|
|
|
mtd.Arguments = append(mtd.Arguments, ParamTmpl{
|
2021-10-25 11:48:21 +00:00
|
|
|
Name: name,
|
|
|
|
Type: typeStr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:43:48 +00:00
|
|
|
typeStr, pkg := scTypeConverter(m.Name, m.ReturnType, cfg.Overrides)
|
|
|
|
if pkg != "" {
|
|
|
|
imports[pkg] = struct{}{}
|
2021-10-25 11:48:21 +00:00
|
|
|
}
|
2022-10-26 09:43:48 +00:00
|
|
|
mtd.ReturnType = typeStr
|
2021-10-25 11:48:21 +00:00
|
|
|
ctr.Methods = append(ctr.Methods, mtd)
|
|
|
|
}
|
|
|
|
|
|
|
|
for imp := range imports {
|
|
|
|
ctr.Imports = append(ctr.Imports, imp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func upperFirst(s string) string {
|
|
|
|
return strings.ToUpper(s[0:1]) + s[1:]
|
|
|
|
}
|