@@ -10,7 +10,7 @@ use ethereum_types::U256;
1010use eventuals:: Eventual ;
1111use indexer_common:: prelude:: SubgraphClient ;
1212use jsonrpsee:: { core:: client:: ClientT , http_client:: HttpClientBuilder , rpc_params} ;
13- use sqlx:: PgPool ;
13+ use sqlx:: { types :: BigDecimal , PgPool } ;
1414use tap_aggregator:: jsonrpsee_helpers:: JsonRpcResponse ;
1515use tap_core:: {
1616 eip_712_signed_message:: EIP712SignedMessage ,
@@ -219,7 +219,6 @@ impl SenderAllocationRelationship {
219219 AND sender_address = $2
220220 )
221221 SELECT
222- COUNT(*),
223222 MAX(id),
224223 SUM(value)
225224 FROM
@@ -251,28 +250,17 @@ impl SenderAllocationRelationship {
251250
252251 let mut unaggregated_fees = inner. unaggregated_fees . lock ( ) . await ;
253252
254- // `COUNT(*)` will always return a value, so we don't need to check for `None`.
255- match res. count . unwrap ( ) {
256- 0 => {
257- unaggregated_fees. last_id = 0 ;
258- unaggregated_fees. value = 0 ;
259- }
260- // If the count is non-zero, then `MAX(id)` and `SUM(value)` will be non-null.
261- // If they are null, then something is extremely wrong with the database.
262- _ => {
263- ensure ! (
264- res. max. is_some( ) ,
265- "MAX(id) is null but the receipt COUNT(*) is not zero"
266- ) ;
267- ensure ! (
268- res. sum. is_some( ) ,
269- "SUM(value) is null, but the receipt COUNT(*) is not zero"
270- ) ;
253+ ensure ! (
254+ res. sum. is_none( ) == res. max. is_none( ) ,
255+ "Exactly one of SUM(value) and MAX(id) is null. This should not happen."
256+ ) ;
271257
272- unaggregated_fees. last_id = res. max . unwrap ( ) . try_into ( ) ?;
273- unaggregated_fees. value = res. sum . unwrap ( ) . to_string ( ) . parse :: < u128 > ( ) ?;
274- }
275- }
258+ unaggregated_fees. last_id = res. max . unwrap_or ( 0 ) . try_into ( ) ?;
259+ unaggregated_fees. value = res
260+ . sum
261+ . unwrap_or ( BigDecimal :: from ( 0 ) )
262+ . to_string ( )
263+ . parse :: < u128 > ( ) ?;
276264
277265 // TODO: check if we need to run a RAV request here.
278266
0 commit comments