Skip to content

Commit 8515c03

Browse files
committed
refactor: simplify update_unaggregated_fees_static
In particular, simplify handling of NULL results from the DB. Signed-off-by: Alexis Asseman <alexis@semiotic.ai>
1 parent 226a8ff commit 8515c03

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

tap-agent/src/tap/sender_allocation_relationship.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ethereum_types::U256;
1010
use eventuals::Eventual;
1111
use indexer_common::prelude::SubgraphClient;
1212
use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder, rpc_params};
13-
use sqlx::PgPool;
13+
use sqlx::{types::BigDecimal, PgPool};
1414
use tap_aggregator::jsonrpsee_helpers::JsonRpcResponse;
1515
use 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

Comments
 (0)