Skip to content

Commit 55647aa

Browse files
committed
cargo fmt
1 parent 2b6b123 commit 55647aa

8 files changed

Lines changed: 64 additions & 87 deletions

File tree

goCode/internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func LoadEnv() (string, string, string) {
1111
err := godotenv.Load("../.env")
1212
if err != nil {
13-
log.Fatal("Greska pri učitavanju .env fajla")
13+
log.Fatal("Greska pri ucitavanju .env fajla")
1414
}
1515

1616
rpcURL := os.Getenv("RPC_URL")

goCode/internal/rpc/rpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func GetLatestBlock(rpcURL string) string {
3232
// Citamo telo odgovora
3333
body, err := io.ReadAll(resp.Body)
3434
if err != nil {
35-
log.Fatalf("Greska pri čitanju odgovora: %v", err)
35+
log.Fatalf("Greska pri citanju odgovora: %v", err)
3636
}
3737

3838
// Parsiramo odgovor

goCode/internal/transaction/transaction.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package transaction
22

33
import (
4-
"goCode/internal/utilis"
54
"context"
65
"crypto/ecdsa"
76
"fmt"
7+
"goCode/internal/utilis"
88
"log"
99

1010
"github.com/ethereum/go-ethereum/common"
@@ -25,7 +25,7 @@ func SendTransaction(rpcURL string, recipientAddress string, sendEth int64, priv
2525
log.Fatalf("Neuspesna konverzija privatnog kljuca: %v", err)
2626
}
2727

28-
// Adresa pošiljaoca
28+
// Adresa posiljaoca
2929
publicKey := privateKey.Public().(*ecdsa.PublicKey)
3030
fromAddress := crypto.PubkeyToAddress(*publicKey)
3131

@@ -51,7 +51,7 @@ func SendTransaction(rpcURL string, recipientAddress string, sendEth int64, priv
5151
// Potpisivanje transakcije
5252
chainID, err := client.NetworkID(context.Background())
5353
if err != nil {
54-
log.Fatalf("Greska pri dohvatanju ID mreže: %v", err)
54+
log.Fatalf("Greska pri dohvatanju ID mreze: %v", err)
5555
}
5656
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
5757
if err != nil {

rustCode/src/config/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::{env, path::Path};
2-
use dotenvy::from_path;
3-
4-
pub fn load_env() {
5-
let env_path = Path::new("../goCode/.env");
6-
from_path(env_path).expect("Nije moguce ucitati .env fajl sa date putanje");
7-
}
8-
9-
pub fn get_rpc_url() -> String {
10-
env::var("RPC_URL").expect("RPC_URL nije definisan")
11-
}
1+
use dotenvy::from_path;
2+
use std::{env, path::Path};
3+
4+
pub fn load_env() {
5+
let env_path = Path::new("../goCode/.env");
6+
from_path(env_path).expect("Nije moguce ucitati .env fajl sa date putanje");
7+
}
8+
9+
pub fn get_rpc_url() -> String {
10+
env::var("RPC_URL").expect("RPC_URL nije definisan")
11+
}

rustCode/src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
pub mod rpc;
21
pub mod models;
3-
2+
pub mod rpc;
43

54
use crate::rpc::fetch::fetch_latest_block;
65
use std::ffi::CString;
7-
use std::os::raw::c_char;
8-
use crate::models::SimpleBlock;
9-
6+
use std::os::raw::c_char;
107

118
#[no_mangle]
129
pub extern "C" fn fetch_transactions(rpc_url: *const c_char) -> *mut c_char {
1310
// Pretvaranje C stringa u Rust string
1411
let c_str = unsafe { std::ffi::CStr::from_ptr(rpc_url) };
1512
let rpc_url_str = c_str.to_str().unwrap();
1613
// Pozivanje fetch_latest_block funkcije za dobijanje najnovijeg bloka
17-
let future = tokio::runtime::Runtime::new().unwrap().block_on(fetch_latest_block(rpc_url_str));
14+
let future = tokio::runtime::Runtime::new()
15+
.unwrap()
16+
.block_on(fetch_latest_block(rpc_url_str));
1817

1918
// Prikupljanje transakcija iz rezultata
2019
let mut transaction_data = String::new();
2120
for tx in &future.transactions {
22-
transaction_data.push_str(&format!("Tx hash: {}\nFrom: {}\nTo: {}\nValue: {}\nGas: {}\n\n",
23-
tx.hash, tx.from, tx.to.clone().unwrap_or_else(|| "N/A".into()),
24-
tx.value, tx.gas));
21+
transaction_data.push_str(&format!(
22+
"Tx hash: {}\nFrom: {}\nTo: {}\nValue: {}\nGas: {}\n\n",
23+
tx.hash,
24+
tx.from,
25+
tx.to.clone().unwrap_or_else(|| "N/A".into()),
26+
tx.value,
27+
tx.gas
28+
));
2529
}
2630

2731
let c_str_result = CString::new(transaction_data).unwrap();
2832

29-
// Vraćanje C stringa koji Go može koristiti
33+
// Vracanje C stringa koji Go moye koristiti
3034
c_str_result.into_raw()
3135
}

rustCode/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
mod config;
2-
mod rpc;
32
mod models;
3+
mod rpc;
44

5-
use rpc::fetch::{fetch_latest_block};
6-
use config::{load_env, get_rpc_url};
5+
use config::{get_rpc_url, load_env};
76
use models::SimpleBlock;
7+
use rpc::fetch::fetch_latest_block;
88
use tokio;
99

1010
fn print_block_info(block: &SimpleBlock) {

rustCode/src/models/mod.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
use serde::{Deserialize};
2-
3-
#[derive(Debug, Deserialize)]
4-
pub struct RPCResponse {
5-
pub result: SimpleBlock,
6-
}
7-
8-
#[derive(Debug, Deserialize)]
9-
#[serde(rename_all = "camelCase")]
10-
pub struct SimpleBlock {
11-
pub number: String,
12-
pub hash: String,
13-
pub timestamp: String,
14-
pub gas_used: String,
15-
pub transactions: Vec<SimpleTransaction>,
16-
}
17-
18-
#[derive(Debug, Deserialize)]
19-
pub struct SimpleTransaction {
20-
pub hash: String,
21-
pub from: String,
22-
pub to: Option<String>,
23-
pub value: String,
24-
pub gas: String,
25-
26-
#[serde(rename = "gasPrice")]
27-
pub gas_price: String,
28-
}
1+
use serde::Deserialize;
2+
3+
#[derive(Debug, Deserialize)]
4+
pub struct RPCResponse {
5+
pub result: SimpleBlock,
6+
}
7+
8+
#[derive(Debug, Deserialize)]
9+
#[serde(rename_all = "camelCase")]
10+
pub struct SimpleBlock {
11+
pub number: String,
12+
pub hash: String,
13+
pub timestamp: String,
14+
pub gas_used: String,
15+
pub transactions: Vec<SimpleTransaction>,
16+
}
17+
18+
#[derive(Debug, Deserialize)]
19+
pub struct SimpleTransaction {
20+
pub hash: String,
21+
pub from: String,
22+
pub to: Option<String>,
23+
pub value: String,
24+
pub gas: String,
25+
26+
#[serde(rename = "gasPrice")]
27+
pub gas_price: String,
28+
}

rustCode/src/rpc/fetch.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use crate::models::{RPCResponse, SimpleBlock};
12
use reqwest::Client;
23
use serde_json::json;
3-
use crate::models::{RPCResponse, SimpleBlock};
4-
54

65
pub async fn fetch_latest_block(rpc_url: &str) -> SimpleBlock {
76
let req_body = json!( {
@@ -19,33 +18,7 @@ pub async fn fetch_latest_block(rpc_url: &str) -> SimpleBlock {
1918
.await
2019
.expect("Greska pri slanju zahteva");
2120

22-
let resp_json: RPCResponse = resp
23-
.json()
24-
.await
25-
.expect("Greska pri parsiranju odgovora");
21+
let resp_json: RPCResponse = resp.json().await.expect("Greska pri parsiranju odgovora");
2622

2723
resp_json.result
28-
}
29-
30-
// #[no_mangle]
31-
// pub extern "C" fn fetch_transactions(rpc_url: *const c_char) -> *mut c_char {
32-
// // Pretvaranje C stringa u Rust string
33-
// let c_str = unsafe { CString::from_raw(rpc_url as *mut c_char) };
34-
// let rpc_url_str = c_str.to_str().unwrap();
35-
36-
// // Pozivanje fetch_latest_block funkcije za dobijanje najnovijeg bloka
37-
// let future = tokio::runtime::Runtime::new().unwrap().block_on(fetch_latest_block(rpc_url_str));
38-
39-
// // Prikupljanje transakcija iz rezultata
40-
// let mut transaction_data = String::new();
41-
// for tx in &future.transactions {
42-
// transaction_data.push_str(&format!("Tx hash: {}\nFrom: {}\nTo: {}\nValue: {}\nGas: {}\n\n",
43-
// tx.hash, tx.from, tx.to.clone().unwrap_or_else(|| "N/A".into()),
44-
// tx.value, tx.gas));
45-
// }
46-
47-
// let c_str_result = CString::new(transaction_data).unwrap();
48-
49-
// // Vraćanje C stringa koji Go može koristiti
50-
// c_str_result.into_raw()
51-
// }
24+
}

0 commit comments

Comments
 (0)