diff --git a/src/rpc/tvl.rs b/src/rpc/tvl.rs index 76275d8..21ec817 100644 --- a/src/rpc/tvl.rs +++ b/src/rpc/tvl.rs @@ -197,3 +197,129 @@ pub fn valuelocked_token( "satoshis": sats }))) } + +#[cfg(test)] +pub mod tests { + use super::*; + use crate::db::cauldron::{pool, tx, utxo_funding, utxo_spending}; + use crate::utiltest::mock_db_pool; + use bitcoin_hashes::Hash; + use bitcoincash::{BlockHash, PubkeyHash, TokenID, Txid}; + use riftenlabs_defi::cauldron::ParsedContract; + use riftenlabs_defi::chainutil::OutPointHash; + use rocket::http::Status; + use rocket::local::blocking::Client; + use rocket::routes; + + fn dummy_cauldron( + txid: &Txid, + utxo: &OutPointHash, + token: &TokenID, + sats: u64, + tokens: i64, + pkh: &PubkeyHash, + ) -> ParsedContract { + ParsedContract { + pkh: *pkh, + is_withdrawn: false, + spent_utxo_hash: OutPointHash::all_zeros(), + new_utxo_hash: Some(*utxo), + new_utxo_txid: Some(*txid), + new_utxo_n: Some(0), + token_id: Some(*token), + sats: Some(sats), + token_amount: Some(tokens), + } + } + + fn setup_mock_db(conn: &Connection) { + // Create required tables + utxo_funding::create_table(conn); + utxo_spending::create_table(conn); + tx::create_table(conn); + pool::create_table(conn); + pool::dummy_init_seq(); + + let token_zero = TokenID::all_zeros(); + let pkh_zero = PubkeyHash::all_zeros(); + let block_zero = BlockHash::all_zeros(); + + // Create test data with different timestamps + let txid1 = Txid::from_inner([0xf1; 32]); + let txid2 = Txid::from_inner([0xf2; 32]); + let txid3 = Txid::from_inner([0xf3; 32]); + let utxo1 = OutPointHash::from_inner([0xe1; 32]); + let utxo2 = OutPointHash::from_inner([0xe2; 32]); + let utxo3 = OutPointHash::from_inner([0xe3; 32]); + + // Insert transactions with different timestamps + tx::insert_block_tx(conn, &txid1, &block_zero, 1000).unwrap(); + tx::insert_block_tx(conn, &txid2, &block_zero, 2000).unwrap(); + tx::insert_block_tx(conn, &txid3, &block_zero, 3000).unwrap(); + + // Create cauldrons with different sats amounts + let cauldron1 = dummy_cauldron(&txid1, &utxo1, &token_zero, 100_000, 1_000, &pkh_zero); + let cauldron2 = dummy_cauldron(&txid2, &utxo2, &token_zero, 200_000, 2_000, &pkh_zero); + let cauldron3 = dummy_cauldron(&txid3, &utxo3, &token_zero, 300_000, 3_000, &pkh_zero); + + // Insert utxo funding data + utxo_funding::insert_utxo_funding(conn, &vec![cauldron1.clone()], &txid1, true).unwrap(); + utxo_funding::insert_utxo_funding(conn, &vec![cauldron2.clone()], &txid2, true).unwrap(); + utxo_funding::insert_utxo_funding(conn, &vec![cauldron3.clone()], &txid3, true).unwrap(); + + // Insert pool data + pool::insert_new_pool(conn, &cauldron1).unwrap(); + pool::insert_new_pool(conn, &cauldron2).unwrap(); + pool::insert_new_pool(conn, &cauldron3).unwrap(); + + // Insert pool history entries + pool::insert_pool_history_entry(conn, &utxo1, &cauldron1, Some(1000), Some(1000)).unwrap(); + pool::insert_pool_history_entry(conn, &utxo2, &cauldron2, Some(2000), Some(2000)).unwrap(); + pool::insert_pool_history_entry(conn, &utxo3, &cauldron3, Some(3000), Some(3000)).unwrap(); + } + + #[test] + fn test_get_valuelocked_all() { + // Set up a fresh mock DB + let mock_db = mock_db_pool(setup_mock_db); + + // Test the function directly first + let db = mock_db.cauldron_r.get().unwrap(); + + // Test 1: Get total TVL without time filter (should include all data) + let result = get_total_sats_tvl(&db, None).unwrap(); + assert_eq!(result, 600_000); // 100k + 200k + 300k + + // Test 2: Get TVL with time filter (should only include data before timestamp 2500) + let result = get_total_sats_tvl(&db, Some(2500)).unwrap(); + assert_eq!(result, 300_000); // Only 100k + 200k (before timestamp 2500) + + // Test 3: Get TVL with time filter (should only include data before timestamp 1500) + let result = get_total_sats_tvl(&db, Some(1500)).unwrap(); + assert_eq!(result, 100_000); // Only 100k (before timestamp 1500) + + // Test 4: Get TVL with time filter (should include no data before timestamp 500) + let result = get_total_sats_tvl(&db, Some(500)).unwrap(); + assert_eq!(result, 0); // No data before timestamp 500 + + // Now test the HTTP endpoint + let rocket = rocket::build() + .manage(mock_db) + .mount("/api", routes![super::valuelocked_all]); + let client = Client::tracked(rocket).expect("valid rocket instance"); + + // Test HTTP endpoint without time filter + let response = client.get("/api/valuelocked").dispatch(); + assert_eq!(response.status(), Status::Ok); + + let body: Value = serde_json::from_str(&response.into_string().unwrap()).unwrap(); + assert_eq!(body["satoshis"], 600_000); + + // Test HTTP endpoint with time filter + let response = client.get("/api/valuelocked?time=2500").dispatch(); + assert_eq!(response.status(), Status::Ok); + + let body: Value = serde_json::from_str(&response.into_string().unwrap()).unwrap(); + assert_eq!(body["satoshis"], 300_000); + } +}