mirror of
https://github.com/fluencelabs/examples
synced 2025-04-25 10:42:16 +00:00
add structured account view with serde_json and update aqua example
This commit is contained in:
parent
ba79f5ce9c
commit
faf2cf64d7
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "curl_adapter",
|
||||
"mountedBinaries": {
|
||||
"curl": "/usr/bin/curl"
|
||||
},
|
||||
"mem_page_count": 1,
|
||||
"logger_enabled": false
|
||||
"name": "curl_adapter",
|
||||
"mountedBinaries": {
|
||||
"curl": "/usr/bin/curl"
|
||||
},
|
||||
"mem_page_count": 10,
|
||||
"logger_enabled": false
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "near_rpc_services",
|
||||
"mem_page_count": 10,
|
||||
"logger_enabled": true
|
||||
"name": "near_rpc_services",
|
||||
"mem_page_count": 10,
|
||||
"logger_enabled": true
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
marine-rs-sdk = { version = "0.6.15", features = ["logger"] }
|
||||
log = "0.4.14"
|
||||
serde = { version = "1.0.132", default-features = false }
|
||||
serde_json = { version = "1.0.73", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
marine-rs-sdk-test = "0.4.0"
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
use marine_rs_sdk::{marine, module_manifest, MountedBinaryResult, WasmLoggerBuilder};
|
||||
use serde;
|
||||
use serde_json;
|
||||
|
||||
mod utils;
|
||||
@ -78,7 +79,7 @@ pub fn node_status(network_id: String) -> Result {
|
||||
|
||||
#[marine]
|
||||
pub fn view_account(network_id: String, account_id: String) -> Result {
|
||||
let method = "queryX".to_string();
|
||||
let method = "query".to_string();
|
||||
let url = url_maker(network_id);
|
||||
let params = format!(
|
||||
"{{\"request_type\": \"view_account\", \"finality\": \"final\",\"account_id\": \"{}\"}}",
|
||||
@ -91,6 +92,109 @@ pub fn view_account(network_id: String, account_id: String) -> Result {
|
||||
stdout: String::from_utf8(response.stdout).unwrap(),
|
||||
}
|
||||
}
|
||||
#[marine]
|
||||
#[derive(Default)]
|
||||
pub struct VAResult {
|
||||
pub stderr: String,
|
||||
pub stdout: VAResponse,
|
||||
}
|
||||
#[marine]
|
||||
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct VAResponse {
|
||||
pub amount: String,
|
||||
pub locked: String,
|
||||
pub code_hash: String,
|
||||
pub storage_usage: u64,
|
||||
pub storage_paid_at: u64,
|
||||
pub block_height: u64,
|
||||
pub block_hash: String,
|
||||
}
|
||||
|
||||
impl VAResponse {
|
||||
pub fn new(
|
||||
amount: String,
|
||||
locked: String,
|
||||
code_hash: String,
|
||||
storage_usage: u64,
|
||||
storage_paid_at: u64,
|
||||
block_height: u64,
|
||||
block_hash: String,
|
||||
) -> Self {
|
||||
VAResponse {
|
||||
amount,
|
||||
locked,
|
||||
code_hash,
|
||||
storage_usage,
|
||||
storage_paid_at,
|
||||
block_height,
|
||||
block_hash,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_obj(obj: &serde_json::Value) -> Self {
|
||||
VAResponse {
|
||||
amount: serde_json::ser::to_string(&obj["amount"]).unwrap(),
|
||||
locked: serde_json::ser::to_string(&obj["locked"]).unwrap(),
|
||||
code_hash: serde_json::ser::to_string(&obj["code_hash"]).unwrap(),
|
||||
storage_usage: obj["storage_usage"].as_u64().unwrap(),
|
||||
storage_paid_at: obj["storage_paid_at"].as_u64().unwrap(),
|
||||
block_height: obj["block_height"].as_u64().unwrap(),
|
||||
block_hash: serde_json::ser::to_string(&obj["block_hash"]).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn view_account_structured(network_id: String, account_id: String) -> VAResult {
|
||||
let method = "query".to_string();
|
||||
let url = url_maker(network_id);
|
||||
let params = format!(
|
||||
"{{\"request_type\": \"view_account\", \"finality\": \"final\",\"account_id\": \"{}\"}}",
|
||||
account_id
|
||||
);
|
||||
let curl_params: Vec<String> = rpc_maker(url, method, params);
|
||||
let response = curl_request(curl_params);
|
||||
let stderr = response.error;
|
||||
let stdout = String::from_utf8(response.stdout).unwrap();
|
||||
|
||||
if stderr.len() > 0 {
|
||||
return VAResult {
|
||||
stderr,
|
||||
stdout: VAResponse { ..<_>::default() },
|
||||
};
|
||||
}
|
||||
|
||||
let response: serde_json::Result<serde_json::Value> = serde_json::from_str(&stdout);
|
||||
|
||||
if response.is_err() {
|
||||
return VAResult {
|
||||
stderr: "unable to deserialize".to_string(),
|
||||
stdout: VAResponse { ..<_>::default() },
|
||||
};
|
||||
}
|
||||
|
||||
let response: serde_json::Value = response.unwrap();
|
||||
|
||||
if response["error"] != serde_json::Value::Null {
|
||||
return VAResult {
|
||||
stderr: stdout,
|
||||
stdout: VAResponse { ..<_>::default() },
|
||||
};
|
||||
};
|
||||
|
||||
if response["result"] == serde_json::Value::Null {
|
||||
return VAResult {
|
||||
stderr: format!("Something went really wrong: {}", stdout),
|
||||
stdout: VAResponse { ..<_>::default() },
|
||||
};
|
||||
};
|
||||
let response = VAResponse::from_obj(&response["result"]);
|
||||
|
||||
VAResult {
|
||||
stderr: "".to_string(),
|
||||
stdout: response,
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
#[link(wasm_import_module = "curl_adapter")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user