1+ use duration_string:: DurationString ;
12use eyre:: { Context , Result } ;
23use lib:: prelude:: * ;
34use serde:: Deserialize ;
4- use std:: { fs, path:: Path } ;
5+ use std:: { fs, path:: Path , time :: Duration } ;
56
67#[ derive( Deserialize ) ]
78struct RawConfig {
8- url_one : String ,
9- url_two : String ,
9+ tick_rate : DurationString ,
10+ monitors : Vec < RawMonitorConfig > ,
11+ }
12+
13+ #[ derive( Deserialize ) ]
14+ struct RawMonitorConfig {
15+ rpc_url : String ,
16+ factory : Address ,
1017 token_one : Address ,
1118 token_two : Address ,
1219}
1320
14- // TODO: remove allow when this is used
15- #[ allow( dead_code) ]
1621#[ derive( Debug ) ]
1722pub struct Config {
18- provider_one : RootProvider ,
19- provider_two : RootProvider ,
20- token_address_one : Address ,
21- token_address_two : Address ,
23+ pub tick_rate : Duration ,
24+ pub monitors : Vec < MonitorConfig > ,
25+ }
26+
27+ impl From < RawConfig > for Config {
28+ fn from ( raw : RawConfig ) -> Self {
29+ let monitors = raw. monitors . into_iter ( ) . map ( MonitorConfig :: from) . collect ( ) ;
30+ Self {
31+ tick_rate : raw. tick_rate . into ( ) ,
32+ monitors,
33+ }
34+ }
35+ }
36+
37+ #[ derive( Debug ) ]
38+ pub struct MonitorConfig {
39+ pub provider : RootProvider ,
40+ pub factory : Address ,
41+ pub token_one : Address ,
42+ pub token_two : Address ,
43+ }
44+
45+ impl From < RawMonitorConfig > for MonitorConfig {
46+ fn from ( raw : RawMonitorConfig ) -> Self {
47+ let rpc_url = Url :: parse ( & raw . rpc_url) . unwrap ( ) ;
48+ let provider = ProviderBuilder :: new ( ) . on_http ( rpc_url) ;
49+ Self {
50+ provider,
51+ factory : raw. factory ,
52+ token_one : raw. token_one ,
53+ token_two : raw. token_two ,
54+ }
55+ }
2256}
2357
2458impl Config {
@@ -29,19 +63,6 @@ impl Config {
2963 let raw =
3064 toml:: from_str :: < RawConfig > ( & file_str) . wrap_err ( "failed to parse config from toml" ) ?;
3165
32- // Parse URLs and create the providers.
33- let url_one =
34- Url :: parse ( & raw . url_one) . wrap_err ( format ! ( "failed to parse url: {}" , raw. url_one) ) ?;
35- let url_two =
36- Url :: parse ( & raw . url_two) . wrap_err ( format ! ( "failed to parse url: {}" , raw. url_two) ) ?;
37- let provider_one = ProviderBuilder :: new ( ) . on_http ( url_one) ;
38- let provider_two = ProviderBuilder :: new ( ) . on_http ( url_two) ;
39-
40- Ok ( Self {
41- provider_one,
42- provider_two,
43- token_address_one : raw. token_one ,
44- token_address_two : raw. token_two ,
45- } )
66+ Ok ( raw. into ( ) )
4667 }
4768}
0 commit comments