Skip to content

Commit 7c3ff9a

Browse files
committed
feat: calculating gas via borrowing a reference
1 parent 73dca81 commit 7c3ff9a

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

rustCode/src/utils/functions.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ pub fn calculate_gas_percentage(tx_gas_used: u64, block_gas_used: u64) -> f64 {
1818
(tx_gas_used as f64 / block_gas_used as f64) * 100.0
1919
}
2020

21+
pub fn borrow_and_calculate_gas(receipt: &TransactionReceipt, block_gas_used_hex: &str) -> f64 {
22+
let tx_gas_used = hex_to_u64(&receipt.gas_used);
23+
let block_gas_used = hex_to_u64(block_gas_used_hex);
24+
calculate_gas_percentage(tx_gas_used, block_gas_used)
25+
}
26+
2127
pub fn consume_and_calculate_gas(receipt: TransactionReceipt, block_gas_used_hex: String) -> f64 {
2228
let tx_gas_used = hex_to_u64(&receipt.gas_used);
2329
let block_gas_used = hex_to_u64(&block_gas_used_hex);
@@ -38,24 +44,29 @@ pub async fn analyze_max_gas_transaction(
3844
println!();
3945

4046
let receipt = fetch_transaction_receipt(mainnet_rpc_url, &max_tx.hash).await;
41-
//println!("{:?}", receipt); // OVO MOZE
47+
4248
let tx_gas_used = hex_to_u64(&receipt.gas_used);
4349
let block_gas_used = hex_to_u64(block_gas_used_hex);
44-
let percent_of_block = calculate_gas_percentage(tx_gas_used, block_gas_used);
45-
4650
println!("Gas potrosen od ove transakcije: {}", tx_gas_used);
4751
println!("Gas potrosen u bloku: {}", block_gas_used);
52+
53+
let percent_of_block = calculate_gas_percentage(tx_gas_used, block_gas_used);
4854
println!("Procenat potrosnje u bloku: {:.6}%", percent_of_block);
55+
//println!("{:?}", receipt); // DOZVOLJENO
56+
57+
println!();
58+
println!("Ponovljeno izracunavanje preko borrow reference");
59+
let percent_of_block1 = borrow_and_calculate_gas(&receipt, block_gas_used_hex);
60+
println!("Procenat potrosnje u bloku: {:.6}%", percent_of_block1);
61+
//println!("{:?}", receipt); // DOZVOLJENO
4962
println!();
5063

5164
// Ownership primer
5265
println!("Ponovljeno izracunavanje preko dodele ownership-a");
5366
let percent_of_block1 = consume_and_calculate_gas(receipt, block_gas_used_hex.to_string());
5467
println!("Procenat potrosnje u bloku: {:.6}%", percent_of_block1);
55-
//println!("{:?}", receipt); //OVO NE MOZE jer je promenjen owner
56-
//println!("{}",block_mainnet.gas_used);
57-
//let tx_gas_used1 = hex_to_u64(&receipt.gas_used);
58-
//let block_gas_used1 = hex_to_u64(&block_mainnet.gas_used);
68+
//println!("{:?}", receipt); //NIJE DOZVOLJENO jer je promenjen owner
69+
//println!("{:?}", block_gas_used_hex); //DOZVOLJENO jer se napravila kopija na hip-u
5970

6071
} else {
6172
println!("Nema transakcija u ovom bloku");

0 commit comments

Comments
 (0)