16
16
17
17
package ethclient
18
18
19
- import "github.com/ethereum/go-ethereum"
19
+ import (
20
+ "fmt"
21
+ "math/big"
22
+ "reflect"
23
+ "testing"
24
+
25
+ "github.com/ethereum/go-ethereum"
26
+ "github.com/ethereum/go-ethereum/common"
27
+ )
20
28
21
29
// Verify that Client implements the ethereum interfaces.
22
30
var (
@@ -32,3 +40,113 @@ var (
32
40
// _ = ethereum.PendingStateEventer(&Client{})
33
41
_ = ethereum .PendingContractCaller (& Client {})
34
42
)
43
+
44
+ func TestToFilterArg (t * testing.T ) {
45
+ blockHashErr := fmt .Errorf ("cannot specify both BlockHash and FromBlock/ToBlock" )
46
+ addresses := []common.Address {
47
+ common .HexToAddress ("0xD36722ADeC3EdCB29c8e7b5a47f352D701393462" ),
48
+ }
49
+ blockHash := common .HexToHash (
50
+ "0xeb94bb7d78b73657a9d7a99792413f50c0a45c51fc62bdcb08a53f18e9a2b4eb" ,
51
+ )
52
+
53
+ for _ , testCase := range []struct {
54
+ name string
55
+ input ethereum.FilterQuery
56
+ output interface {}
57
+ err error
58
+ }{
59
+ {
60
+ "without BlockHash" ,
61
+ ethereum.FilterQuery {
62
+ Addresses : addresses ,
63
+ FromBlock : big .NewInt (1 ),
64
+ ToBlock : big .NewInt (2 ),
65
+ Topics : [][]common.Hash {},
66
+ },
67
+ map [string ]interface {}{
68
+ "address" : addresses ,
69
+ "fromBlock" : "0x1" ,
70
+ "toBlock" : "0x2" ,
71
+ "topics" : [][]common.Hash {},
72
+ },
73
+ nil ,
74
+ },
75
+ {
76
+ "with nil fromBlock and nil toBlock" ,
77
+ ethereum.FilterQuery {
78
+ Addresses : addresses ,
79
+ Topics : [][]common.Hash {},
80
+ },
81
+ map [string ]interface {}{
82
+ "address" : addresses ,
83
+ "fromBlock" : "0x0" ,
84
+ "toBlock" : "latest" ,
85
+ "topics" : [][]common.Hash {},
86
+ },
87
+ nil ,
88
+ },
89
+ {
90
+ "with blockhash" ,
91
+ ethereum.FilterQuery {
92
+ Addresses : addresses ,
93
+ BlockHash : & blockHash ,
94
+ Topics : [][]common.Hash {},
95
+ },
96
+ map [string ]interface {}{
97
+ "address" : addresses ,
98
+ "blockHash" : blockHash ,
99
+ "topics" : [][]common.Hash {},
100
+ },
101
+ nil ,
102
+ },
103
+ {
104
+ "with blockhash and from block" ,
105
+ ethereum.FilterQuery {
106
+ Addresses : addresses ,
107
+ BlockHash : & blockHash ,
108
+ FromBlock : big .NewInt (1 ),
109
+ Topics : [][]common.Hash {},
110
+ },
111
+ nil ,
112
+ blockHashErr ,
113
+ },
114
+ {
115
+ "with blockhash and to block" ,
116
+ ethereum.FilterQuery {
117
+ Addresses : addresses ,
118
+ BlockHash : & blockHash ,
119
+ ToBlock : big .NewInt (1 ),
120
+ Topics : [][]common.Hash {},
121
+ },
122
+ nil ,
123
+ blockHashErr ,
124
+ },
125
+ {
126
+ "with blockhash and both from / to block" ,
127
+ ethereum.FilterQuery {
128
+ Addresses : addresses ,
129
+ BlockHash : & blockHash ,
130
+ FromBlock : big .NewInt (1 ),
131
+ ToBlock : big .NewInt (2 ),
132
+ Topics : [][]common.Hash {},
133
+ },
134
+ nil ,
135
+ blockHashErr ,
136
+ },
137
+ } {
138
+ t .Run (testCase .name , func (t * testing.T ) {
139
+ output , err := toFilterArg (testCase .input )
140
+ if (testCase .err == nil ) != (err == nil ) {
141
+ t .Fatalf ("expected error %v but got %v" , testCase .err , err )
142
+ }
143
+ if testCase .err != nil {
144
+ if testCase .err .Error () != err .Error () {
145
+ t .Fatalf ("expected error %v but got %v" , testCase .err , err )
146
+ }
147
+ } else if ! reflect .DeepEqual (testCase .output , output ) {
148
+ t .Fatalf ("expected filter arg %v but got %v" , testCase .output , output )
149
+ }
150
+ })
151
+ }
152
+ }
0 commit comments