-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
53 lines (44 loc) · 1.82 KB
/
Copy pathlib.rs
File metadata and controls
53 lines (44 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pub mod models;
pub mod rpc;
pub mod utils;
use crate::rpc::fetch::{fetch_latest_block, fetch_last_5_blocks_and_receipts};
use std::ffi::CString;
use std::os::raw::c_char;
use crate::utils::hex_to_u64;
#[no_mangle]
pub extern "C" fn fetch_transactions(rpc_url: *const c_char) -> *mut c_char {
// Pretvaranje C stringa u Rust string
let c_str = unsafe { std::ffi::CStr::from_ptr(rpc_url) };
let rpc_url_str = c_str.to_str().unwrap();
// Pozivanje fetch_latest_block funkcije za dobijanje najnovijeg bloka
let future = tokio::runtime::Runtime::new()
.unwrap()
.block_on(fetch_latest_block(rpc_url_str, true));
// Prikupljanje transakcija iz rezultata
let mut transaction_data = String::new();
for tx in &future.transactions {
transaction_data.push_str(&format!(
"Tx hash: {}\nFrom: {}\nTo: {}\nValue: {}\nGas: {}\n\n",
tx.hash,
tx.from,
tx.to.clone().unwrap_or_else(|| "N/A".into()),
tx.value,
tx.gas
));
}
let c_str_result = CString::new(transaction_data).unwrap();
// Vracanje C stringa koji Go moze koristiti
c_str_result.into_raw()
}
#[no_mangle]
pub extern "C" fn fetch_last_5_blocks(rpc_url: *const c_char) -> *mut c_char {
let c_rpc = unsafe { std::ffi::CStr::from_ptr(rpc_url) };
let rpc_url_str = c_rpc.to_str().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let latest_block = runtime.block_on(fetch_latest_block(rpc_url_str, true));
let latest_block_number = hex_to_u64(&latest_block.number);
let summaries = runtime.block_on(fetch_last_5_blocks_and_receipts(rpc_url_str.to_string(), latest_block_number));
let json_str = serde_json::to_string(&summaries).unwrap();
let c_str_result = CString::new(json_str).unwrap();
c_str_result.into_raw()
}