riftenlabs-indexer/src/rpc/oracle.rs

68 lines
2.3 KiB
Rust
Raw Normal View History

// Copyright (C) 2024 Riften Labs AS
//
// 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 crate::db::oracle::{get_closest, get_range};
use crate::db::DB;
use crate::timeutil::time_now;
use bitcoin_hashes::hex::FromHex;
use bitcoincash::TokenID;
use rocket::http::Status;
use rocket::response::status::Custom;
use rocket::serde::json::Json;
use rocket::{get, State};
#[get("/delphi/closest?<token_id>&<timestamp>")]
pub fn oracle_get_closest(
token_id: Option<String>,
timestamp: Option<i64>,
db: &State<DB>,
) -> Result<Json<serde_json::Value>, Custom<String>> {
let current_timestamp = timestamp.unwrap_or_else(time_now);
let conn = db
.oracle_r
.get()
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?;
let token_id = token_id
.map(|t| {
TokenID::from_hex(&t)
.map_err(|e| Custom(Status::BadRequest, format!("Invalid token ID: {}", e)))
})
.transpose()?;
let entry = get_closest(&conn, &token_id, current_timestamp)
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?;
Ok(Json(entry.map_or(serde_json::Value::Null, |e| {
serde_json::to_value(e).unwrap()
})))
}
#[get("/delphi/range?<token_id>&<start>&<end>")]
pub fn oracle_get_range(
token_id: Option<String>,
start: Option<i64>,
end: Option<i64>,
db: &State<DB>,
) -> Result<Json<Vec<serde_json::Value>>, Custom<String>> {
let end_timestamp = end.unwrap_or_else(time_now);
let start_timestamp = start.unwrap_or_else(|| end_timestamp - 86400); // 1 day in seconds
let conn = db
.oracle_r
.get()
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?;
let token_id = token_id
.map(|t| {
TokenID::from_hex(&t)
.map_err(|e| Custom(Status::BadRequest, format!("Invalid token ID: {}", e)))
})
.transpose()?;
let entries = get_range(&conn, &token_id, start_timestamp, end_timestamp)
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?;
Ok(Json(
entries
.into_iter()
.map(|e| serde_json::to_value(e).unwrap())
.collect(),
))
}