44 api:: ChainId , chain:: ethereum:: InstrumentedPythContract ,
55 eth_utils:: traced_client:: TracedClient ,
66 } ,
7+ anyhow:: { anyhow, Result } ,
78 ethers:: middleware:: Middleware ,
89 ethers:: { prelude:: BlockNumber , providers:: Provider , types:: Address } ,
910 std:: {
@@ -14,23 +15,17 @@ use {
1415} ;
1516
1617/// tracks the balance of the given address on the given chain
17- /// if there was an error, the function will just return
1818#[ tracing:: instrument( skip_all) ]
1919pub async fn track_balance (
2020 chain_id : String ,
2121 provider : Arc < Provider < TracedClient > > ,
2222 address : Address ,
2323 metrics : Arc < KeeperMetrics > ,
24- ) {
25- let balance = match provider. get_balance ( address, None ) . await {
26- // This conversion to u128 is fine as the total balance will never cross the limits
27- // of u128 practically.
28- Ok ( r) => r. as_u128 ( ) ,
29- Err ( e) => {
30- tracing:: error!( "Error while getting balance. error: {:?}" , e) ;
31- return ;
32- }
33- } ;
24+ ) -> Result < ( ) > {
25+ let balance = provider. get_balance ( address, None ) . await ?;
26+ // This conversion to u128 is fine as the total balance will never cross the limits
27+ // of u128 practically.
28+ let balance = balance. as_u128 ( ) ;
3429 // The f64 conversion is made to be able to serve metrics within the constraints of Prometheus.
3530 // The balance is in wei, so we need to divide by 1e18 to convert it to eth.
3631 let balance = balance as f64 / 1e18 ;
@@ -42,6 +37,8 @@ pub async fn track_balance(
4237 address : address. to_string ( ) ,
4338 } )
4439 . set ( balance) ;
40+
41+ Ok ( ( ) )
4542}
4643
4744/// Tracks the difference between the server timestamp and the latest block timestamp for each chain
@@ -50,53 +47,37 @@ pub async fn track_block_timestamp_lag(
5047 chain_id : String ,
5148 provider : Arc < Provider < TracedClient > > ,
5249 metrics : Arc < KeeperMetrics > ,
53- ) {
54- const INF_LAG : i64 = 1000000 ; // value that definitely triggers an alert
55- let lag = match provider. get_block ( BlockNumber :: Latest ) . await {
56- Ok ( block) => match block {
57- Some ( block) => {
58- let block_timestamp = block. timestamp ;
59- let server_timestamp = SystemTime :: now ( )
60- . duration_since ( UNIX_EPOCH )
61- . unwrap ( )
62- . as_secs ( ) ;
63- let lag: i64 = ( server_timestamp as i64 ) - ( block_timestamp. as_u64 ( ) as i64 ) ;
64- lag
65- }
66- None => {
67- tracing:: error!( "Block is None" ) ;
68- INF_LAG
69- }
70- } ,
71- Err ( e) => {
72- tracing:: error!( "Failed to get block - {:?}" , e) ;
73- INF_LAG
74- }
50+ ) -> Result < ( ) > {
51+ let label = ChainIdLabel {
52+ chain_id : chain_id. clone ( ) ,
7553 } ;
54+
55+ let block = provider. get_block ( BlockNumber :: Latest ) . await ?;
56+ let block_timestamp = block. ok_or ( anyhow ! ( "block was none" ) ) ?. timestamp . as_u64 ( ) ;
57+ let block_timestamp = i64:: try_from ( block_timestamp) ?;
58+
7659 metrics
77- . block_timestamp_lag
78- . get_or_create ( & ChainIdLabel {
79- chain_id : chain_id. clone ( ) ,
80- } )
81- . set ( lag) ;
60+ . block_timestamp
61+ . get_or_create ( & label)
62+ . set ( block_timestamp) ;
63+
64+ let server_timestamp = i64:: try_from ( SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) ?. as_secs ( ) ) ?;
65+
66+ let lag = server_timestamp - block_timestamp;
67+ metrics. block_timestamp_lag . get_or_create ( & label) . set ( lag) ;
68+
69+ Ok ( ( ) )
8270}
8371
8472/// tracks the collected fees and the hashchain data of the given provider address on the given chain
85- /// if there is a error the function will just return
8673#[ tracing:: instrument( skip_all) ]
8774pub async fn track_provider (
8875 chain_id : ChainId ,
8976 contract : InstrumentedPythContract ,
9077 provider_address : Address ,
9178 metrics : Arc < KeeperMetrics > ,
92- ) {
93- let provider_info = match contract. get_provider_info ( provider_address) . call ( ) . await {
94- Ok ( info) => info,
95- Err ( e) => {
96- tracing:: error!( "Error while getting provider info. error: {:?}" , e) ;
97- return ;
98- }
99- } ;
79+ ) -> Result < ( ) > {
80+ let provider_info = contract. get_provider_info ( provider_address) . call ( ) . await ?;
10081
10182 // The f64 conversion is made to be able to serve metrics with the constraints of Prometheus.
10283 // The fee is in wei, so we divide by 1e18 to convert it to eth.
@@ -150,23 +131,18 @@ pub async fn track_provider(
150131 address : provider_address. to_string ( ) ,
151132 } )
152133 . set ( end_sequence_number as i64 ) ;
134+
135+ Ok ( ( ) )
153136}
154137
155138/// tracks the accrued pyth fees on the given chain
156- /// if there is an error the function will just return
157139#[ tracing:: instrument( skip_all) ]
158140pub async fn track_accrued_pyth_fees (
159141 chain_id : ChainId ,
160142 contract : InstrumentedPythContract ,
161143 metrics : Arc < KeeperMetrics > ,
162- ) {
163- let accrued_pyth_fees = match contract. get_accrued_pyth_fees ( ) . call ( ) . await {
164- Ok ( fees) => fees,
165- Err ( e) => {
166- tracing:: error!( "Error while getting accrued pyth fees. error: {:?}" , e) ;
167- return ;
168- }
169- } ;
144+ ) -> Result < ( ) > {
145+ let accrued_pyth_fees = contract. get_accrued_pyth_fees ( ) . call ( ) . await ?;
170146
171147 // The f64 conversion is made to be able to serve metrics with the constraints of Prometheus.
172148 // The fee is in wei, so we divide by 1e18 to convert it to eth.
@@ -178,4 +154,6 @@ pub async fn track_accrued_pyth_fees(
178154 chain_id : chain_id. clone ( ) ,
179155 } )
180156 . set ( accrued_pyth_fees) ;
157+
158+ Ok ( ( ) )
181159}
0 commit comments