mirror of
https://github.com/fluencelabs/examples
synced 2024-12-04 19:20:17 +00:00
update block_geter
This commit is contained in:
parent
61c9d5eaf4
commit
19659799c4
@ -7,7 +7,7 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
fluence = { version = "0.2.18", features = ["logger"]}
|
||||
fluence = { version = "=0.3.3", features = ["logger"] }
|
||||
log = "0.4.8"
|
||||
serde_json = "1.0.64"
|
||||
fstrings = "0.2.3"
|
||||
|
@ -16,21 +16,42 @@
|
||||
|
||||
use crate::curl_request;
|
||||
use fluence::fce;
|
||||
use fluence::MountedBinaryResult;
|
||||
|
||||
|
||||
fn result_to_string(result:MountedBinaryResult) -> String {
|
||||
if result.is_success() {
|
||||
return String::from_utf8(result.stdout).expect("Found invalid UTF-8");
|
||||
}
|
||||
String::from_utf8(result.stderr).expect("Found invalid UTF-8")
|
||||
}
|
||||
|
||||
#[fce]
|
||||
pub fn get_latest_block(api_key: String) -> String {
|
||||
let url =
|
||||
f!("https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey={api_key}");
|
||||
let url = f!("https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey={api_key}");
|
||||
let header = "-d \"\"";
|
||||
|
||||
let response: String = unsafe { curl_request(url) };
|
||||
response
|
||||
let curl_cmd:Vec<String> = vec![header.into(), url.into()];
|
||||
let response = unsafe { curl_request(curl_cmd) };
|
||||
result_to_string(response)
|
||||
|
||||
/*
|
||||
let raw_string = result_to_string(response);
|
||||
let obj = serde_json::from_str(&raw_string);
|
||||
let res = match obj {
|
||||
Ok(x) => x["result"],
|
||||
Err(_) => "",
|
||||
};
|
||||
String::from(res)
|
||||
*/
|
||||
}
|
||||
|
||||
#[fce]
|
||||
pub fn get_block(api_key: String, block_number: u64) -> String {
|
||||
// let block_num = format!("{:X}", block_number);
|
||||
pub fn get_block(api_key: String, block_number: u32) -> String {
|
||||
let url = f!("https://api.etherscan.io/api?module=block&action=getblockreward&blockno={block_number}&apikey={api_key}");
|
||||
let header = "-d \"\"";
|
||||
|
||||
let response: String = unsafe { curl_request(url) };
|
||||
response
|
||||
let curl_cmd:Vec<String> = vec![header.into(), url];
|
||||
let response = unsafe { curl_request(curl_cmd) };
|
||||
result_to_string(response)
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
extern crate fstrings;
|
||||
|
||||
use fluence::{fce, WasmLoggerBuilder};
|
||||
use fluence::MountedBinaryResult as Result;
|
||||
|
||||
mod eth_block_getters;
|
||||
|
||||
@ -27,5 +28,6 @@ fn main() {
|
||||
#[fce]
|
||||
#[link(wasm_import_module = "curl_adapter")]
|
||||
extern "C" {
|
||||
pub fn curl_request(url: String) -> String;
|
||||
pub fn curl_request(curl_cmd: Vec<String>) -> Result;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user