2016-03-16 10:48:33 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Package bind generates Ethereum contract Go bindings.
|
2016-03-21 12:34:49 +00:00
|
|
|
//
|
|
|
|
// Detailed usage document and tutorial available on the go-ethereum Wiki page:
|
|
|
|
// https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts
|
2016-03-16 10:48:33 +00:00
|
|
|
package bind
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-07-03 10:17:43 +00:00
|
|
|
"errors"
|
2016-03-16 10:48:33 +00:00
|
|
|
"fmt"
|
2018-10-05 20:24:54 +00:00
|
|
|
"go/format"
|
2016-04-05 15:20:50 +00:00
|
|
|
"regexp"
|
2016-03-16 10:48:33 +00:00
|
|
|
"strings"
|
2016-03-21 12:34:49 +00:00
|
|
|
"text/template"
|
|
|
|
"unicode"
|
2016-03-16 10:48:33 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2019-07-08 08:27:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-03-16 10:48:33 +00:00
|
|
|
)
|
|
|
|
|
2016-09-05 16:07:57 +00:00
|
|
|
// Lang is a target programming language selector to generate bindings for.
|
|
|
|
type Lang int
|
|
|
|
|
|
|
|
const (
|
|
|
|
LangGo Lang = iota
|
|
|
|
LangJava
|
2019-07-03 10:17:43 +00:00
|
|
|
LangObjC
|
2016-09-05 16:07:57 +00:00
|
|
|
)
|
|
|
|
|
2016-03-16 10:48:33 +00:00
|
|
|
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
|
|
|
|
// 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.
|
2019-11-14 07:26:10 +00:00
|
|
|
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (string, error) {
|
2019-11-25 13:03:22 +00:00
|
|
|
var (
|
|
|
|
// contracts is the map of each individual contract requested binding
|
|
|
|
contracts = make(map[string]*tmplContract)
|
2016-03-21 12:34:49 +00:00
|
|
|
|
2019-11-25 13:03:22 +00:00
|
|
|
// structs is the map of all reclared structs shared by passed contracts.
|
|
|
|
structs = make(map[string]*tmplStruct)
|
2019-07-08 08:27:05 +00:00
|
|
|
|
2019-11-25 13:03:22 +00:00
|
|
|
// isLib is the map used to flag each encountered library as such
|
|
|
|
isLib = make(map[string]struct{})
|
|
|
|
)
|
2016-03-21 12:34:49 +00:00
|
|
|
for i := 0; i < len(types); i++ {
|
|
|
|
// Parse the actual ABI to generate the binding for
|
|
|
|
evmABI, err := abi.JSON(strings.NewReader(abis[i]))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
// Strip any whitespace from the JSON ABI
|
|
|
|
strippedABI := strings.Map(func(r rune) rune {
|
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, abis[i])
|
|
|
|
|
2019-07-03 10:17:43 +00:00
|
|
|
// Extract the call and transact methods; events, struct definitions; and sort them alphabetically
|
2016-03-21 12:34:49 +00:00
|
|
|
var (
|
|
|
|
calls = make(map[string]*tmplMethod)
|
|
|
|
transacts = make(map[string]*tmplMethod)
|
2018-01-05 10:39:24 +00:00
|
|
|
events = make(map[string]*tmplEvent)
|
2020-04-15 07:23:58 +00:00
|
|
|
fallback *tmplMethod
|
|
|
|
receive *tmplMethod
|
2019-11-14 07:26:10 +00:00
|
|
|
|
|
|
|
// identifiers are used to detect duplicated identifier of function
|
|
|
|
// and event. For all calls, transacts and events, abigen will generate
|
|
|
|
// corresponding bindings. However we have to ensure there is no
|
|
|
|
// identifier coliision in the bindings of these categories.
|
|
|
|
callIdentifiers = make(map[string]bool)
|
|
|
|
transactIdentifiers = make(map[string]bool)
|
|
|
|
eventIdentifiers = make(map[string]bool)
|
2016-03-21 12:34:49 +00:00
|
|
|
)
|
|
|
|
for _, original := range evmABI.Methods {
|
|
|
|
// Normalize the method for capital cases and non-anonymous inputs/outputs
|
|
|
|
normalized := original
|
2019-11-14 07:26:10 +00:00
|
|
|
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
|
|
|
|
// Ensure there is no duplicated identifier
|
|
|
|
var identifiers = callIdentifiers
|
2020-04-15 07:23:58 +00:00
|
|
|
if !original.IsConstant() {
|
2019-11-14 07:26:10 +00:00
|
|
|
identifiers = transactIdentifiers
|
|
|
|
}
|
|
|
|
if identifiers[normalizedName] {
|
|
|
|
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
|
|
|
|
}
|
|
|
|
identifiers[normalizedName] = true
|
|
|
|
normalized.Name = normalizedName
|
2016-03-21 12:34:49 +00:00
|
|
|
normalized.Inputs = make([]abi.Argument, len(original.Inputs))
|
|
|
|
copy(normalized.Inputs, original.Inputs)
|
|
|
|
for j, input := range normalized.Inputs {
|
|
|
|
if input.Name == "" {
|
|
|
|
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
|
|
|
|
}
|
2019-10-31 13:17:51 +00:00
|
|
|
if hasStruct(input.Type) {
|
2019-07-03 10:17:43 +00:00
|
|
|
bindStructType[lang](input.Type, structs)
|
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
}
|
|
|
|
normalized.Outputs = make([]abi.Argument, len(original.Outputs))
|
|
|
|
copy(normalized.Outputs, original.Outputs)
|
|
|
|
for j, output := range normalized.Outputs {
|
|
|
|
if output.Name != "" {
|
|
|
|
normalized.Outputs[j].Name = capitalise(output.Name)
|
|
|
|
}
|
2019-10-31 13:17:51 +00:00
|
|
|
if hasStruct(output.Type) {
|
2019-07-03 10:17:43 +00:00
|
|
|
bindStructType[lang](output.Type, structs)
|
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
}
|
2016-09-05 16:07:57 +00:00
|
|
|
// Append the methods to the call or transact lists
|
2020-04-15 07:23:58 +00:00
|
|
|
if original.IsConstant() {
|
2018-01-05 10:39:24 +00:00
|
|
|
calls[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
|
2016-03-21 12:34:49 +00:00
|
|
|
} else {
|
2018-01-05 10:39:24 +00:00
|
|
|
transacts[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
|
2016-03-21 12:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 10:39:24 +00:00
|
|
|
for _, original := range evmABI.Events {
|
|
|
|
// Skip anonymous events as they don't support explicit filtering
|
|
|
|
if original.Anonymous {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Normalize the event for capital cases and non-anonymous outputs
|
|
|
|
normalized := original
|
2019-11-14 07:26:10 +00:00
|
|
|
|
|
|
|
// Ensure there is no duplicated identifier
|
|
|
|
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
|
|
|
|
if eventIdentifiers[normalizedName] {
|
|
|
|
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
|
|
|
|
}
|
|
|
|
eventIdentifiers[normalizedName] = true
|
|
|
|
normalized.Name = normalizedName
|
2018-01-05 10:39:24 +00:00
|
|
|
|
|
|
|
normalized.Inputs = make([]abi.Argument, len(original.Inputs))
|
|
|
|
copy(normalized.Inputs, original.Inputs)
|
|
|
|
for j, input := range normalized.Inputs {
|
2019-10-31 13:17:51 +00:00
|
|
|
if input.Name == "" {
|
|
|
|
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
|
|
|
|
}
|
|
|
|
if hasStruct(input.Type) {
|
|
|
|
bindStructType[lang](input.Type, structs)
|
2018-01-05 10:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Append the event to the accumulator list
|
|
|
|
events[original.Name] = &tmplEvent{Original: original, Normalized: normalized}
|
|
|
|
}
|
2020-04-15 07:23:58 +00:00
|
|
|
// Add two special fallback functions if they exist
|
|
|
|
if evmABI.HasFallback() {
|
|
|
|
fallback = &tmplMethod{Original: evmABI.Fallback}
|
|
|
|
}
|
|
|
|
if evmABI.HasReceive() {
|
|
|
|
receive = &tmplMethod{Original: evmABI.Receive}
|
|
|
|
}
|
2019-07-03 10:17:43 +00:00
|
|
|
// There is no easy way to pass arbitrary java objects to the Go side.
|
|
|
|
if len(structs) > 0 && lang == LangJava {
|
|
|
|
return "", errors.New("java binding for tuple arguments is not supported yet")
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:34:49 +00:00
|
|
|
contracts[types[i]] = &tmplContract{
|
|
|
|
Type: capitalise(types[i]),
|
2016-09-05 16:07:57 +00:00
|
|
|
InputABI: strings.Replace(strippedABI, "\"", "\\\"", -1),
|
2019-07-08 08:27:05 +00:00
|
|
|
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
|
2016-03-21 12:34:49 +00:00
|
|
|
Constructor: evmABI.Constructor,
|
|
|
|
Calls: calls,
|
|
|
|
Transacts: transacts,
|
2020-04-15 07:23:58 +00:00
|
|
|
Fallback: fallback,
|
|
|
|
Receive: receive,
|
2018-01-05 10:39:24 +00:00
|
|
|
Events: events,
|
2019-07-08 08:27:05 +00:00
|
|
|
Libraries: make(map[string]string),
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2019-07-08 08:27:05 +00:00
|
|
|
// Function 4-byte signatures are stored in the same sequence
|
|
|
|
// as types, if available.
|
2019-07-02 07:52:58 +00:00
|
|
|
if len(fsigs) > i {
|
|
|
|
contracts[types[i]].FuncSigs = fsigs[i]
|
|
|
|
}
|
2019-07-08 08:27:05 +00:00
|
|
|
// Parse library references.
|
|
|
|
for pattern, name := range libs {
|
|
|
|
matched, err := regexp.Match("__\\$"+pattern+"\\$__", []byte(contracts[types[i]].InputBin))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not search for pattern", "pattern", pattern, "contract", contracts[types[i]], "err", err)
|
|
|
|
}
|
|
|
|
if matched {
|
|
|
|
contracts[types[i]].Libraries[pattern] = name
|
|
|
|
// keep track that this type is a library
|
|
|
|
if _, ok := isLib[name]; !ok {
|
|
|
|
isLib[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if that type has already been identified as a library
|
|
|
|
for i := 0; i < len(types); i++ {
|
|
|
|
_, ok := isLib[types[i]]
|
|
|
|
contracts[types[i]].Library = ok
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
// Generate the contract template data content and render it
|
|
|
|
data := &tmplData{
|
|
|
|
Package: pkg,
|
|
|
|
Contracts: contracts,
|
2019-07-08 08:27:05 +00:00
|
|
|
Libraries: libs,
|
2019-11-25 13:03:22 +00:00
|
|
|
Structs: structs,
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
buffer := new(bytes.Buffer)
|
2016-03-16 10:48:33 +00:00
|
|
|
|
2016-03-21 12:34:49 +00:00
|
|
|
funcs := map[string]interface{}{
|
2018-01-05 10:39:24 +00:00
|
|
|
"bindtype": bindType[lang],
|
|
|
|
"bindtopictype": bindTopicType[lang],
|
|
|
|
"namedtype": namedType[lang],
|
2019-07-03 10:17:43 +00:00
|
|
|
"formatmethod": formatMethod,
|
|
|
|
"formatevent": formatEvent,
|
2018-01-05 10:39:24 +00:00
|
|
|
"capitalise": capitalise,
|
|
|
|
"decapitalise": decapitalise,
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
2016-09-05 16:07:57 +00:00
|
|
|
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource[lang]))
|
2016-03-21 12:34:49 +00:00
|
|
|
if err := tmpl.Execute(buffer, data); err != nil {
|
|
|
|
return "", err
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
2018-10-05 20:24:54 +00:00
|
|
|
// For Go bindings pass the code through gofmt to clean it up
|
2016-11-11 08:27:30 +00:00
|
|
|
if lang == LangGo {
|
2018-10-05 20:24:54 +00:00
|
|
|
code, err := format.Source(buffer.Bytes())
|
2016-11-11 08:27:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%v\n%s", err, buffer)
|
|
|
|
}
|
|
|
|
return string(code), nil
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2016-11-11 08:27:30 +00:00
|
|
|
// For all others just return as is for now
|
2017-12-12 18:05:47 +00:00
|
|
|
return buffer.String(), nil
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 16:07:57 +00:00
|
|
|
// bindType is a set of type binders that convert Solidity types to some supported
|
2018-01-05 10:39:24 +00:00
|
|
|
// programming language types.
|
2019-07-03 10:17:43 +00:00
|
|
|
var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
2016-09-05 16:07:57 +00:00
|
|
|
LangGo: bindTypeGo,
|
|
|
|
LangJava: bindTypeJava,
|
|
|
|
}
|
|
|
|
|
2019-06-27 08:48:13 +00:00
|
|
|
// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go one.
|
|
|
|
func bindBasicTypeGo(kind abi.Type) string {
|
|
|
|
switch kind.T {
|
|
|
|
case abi.AddressTy:
|
|
|
|
return "common.Address"
|
|
|
|
case abi.IntTy, abi.UintTy:
|
|
|
|
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
|
2016-04-05 15:20:50 +00:00
|
|
|
switch parts[2] {
|
2016-03-16 10:48:33 +00:00
|
|
|
case "8", "16", "32", "64":
|
2019-06-27 08:48:13 +00:00
|
|
|
return fmt.Sprintf("%sint%s", parts[1], parts[2])
|
2016-04-05 15:20:50 +00:00
|
|
|
}
|
2019-06-27 08:48:13 +00:00
|
|
|
return "*big.Int"
|
|
|
|
case abi.FixedBytesTy:
|
|
|
|
return fmt.Sprintf("[%d]byte", kind.Size)
|
|
|
|
case abi.BytesTy:
|
|
|
|
return "[]byte"
|
|
|
|
case abi.FunctionTy:
|
2019-07-02 07:52:58 +00:00
|
|
|
return "[24]byte"
|
2016-03-16 10:48:33 +00:00
|
|
|
default:
|
2019-06-27 08:48:13 +00:00
|
|
|
// string, bool types
|
|
|
|
return kind.String()
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 08:48:13 +00:00
|
|
|
// bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
|
|
|
|
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
|
2016-09-05 16:07:57 +00:00
|
|
|
// mapped will use an upscaled type (e.g. BigDecimal).
|
2019-07-03 10:17:43 +00:00
|
|
|
func bindTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
|
2019-06-27 08:48:13 +00:00
|
|
|
switch kind.T {
|
2019-07-03 10:17:43 +00:00
|
|
|
case abi.TupleTy:
|
2019-10-31 13:17:51 +00:00
|
|
|
return structs[kind.TupleRawName+kind.String()].Name
|
2019-06-27 08:48:13 +00:00
|
|
|
case abi.ArrayTy:
|
2019-07-03 10:17:43 +00:00
|
|
|
return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem, structs)
|
2019-06-27 08:48:13 +00:00
|
|
|
case abi.SliceTy:
|
2019-07-03 10:17:43 +00:00
|
|
|
return "[]" + bindTypeGo(*kind.Elem, structs)
|
2019-06-27 08:48:13 +00:00
|
|
|
default:
|
|
|
|
return bindBasicTypeGo(kind)
|
|
|
|
}
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 08:48:13 +00:00
|
|
|
// bindBasicTypeJava converts basic solidity types(except array, slice and tuple) to Java one.
|
|
|
|
func bindBasicTypeJava(kind abi.Type) string {
|
|
|
|
switch kind.T {
|
|
|
|
case abi.AddressTy:
|
|
|
|
return "Address"
|
|
|
|
case abi.IntTy, abi.UintTy:
|
|
|
|
// Note that uint and int (without digits) are also matched,
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
// these are size 256, and will translate to BigInt (the default).
|
2019-06-27 08:48:13 +00:00
|
|
|
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
if len(parts) != 3 {
|
2019-06-27 08:48:13 +00:00
|
|
|
return kind.String()
|
|
|
|
}
|
|
|
|
// All unsigned integers should be translated to BigInt since gomobile doesn't
|
|
|
|
// support them.
|
|
|
|
if parts[1] == "u" {
|
|
|
|
return "BigInt"
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
|
|
|
|
namedSize := map[string]string{
|
|
|
|
"8": "byte",
|
|
|
|
"16": "short",
|
|
|
|
"32": "int",
|
|
|
|
"64": "long",
|
|
|
|
}[parts[2]]
|
|
|
|
|
2019-06-27 08:48:13 +00:00
|
|
|
// default to BigInt
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
if namedSize == "" {
|
|
|
|
namedSize = "BigInt"
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
2019-06-27 08:48:13 +00:00
|
|
|
return namedSize
|
|
|
|
case abi.FixedBytesTy, abi.BytesTy:
|
|
|
|
return "byte[]"
|
|
|
|
case abi.BoolTy:
|
|
|
|
return "boolean"
|
|
|
|
case abi.StringTy:
|
|
|
|
return "String"
|
|
|
|
case abi.FunctionTy:
|
2019-07-02 07:52:58 +00:00
|
|
|
return "byte[24]"
|
2019-06-27 08:48:13 +00:00
|
|
|
default:
|
|
|
|
return kind.String()
|
|
|
|
}
|
|
|
|
}
|
2016-09-05 16:07:57 +00:00
|
|
|
|
2019-07-03 10:17:43 +00:00
|
|
|
// pluralizeJavaType explicitly converts multidimensional types to predefined
|
|
|
|
// type in go side.
|
|
|
|
func pluralizeJavaType(typ string) string {
|
|
|
|
switch typ {
|
|
|
|
case "boolean":
|
|
|
|
return "Bools"
|
|
|
|
case "String":
|
|
|
|
return "Strings"
|
|
|
|
case "Address":
|
|
|
|
return "Addresses"
|
|
|
|
case "byte[]":
|
|
|
|
return "Binaries"
|
|
|
|
case "BigInt":
|
|
|
|
return "BigInts"
|
|
|
|
}
|
|
|
|
return typ + "[]"
|
|
|
|
}
|
|
|
|
|
2019-06-27 08:48:13 +00:00
|
|
|
// bindTypeJava converts a Solidity type to a Java one. Since there is no clear mapping
|
|
|
|
// from all Solidity types to Java ones (e.g. uint17), those that cannot be exactly
|
|
|
|
// mapped will use an upscaled type (e.g. BigDecimal).
|
2019-07-03 10:17:43 +00:00
|
|
|
func bindTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
|
2019-06-27 08:48:13 +00:00
|
|
|
switch kind.T {
|
2019-07-03 10:17:43 +00:00
|
|
|
case abi.TupleTy:
|
2019-10-31 13:17:51 +00:00
|
|
|
return structs[kind.TupleRawName+kind.String()].Name
|
2019-06-27 08:48:13 +00:00
|
|
|
case abi.ArrayTy, abi.SliceTy:
|
2019-07-03 10:17:43 +00:00
|
|
|
return pluralizeJavaType(bindTypeJava(*kind.Elem, structs))
|
2016-09-05 16:07:57 +00:00
|
|
|
default:
|
2019-06-27 08:48:13 +00:00
|
|
|
return bindBasicTypeJava(kind)
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 10:39:24 +00:00
|
|
|
// bindTopicType is a set of type binders that convert Solidity types to some
|
|
|
|
// supported programming language topic types.
|
2019-07-03 10:17:43 +00:00
|
|
|
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
2018-01-05 10:39:24 +00:00
|
|
|
LangGo: bindTopicTypeGo,
|
|
|
|
LangJava: bindTopicTypeJava,
|
|
|
|
}
|
|
|
|
|
2019-07-04 08:00:34 +00:00
|
|
|
// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
|
2018-01-05 10:39:24 +00:00
|
|
|
// funcionality as for simple types, but dynamic types get converted to hashes.
|
2019-07-03 10:17:43 +00:00
|
|
|
func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
|
|
|
|
bound := bindTypeGo(kind, structs)
|
2019-10-31 13:17:51 +00:00
|
|
|
|
|
|
|
// todo(rjl493456442) according solidity documentation, indexed event
|
|
|
|
// parameters that are not value types i.e. arrays and structs are not
|
|
|
|
// stored directly but instead a keccak256-hash of an encoding is stored.
|
|
|
|
//
|
|
|
|
// We only convert stringS and bytes to hash, still need to deal with
|
|
|
|
// array(both fixed-size and dynamic-size) and struct.
|
2018-01-05 10:39:24 +00:00
|
|
|
if bound == "string" || bound == "[]byte" {
|
|
|
|
bound = "common.Hash"
|
|
|
|
}
|
|
|
|
return bound
|
|
|
|
}
|
|
|
|
|
2019-07-04 08:00:34 +00:00
|
|
|
// bindTopicTypeJava converts a Solidity topic type to a Java one. It is almost the same
|
2018-01-05 10:39:24 +00:00
|
|
|
// funcionality as for simple types, but dynamic types get converted to hashes.
|
2019-07-03 10:17:43 +00:00
|
|
|
func bindTopicTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
|
|
|
|
bound := bindTypeJava(kind, structs)
|
2019-10-31 13:17:51 +00:00
|
|
|
|
|
|
|
// todo(rjl493456442) according solidity documentation, indexed event
|
|
|
|
// parameters that are not value types i.e. arrays and structs are not
|
|
|
|
// stored directly but instead a keccak256-hash of an encoding is stored.
|
|
|
|
//
|
|
|
|
// We only convert stringS and bytes to hash, still need to deal with
|
|
|
|
// array(both fixed-size and dynamic-size) and struct.
|
2019-06-27 08:48:13 +00:00
|
|
|
if bound == "String" || bound == "byte[]" {
|
2018-01-05 10:39:24 +00:00
|
|
|
bound = "Hash"
|
|
|
|
}
|
|
|
|
return bound
|
|
|
|
}
|
|
|
|
|
2019-07-03 10:17:43 +00:00
|
|
|
// bindStructType is a set of type binders that convert Solidity tuple types to some supported
|
|
|
|
// programming language struct definition.
|
|
|
|
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
|
|
|
|
LangGo: bindStructTypeGo,
|
|
|
|
LangJava: bindStructTypeJava,
|
|
|
|
}
|
|
|
|
|
|
|
|
// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
|
|
|
|
// in the given map.
|
|
|
|
// Notably, this function will resolve and record nested struct recursively.
|
|
|
|
func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
|
|
|
|
switch kind.T {
|
|
|
|
case abi.TupleTy:
|
2019-10-31 13:17:51 +00:00
|
|
|
// We compose raw struct name and canonical parameter expression
|
|
|
|
// together here. The reason is before solidity v0.5.11, kind.TupleRawName
|
|
|
|
// is empty, so we use canonical parameter expression to distinguish
|
|
|
|
// different struct definition. From the consideration of backward
|
|
|
|
// compatibility, we concat these two together so that if kind.TupleRawName
|
|
|
|
// is not empty, it can have unique id.
|
|
|
|
id := kind.TupleRawName + kind.String()
|
|
|
|
if s, exist := structs[id]; exist {
|
2019-07-03 10:17:43 +00:00
|
|
|
return s.Name
|
|
|
|
}
|
|
|
|
var fields []*tmplField
|
|
|
|
for i, elem := range kind.TupleElems {
|
|
|
|
field := bindStructTypeGo(*elem, structs)
|
|
|
|
fields = append(fields, &tmplField{Type: field, Name: capitalise(kind.TupleRawNames[i]), SolKind: *elem})
|
|
|
|
}
|
2019-10-31 13:17:51 +00:00
|
|
|
name := kind.TupleRawName
|
|
|
|
if name == "" {
|
|
|
|
name = fmt.Sprintf("Struct%d", len(structs))
|
|
|
|
}
|
|
|
|
structs[id] = &tmplStruct{
|
2019-07-03 10:17:43 +00:00
|
|
|
Name: name,
|
|
|
|
Fields: fields,
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
case abi.ArrayTy:
|
|
|
|
return fmt.Sprintf("[%d]", kind.Size) + bindStructTypeGo(*kind.Elem, structs)
|
|
|
|
case abi.SliceTy:
|
|
|
|
return "[]" + bindStructTypeGo(*kind.Elem, structs)
|
|
|
|
default:
|
|
|
|
return bindBasicTypeGo(kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bindStructTypeJava converts a Solidity tuple type to a Java one and records the mapping
|
|
|
|
// in the given map.
|
|
|
|
// Notably, this function will resolve and record nested struct recursively.
|
|
|
|
func bindStructTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
|
|
|
|
switch kind.T {
|
|
|
|
case abi.TupleTy:
|
2019-10-31 13:17:51 +00:00
|
|
|
// We compose raw struct name and canonical parameter expression
|
|
|
|
// together here. The reason is before solidity v0.5.11, kind.TupleRawName
|
|
|
|
// is empty, so we use canonical parameter expression to distinguish
|
|
|
|
// different struct definition. From the consideration of backward
|
|
|
|
// compatibility, we concat these two together so that if kind.TupleRawName
|
|
|
|
// is not empty, it can have unique id.
|
|
|
|
id := kind.TupleRawName + kind.String()
|
|
|
|
if s, exist := structs[id]; exist {
|
2019-07-03 10:17:43 +00:00
|
|
|
return s.Name
|
|
|
|
}
|
|
|
|
var fields []*tmplField
|
|
|
|
for i, elem := range kind.TupleElems {
|
|
|
|
field := bindStructTypeJava(*elem, structs)
|
|
|
|
fields = append(fields, &tmplField{Type: field, Name: decapitalise(kind.TupleRawNames[i]), SolKind: *elem})
|
|
|
|
}
|
2019-10-31 13:17:51 +00:00
|
|
|
name := kind.TupleRawName
|
|
|
|
if name == "" {
|
|
|
|
name = fmt.Sprintf("Class%d", len(structs))
|
|
|
|
}
|
|
|
|
structs[id] = &tmplStruct{
|
2019-07-03 10:17:43 +00:00
|
|
|
Name: name,
|
|
|
|
Fields: fields,
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
case abi.ArrayTy, abi.SliceTy:
|
|
|
|
return pluralizeJavaType(bindStructTypeJava(*kind.Elem, structs))
|
|
|
|
default:
|
|
|
|
return bindBasicTypeJava(kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:07:57 +00:00
|
|
|
// namedType is a set of functions that transform language specific types to
|
|
|
|
// named versions that my be used inside method names.
|
|
|
|
var namedType = map[Lang]func(string, abi.Type) string{
|
|
|
|
LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") },
|
|
|
|
LangJava: namedTypeJava,
|
|
|
|
}
|
|
|
|
|
|
|
|
// namedTypeJava converts some primitive data types to named variants that can
|
|
|
|
// be used as parts of method names.
|
|
|
|
func namedTypeJava(javaKind string, solKind abi.Type) string {
|
|
|
|
switch javaKind {
|
|
|
|
case "byte[]":
|
|
|
|
return "Binary"
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
case "boolean":
|
2016-09-05 16:07:57 +00:00
|
|
|
return "Bool"
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
default:
|
2017-01-06 15:44:20 +00:00
|
|
|
parts := regexp.MustCompile(`(u)?int([0-9]*)(\[[0-9]*\])?`).FindStringSubmatch(solKind.String())
|
2016-09-05 16:07:57 +00:00
|
|
|
if len(parts) != 4 {
|
|
|
|
return javaKind
|
|
|
|
}
|
|
|
|
switch parts[2] {
|
|
|
|
case "8", "16", "32", "64":
|
|
|
|
if parts[3] == "" {
|
|
|
|
return capitalise(fmt.Sprintf("%sint%s", parts[1], parts[2]))
|
|
|
|
}
|
|
|
|
return capitalise(fmt.Sprintf("%sint%ss", parts[1], parts[2]))
|
|
|
|
|
|
|
|
default:
|
|
|
|
return javaKind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 07:26:10 +00:00
|
|
|
// alias returns an alias of the given string based on the aliasing rules
|
|
|
|
// or returns itself if no rule is matched.
|
|
|
|
func alias(aliases map[string]string, n string) string {
|
|
|
|
if alias, exist := aliases[n]; exist {
|
|
|
|
return alias
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:07:57 +00:00
|
|
|
// methodNormalizer is a name transformer that modifies Solidity method names to
|
|
|
|
// conform to target language naming concentions.
|
|
|
|
var methodNormalizer = map[Lang]func(string) string{
|
2019-01-02 09:22:10 +00:00
|
|
|
LangGo: abi.ToCamelCase,
|
2016-09-05 16:07:57 +00:00
|
|
|
LangJava: decapitalise,
|
|
|
|
}
|
|
|
|
|
2018-03-07 12:34:53 +00:00
|
|
|
// capitalise makes a camel-case string which starts with an upper case character.
|
2016-03-21 12:34:49 +00:00
|
|
|
func capitalise(input string) string {
|
2018-12-29 10:32:58 +00:00
|
|
|
return abi.ToCamelCase(input)
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 12:34:53 +00:00
|
|
|
// decapitalise makes a camel-case string which starts with a lower case character.
|
2016-09-05 16:07:57 +00:00
|
|
|
func decapitalise(input string) string {
|
2019-01-02 09:22:10 +00:00
|
|
|
if len(input) == 0 {
|
|
|
|
return input
|
|
|
|
}
|
2018-03-07 12:34:53 +00:00
|
|
|
|
2019-01-02 09:22:10 +00:00
|
|
|
goForm := abi.ToCamelCase(input)
|
|
|
|
return strings.ToLower(goForm[:1]) + goForm[1:]
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 10:39:24 +00:00
|
|
|
// structured checks whether a list of ABI data types has enough information to
|
|
|
|
// operate through a proper Go struct or if flat returns are needed.
|
|
|
|
func structured(args abi.Arguments) bool {
|
|
|
|
if len(args) < 2 {
|
2016-03-21 12:34:49 +00:00
|
|
|
return false
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2017-12-29 21:20:02 +00:00
|
|
|
exists := make(map[string]bool)
|
2018-01-05 10:39:24 +00:00
|
|
|
for _, out := range args {
|
2017-12-29 21:20:02 +00:00
|
|
|
// If the name is anonymous, we can't organize into a struct
|
2016-03-21 12:34:49 +00:00
|
|
|
if out.Name == "" {
|
|
|
|
return false
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2017-12-29 21:20:02 +00:00
|
|
|
// If the field name is empty when normalized or collides (var, Var, _var, _Var),
|
|
|
|
// we can't organize into a struct
|
|
|
|
field := capitalise(out.Name)
|
|
|
|
if field == "" || exists[field] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
exists[field] = true
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
return true
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2019-07-03 10:17:43 +00:00
|
|
|
|
2019-10-31 13:17:51 +00:00
|
|
|
// hasStruct returns an indicator whether the given type is struct, struct slice
|
|
|
|
// or struct array.
|
|
|
|
func hasStruct(t abi.Type) bool {
|
|
|
|
switch t.T {
|
|
|
|
case abi.SliceTy:
|
|
|
|
return hasStruct(*t.Elem)
|
|
|
|
case abi.ArrayTy:
|
|
|
|
return hasStruct(*t.Elem)
|
|
|
|
case abi.TupleTy:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 10:17:43 +00:00
|
|
|
// resolveArgName converts a raw argument representation into a user friendly format.
|
|
|
|
func resolveArgName(arg abi.Argument, structs map[string]*tmplStruct) string {
|
|
|
|
var (
|
|
|
|
prefix string
|
|
|
|
embedded string
|
|
|
|
typ = &arg.Type
|
|
|
|
)
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
switch typ.T {
|
|
|
|
case abi.SliceTy:
|
|
|
|
prefix += "[]"
|
|
|
|
case abi.ArrayTy:
|
|
|
|
prefix += fmt.Sprintf("[%d]", typ.Size)
|
|
|
|
default:
|
2019-10-31 13:17:51 +00:00
|
|
|
embedded = typ.TupleRawName + typ.String()
|
2019-07-03 10:17:43 +00:00
|
|
|
break loop
|
|
|
|
}
|
|
|
|
typ = typ.Elem
|
|
|
|
}
|
|
|
|
if s, exist := structs[embedded]; exist {
|
|
|
|
return prefix + s.Name
|
|
|
|
} else {
|
|
|
|
return arg.Type.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatMethod transforms raw method representation into a user friendly one.
|
|
|
|
func formatMethod(method abi.Method, structs map[string]*tmplStruct) string {
|
|
|
|
inputs := make([]string, len(method.Inputs))
|
|
|
|
for i, input := range method.Inputs {
|
|
|
|
inputs[i] = fmt.Sprintf("%v %v", resolveArgName(input, structs), input.Name)
|
|
|
|
}
|
|
|
|
outputs := make([]string, len(method.Outputs))
|
|
|
|
for i, output := range method.Outputs {
|
|
|
|
outputs[i] = resolveArgName(output, structs)
|
|
|
|
if len(output.Name) > 0 {
|
|
|
|
outputs[i] += fmt.Sprintf(" %v", output.Name)
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 07:23:58 +00:00
|
|
|
// Extract meaningful state mutability of solidity method.
|
|
|
|
// If it's default value, never print it.
|
|
|
|
state := method.StateMutability
|
|
|
|
if state == "nonpayable" {
|
|
|
|
state = ""
|
|
|
|
}
|
|
|
|
if state != "" {
|
|
|
|
state = state + " "
|
|
|
|
}
|
|
|
|
identity := fmt.Sprintf("function %v", method.RawName)
|
|
|
|
if method.IsFallback {
|
|
|
|
identity = "fallback"
|
|
|
|
} else if method.IsReceive {
|
|
|
|
identity = "receive"
|
2019-07-03 10:17:43 +00:00
|
|
|
}
|
2020-04-15 07:23:58 +00:00
|
|
|
return fmt.Sprintf("%s(%v) %sreturns(%v)", identity, strings.Join(inputs, ", "), state, strings.Join(outputs, ", "))
|
2019-07-03 10:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// formatEvent transforms raw event representation into a user friendly one.
|
|
|
|
func formatEvent(event abi.Event, structs map[string]*tmplStruct) string {
|
|
|
|
inputs := make([]string, len(event.Inputs))
|
|
|
|
for i, input := range event.Inputs {
|
|
|
|
if input.Indexed {
|
|
|
|
inputs[i] = fmt.Sprintf("%v indexed %v", resolveArgName(input, structs), input.Name)
|
|
|
|
} else {
|
|
|
|
inputs[i] = fmt.Sprintf("%v %v", resolveArgName(input, structs), input.Name)
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 07:20:46 +00:00
|
|
|
return fmt.Sprintf("event %v(%v)", event.RawName, strings.Join(inputs, ", "))
|
2019-07-03 10:17:43 +00:00
|
|
|
}
|