use b64::{FromBase64, ToBase64, STANDARD};
use reqwest::header::CONTENT_TYPE;
use serde_json::Value;
use borsh::de::BorshDeserialize;
use rhizo_types::SignedOnchainBytes;
use borsh::BorshSerialize;
use std::io::Read;
pub fn read(pda: &str) -> Result<Vec<u8>, ()> {
let url = "http://api.devnet.solana.com/";
let client = reqwest::blocking::Client::new();
let body_bytes = "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"getAccountInfo\", \"params\": [\"$PDA\", {\"encoding\": \"base64\"}]}";
let body_bytes = body_bytes.replace("$PDA", &pda.to_string());
let response = client.post(url).body(body_bytes).header(CONTENT_TYPE, "application/json").send();
if response.is_ok() {
let text = response.unwrap().text();
if text.is_ok() {
let json_body = text.unwrap();
let parsed_response: Value = serde_json::from_str(&json_body).unwrap();
if let Value::Object(map) = parsed_response {
let account_data = map.get("result")
.unwrap().as_object()
.unwrap().get("value")
.unwrap().as_object()
.unwrap().get("data")
.unwrap().as_array()
.unwrap();
let try_encoded = account_data.first().unwrap().as_str();
let encoded = try_encoded.unwrap().as_bytes().to_vec();
let decoded = encoded.from_base64().map_err(|_| ());
let decoded = decoded.unwrap();
let mut decoded_slice = decoded.as_slice();
let socb = SignedOnchainBytes::deserialize(&mut decoded_slice).unwrap();
return Ok(socb.inner)
}
}
}
Err(())
}
pub fn write(tx: &str) -> Result<String, ()> {
let url = "http://api.devnet.solana.com/";
let client = reqwest::blocking::Client::new();
let body_bytes = "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"sendTransaction\", \"params\": [\"$TX\", {\"encoding\": \"base64\"}]}";
let tx = tx.from_base64().unwrap().to_base64(b64::STANDARD);
let body_bytes = body_bytes.replace("$TX", &tx.to_string());
let response = client.post(url).body(body_bytes).header(CONTENT_TYPE, "application/json").send();
if response.is_ok() {
let text = response.unwrap().text();
if text.is_ok() {
return Ok(text.unwrap())
}
}
Err(())
}