rhizo-sdk 0.1.1

SDK for building serverless API routes on the rhizo network
Documentation
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;

/// Read mutable on-chain bytes specified by the program derived address.
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(())
}

/// Write mutable on-chain bytes. Provide a signed transaction. This function is not recommended to
/// be used unless you intend to side-effect along side the write in your funciton. Because transactions
/// are signed against the latest block there is a chance the block expires before the write
/// invalidating the write, and the signed transaction. This means if calling this function is
/// necessary, sufficient retry logic should be provided on the client side.
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(())
}