@@ -22,11 +22,10 @@ import (
22
22
"fmt"
23
23
"log"
24
24
"math/big"
25
+ "reflect"
25
26
"strings"
26
27
"testing"
27
28
28
- "reflect"
29
-
30
29
"github.com/ethereum/go-ethereum/common"
31
30
"github.com/ethereum/go-ethereum/crypto"
32
31
)
@@ -52,11 +51,14 @@ const jsondata2 = `
52
51
{ "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
53
52
{ "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
54
53
{ "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
55
- { "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
54
+ { "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] },
55
+ { "type" : "function", "name" : "nestedArray", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint256[2][2]" }, { "name" : "b", "type" : "address[]" } ] },
56
+ { "type" : "function", "name" : "nestedArray2", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint8[][2]" } ] },
57
+ { "type" : "function", "name" : "nestedSlice", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint8[][]" } ] }
56
58
]`
57
59
58
60
func TestReader (t * testing.T ) {
59
- Uint256 , _ := NewType ("uint256" )
61
+ Uint256 , _ := NewType ("uint256" , nil )
60
62
exp := ABI {
61
63
Methods : map [string ]Method {
62
64
"balance" : {
@@ -177,7 +179,7 @@ func TestTestSlice(t *testing.T) {
177
179
}
178
180
179
181
func TestMethodSignature (t * testing.T ) {
180
- String , _ := NewType ("string" )
182
+ String , _ := NewType ("string" , nil )
181
183
m := Method {"foo" , false , []Argument {{"bar" , String , false }, {"baz" , String , false }}, nil }
182
184
exp := "foo(string,string)"
183
185
if m .Sig () != exp {
@@ -189,12 +191,31 @@ func TestMethodSignature(t *testing.T) {
189
191
t .Errorf ("expected ids to match %x != %x" , m .Id (), idexp )
190
192
}
191
193
192
- uintt , _ := NewType ("uint256" )
194
+ uintt , _ := NewType ("uint256" , nil )
193
195
m = Method {"foo" , false , []Argument {{"bar" , uintt , false }}, nil }
194
196
exp = "foo(uint256)"
195
197
if m .Sig () != exp {
196
198
t .Error ("signature mismatch" , exp , "!=" , m .Sig ())
197
199
}
200
+
201
+ // Method with tuple arguments
202
+ s , _ := NewType ("tuple" , []ArgumentMarshaling {
203
+ {Name : "a" , Type : "int256" },
204
+ {Name : "b" , Type : "int256[]" },
205
+ {Name : "c" , Type : "tuple[]" , Components : []ArgumentMarshaling {
206
+ {Name : "x" , Type : "int256" },
207
+ {Name : "y" , Type : "int256" },
208
+ }},
209
+ {Name : "d" , Type : "tuple[2]" , Components : []ArgumentMarshaling {
210
+ {Name : "x" , Type : "int256" },
211
+ {Name : "y" , Type : "int256" },
212
+ }},
213
+ })
214
+ m = Method {"foo" , false , []Argument {{"s" , s , false }, {"bar" , String , false }}, nil }
215
+ exp = "foo((int256,int256[],(int256,int256)[],(int256,int256)[2]),string)"
216
+ if m .Sig () != exp {
217
+ t .Error ("signature mismatch" , exp , "!=" , m .Sig ())
218
+ }
198
219
}
199
220
200
221
func TestMultiPack (t * testing.T ) {
@@ -564,11 +585,13 @@ func TestBareEvents(t *testing.T) {
564
585
const definition = `[
565
586
{ "type" : "event", "name" : "balance" },
566
587
{ "type" : "event", "name" : "anon", "anonymous" : true},
567
- { "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] }
588
+ { "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] },
589
+ { "type" : "event", "name" : "tuple", "inputs" : [{ "indexed":false, "name":"t", "type":"tuple", "components":[{"name":"a", "type":"uint256"}] }, { "indexed":true, "name":"arg1", "type":"address" }] }
568
590
]`
569
591
570
- arg0 , _ := NewType ("uint256" )
571
- arg1 , _ := NewType ("address" )
592
+ arg0 , _ := NewType ("uint256" , nil )
593
+ arg1 , _ := NewType ("address" , nil )
594
+ tuple , _ := NewType ("tuple" , []ArgumentMarshaling {{Name : "a" , Type : "uint256" }})
572
595
573
596
expectedEvents := map [string ]struct {
574
597
Anonymous bool
@@ -580,6 +603,10 @@ func TestBareEvents(t *testing.T) {
580
603
{Name : "arg0" , Type : arg0 , Indexed : false },
581
604
{Name : "arg1" , Type : arg1 , Indexed : true },
582
605
}},
606
+ "tuple" : {false , []Argument {
607
+ {Name : "t" , Type : tuple , Indexed : false },
608
+ {Name : "arg1" , Type : arg1 , Indexed : true },
609
+ }},
583
610
}
584
611
585
612
abi , err := JSON (strings .NewReader (definition ))
@@ -646,28 +673,24 @@ func TestUnpackEvent(t *testing.T) {
646
673
}
647
674
648
675
type ReceivedEvent struct {
649
- Address common.Address
650
- Amount * big.Int
651
- Memo []byte
676
+ Sender common.Address
677
+ Amount * big.Int
678
+ Memo []byte
652
679
}
653
680
var ev ReceivedEvent
654
681
655
682
err = abi .Unpack (& ev , "received" , data )
656
683
if err != nil {
657
684
t .Error (err )
658
- } else {
659
- t .Logf ("len(data): %d; received event: %+v" , len (data ), ev )
660
685
}
661
686
662
687
type ReceivedAddrEvent struct {
663
- Address common.Address
688
+ Sender common.Address
664
689
}
665
690
var receivedAddrEv ReceivedAddrEvent
666
691
err = abi .Unpack (& receivedAddrEv , "receivedAddr" , data )
667
692
if err != nil {
668
693
t .Error (err )
669
- } else {
670
- t .Logf ("len(data): %d; received event: %+v" , len (data ), receivedAddrEv )
671
694
}
672
695
}
673
696
0 commit comments