|
@@ -19,6 +19,7 @@ package abi
|
|
|
import (
|
|
import (
|
|
|
"bytes"
|
|
"bytes"
|
|
|
"encoding/hex"
|
|
"encoding/hex"
|
|
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"math/big"
|
|
"math/big"
|
|
|
"reflect"
|
|
"reflect"
|
|
@@ -26,17 +27,13 @@ import (
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
+ "github.com/ethereum/go-ethereum/common/math"
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
const jsondata = `
|
|
const jsondata = `
|
|
|
[
|
|
[
|
|
|
- { "type" : "function", "name" : "balance", "stateMutability" : "view" },
|
|
|
|
|
- { "type" : "function", "name" : "send", "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
|
|
|
|
|
-]`
|
|
|
|
|
-
|
|
|
|
|
-const jsondata2 = `
|
|
|
|
|
-[
|
|
|
|
|
|
|
+ { "type" : "function", "name" : ""},
|
|
|
{ "type" : "function", "name" : "balance", "stateMutability" : "view" },
|
|
{ "type" : "function", "name" : "balance", "stateMutability" : "view" },
|
|
|
{ "type" : "function", "name" : "send", "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
|
|
{ "type" : "function", "name" : "send", "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
|
|
|
{ "type" : "function", "name" : "test", "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
|
|
{ "type" : "function", "name" : "test", "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
|
|
@@ -45,6 +42,8 @@ const jsondata2 = `
|
|
|
{ "type" : "function", "name" : "address", "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
|
|
{ "type" : "function", "name" : "address", "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
|
|
|
{ "type" : "function", "name" : "uint64[2]", "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
|
|
{ "type" : "function", "name" : "uint64[2]", "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
|
|
|
{ "type" : "function", "name" : "uint64[]", "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
|
|
{ "type" : "function", "name" : "uint64[]", "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "int8", "inputs" : [ { "name" : "inputs", "type" : "int8" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "bytes32", "inputs" : [ { "name" : "inputs", "type" : "bytes32" } ] },
|
|
|
{ "type" : "function", "name" : "foo", "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
|
|
{ "type" : "function", "name" : "foo", "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
|
|
|
{ "type" : "function", "name" : "bar", "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
|
|
{ "type" : "function", "name" : "bar", "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
|
|
|
{ "type" : "function", "name" : "slice", "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
|
|
{ "type" : "function", "name" : "slice", "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
|
|
@@ -53,30 +52,83 @@ const jsondata2 = `
|
|
|
{ "type" : "function", "name" : "sliceMultiAddress", "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] },
|
|
{ "type" : "function", "name" : "sliceMultiAddress", "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] },
|
|
|
{ "type" : "function", "name" : "nestedArray", "inputs" : [ { "name" : "a", "type" : "uint256[2][2]" }, { "name" : "b", "type" : "address[]" } ] },
|
|
{ "type" : "function", "name" : "nestedArray", "inputs" : [ { "name" : "a", "type" : "uint256[2][2]" }, { "name" : "b", "type" : "address[]" } ] },
|
|
|
{ "type" : "function", "name" : "nestedArray2", "inputs" : [ { "name" : "a", "type" : "uint8[][2]" } ] },
|
|
{ "type" : "function", "name" : "nestedArray2", "inputs" : [ { "name" : "a", "type" : "uint8[][2]" } ] },
|
|
|
- { "type" : "function", "name" : "nestedSlice", "inputs" : [ { "name" : "a", "type" : "uint8[][]" } ] }
|
|
|
|
|
|
|
+ { "type" : "function", "name" : "nestedSlice", "inputs" : [ { "name" : "a", "type" : "uint8[][]" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "receive", "inputs" : [ { "name" : "memo", "type" : "bytes" }], "outputs" : [], "payable" : true, "stateMutability" : "payable" },
|
|
|
|
|
+ { "type" : "function", "name" : "fixedArrStr", "stateMutability" : "view", "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "fixedArrBytes", "stateMutability" : "view", "inputs" : [ { "name" : "bytes", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "mixedArrStr", "stateMutability" : "view", "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "doubleFixedArrStr", "stateMutability" : "view", "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type" : "uint256[2]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "multipleMixedArrStr", "stateMutability" : "view", "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type" : "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] },
|
|
|
|
|
+ { "type" : "function", "name" : "overloadedNames", "stateMutability" : "view", "inputs": [ { "components": [ { "internalType": "uint256", "name": "_f", "type": "uint256" }, { "internalType": "uint256", "name": "__f", "type": "uint256"}, { "internalType": "uint256", "name": "f", "type": "uint256"}],"internalType": "struct Overloader.F", "name": "f","type": "tuple"}]}
|
|
|
]`
|
|
]`
|
|
|
|
|
|
|
|
|
|
+var (
|
|
|
|
|
+ Uint256, _ = NewType("uint256", "", nil)
|
|
|
|
|
+ Uint32, _ = NewType("uint32", "", nil)
|
|
|
|
|
+ Uint16, _ = NewType("uint16", "", nil)
|
|
|
|
|
+ String, _ = NewType("string", "", nil)
|
|
|
|
|
+ Bool, _ = NewType("bool", "", nil)
|
|
|
|
|
+ Bytes, _ = NewType("bytes", "", nil)
|
|
|
|
|
+ Bytes32, _ = NewType("bytes32", "", nil)
|
|
|
|
|
+ Address, _ = NewType("address", "", nil)
|
|
|
|
|
+ Uint64Arr, _ = NewType("uint64[]", "", nil)
|
|
|
|
|
+ AddressArr, _ = NewType("address[]", "", nil)
|
|
|
|
|
+ Int8, _ = NewType("int8", "", nil)
|
|
|
|
|
+ // Special types for testing
|
|
|
|
|
+ Uint32Arr2, _ = NewType("uint32[2]", "", nil)
|
|
|
|
|
+ Uint64Arr2, _ = NewType("uint64[2]", "", nil)
|
|
|
|
|
+ Uint256Arr, _ = NewType("uint256[]", "", nil)
|
|
|
|
|
+ Uint256Arr2, _ = NewType("uint256[2]", "", nil)
|
|
|
|
|
+ Uint256Arr3, _ = NewType("uint256[3]", "", nil)
|
|
|
|
|
+ Uint256ArrNested, _ = NewType("uint256[2][2]", "", nil)
|
|
|
|
|
+ Uint8ArrNested, _ = NewType("uint8[][2]", "", nil)
|
|
|
|
|
+ Uint8SliceNested, _ = NewType("uint8[][]", "", nil)
|
|
|
|
|
+ TupleF, _ = NewType("tuple", "struct Overloader.F", []ArgumentMarshaling{
|
|
|
|
|
+ {Name: "_f", Type: "uint256"},
|
|
|
|
|
+ {Name: "__f", Type: "uint256"},
|
|
|
|
|
+ {Name: "f", Type: "uint256"}})
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+var methods = map[string]Method{
|
|
|
|
|
+ "": NewMethod("", "", Function, "", false, false, nil, nil),
|
|
|
|
|
+ "balance": NewMethod("balance", "balance", Function, "view", false, false, nil, nil),
|
|
|
|
|
+ "send": NewMethod("send", "send", Function, "", false, false, []Argument{{"amount", Uint256, false}}, nil),
|
|
|
|
|
+ "test": NewMethod("test", "test", Function, "", false, false, []Argument{{"number", Uint32, false}}, nil),
|
|
|
|
|
+ "string": NewMethod("string", "string", Function, "", false, false, []Argument{{"inputs", String, false}}, nil),
|
|
|
|
|
+ "bool": NewMethod("bool", "bool", Function, "", false, false, []Argument{{"inputs", Bool, false}}, nil),
|
|
|
|
|
+ "address": NewMethod("address", "address", Function, "", false, false, []Argument{{"inputs", Address, false}}, nil),
|
|
|
|
|
+ "uint64[]": NewMethod("uint64[]", "uint64[]", Function, "", false, false, []Argument{{"inputs", Uint64Arr, false}}, nil),
|
|
|
|
|
+ "uint64[2]": NewMethod("uint64[2]", "uint64[2]", Function, "", false, false, []Argument{{"inputs", Uint64Arr2, false}}, nil),
|
|
|
|
|
+ "int8": NewMethod("int8", "int8", Function, "", false, false, []Argument{{"inputs", Int8, false}}, nil),
|
|
|
|
|
+ "bytes32": NewMethod("bytes32", "bytes32", Function, "", false, false, []Argument{{"inputs", Bytes32, false}}, nil),
|
|
|
|
|
+ "foo": NewMethod("foo", "foo", Function, "", false, false, []Argument{{"inputs", Uint32, false}}, nil),
|
|
|
|
|
+ "bar": NewMethod("bar", "bar", Function, "", false, false, []Argument{{"inputs", Uint32, false}, {"string", Uint16, false}}, nil),
|
|
|
|
|
+ "slice": NewMethod("slice", "slice", Function, "", false, false, []Argument{{"inputs", Uint32Arr2, false}}, nil),
|
|
|
|
|
+ "slice256": NewMethod("slice256", "slice256", Function, "", false, false, []Argument{{"inputs", Uint256Arr2, false}}, nil),
|
|
|
|
|
+ "sliceAddress": NewMethod("sliceAddress", "sliceAddress", Function, "", false, false, []Argument{{"inputs", AddressArr, false}}, nil),
|
|
|
|
|
+ "sliceMultiAddress": NewMethod("sliceMultiAddress", "sliceMultiAddress", Function, "", false, false, []Argument{{"a", AddressArr, false}, {"b", AddressArr, false}}, nil),
|
|
|
|
|
+ "nestedArray": NewMethod("nestedArray", "nestedArray", Function, "", false, false, []Argument{{"a", Uint256ArrNested, false}, {"b", AddressArr, false}}, nil),
|
|
|
|
|
+ "nestedArray2": NewMethod("nestedArray2", "nestedArray2", Function, "", false, false, []Argument{{"a", Uint8ArrNested, false}}, nil),
|
|
|
|
|
+ "nestedSlice": NewMethod("nestedSlice", "nestedSlice", Function, "", false, false, []Argument{{"a", Uint8SliceNested, false}}, nil),
|
|
|
|
|
+ "receive": NewMethod("receive", "receive", Function, "payable", false, true, []Argument{{"memo", Bytes, false}}, []Argument{}),
|
|
|
|
|
+ "fixedArrStr": NewMethod("fixedArrStr", "fixedArrStr", Function, "view", false, false, []Argument{{"str", String, false}, {"fixedArr", Uint256Arr2, false}}, nil),
|
|
|
|
|
+ "fixedArrBytes": NewMethod("fixedArrBytes", "fixedArrBytes", Function, "view", false, false, []Argument{{"bytes", Bytes, false}, {"fixedArr", Uint256Arr2, false}}, nil),
|
|
|
|
|
+ "mixedArrStr": NewMethod("mixedArrStr", "mixedArrStr", Function, "view", false, false, []Argument{{"str", String, false}, {"fixedArr", Uint256Arr2, false}, {"dynArr", Uint256Arr, false}}, nil),
|
|
|
|
|
+ "doubleFixedArrStr": NewMethod("doubleFixedArrStr", "doubleFixedArrStr", Function, "view", false, false, []Argument{{"str", String, false}, {"fixedArr1", Uint256Arr2, false}, {"fixedArr2", Uint256Arr3, false}}, nil),
|
|
|
|
|
+ "multipleMixedArrStr": NewMethod("multipleMixedArrStr", "multipleMixedArrStr", Function, "view", false, false, []Argument{{"str", String, false}, {"fixedArr1", Uint256Arr2, false}, {"dynArr", Uint256Arr, false}, {"fixedArr2", Uint256Arr3, false}}, nil),
|
|
|
|
|
+ "overloadedNames": NewMethod("overloadedNames", "overloadedNames", Function, "view", false, false, []Argument{{"f", TupleF, false}}, nil),
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func TestReader(t *testing.T) {
|
|
func TestReader(t *testing.T) {
|
|
|
- Uint256, _ := NewType("uint256", "", nil)
|
|
|
|
|
- exp := ABI{
|
|
|
|
|
- Methods: map[string]Method{
|
|
|
|
|
- "balance": {
|
|
|
|
|
- "balance", "balance", "view", false, false, false, false, nil, nil,
|
|
|
|
|
- },
|
|
|
|
|
- "send": {
|
|
|
|
|
- "send", "send", "", false, false, false, false, []Argument{
|
|
|
|
|
- {"amount", Uint256, false},
|
|
|
|
|
- }, nil,
|
|
|
|
|
- },
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ abi := ABI{
|
|
|
|
|
+ Methods: methods,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- abi, err := JSON(strings.NewReader(jsondata))
|
|
|
|
|
|
|
+ exp, err := JSON(strings.NewReader(jsondata))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- t.Error(err)
|
|
|
|
|
|
|
+ t.Fatal(err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // deep equal fails for some reason
|
|
|
|
|
for name, expM := range exp.Methods {
|
|
for name, expM := range exp.Methods {
|
|
|
gotM, exist := abi.Methods[name]
|
|
gotM, exist := abi.Methods[name]
|
|
|
if !exist {
|
|
if !exist {
|
|
@@ -98,8 +150,55 @@ func TestReader(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func TestInvalidABI(t *testing.T) {
|
|
|
|
|
+ json := `[{ "type" : "function", "name" : "", "constant" : fals }]`
|
|
|
|
|
+ _, err := JSON(strings.NewReader(json))
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ t.Fatal("invalid json should produce error")
|
|
|
|
|
+ }
|
|
|
|
|
+ json2 := `[{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "typ" : "uint256" } ] }]`
|
|
|
|
|
+ _, err = JSON(strings.NewReader(json2))
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ t.Fatal("invalid json should produce error")
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// TestConstructor tests a constructor function.
|
|
|
|
|
+// The test is based on the following contract:
|
|
|
|
|
+// contract TestConstructor {
|
|
|
|
|
+// constructor(uint256 a, uint256 b) public{}
|
|
|
|
|
+// }
|
|
|
|
|
+func TestConstructor(t *testing.T) {
|
|
|
|
|
+ json := `[{ "inputs": [{"internalType": "uint256","name": "a","type": "uint256" },{ "internalType": "uint256","name": "b","type": "uint256"}],"stateMutability": "nonpayable","type": "constructor"}]`
|
|
|
|
|
+ method := NewMethod("", "", Constructor, "nonpayable", false, false, []Argument{{"a", Uint256, false}, {"b", Uint256, false}}, nil)
|
|
|
|
|
+ // Test from JSON
|
|
|
|
|
+ abi, err := JSON(strings.NewReader(json))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatal(err)
|
|
|
|
|
+ }
|
|
|
|
|
+ if !reflect.DeepEqual(abi.Constructor, method) {
|
|
|
|
|
+ t.Error("Missing expected constructor")
|
|
|
|
|
+ }
|
|
|
|
|
+ // Test pack/unpack
|
|
|
|
|
+ packed, err := abi.Pack("", big.NewInt(1), big.NewInt(2))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Error(err)
|
|
|
|
|
+ }
|
|
|
|
|
+ unpacked, err := abi.Constructor.Inputs.Unpack(packed)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Error(err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if !reflect.DeepEqual(unpacked[0], big.NewInt(1)) {
|
|
|
|
|
+ t.Error("Unable to pack/unpack from constructor")
|
|
|
|
|
+ }
|
|
|
|
|
+ if !reflect.DeepEqual(unpacked[1], big.NewInt(2)) {
|
|
|
|
|
+ t.Error("Unable to pack/unpack from constructor")
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func TestTestNumbers(t *testing.T) {
|
|
func TestTestNumbers(t *testing.T) {
|
|
|
- abi, err := JSON(strings.NewReader(jsondata2))
|
|
|
|
|
|
|
+ abi, err := JSON(strings.NewReader(jsondata))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
|
}
|
|
}
|
|
@@ -135,60 +234,22 @@ func TestTestNumbers(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func TestTestString(t *testing.T) {
|
|
|
|
|
- abi, err := JSON(strings.NewReader(jsondata2))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Fatal(err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if _, err := abi.Pack("string", "hello world"); err != nil {
|
|
|
|
|
- t.Error(err)
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func TestTestBool(t *testing.T) {
|
|
|
|
|
- abi, err := JSON(strings.NewReader(jsondata2))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Fatal(err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if _, err := abi.Pack("bool", true); err != nil {
|
|
|
|
|
- t.Error(err)
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func TestTestSlice(t *testing.T) {
|
|
|
|
|
- abi, err := JSON(strings.NewReader(jsondata2))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Fatal(err)
|
|
|
|
|
- }
|
|
|
|
|
- slice := make([]uint64, 2)
|
|
|
|
|
- if _, err := abi.Pack("uint64[2]", slice); err != nil {
|
|
|
|
|
- t.Error(err)
|
|
|
|
|
- }
|
|
|
|
|
- if _, err := abi.Pack("uint64[]", slice); err != nil {
|
|
|
|
|
- t.Error(err)
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func TestMethodSignature(t *testing.T) {
|
|
func TestMethodSignature(t *testing.T) {
|
|
|
- String, _ := NewType("string", "", nil)
|
|
|
|
|
- m := Method{"foo", "foo", "", false, false, false, false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil}
|
|
|
|
|
|
|
+ m := NewMethod("foo", "foo", Function, "", false, false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil)
|
|
|
exp := "foo(string,string)"
|
|
exp := "foo(string,string)"
|
|
|
- if m.Sig() != exp {
|
|
|
|
|
- t.Error("signature mismatch", exp, "!=", m.Sig())
|
|
|
|
|
|
|
+ if m.Sig != exp {
|
|
|
|
|
+ t.Error("signature mismatch", exp, "!=", m.Sig)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
idexp := crypto.Keccak256([]byte(exp))[:4]
|
|
idexp := crypto.Keccak256([]byte(exp))[:4]
|
|
|
- if !bytes.Equal(m.ID(), idexp) {
|
|
|
|
|
- t.Errorf("expected ids to match %x != %x", m.ID(), idexp)
|
|
|
|
|
|
|
+ if !bytes.Equal(m.ID, idexp) {
|
|
|
|
|
+ t.Errorf("expected ids to match %x != %x", m.ID, idexp)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- uintt, _ := NewType("uint256", "", nil)
|
|
|
|
|
- m = Method{"foo", "foo", "", false, false, false, false, []Argument{{"bar", uintt, false}}, nil}
|
|
|
|
|
|
|
+ m = NewMethod("foo", "foo", Function, "", false, false, []Argument{{"bar", Uint256, false}}, nil)
|
|
|
exp = "foo(uint256)"
|
|
exp = "foo(uint256)"
|
|
|
- if m.Sig() != exp {
|
|
|
|
|
- t.Error("signature mismatch", exp, "!=", m.Sig())
|
|
|
|
|
|
|
+ if m.Sig != exp {
|
|
|
|
|
+ t.Error("signature mismatch", exp, "!=", m.Sig)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Method with tuple arguments
|
|
// Method with tuple arguments
|
|
@@ -204,10 +265,10 @@ func TestMethodSignature(t *testing.T) {
|
|
|
{Name: "y", Type: "int256"},
|
|
{Name: "y", Type: "int256"},
|
|
|
}},
|
|
}},
|
|
|
})
|
|
})
|
|
|
- m = Method{"foo", "foo", "", false, false, false, false, []Argument{{"s", s, false}, {"bar", String, false}}, nil}
|
|
|
|
|
|
|
+ m = NewMethod("foo", "foo", Function, "", false, false, []Argument{{"s", s, false}, {"bar", String, false}}, nil)
|
|
|
exp = "foo((int256,int256[],(int256,int256)[],(int256,int256)[2]),string)"
|
|
exp = "foo((int256,int256[],(int256,int256)[],(int256,int256)[2]),string)"
|
|
|
- if m.Sig() != exp {
|
|
|
|
|
- t.Error("signature mismatch", exp, "!=", m.Sig())
|
|
|
|
|
|
|
+ if m.Sig != exp {
|
|
|
|
|
+ t.Error("signature mismatch", exp, "!=", m.Sig)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -219,12 +280,12 @@ func TestOverloadedMethodSignature(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
check := func(name string, expect string, method bool) {
|
|
check := func(name string, expect string, method bool) {
|
|
|
if method {
|
|
if method {
|
|
|
- if abi.Methods[name].Sig() != expect {
|
|
|
|
|
- t.Fatalf("The signature of overloaded method mismatch, want %s, have %s", expect, abi.Methods[name].Sig())
|
|
|
|
|
|
|
+ if abi.Methods[name].Sig != expect {
|
|
|
|
|
+ t.Fatalf("The signature of overloaded method mismatch, want %s, have %s", expect, abi.Methods[name].Sig)
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- if abi.Events[name].Sig() != expect {
|
|
|
|
|
- t.Fatalf("The signature of overloaded event mismatch, want %s, have %s", expect, abi.Events[name].Sig())
|
|
|
|
|
|
|
+ if abi.Events[name].Sig != expect {
|
|
|
|
|
+ t.Fatalf("The signature of overloaded event mismatch, want %s, have %s", expect, abi.Events[name].Sig)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -235,7 +296,7 @@ func TestOverloadedMethodSignature(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestMultiPack(t *testing.T) {
|
|
func TestMultiPack(t *testing.T) {
|
|
|
- abi, err := JSON(strings.NewReader(jsondata2))
|
|
|
|
|
|
|
+ abi, err := JSON(strings.NewReader(jsondata))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
|
}
|
|
}
|
|
@@ -400,15 +461,7 @@ func TestInputVariableInputLength(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
|
|
func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
|
|
|
- const definition = `[
|
|
|
|
|
- { "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
|
|
|
|
|
- { "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
|
|
|
|
|
- { "type" : "function", "name" : "mixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type": "uint256[2]" }, { "name" : "dynArr", "type": "uint256[]" } ] },
|
|
|
|
|
- { "type" : "function", "name" : "doubleFixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "fixedArr2", "type": "uint256[3]" } ] },
|
|
|
|
|
- { "type" : "function", "name" : "multipleMixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] }
|
|
|
|
|
- ]`
|
|
|
|
|
-
|
|
|
|
|
- abi, err := JSON(strings.NewReader(definition))
|
|
|
|
|
|
|
+ abi, err := JSON(strings.NewReader(jsondata))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
t.Error(err)
|
|
|
}
|
|
}
|
|
@@ -555,7 +608,7 @@ func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
|
|
|
strvalue = common.RightPadBytes([]byte(strin), 32)
|
|
strvalue = common.RightPadBytes([]byte(strin), 32)
|
|
|
fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
|
|
fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
|
|
|
fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
|
|
fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
|
|
|
- dynarroffset = U256(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
|
|
|
|
|
|
|
+ dynarroffset = math.U256Bytes(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
|
|
|
dynarrlength = make([]byte, 32)
|
|
dynarrlength = make([]byte, 32)
|
|
|
dynarrlength[31] = byte(len(dynarrin))
|
|
dynarrlength[31] = byte(len(dynarrin))
|
|
|
dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
|
|
dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
|
|
@@ -602,8 +655,6 @@ func TestBareEvents(t *testing.T) {
|
|
|
{ "type" : "event", "name" : "tuple", "inputs" : [{ "indexed":false, "name":"t", "type":"tuple", "components":[{"name":"a", "type":"uint256"}] }, { "indexed":true, "name":"arg1", "type":"address" }] }
|
|
{ "type" : "event", "name" : "tuple", "inputs" : [{ "indexed":false, "name":"t", "type":"tuple", "components":[{"name":"a", "type":"uint256"}] }, { "indexed":true, "name":"arg1", "type":"address" }] }
|
|
|
]`
|
|
]`
|
|
|
|
|
|
|
|
- arg0, _ := NewType("uint256", "", nil)
|
|
|
|
|
- arg1, _ := NewType("address", "", nil)
|
|
|
|
|
tuple, _ := NewType("tuple", "", []ArgumentMarshaling{{Name: "a", Type: "uint256"}})
|
|
tuple, _ := NewType("tuple", "", []ArgumentMarshaling{{Name: "a", Type: "uint256"}})
|
|
|
|
|
|
|
|
expectedEvents := map[string]struct {
|
|
expectedEvents := map[string]struct {
|
|
@@ -613,12 +664,12 @@ func TestBareEvents(t *testing.T) {
|
|
|
"balance": {false, nil},
|
|
"balance": {false, nil},
|
|
|
"anon": {true, nil},
|
|
"anon": {true, nil},
|
|
|
"args": {false, []Argument{
|
|
"args": {false, []Argument{
|
|
|
- {Name: "arg0", Type: arg0, Indexed: false},
|
|
|
|
|
- {Name: "arg1", Type: arg1, Indexed: true},
|
|
|
|
|
|
|
+ {Name: "arg0", Type: Uint256, Indexed: false},
|
|
|
|
|
+ {Name: "arg1", Type: Address, Indexed: true},
|
|
|
}},
|
|
}},
|
|
|
"tuple": {false, []Argument{
|
|
"tuple": {false, []Argument{
|
|
|
{Name: "t", Type: tuple, Indexed: false},
|
|
{Name: "t", Type: tuple, Indexed: false},
|
|
|
- {Name: "arg1", Type: arg1, Indexed: true},
|
|
|
|
|
|
|
+ {Name: "arg1", Type: Address, Indexed: true},
|
|
|
}},
|
|
}},
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -692,7 +743,7 @@ func TestUnpackEvent(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
var ev ReceivedEvent
|
|
var ev ReceivedEvent
|
|
|
|
|
|
|
|
- err = abi.Unpack(&ev, "received", data)
|
|
|
|
|
|
|
+ err = abi.UnpackIntoInterface(&ev, "received", data)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
t.Error(err)
|
|
|
}
|
|
}
|
|
@@ -701,7 +752,7 @@ func TestUnpackEvent(t *testing.T) {
|
|
|
Sender common.Address
|
|
Sender common.Address
|
|
|
}
|
|
}
|
|
|
var receivedAddrEv ReceivedAddrEvent
|
|
var receivedAddrEv ReceivedAddrEvent
|
|
|
- err = abi.Unpack(&receivedAddrEv, "receivedAddr", data)
|
|
|
|
|
|
|
+ err = abi.UnpackIntoInterface(&receivedAddrEv, "receivedAddr", data)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
t.Error(err)
|
|
|
}
|
|
}
|
|
@@ -891,45 +942,25 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestABI_MethodById(t *testing.T) {
|
|
func TestABI_MethodById(t *testing.T) {
|
|
|
- const abiJSON = `[
|
|
|
|
|
- {"type":"function","name":"receive","constant":false,"inputs":[{"name":"memo","type":"bytes"}],"outputs":[],"payable":true,"stateMutability":"payable"},
|
|
|
|
|
- {"type":"event","name":"received","anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}]},
|
|
|
|
|
- {"type":"function","name":"fixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"}]},
|
|
|
|
|
- {"type":"function","name":"fixedArrBytes","constant":true,"inputs":[{"name":"str","type":"bytes"},{"name":"fixedArr","type":"uint256[2]"}]},
|
|
|
|
|
- {"type":"function","name":"mixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"}]},
|
|
|
|
|
- {"type":"function","name":"doubleFixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"fixedArr2","type":"uint256[3]"}]},
|
|
|
|
|
- {"type":"function","name":"multipleMixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"},{"name":"fixedArr2","type":"uint256[3]"}]},
|
|
|
|
|
- {"type":"function","name":"balance","constant":true},
|
|
|
|
|
- {"type":"function","name":"send","constant":false,"inputs":[{"name":"amount","type":"uint256"}]},
|
|
|
|
|
- {"type":"function","name":"test","constant":false,"inputs":[{"name":"number","type":"uint32"}]},
|
|
|
|
|
- {"type":"function","name":"string","constant":false,"inputs":[{"name":"inputs","type":"string"}]},
|
|
|
|
|
- {"type":"function","name":"bool","constant":false,"inputs":[{"name":"inputs","type":"bool"}]},
|
|
|
|
|
- {"type":"function","name":"address","constant":false,"inputs":[{"name":"inputs","type":"address"}]},
|
|
|
|
|
- {"type":"function","name":"uint64[2]","constant":false,"inputs":[{"name":"inputs","type":"uint64[2]"}]},
|
|
|
|
|
- {"type":"function","name":"uint64[]","constant":false,"inputs":[{"name":"inputs","type":"uint64[]"}]},
|
|
|
|
|
- {"type":"function","name":"foo","constant":false,"inputs":[{"name":"inputs","type":"uint32"}]},
|
|
|
|
|
- {"type":"function","name":"bar","constant":false,"inputs":[{"name":"inputs","type":"uint32"},{"name":"string","type":"uint16"}]},
|
|
|
|
|
- {"type":"function","name":"_slice","constant":false,"inputs":[{"name":"inputs","type":"uint32[2]"}]},
|
|
|
|
|
- {"type":"function","name":"__slice256","constant":false,"inputs":[{"name":"inputs","type":"uint256[2]"}]},
|
|
|
|
|
- {"type":"function","name":"sliceAddress","constant":false,"inputs":[{"name":"inputs","type":"address[]"}]},
|
|
|
|
|
- {"type":"function","name":"sliceMultiAddress","constant":false,"inputs":[{"name":"a","type":"address[]"},{"name":"b","type":"address[]"}]}
|
|
|
|
|
- ]
|
|
|
|
|
-`
|
|
|
|
|
- abi, err := JSON(strings.NewReader(abiJSON))
|
|
|
|
|
|
|
+ abi, err := JSON(strings.NewReader(jsondata))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
|
}
|
|
}
|
|
|
for name, m := range abi.Methods {
|
|
for name, m := range abi.Methods {
|
|
|
a := fmt.Sprintf("%v", m)
|
|
a := fmt.Sprintf("%v", m)
|
|
|
- m2, err := abi.MethodById(m.ID())
|
|
|
|
|
|
|
+ m2, err := abi.MethodById(m.ID)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Fatalf("Failed to look up ABI method: %v", err)
|
|
t.Fatalf("Failed to look up ABI method: %v", err)
|
|
|
}
|
|
}
|
|
|
b := fmt.Sprintf("%v", m2)
|
|
b := fmt.Sprintf("%v", m2)
|
|
|
if a != b {
|
|
if a != b {
|
|
|
- t.Errorf("Method %v (id %x) not 'findable' by id in ABI", name, m.ID())
|
|
|
|
|
|
|
+ t.Errorf("Method %v (id %x) not 'findable' by id in ABI", name, m.ID)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // test unsuccessful lookups
|
|
|
|
|
+ if _, err = abi.MethodById(crypto.Keccak256()); err == nil {
|
|
|
|
|
+ t.Error("Expected error: no method with this id")
|
|
|
|
|
+ }
|
|
|
// Also test empty
|
|
// Also test empty
|
|
|
if _, err := abi.MethodById([]byte{0x00}); err == nil {
|
|
if _, err := abi.MethodById([]byte{0x00}); err == nil {
|
|
|
t.Errorf("Expected error, too short to decode data")
|
|
t.Errorf("Expected error, too short to decode data")
|
|
@@ -995,8 +1026,8 @@ func TestABI_EventById(t *testing.T) {
|
|
|
t.Errorf("We should find a event for topic %s, test #%d", topicID.Hex(), testnum)
|
|
t.Errorf("We should find a event for topic %s, test #%d", topicID.Hex(), testnum)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if event.ID() != topicID {
|
|
|
|
|
- t.Errorf("Event id %s does not match topic %s, test #%d", event.ID().Hex(), topicID.Hex(), testnum)
|
|
|
|
|
|
|
+ if event.ID != topicID {
|
|
|
|
|
+ t.Errorf("Event id %s does not match topic %s, test #%d", event.ID.Hex(), topicID.Hex(), testnum)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
unknowntopicID := crypto.Keccak256Hash([]byte("unknownEvent"))
|
|
unknowntopicID := crypto.Keccak256Hash([]byte("unknownEvent"))
|
|
@@ -1010,8 +1041,10 @@ func TestABI_EventById(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func TestDuplicateMethodNames(t *testing.T) {
|
|
|
|
|
- abiJSON := `[{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"},{"name":"customFallback","type":"string"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]`
|
|
|
|
|
|
|
+// TestDoubleDuplicateMethodNames checks that if transfer0 already exists, there won't be a name
|
|
|
|
|
+// conflict and that the second transfer method will be renamed transfer1.
|
|
|
|
|
+func TestDoubleDuplicateMethodNames(t *testing.T) {
|
|
|
|
|
+ abiJSON := `[{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"}],"name":"transfer0","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"},{"name":"customFallback","type":"string"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]`
|
|
|
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
|
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
@@ -1030,24 +1063,86 @@ func TestDuplicateMethodNames(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// TestDoubleDuplicateMethodNames checks that if transfer0 already exists, there won't be a name
|
|
|
|
|
-// conflict and that the second transfer method will be renamed transfer1.
|
|
|
|
|
-func TestDoubleDuplicateMethodNames(t *testing.T) {
|
|
|
|
|
- abiJSON := `[{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"}],"name":"transfer0","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"},{"name":"data","type":"bytes"},{"name":"customFallback","type":"string"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]`
|
|
|
|
|
|
|
+// TestDoubleDuplicateEventNames checks that if send0 already exists, there won't be a name
|
|
|
|
|
+// conflict and that the second send event will be renamed send1.
|
|
|
|
|
+// The test runs the abi of the following contract.
|
|
|
|
|
+// contract DuplicateEvent {
|
|
|
|
|
+// event send(uint256 a);
|
|
|
|
|
+// event send0();
|
|
|
|
|
+// event send();
|
|
|
|
|
+// }
|
|
|
|
|
+func TestDoubleDuplicateEventNames(t *testing.T) {
|
|
|
|
|
+ abiJSON := `[{"anonymous": false,"inputs": [{"indexed": false,"internalType": "uint256","name": "a","type": "uint256"}],"name": "send","type": "event"},{"anonymous": false,"inputs": [],"name": "send0","type": "event"},{ "anonymous": false, "inputs": [],"name": "send","type": "event"}]`
|
|
|
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
|
contractAbi, err := JSON(strings.NewReader(abiJSON))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
|
}
|
|
}
|
|
|
- if _, ok := contractAbi.Methods["transfer"]; !ok {
|
|
|
|
|
- t.Fatalf("Could not find original method")
|
|
|
|
|
|
|
+ if _, ok := contractAbi.Events["send"]; !ok {
|
|
|
|
|
+ t.Fatalf("Could not find original event")
|
|
|
}
|
|
}
|
|
|
- if _, ok := contractAbi.Methods["transfer0"]; !ok {
|
|
|
|
|
- t.Fatalf("Could not find duplicate method")
|
|
|
|
|
|
|
+ if _, ok := contractAbi.Events["send0"]; !ok {
|
|
|
|
|
+ t.Fatalf("Could not find duplicate event")
|
|
|
}
|
|
}
|
|
|
- if _, ok := contractAbi.Methods["transfer1"]; !ok {
|
|
|
|
|
- t.Fatalf("Could not find duplicate method")
|
|
|
|
|
|
|
+ if _, ok := contractAbi.Events["send1"]; !ok {
|
|
|
|
|
+ t.Fatalf("Could not find duplicate event")
|
|
|
}
|
|
}
|
|
|
- if _, ok := contractAbi.Methods["transfer2"]; ok {
|
|
|
|
|
- t.Fatalf("Should not have found extra method")
|
|
|
|
|
|
|
+ if _, ok := contractAbi.Events["send2"]; ok {
|
|
|
|
|
+ t.Fatalf("Should not have found extra event")
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// TestUnnamedEventParam checks that an event with unnamed parameters is
|
|
|
|
|
+// correctly handled.
|
|
|
|
|
+// The test runs the abi of the following contract.
|
|
|
|
|
+// contract TestEvent {
|
|
|
|
|
+// event send(uint256, uint256);
|
|
|
|
|
+// }
|
|
|
|
|
+func TestUnnamedEventParam(t *testing.T) {
|
|
|
|
|
+ abiJSON := `[{ "anonymous": false, "inputs": [{ "indexed": false,"internalType": "uint256", "name": "","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "","type": "uint256"}],"name": "send","type": "event"}]`
|
|
|
|
|
+ contractAbi, err := JSON(strings.NewReader(abiJSON))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatal(err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ event, ok := contractAbi.Events["send"]
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ t.Fatalf("Could not find event")
|
|
|
|
|
+ }
|
|
|
|
|
+ if event.Inputs[0].Name != "arg0" {
|
|
|
|
|
+ t.Fatalf("Could not find input")
|
|
|
|
|
+ }
|
|
|
|
|
+ if event.Inputs[1].Name != "arg1" {
|
|
|
|
|
+ t.Fatalf("Could not find input")
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestUnpackRevert(t *testing.T) {
|
|
|
|
|
+ t.Parallel()
|
|
|
|
|
+
|
|
|
|
|
+ var cases = []struct {
|
|
|
|
|
+ input string
|
|
|
|
|
+ expect string
|
|
|
|
|
+ expectErr error
|
|
|
|
|
+ }{
|
|
|
|
|
+ {"", "", errors.New("invalid data for unpacking")},
|
|
|
|
|
+ {"08c379a1", "", errors.New("invalid data for unpacking")},
|
|
|
|
|
+ {"08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d72657665727420726561736f6e00000000000000000000000000000000000000", "revert reason", nil},
|
|
|
|
|
+ }
|
|
|
|
|
+ for index, c := range cases {
|
|
|
|
|
+ t.Run(fmt.Sprintf("case %d", index), func(t *testing.T) {
|
|
|
|
|
+ got, err := UnpackRevert(common.Hex2Bytes(c.input))
|
|
|
|
|
+ if c.expectErr != nil {
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ t.Fatalf("Expected non-nil error")
|
|
|
|
|
+ }
|
|
|
|
|
+ if err.Error() != c.expectErr.Error() {
|
|
|
|
|
+ t.Fatalf("Expected error mismatch, want %v, got %v", c.expectErr, err)
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ if c.expect != got {
|
|
|
|
|
+ t.Fatalf("Output mismatch, want %v, got %v", c.expect, got)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|