11package app
22
33import (
4- "context"
54 "net/http"
6- "strconv"
75 "time"
86
97 "github.com/KyberNetwork/evmlistener/pkg/block"
@@ -21,7 +19,7 @@ const (
2119 defaultRequestTimeout = 10 * time .Second
2220)
2321
24- // NewApp creates a new cli App instance with common flags pre-loaded .
22+ // NewApp creates a new cli App instance with common flags preloaded .
2523func NewApp () * cli.App {
2624 app := cli .NewApp ()
2725 app .Flags = NewFlags ()
@@ -31,18 +29,18 @@ func NewApp() *cli.App {
3129
3230func redisConfigFromCli (c * cli.Context ) redis.Config {
3331 cfg := redis.Config {
34- MasterName : c . String ( redisMasterNameFlag .Name ) ,
35- Addrs : c . StringSlice ( redisAddrsFlag .Name ),
36- DB : c . Int ( redisDBFlag .Name ) ,
37- KeyPrefix : c . String ( redisKeyPrefixFlag .Name ) ,
38- ReadTimeout : c . Duration ( redisReadTimeoutFlag .Name ) ,
39- WriteTimeout : c . Duration ( redisWriteTimeoutFlag .Name ) ,
32+ MasterName : redisMasterNameFlag .Value ,
33+ Addrs : redisAddrsFlag .Get ( c ),
34+ DB : redisDBFlag .Value ,
35+ KeyPrefix : redisKeyPrefixFlag .Value ,
36+ ReadTimeout : redisReadTimeoutFlag .Value ,
37+ WriteTimeout : redisWriteTimeoutFlag .Value ,
4038 }
4139
42- cfg .SentinelUsername = c . String ( redisUsernameFlag .Name )
43- cfg .SentinelPassword = c . String ( redisPasswordFlag .Name )
44- cfg .Username = c . String ( redisUsernameFlag .Name )
45- cfg .Password = c . String ( redisPasswordFlag .Name )
40+ cfg .SentinelUsername = redisUsernameFlag .Value
41+ cfg .SentinelPassword = redisPasswordFlag .Value
42+ cfg .Username = redisUsernameFlag .Value
43+ cfg .Password = redisPasswordFlag .Value
4644
4745 return cfg
4846}
@@ -51,50 +49,52 @@ func redisConfigFromCli(c *cli.Context) redis.Config {
5149func NewListener (c * cli.Context ) (* listener.Listener , error ) {
5250 l := zap .S ()
5351
54- rpcRequestTimeout := c .Duration (rpcRequestTimeoutFlag .Name )
52+ evmclient .UseCustomClient = useCustomClientFlag .Value
53+
54+ rpcRequestTimeout := rpcRequestTimeoutFlag .Value
5555 if rpcRequestTimeout == 0 {
5656 rpcRequestTimeout = defaultRequestTimeout
5757 }
5858
5959 httpClient := & http.Client {
6060 Timeout : rpcRequestTimeout ,
6161 }
62- wsRPC := c . String ( wsRPCFlag .Name )
62+ wsRPC := wsRPCFlag .Value
6363 l .Infow ("Connect to node websocket rpc" , "rpc" , wsRPC )
6464 wsEVMClient , err := evmclient .DialContextWithTimeout (
65- context . Background () , wsRPC , httpClient , rpcRequestTimeout )
65+ c . Context , wsRPC , httpClient , rpcRequestTimeout )
6666 if err != nil {
6767 l .Errorw ("Fail to connect to node" , "rpc" , wsRPC , "error" , err )
6868
6969 return nil , err
7070 }
7171
72- httpRPC := c . String ( httpRPCFlag .Name )
72+ httpRPC := httpRPCFlag .Value
7373 l .Infow ("Connect to node http rpc" , "rpc" , httpRPC )
7474 httpEVMClient , err := evmclient .DialContextWithTimeout (
75- context . Background () , httpRPC , httpClient , rpcRequestTimeout )
75+ c . Context , httpRPC , httpClient , rpcRequestTimeout )
7676 if err != nil {
7777 l .Errorw ("Fail to connect to node" , "rpc" , httpRPC , "error" , err )
7878
7979 return nil , err
8080 }
8181
8282 l .Infow ("Get chainID from node" )
83- chainID , err := httpEVMClient .ChainID (context . Background () )
83+ chainID , err := httpEVMClient .ChainID (c . Context )
8484 if err != nil {
8585 l .Errorw ("Fail to get chainID" , "error" , err )
8686
8787 return nil , err
8888 }
8989
90- l = l .With ("chainName " , chainIDToName ( chainID .Int64 () ))
90+ l = l .With ("chainID " , chainID .Int64 ())
9191
92- sanityCheckInterval := c . Duration ( sanityCheckIntervalFlag .Name )
92+ sanityCheckInterval := sanityCheckIntervalFlag .Value
9393 var sanityEVMClient evmclient.IClient
94- sanityRPC := c . String ( sanityNodeRPCFlag .Name )
94+ sanityRPC := sanityNodeRPCFlag .Value
9595 if sanityRPC != "" {
9696 l .Infow ("Connect to public node rpc for sanity check" , "rpc" , sanityRPC )
97- sanityEVMClient , err = evmclient .DialContext (context . Background () , sanityRPC , httpClient )
97+ sanityEVMClient , err = evmclient .DialContext (c . Context , sanityRPC , httpClient )
9898 if err != nil {
9999 l .Errorw ("Fail to setup EVM client for sanity check" , "error" , err )
100100
@@ -114,22 +114,22 @@ func NewListener(c *cli.Context) (*listener.Listener, error) {
114114 return nil , err
115115 }
116116
117- maxNumBlocks := c . Int ( maxNumBlocksFlag .Name )
118- blockExpiration := c . Duration ( blockExpirationFlag .Name )
117+ maxNumBlocks := maxNumBlocksFlag .Value
118+ blockExpiration := blockExpirationFlag .Value
119119 l .Infow ("Setup new BlockKeeper" , "maxNumBlocks" , maxNumBlocks , "expiration" , blockExpiration )
120120 blockKeeper := block .NewRedisBlockKeeper (l , redisClient , maxNumBlocks , blockExpiration )
121121
122- topic := c . String ( publisherTopicFlag .Name )
122+ topic := publisherTopicFlag .Value
123123 publisher , err := getPublisher (c , redisClient , topic )
124124 if err != nil {
125125 l .Errorw ("Fail to get publisher" , "error" , err )
126126
127127 return nil , err
128128 }
129- encoder := getMessageEncoder (c )
129+ msgEncoder := getMessageEncoder ()
130130
131131 l .Infow ("Setup handler" , "topic" , topic )
132- handler := listener .NewHandler (l , topic , httpEVMClient , blockKeeper , publisher , encoder ,
132+ handler := listener .NewHandler (l , topic , httpEVMClient , blockKeeper , publisher , msgEncoder ,
133133 listener .WithEventLogs (nil , nil ))
134134
135135 l .Infow ("Setup listener" )
@@ -142,14 +142,14 @@ func getPublisher(c *cli.Context, redisClient *redis.Client, topic string) (publ
142142 var publisher publisherpkg.Publisher
143143 var err error
144144
145- publisherType := c . String ( publisherTypeFlag .Name )
145+ publisherType := publisherTypeFlag .Value
146146 switch publisherType {
147147 case publisherpkg .PublisherTypeKafka :
148148 config := & kafka.Config {
149- Addresses : c . StringSlice ( kafkaAddrsFlag .Name ),
150- UseAuthentication : c . Bool ( kafkaUseAuthenticationFlag .Name ) ,
151- Username : c . String ( kafkaUsernameFlag .Name ) ,
152- Password : c . String ( kafkaPasswordFlag .Name ) ,
149+ Addresses : kafkaAddrsFlag .Get ( c ),
150+ UseAuthentication : kafkaUseAuthenticationFlag .Value ,
151+ Username : kafkaUsernameFlag .Value ,
152+ Password : kafkaPasswordFlag .Value ,
153153 }
154154 publisher , err = kafka .NewPublisher (config )
155155 if err != nil {
@@ -159,78 +159,18 @@ func getPublisher(c *cli.Context, redisClient *redis.Client, topic string) (publ
159159 return nil , err
160160 }
161161 default :
162- maxLen := c . Int64 ( publisherMaxLenFlag .Name )
162+ maxLen := publisherMaxLenFlag .Value
163163 publisher = redis .NewStream (redisClient , maxLen )
164164 }
165165
166166 return publisher , err
167167}
168168
169- func getMessageEncoder (c * cli.Context ) encoder.Encoder {
170- encoderType := c .String (encoderTypeFlag .Name )
171- switch encoderType {
169+ func getMessageEncoder () encoder.Encoder {
170+ switch encoderTypeFlag .Value {
172171 case encoder .EncoderTypeProtobuf :
173172 return encoder .NewProtobufEncoder ()
174173 default :
175174 return encoder .NewJSONEncoder ()
176175 }
177176}
178-
179- const (
180- chainIDEthereum = 1
181- chainIDOptimism = 10
182- chainIDCronos = 25
183- chainIDBSC = 56
184- chainIDVelas = 106
185- chainIDPolygon = 137
186- chainIDBitTorrent = 199
187- chainIDFantom = 250
188- chainIDZKSyncEra = 324
189- chainIDPolygonZKEVM = 1101
190- chainIDBase = 8453
191- chainIDArbitrum = 42161
192- chainIDOasis = 42262
193- chainIDAvalanche = 43114
194- chainIDLinea = 59144
195- chainIDAurora = 1313161554
196- )
197-
198- //nolint:cyclop
199- func chainIDToName (chainID int64 ) string {
200- switch chainID {
201- case chainIDEthereum :
202- return "Ethereum"
203- case chainIDOptimism :
204- return "Optimism"
205- case chainIDCronos :
206- return "Cronos"
207- case chainIDBSC :
208- return "BSC"
209- case chainIDVelas :
210- return "Velas"
211- case chainIDPolygon :
212- return "Polygon"
213- case chainIDBitTorrent :
214- return "BitTorrent"
215- case chainIDFantom :
216- return "Fantom"
217- case chainIDArbitrum :
218- return "Arbitrum"
219- case chainIDOasis :
220- return "Oasis"
221- case chainIDAvalanche :
222- return "Avalanche"
223- case chainIDAurora :
224- return "Aurora"
225- case chainIDLinea :
226- return "Linea"
227- case chainIDPolygonZKEVM :
228- return "Polygon zkEVM"
229- case chainIDZKSyncEra :
230- return "zkSync Era"
231- case chainIDBase :
232- return "Base"
233- default :
234- return strconv .FormatInt (chainID , 10 )
235- }
236- }
0 commit comments