bug: TVL filtered by token

Fix query bug for TVL filtered by token and add a test.
This commit is contained in:
Dagur Valberg Johannsson 2025-08-15 14:55:08 +02:00
parent df8dff5ada
commit 5017713ab2
No known key found for this signature in database
GPG key ID: FD701804AEE88107
3 changed files with 31 additions and 2 deletions

View file

@ -47,7 +47,7 @@ pub(crate) fn db_visit_pool_entries<T: PoolVisitor>(
) -> Result<()> {
let mut sql_filters = Vec::new();
let mut params: Vec<rusqlite::types::ToSqlOutput> = Vec::new();
let mut param_index = 1;
let mut param_index = 2; // Start from 2 since ?1 is used by time filter
if let Some(token_id) = &filters.token_id {
sql_filters.push(format!("p.token_id = ?{}", param_index));

View file

@ -31,7 +31,6 @@ pub fn db_list_tokens_by_volume(
seconds: usize,
limit: usize,
) -> Result<Vec<TokenListItem>> {
// List token ID's by volume last n seconds
let mut statement = cauldron_conn
.prepare(
"
@ -80,6 +79,7 @@ pub fn db_list_tokens_by_volume(
}
let bcmr = get_token_bcmr(bcmr_conn, &token_id)?;
let bcmr_well_known = get_well_known_bcmr(bcmr_conn, &token_id)?;
tokens.push(TokenListItem {

View file

@ -350,4 +350,33 @@ pub mod tests {
let body: Value = serde_json::from_str(&response.into_string().unwrap()).unwrap();
assert_eq!(body["satoshis"], 300_000);
}
#[test]
fn test_get_token_tvl_specific_token() {
// Set up a fresh mock DB
let mock_db = mock_db_pool(setup_mock_db);
// Test the function directly
let db = mock_db.cauldron_r.get().unwrap();
// Use token_zero as the token ID since that's what's in the mock data
let token_id = "0000000000000000000000000000000000000000000000000000000000000000";
// Test 1: Get TVL for the specific token without time filter (should include all data)
let result = get_token_tvl(&db, None, token_id).unwrap();
assert_eq!(result, (600_000, 6_000)); // Total: 100k+200k+300k sats, 1k+2k+3k tokens
// Test 2: Get TVL for the specific token with time filter (before timestamp 2500)
let result = get_token_tvl(&db, Some(2500), token_id).unwrap();
assert_eq!(result, (300_000, 3_000)); // Only cauldron1 and cauldron2: 100k+200k sats, 1k+2k tokens
// Test 3: Get TVL for the specific token with time filter (before timestamp 1500)
let result = get_token_tvl(&db, Some(1500), token_id).unwrap();
assert_eq!(result, (100_000, 1_000)); // Only cauldron1: 100k sats, 1k tokens
// Test 4: Get TVL for a non-existent token
let token_id = "1111111111111111111111111111111111111111111111111111111111111111";
let result = get_token_tvl(&db, None, token_id).unwrap();
assert_eq!(result, (0, 0)); // No data for non-existent token
}
}