more structs, funcs, mounts, routes

master
lalanza808 5 years ago
parent 7addf838ae
commit 43f8de2de7

@ -2,7 +2,8 @@
pub struct RPCPayload {
pub jsonrpc: String,
pub id: String,
pub method: String
pub method: String,
pub params: Option<RPCParams>
}
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<String>,
pub txs_hashes: Option<Vec<String>>
}
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<GetTransactionsTxs>
}
#[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<u32>,
pub prunable_as_hex: String,
pub prunable_hash: String,
pub pruned_as_hex: String,
pub tx_hash: String
}

@ -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<RPCParams>) -> 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(&params)
}
#[get("/transaction/<hash>")]
fn get_transaction(hash: String) -> Json<BlockHeader> {
let res: BlockByHeaderHash = issue_rpc(&"get_block_header_by_hash")
.send()
.unwrap().json().unwrap();
// /block
#[get("/hash/<block_hash>")]
fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
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/<tx_hash>")]
fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions> {
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<GetInfoResult> {
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();

Loading…
Cancel
Save