riftenlabs-indexer/src/rpc/tx.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2026-01-21 12:34:59 +01:00
// Copyright (C) 2024-2026 Whiterun LLC
2024-09-25 11:56:55 +02:00
//
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
use bitcoin_hashes::hex::FromHex;
use bitcoincash::TokenID;
use rocket::{get, State};
2024-09-25 11:56:55 +02:00
use serde_json::json;
use serde_json::Value;
use crate::db::DB;
use crate::rpc::err::{bad_request, db_error, ApiErrorCode, CachedApiResult};
use crate::rpc::response::{cached_ok, CACHE_NONE};
2024-09-25 11:56:55 +02:00
#[get("/tx/latest?<limit>&<offset>&<token>")]
pub async fn tx_latest(
2024-09-25 11:56:55 +02:00
limit: Option<usize>,
offset: Option<usize>,
token: Option<&str>,
conn: &State<DB>,
) -> CachedApiResult<Value> {
2024-09-25 11:56:55 +02:00
let limit = limit.unwrap_or(100).min(10000);
let offset = offset.unwrap_or(0);
let token = match token {
None => None,
Some(tokenhex) => match TokenID::from_hex(tokenhex) {
Ok(token) => Some(token),
Err(_) => {
return Err(bad_request(
ApiErrorCode::InvalidTokenId,
"Invalid token ID",
))
}
2024-09-25 11:56:55 +02:00
},
};
let txs = crate::db::cauldron::tx::latest(&conn.cauldron_r, limit, offset, token)
.await
.map_err(db_error)?;
2024-09-25 11:56:55 +02:00
let txs_json: Vec<Value> = txs
.into_iter()
.map(|tx| {
json!({
"txid": tx.0.to_string(),
"blockhash": tx.1.map(|blockhex| blockhex.to_string()),
"timestamp_guess": tx.2
})
})
.collect();
Ok(cached_ok(json!(txs_json), CACHE_NONE))
2024-09-25 11:56:55 +02:00
}