From d7047c8056c2c89576c17ca03d4c0c9537deea6d Mon Sep 17 00:00:00 2001 From: Daksh <41485688+Daksh14@users.noreply.github.com> Date: Mon, 23 Dec 2024 01:42:56 -0500 Subject: [PATCH] rusk-wallet: Fix transaction direction and sign of amount --- rusk-wallet/src/bin/command.rs | 4 +--- rusk-wallet/src/bin/command/history.rs | 29 +++++++++++++++++++------- rusk-wallet/src/gql.rs | 6 ------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/rusk-wallet/src/bin/command.rs b/rusk-wallet/src/bin/command.rs index ab0d48672..f617c5325 100644 --- a/rusk-wallet/src/bin/command.rs +++ b/rusk-wallet/src/bin/command.rs @@ -27,12 +27,10 @@ use rusk_wallet::{ }; use wallet_core::BalanceInfo; -use crate::io::prompt::{self, create_password, request_transaction_model}; +use crate::io::prompt::{self, create_password, TransactionModel}; use crate::settings::Settings; use crate::{WalletFile, WalletPath}; -use self::prompt::TransactionModel; - /// Commands that can be run against the Dusk wallet #[allow(clippy::large_enum_variant)] #[derive(PartialEq, Eq, Hash, Clone, Subcommand, Debug)] diff --git a/rusk-wallet/src/bin/command/history.rs b/rusk-wallet/src/bin/command/history.rs index 50251f94b..1a2fa1e5b 100644 --- a/rusk-wallet/src/bin/command/history.rs +++ b/rusk-wallet/src/bin/command/history.rs @@ -8,7 +8,6 @@ use std::collections::hash_map::Entry; use std::collections::HashMap; use std::fmt::{self, Display}; -use dusk_core::signatures::bls::PublicKey; use dusk_core::transfer::Transaction; use dusk_core::{dusk, from_dusk}; use rusk_wallet::{BlockTransaction, DecodedNote, GraphQL}; @@ -45,7 +44,7 @@ impl Display for TransactionHistory { let fee = match self.direction { TransactionDirection::In => "".into(), TransactionDirection::Out => { - let fee = self.fee; + let fee: u64 = self.fee; let fee = from_dusk(fee); format!("{: >12.9}", fee) } @@ -160,7 +159,10 @@ pub(crate) async fn moonlight_history( let gql = GraphQL::new(settings.state.to_string(), io::status::interactive)?; - let history = gql.moonlight_history(address).await?.full_moonlight_history; + let history = gql + .moonlight_history(address.clone()) + .await? + .full_moonlight_history; let mut collected_history = Vec::new(); @@ -172,14 +174,25 @@ pub(crate) async fn moonlight_history( for event in events { let data = event.data; - let fee = data.gas_spent; - let amount = data.value; + let gas_spent = data.gas_spent; + let mut amount = data.value; + let sender = data.sender; + + let direction: TransactionDirection = + match sender == address.to_string() { + true => { + amount = -amount; + + TransactionDirection::Out + } + false => TransactionDirection::In, + }; collected_history.push(TransactionHistory { - direction: TransactionDirection::In, + direction, height, amount, - fee, + fee: gas_spent * tx.gas_price(), tx: tx.clone(), id: id.clone(), }) @@ -189,7 +202,7 @@ pub(crate) async fn moonlight_history( Ok(collected_history) } -#[derive(PartialEq)] +#[derive(PartialEq, Debug)] enum TransactionDirection { In, Out, diff --git a/rusk-wallet/src/gql.rs b/rusk-wallet/src/gql.rs index f18ad01f1..d942a17e8 100644 --- a/rusk-wallet/src/gql.rs +++ b/rusk-wallet/src/gql.rs @@ -59,7 +59,6 @@ struct BlockResponse { #[derive(Deserialize, Debug)] pub struct BlockData { pub gas_spent: u64, - pub receiver: String, pub sender: String, pub value: f64, } @@ -92,11 +91,6 @@ struct SpentTxResponse { pub tx: Option, } -#[derive(Deserialize)] -struct RawTx { - tx: SpentTxResponse, -} - /// Transaction status #[derive(Debug)] pub enum TxStatus {