mirror of
https://github.com/fluencelabs/examples
synced 2025-04-25 18:52:15 +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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fluence = { version = "0.2.18", features = ["logger"]}
|
fluence = { version = "=0.3.3", features = ["logger"] }
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
serde_json = "1.0.64"
|
serde_json = "1.0.64"
|
||||||
fstrings = "0.2.3"
|
fstrings = "0.2.3"
|
||||||
|
@ -16,21 +16,42 @@
|
|||||||
|
|
||||||
use crate::curl_request;
|
use crate::curl_request;
|
||||||
use fluence::fce;
|
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]
|
#[fce]
|
||||||
pub fn get_latest_block(api_key: String) -> String {
|
pub fn get_latest_block(api_key: String) -> String {
|
||||||
let url =
|
let url = f!("https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey={api_key}");
|
||||||
f!("https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey={api_key}");
|
let header = "-d \"\"";
|
||||||
|
|
||||||
let response: String = unsafe { curl_request(url) };
|
let curl_cmd:Vec<String> = vec![header.into(), url.into()];
|
||||||
response
|
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]
|
#[fce]
|
||||||
pub fn get_block(api_key: String, block_number: u64) -> String {
|
pub fn get_block(api_key: String, block_number: u32) -> String {
|
||||||
// let block_num = format!("{:X}", block_number);
|
|
||||||
let url = f!("https://api.etherscan.io/api?module=block&action=getblockreward&blockno={block_number}&apikey={api_key}");
|
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) };
|
let curl_cmd:Vec<String> = vec![header.into(), url];
|
||||||
response
|
let response = unsafe { curl_request(curl_cmd) };
|
||||||
|
result_to_string(response)
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
extern crate fstrings;
|
extern crate fstrings;
|
||||||
|
|
||||||
use fluence::{fce, WasmLoggerBuilder};
|
use fluence::{fce, WasmLoggerBuilder};
|
||||||
|
use fluence::MountedBinaryResult as Result;
|
||||||
|
|
||||||
mod eth_block_getters;
|
mod eth_block_getters;
|
||||||
|
|
||||||
@ -27,5 +28,6 @@ fn main() {
|
|||||||
#[fce]
|
#[fce]
|
||||||
#[link(wasm_import_module = "curl_adapter")]
|
#[link(wasm_import_module = "curl_adapter")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn curl_request(url: String) -> String;
|
pub fn curl_request(curl_cmd: Vec<String>) -> Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user