2026-01-21 12:34:59 +01:00
|
|
|
// Copyright (C) 2024-2026 Whiterun LLC
|
2025-05-20 16:17:32 +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
|
|
|
|
|
|
2025-06-17 14:04:26 +00:00
|
|
|
use crate::db::oracle::{get_closest, get_range, get_range_with_step};
|
2025-05-20 16:17:32 +02:00
|
|
|
use crate::db::DB;
|
2026-01-21 10:37:55 +01:00
|
|
|
use crate::rpc::err::{bad_request, db_error, ApiErrorCode, CachedApiResult};
|
|
|
|
|
use crate::rpc::response::{cached_ok, CACHE_AGGREGATE};
|
2025-05-20 16:17:32 +02:00
|
|
|
use crate::timeutil::time_now;
|
|
|
|
|
use bitcoin_hashes::hex::FromHex;
|
|
|
|
|
use bitcoincash::TokenID;
|
|
|
|
|
use rocket::{get, State};
|
2025-06-17 14:04:26 +00:00
|
|
|
use serde_json::{json, Value};
|
2025-05-20 16:17:32 +02:00
|
|
|
|
2025-07-22 11:11:39 +02:00
|
|
|
/// Get the closest oracle price for a given token and timestamp.
|
|
|
|
|
///
|
|
|
|
|
/// Status: Stable
|
|
|
|
|
///
|
2026-02-24 11:31:17 +01:00
|
|
|
/// - token_id: The 32 byte token ID of the oracle contract. Known oracle contract IDs:
|
|
|
|
|
/// - BCH/USD: `d0d46f5cbd82188acede0d3e49c75700c19cb8331a30101f0bb6a260066ac972`
|
|
|
|
|
/// - timestamp: Unix timestamp (optional, defaults to now)
|
2026-01-21 08:27:57 +01:00
|
|
|
///
|
|
|
|
|
/// Returns `null` if no oracle data is found.
|
2026-02-24 11:31:17 +01:00
|
|
|
///
|
|
|
|
|
/// **Important:** `oracle_price` is returned in **cents** (not dollars). Divide by 100
|
|
|
|
|
/// to get the price in USD:
|
|
|
|
|
/// ```
|
|
|
|
|
/// price_usd = oracle_price / 100
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// **Response Example:**
|
|
|
|
|
/// ```json
|
|
|
|
|
/// {
|
|
|
|
|
/// "oracle_timestamp": 1709468902,
|
|
|
|
|
/// "oracle_price": 64320,
|
|
|
|
|
/// "oracle_sequence": 12345,
|
|
|
|
|
/// "token_id": "d0d46f5cbd82188acede0d3e49c75700c19cb8331a30101f0bb6a260066ac972",
|
|
|
|
|
/// "txid": "...",
|
|
|
|
|
/// "blockhash": "..."
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
/// In this example, `oracle_price` of 64320 cents = $643.20 USD.
|
2025-05-20 16:17:32 +02:00
|
|
|
#[get("/delphi/closest?<token_id>&<timestamp>")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn oracle_get_closest(
|
2025-05-20 16:17:32 +02:00
|
|
|
token_id: Option<String>,
|
|
|
|
|
timestamp: Option<i64>,
|
|
|
|
|
db: &State<DB>,
|
2026-01-21 10:37:55 +01:00
|
|
|
) -> CachedApiResult<serde_json::Value> {
|
2025-05-20 16:17:32 +02:00
|
|
|
let current_timestamp = timestamp.unwrap_or_else(time_now);
|
|
|
|
|
let token_id = token_id
|
|
|
|
|
.map(|t| {
|
2026-01-21 08:27:57 +01:00
|
|
|
TokenID::from_hex(&t).map_err(|e| {
|
|
|
|
|
bad_request(
|
|
|
|
|
ApiErrorCode::InvalidTokenId,
|
|
|
|
|
&format!("Invalid token ID: {e}"),
|
|
|
|
|
)
|
|
|
|
|
})
|
2025-05-20 16:17:32 +02:00
|
|
|
})
|
|
|
|
|
.transpose()?;
|
2026-02-17 17:47:43 +01:00
|
|
|
let entry = get_closest(&db.oracle_r, &token_id, current_timestamp)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(db_error)?;
|
2026-01-21 10:37:55 +01:00
|
|
|
Ok(cached_ok(
|
|
|
|
|
entry.map_or(serde_json::Value::Null, |e| {
|
|
|
|
|
serde_json::to_value(e).unwrap()
|
|
|
|
|
}),
|
|
|
|
|
CACHE_AGGREGATE,
|
|
|
|
|
))
|
2025-05-20 16:17:32 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 11:11:39 +02:00
|
|
|
/// Status: Deprecated
|
2025-05-20 16:17:32 +02:00
|
|
|
#[get("/delphi/range?<token_id>&<start>&<end>")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn oracle_get_range(
|
2025-05-20 16:17:32 +02:00
|
|
|
token_id: Option<String>,
|
|
|
|
|
start: Option<i64>,
|
|
|
|
|
end: Option<i64>,
|
|
|
|
|
db: &State<DB>,
|
2026-01-21 10:37:55 +01:00
|
|
|
) -> CachedApiResult<Vec<serde_json::Value>> {
|
2025-05-20 16:17:32 +02:00
|
|
|
let end_timestamp = end.unwrap_or_else(time_now);
|
2026-02-17 17:47:43 +01:00
|
|
|
let start_timestamp = start.unwrap_or_else(|| end_timestamp - 86400);
|
2025-05-20 16:17:32 +02:00
|
|
|
let token_id = token_id
|
|
|
|
|
.map(|t| {
|
2026-01-21 08:27:57 +01:00
|
|
|
TokenID::from_hex(&t).map_err(|e| {
|
|
|
|
|
bad_request(
|
|
|
|
|
ApiErrorCode::InvalidTokenId,
|
|
|
|
|
&format!("Invalid token ID: {e}"),
|
|
|
|
|
)
|
|
|
|
|
})
|
2025-05-20 16:17:32 +02:00
|
|
|
})
|
|
|
|
|
.transpose()?;
|
2026-02-17 17:47:43 +01:00
|
|
|
let entries = get_range(&db.oracle_r, &token_id, start_timestamp, end_timestamp)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(db_error)?;
|
2026-01-21 10:37:55 +01:00
|
|
|
Ok(cached_ok(
|
2025-05-20 16:17:32 +02:00
|
|
|
entries
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|e| serde_json::to_value(e).unwrap())
|
|
|
|
|
.collect(),
|
2026-01-21 10:37:55 +01:00
|
|
|
CACHE_AGGREGATE,
|
2025-05-20 16:17:32 +02:00
|
|
|
))
|
|
|
|
|
}
|
2025-06-17 14:04:26 +00:00
|
|
|
|
2025-07-22 11:11:39 +02:00
|
|
|
/// Get historical oracle prices for a given token.
|
|
|
|
|
///
|
2026-02-24 11:31:17 +01:00
|
|
|
/// - token: The 32 byte token ID of the oracle contract. Known oracle contract IDs:
|
|
|
|
|
/// - BCH/USD: `d0d46f5cbd82188acede0d3e49c75700c19cb8331a30101f0bb6a260066ac972`
|
2025-07-22 11:11:39 +02:00
|
|
|
/// - start: Unix timestamp for start of period
|
2026-02-24 11:31:17 +01:00
|
|
|
/// - end: Unix timestamp for end of period (optional, defaults to now)
|
|
|
|
|
/// - stepsize: Seconds per interval (optional)
|
|
|
|
|
///
|
|
|
|
|
/// **Important:** `oracle_price` values are in **cents**. Divide by 100 to convert to USD.
|
|
|
|
|
///
|
|
|
|
|
/// **Response Example:**
|
|
|
|
|
/// ```json
|
|
|
|
|
/// [
|
|
|
|
|
/// { "time": 1709468902, "price": 64320, "txid": "...", "blockhash": "...", "sequence": 12345 },
|
|
|
|
|
/// { "time": 1709555302, "price": 65100, "txid": "...", "blockhash": "...", "sequence": 12346 }
|
|
|
|
|
/// ]
|
|
|
|
|
/// ```
|
|
|
|
|
/// In this example, `price` values of 64320 cents = $643.20 USD.
|
2025-06-17 14:04:26 +00:00
|
|
|
#[get("/delphi/<token>/history?<start>&<end>&<stepsize>")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn oracle_get_history(
|
2025-06-17 14:04:26 +00:00
|
|
|
token: &str,
|
|
|
|
|
start: Option<i64>,
|
|
|
|
|
end: Option<i64>,
|
|
|
|
|
stepsize: Option<i64>,
|
|
|
|
|
db: &State<DB>,
|
2026-01-21 10:37:55 +01:00
|
|
|
) -> CachedApiResult<Value> {
|
2025-06-17 14:04:26 +00:00
|
|
|
let current_timestamp = time_now();
|
|
|
|
|
|
2026-01-21 08:27:57 +01:00
|
|
|
let token_id = token.parse::<TokenID>().map_err(|e| {
|
|
|
|
|
bad_request(
|
|
|
|
|
ApiErrorCode::InvalidTokenId,
|
|
|
|
|
&format!("Invalid token ID: {e}"),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
2025-06-17 14:04:26 +00:00
|
|
|
|
2026-02-17 17:47:43 +01:00
|
|
|
let start_ts = start.unwrap_or(current_timestamp - 30 * 24 * 3600);
|
2025-06-17 14:04:26 +00:00
|
|
|
let end_ts = end.unwrap_or(current_timestamp);
|
|
|
|
|
|
|
|
|
|
let entries = if let Some(step) = stepsize {
|
2026-02-17 17:47:43 +01:00
|
|
|
get_range_with_step(&db.oracle_r, &Some(token_id), start_ts, end_ts, step)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
bad_request(
|
|
|
|
|
ApiErrorCode::InvalidParameters,
|
|
|
|
|
&format!("Query error: {e}"),
|
|
|
|
|
)
|
|
|
|
|
})?
|
2025-06-17 14:04:26 +00:00
|
|
|
} else {
|
2026-02-17 17:47:43 +01:00
|
|
|
get_range(&db.oracle_r, &Some(token_id), start_ts, end_ts)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
bad_request(
|
|
|
|
|
ApiErrorCode::InvalidParameters,
|
|
|
|
|
&format!("Query error: {e}"),
|
|
|
|
|
)
|
|
|
|
|
})?
|
2025-06-17 14:04:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let json_entries: Vec<Value> = entries
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|entry| {
|
|
|
|
|
json!({
|
|
|
|
|
"time": entry.oracle_timestamp,
|
|
|
|
|
"price": entry.oracle_price,
|
|
|
|
|
"txid": entry.txid,
|
|
|
|
|
"blockhash": entry.blockhash,
|
|
|
|
|
"sequence": entry.oracle_sequence,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2026-01-21 10:37:55 +01:00
|
|
|
Ok(cached_ok(
|
|
|
|
|
serde_json::Value::Array(json_entries),
|
|
|
|
|
CACHE_AGGREGATE,
|
|
|
|
|
))
|
2025-06-17 14:04:26 +00:00
|
|
|
}
|