From eedd093038e018201808d341e816cd51e3ca6c39 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 23 Jan 2026 20:07:05 +0000 Subject: [PATCH] explorer: Add tx fee support --- Cargo.lock | 1 + bin/explorer/Cargo.toml | 2 ++ bin/explorer/src/rpc.rs | 48 ++++++++++++++++++++++------------------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d08795465..9af8e9f30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2841,6 +2841,7 @@ dependencies = [ "darkfi", "darkfi-sdk", "darkfi-serial", + "darkfi_money_contract", "easy-parallel", "hex", "sled", diff --git a/bin/explorer/Cargo.toml b/bin/explorer/Cargo.toml index a02c5f337..52ea0ffad 100644 --- a/bin/explorer/Cargo.toml +++ b/bin/explorer/Cargo.toml @@ -8,6 +8,8 @@ darkfi = {path = "../../", features = ["blockchain", "rpc"]} darkfi-serial = {path = "../../src/serial", features = ["derive"]} darkfi-sdk = {path = "../../src/sdk"} +darkfi_money_contract = {path = "../../src/contract/money", features = ["no-entrypoint", "client"]} + async-channel = "2.5.0" async-trait = "0.1.89" easy-parallel = "3.3.1" diff --git a/bin/explorer/src/rpc.rs b/bin/explorer/src/rpc.rs index 56ef2af09..ef0a15b67 100644 --- a/bin/explorer/src/rpc.rs +++ b/bin/explorer/src/rpc.rs @@ -25,9 +25,11 @@ use darkfi::{ JsonError, JsonResponse, JsonResult, }, tx::Transaction, - util::encoding::base64, + util::{encoding::base64, parse::encode_base10}, }; -use darkfi_serial::serialize_async; +use darkfi_money_contract::MoneyFunction; +use darkfi_sdk::crypto::contract_id::MONEY_CONTRACT_ID; +use darkfi_serial::{deserialize_async, serialize_async}; use tinyjson::JsonValue; use crate::{DifficultyIndex, Explorer}; @@ -75,25 +77,26 @@ struct TransactionInfo { impl TransactionInfo { async fn new(tx: &Transaction) -> Self { + let mut fee = 0; let mut calls = Vec::with_capacity(tx.calls.len()); for call in &tx.calls { - let call_data = serialize_async(&call.data).await; - let func = if call_data.is_empty() { - "0x00".to_string() - } else { - format!("0x{:02x}", call_data[0]) - }; + let func = call.data.data[0]; + + if call.data.contract_id == *MONEY_CONTRACT_ID && func == MoneyFunction::FeeV1 as u8 { + fee = deserialize_async(&call.data.data[1..9]).await.unwrap(); + } + calls.push(ContractCallInfo::new( call.data.contract_id.to_string(), - func, - call_data.len() as u64, + format!("0x{:02x}", func), + call.data.data.len() as u64, )); } Self { hash: tx.hash().to_string(), calls, - fee: 0, + fee, size: serialize_async(tx).await.len() as u64, } } @@ -104,7 +107,7 @@ impl TransactionInfo { JsonValue::Object(HashMap::from([ ("hash".to_string(), JsonValue::String(self.hash.clone())), ("calls".to_string(), JsonValue::Array(calls)), - ("fee".to_string(), JsonValue::Number(self.fee as f64)), + ("fee".to_string(), JsonValue::String(encode_base10(self.fee, 8))), ("size".to_string(), JsonValue::Number(self.size as f64)), ])) } @@ -124,18 +127,19 @@ struct ExplTxInfo { impl ExplTxInfo { async fn new(tx: &Transaction, block_height: u64, current_height: u64) -> Self { + let mut fee = 0; let mut calls = Vec::with_capacity(tx.calls.len()); for call in &tx.calls { - let call_data = serialize_async(&call.data).await; - let func = if call_data.is_empty() { - "0x00".to_string() - } else { - format!("0x{:02x}", call_data[0]) - }; + let func = call.data.data[0]; + + if call.data.contract_id == *MONEY_CONTRACT_ID && func == MoneyFunction::FeeV1 as u8 { + fee = deserialize_async(&call.data.data[1..9]).await.unwrap(); + } + calls.push(ContractCallInfo::new( call.data.contract_id.to_string(), - func, - call_data.len() as u64, + format!("0x{:02x}", func), + call.data.data.len() as u64, )); } @@ -147,7 +151,7 @@ impl ExplTxInfo { hash: tx.hash().to_string(), from_block: block_height, confirmations, - fee: 0, + fee, size: raw_bytes.len() as u64, n_calls: tx.calls.len() as u64, calls, @@ -160,7 +164,7 @@ impl ExplTxInfo { ("hash".to_string(), JsonValue::String(self.hash.clone())), ("from_block".to_string(), JsonValue::Number(self.from_block as f64)), ("confirmations".to_string(), JsonValue::Number(self.confirmations as f64)), - ("fee".to_string(), JsonValue::Number(self.fee as f64)), + ("fee".to_string(), JsonValue::String(encode_base10(self.fee, 8))), ("size".to_string(), JsonValue::Number(self.size as f64)), ("n_calls".to_string(), JsonValue::Number(self.n_calls as f64)), (