diff --git a/src/data_types.rs b/src/data_types.rs index 94c93e7..807c472 100644 --- a/src/data_types.rs +++ b/src/data_types.rs @@ -2,7 +2,8 @@ pub struct RPCPayload { pub jsonrpc: String, pub id: String, - pub method: String + pub method: String, + pub params: Option } impl Default for RPCPayload { @@ -10,7 +11,23 @@ impl Default for RPCPayload { RPCPayload { jsonrpc: "2.0".to_string(), id: "0".to_string(), - method: "get_info".to_string() + method: "get_info".to_string(), + params: None, + } + } +} + +#[derive(Serialize, Deserialize)] +pub struct RPCParams { + pub hash: Option, + pub txs_hashes: Option> +} + +impl Default for RPCParams { + fn default() -> RPCParams { + RPCParams { + hash: None, + txs_hashes: None } } } @@ -95,3 +112,24 @@ pub struct BlockHeader { pub reward: u64, pub timestamp: u64 } + +#[derive(Serialize, Deserialize)] +pub struct GetTransactions { + pub credits: u32, + pub status: String, + pub top_hash: String, + pub txs: Vec +} + +#[derive(Serialize, Deserialize)] +pub struct GetTransactionsTxs { + pub block_height: u32, + pub block_timestamp: u64, + pub double_spend_seen: bool, + pub in_pool: bool, + pub output_indices: Vec, + pub prunable_as_hex: String, + pub prunable_hash: String, + pub pruned_as_hex: String, + pub tx_hash: String +} diff --git a/src/main.rs b/src/main.rs index b1fb673..048190c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,52 +5,83 @@ extern crate reqwest; mod data_types; use rocket_contrib::json::{Json, JsonValue}; +use reqwest::blocking::RequestBuilder; use std::env; use data_types::*; - - -fn issue_rpc(method: &str) -> reqwest::blocking::RequestBuilder { - let uri = env::var("DAEMON_URI").unwrap(); +fn issue_rpc(method: &str, params: Option) -> RequestBuilder { let http_client = reqwest::blocking::Client::new(); + let url = format!( + "{}/json_rpc", + env::var("DAEMON_URI").unwrap() + ); let post_data = RPCPayload { method: method.to_string(), + params: params, ..Default::default() }; - return http_client.post(&uri).json(&post_data); + http_client.post(&url).json(&post_data) } -#[catch(404)] -fn not_found() -> JsonValue { - json!({ - "status": "error", - "reason": "Resource was not found." - }) +fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder { + let http_client = reqwest::blocking::Client::new(); + let url = format!( + "{}/{}", + env::var("DAEMON_URI").unwrap(), + &method + ); + http_client.post(&url).json(¶ms) } -#[get("/transaction/")] -fn get_transaction(hash: String) -> Json { - let res: BlockByHeaderHash = issue_rpc(&"get_block_header_by_hash") - .send() - .unwrap().json().unwrap(); +// /block + +#[get("/hash/")] +fn get_block_header_by_block_hash(block_hash: String) -> Json { + let params = RPCParams { + hash: Some(block_hash), + ..Default::default() + }; + let res: BlockByHeaderHash = issue_rpc(&"get_block_header_by_hash", Some(params)) + .send().unwrap().json().unwrap(); Json(res.result.block_header) } +#[get("/tx/")] +fn get_block_header_by_transaction_hash(tx_hash: String) -> Json { + let params: JsonValue = json!({"txs_hashes": [&tx_hash]}); + let res: GetTransactions = issue_raw_rpc(&"get_transactions", params) + .send().unwrap().json().unwrap(); + Json(res) +} + +// / + #[get("/info")] fn get_daemon_info() -> Json { - let res: GetInfo = issue_rpc(&"get_info") - .send() - .unwrap().json().unwrap(); + let res: GetInfo = issue_rpc(&"get_info", None) + .send().unwrap().json().unwrap(); Json(res.result) } +#[catch(404)] +fn not_found() -> JsonValue { + json!({ + "status": "error", + "reason": "Resource was not found." + }) +} + fn main() { let env_url = env::var("DAEMON_URI"); match env_url { Ok(_) => { rocket::ignite() .mount("/", routes![ - get_daemon_info, get_transaction + get_daemon_info + ]) + .mount("/block", routes![ + get_block_header_by_block_hash, + get_block_header_by_transaction_hash ]) .register(catchers![not_found]) .launch();