parent
bb47d971dc
commit
2a4a5ab479
4 changed files with 530 additions and 24 deletions
|
@ -12,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
const srcTmpl = `
|
||||
{{- define "METHOD" -}}
|
||||
{{- define "SAFEMETHOD" -}}
|
||||
// {{.Name}} {{.Comment}}
|
||||
func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}}
|
||||
{{- if ne $index 0}}, {{end}}
|
||||
|
@ -26,6 +26,41 @@ func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}}
|
|||
{{- end}}
|
||||
}
|
||||
{{- end -}}
|
||||
{{- define "METHOD" -}}
|
||||
// {{.Name}} {{.Comment}}
|
||||
// This transaction is signed and immediately sent to the network.
|
||||
// The values returned are its hash, ValidUntilBlock value and error if any.
|
||||
func (c *Contract) {{.Name}}({{range $index, $arg := .Arguments -}}
|
||||
{{- if ne $index 0}}, {{end}}
|
||||
{{- .Name}} {{.Type}}
|
||||
{{- end}}) (util.Uint256, uint32, error) {
|
||||
return c.actor.SendCall(Hash, "{{ .NameABI }}"
|
||||
{{- range $index, $arg := .Arguments -}}, {{.Name}}{{end}})
|
||||
}
|
||||
|
||||
// {{.Name}}Transaction {{.Comment}}
|
||||
// This transaction is signed, but not sent to the network, instead it's
|
||||
// returned to the caller.
|
||||
func (c *Contract) {{.Name}}Transaction({{range $index, $arg := .Arguments -}}
|
||||
{{- if ne $index 0}}, {{end}}
|
||||
{{- .Name}} {{.Type}}
|
||||
{{- end}}) (*transaction.Transaction, error) {
|
||||
return c.actor.MakeCall(Hash, "{{ .NameABI }}"
|
||||
{{- range $index, $arg := .Arguments -}}, {{.Name}}{{end}})
|
||||
}
|
||||
|
||||
// {{.Name}}Unsigned {{.Comment}}
|
||||
// This transaction is not signed, it's simply returned to the caller.
|
||||
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
||||
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
||||
func (c *Contract) {{.Name}}Unsigned({{range $index, $arg := .Arguments -}}
|
||||
{{- if ne $index 0}}, {{end}}
|
||||
{{- .Name}} {{.Type}}
|
||||
{{- end}}) (*transaction.Transaction, error) {
|
||||
return c.actor.MakeUnsignedCall(Hash, "{{ .NameABI }}", nil
|
||||
{{- range $index, $arg := .Arguments -}}, {{.Name}}{{end}})
|
||||
}
|
||||
{{- end -}}
|
||||
// Package {{.PackageName}} contains RPC wrappers for {{.ContractName}} contract.
|
||||
package {{.PackageName}}
|
||||
|
||||
|
@ -45,6 +80,22 @@ type Invoker interface {
|
|||
Call(contract util.Uint160, operation string, params ...interface{}) (*result.Invoke, error)
|
||||
}
|
||||
|
||||
{{if .HasWriter}}// Actor is used by Contract to call state-changing methods.
|
||||
type Actor interface {
|
||||
Invoker
|
||||
{{if or .IsNep11D .IsNep11ND}}nep11.Actor{{end -}}
|
||||
{{if .IsNep17}}nep17.Actor{{end -}}
|
||||
{{if len .Methods}}
|
||||
MakeCall(contract util.Uint160, method string, params ...interface{}) (*transaction.Transaction, error)
|
||||
MakeRun(script []byte) (*transaction.Transaction, error)
|
||||
MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...interface{}) (*transaction.Transaction, error)
|
||||
MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error)
|
||||
SendCall(contract util.Uint160, method string, params ...interface{}) (util.Uint256, uint32, error)
|
||||
SendRun(script []byte) (util.Uint256, uint32, error)
|
||||
{{end -}}
|
||||
}
|
||||
|
||||
{{end -}}
|
||||
// ContractReader implements safe contract methods.
|
||||
type ContractReader struct {
|
||||
{{if .IsNep11D}}nep11.DivisibleReader
|
||||
|
@ -56,6 +107,19 @@ type ContractReader struct {
|
|||
invoker Invoker
|
||||
}
|
||||
|
||||
{{if .HasWriter}}// Contract implements all contract methods.
|
||||
type Contract struct {
|
||||
ContractReader
|
||||
{{if .IsNep11D}}nep11.DivisibleWriter
|
||||
{{end -}}
|
||||
{{if .IsNep11ND}}nep11.BaseWriter
|
||||
{{end -}}
|
||||
{{if .IsNep17}}nep17.TokenWriter
|
||||
{{end -}}
|
||||
actor Actor
|
||||
}
|
||||
|
||||
{{end -}}
|
||||
// NewReader creates an instance of ContractReader using Hash and the given Invoker.
|
||||
func NewReader(invoker Invoker) *ContractReader {
|
||||
return &ContractReader{
|
||||
|
@ -65,7 +129,30 @@ func NewReader(invoker Invoker) *ContractReader {
|
|||
invoker}
|
||||
}
|
||||
|
||||
{{range $m := .Methods}}
|
||||
{{if .HasWriter}}// New creates an instance of Contract using Hash and the given Actor.
|
||||
func New(actor Actor) *Contract {
|
||||
{{if .IsNep11D}}var nep11dt = nep11.NewDivisible(actor, Hash)
|
||||
{{end -}}
|
||||
{{if .IsNep11ND}}var nep11ndt = nep11.NewNonDivisible(actor, Hash)
|
||||
{{end -}}
|
||||
{{if .IsNep17}}var nep17t = nep17.New(actor, Hash)
|
||||
{{end -}}
|
||||
return &Contract{ContractReader{
|
||||
{{- if .IsNep11D}}nep11dt.DivisibleReader, {{end -}}
|
||||
{{- if .IsNep11ND}}nep11ndt.NonDivisibleReader, {{end -}}
|
||||
{{- if .IsNep17}}nep17t.TokenReader, {{end -}}
|
||||
actor},
|
||||
{{- if .IsNep11D}}nep11dt.DivisibleWriter, {{end -}}
|
||||
{{- if .IsNep11ND}}nep11ndt.BaseWriter, {{end -}}
|
||||
{{- if .IsNep17}}nep17t.TokenWriter, {{end -}}
|
||||
actor}
|
||||
}
|
||||
|
||||
{{end -}}
|
||||
{{range $m := .SafeMethods}}
|
||||
{{template "SAFEMETHOD" $m }}
|
||||
{{end}}
|
||||
{{- range $m := .Methods}}
|
||||
{{template "METHOD" $m }}
|
||||
{{end}}`
|
||||
|
||||
|
@ -74,9 +161,14 @@ var srcTemplate = template.Must(template.New("generate").Parse(srcTmpl))
|
|||
type (
|
||||
ContractTmpl struct {
|
||||
binding.ContractTmpl
|
||||
|
||||
SafeMethods []binding.MethodTmpl
|
||||
|
||||
IsNep11D bool
|
||||
IsNep11ND bool
|
||||
IsNep17 bool
|
||||
|
||||
HasWriter bool
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -180,43 +272,54 @@ func scTemplateToRPC(cfg binding.Config, bctr binding.ContractTmpl) ContractTmpl
|
|||
}
|
||||
for i := 0; i < len(ctr.Methods); i++ {
|
||||
abim := cfg.Manifest.ABI.GetMethod(ctr.Methods[i].NameABI, len(ctr.Methods[i].Arguments))
|
||||
if !abim.Safe {
|
||||
if abim.Safe {
|
||||
ctr.SafeMethods = append(ctr.SafeMethods, ctr.Methods[i])
|
||||
ctr.Methods = append(ctr.Methods[:i], ctr.Methods[i+1:]...)
|
||||
i--
|
||||
} else {
|
||||
ctr.Methods[i].Comment = fmt.Sprintf("creates a transaction invoking `%s` method of the contract.", ctr.Methods[i].NameABI)
|
||||
}
|
||||
}
|
||||
// We're misusing CallFlag field for function name here.
|
||||
for i := range ctr.Methods {
|
||||
switch ctr.Methods[i].ReturnType {
|
||||
for i := range ctr.SafeMethods {
|
||||
switch ctr.SafeMethods[i].ReturnType {
|
||||
case "interface{}":
|
||||
imports["github.com/nspcc-dev/neo-go/pkg/vm/stackitem"] = struct{}{}
|
||||
ctr.Methods[i].ReturnType = "stackitem.Item"
|
||||
ctr.Methods[i].CallFlag = "Item"
|
||||
ctr.SafeMethods[i].ReturnType = "stackitem.Item"
|
||||
ctr.SafeMethods[i].CallFlag = "Item"
|
||||
case "bool":
|
||||
ctr.Methods[i].CallFlag = "Bool"
|
||||
ctr.SafeMethods[i].CallFlag = "Bool"
|
||||
case "*big.Int":
|
||||
ctr.Methods[i].CallFlag = "BigInt"
|
||||
ctr.SafeMethods[i].CallFlag = "BigInt"
|
||||
case "string":
|
||||
ctr.Methods[i].CallFlag = "UTF8String"
|
||||
ctr.SafeMethods[i].CallFlag = "UTF8String"
|
||||
case "util.Uint160":
|
||||
ctr.Methods[i].CallFlag = "Uint160"
|
||||
ctr.SafeMethods[i].CallFlag = "Uint160"
|
||||
case "util.Uint256":
|
||||
ctr.Methods[i].CallFlag = "Uint256"
|
||||
ctr.SafeMethods[i].CallFlag = "Uint256"
|
||||
case "*keys.PublicKey":
|
||||
ctr.Methods[i].CallFlag = "PublicKey"
|
||||
ctr.SafeMethods[i].CallFlag = "PublicKey"
|
||||
case "[]byte":
|
||||
ctr.Methods[i].CallFlag = "Bytes"
|
||||
ctr.SafeMethods[i].CallFlag = "Bytes"
|
||||
case "[]interface{}":
|
||||
imports["github.com/nspcc-dev/neo-go/pkg/vm/stackitem"] = struct{}{}
|
||||
ctr.Methods[i].ReturnType = "[]stackitem.Item"
|
||||
ctr.Methods[i].CallFlag = "Array"
|
||||
ctr.SafeMethods[i].ReturnType = "[]stackitem.Item"
|
||||
ctr.SafeMethods[i].CallFlag = "Array"
|
||||
case "*stackitem.Map":
|
||||
ctr.Methods[i].CallFlag = "Map"
|
||||
ctr.SafeMethods[i].CallFlag = "Map"
|
||||
}
|
||||
}
|
||||
|
||||
imports["github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"] = struct{}{}
|
||||
imports["github.com/nspcc-dev/neo-go/pkg/neorpc/result"] = struct{}{}
|
||||
if len(ctr.SafeMethods) > 0 {
|
||||
imports["github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"] = struct{}{}
|
||||
}
|
||||
if len(ctr.Methods) > 0 {
|
||||
imports["github.com/nspcc-dev/neo-go/pkg/core/transaction"] = struct{}{}
|
||||
}
|
||||
if len(ctr.Methods) > 0 || ctr.IsNep17 || ctr.IsNep11D || ctr.IsNep11ND {
|
||||
ctr.HasWriter = true
|
||||
}
|
||||
ctr.Imports = ctr.Imports[:0]
|
||||
for imp := range imports {
|
||||
ctr.Imports = append(ctr.Imports, imp)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue