Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed fee calculation for the second transaction when spending spark funds to EX-address #1465

Merged
merged 1 commit into from
Jul 17, 2024

Conversation

psolstice
Copy link
Contributor

PR intention

Calculate the fee for the second tx correctly

Copy link

coderabbitai bot commented Jun 28, 2024

Walkthrough

The primary change involves the SendCoinsDialog::on_sendButton_clicked() function in the QT interface of the wallet. Specifically, the update modifies the parameter passed to the CWallet::GetMinimumFee() method from a hard-coded value of 0 to nTxConfirmTarget, likely to use a more dynamic fee calculation based on transaction confirmation targets.

Changes

File Path Change Summary
src/qt/sendcoinsdialog.cpp Modified the on_sendButton_clicked() function to change CWallet::GetMinimumFee() parameter from 0 to nTxConfirmTarget

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SendCoinsDialog
    participant CWallet

    User ->> SendCoinsDialog: Click send button
    SendCoinsDialog ->> CWallet: GetMinimumFee(nTxConfirmTarget)
    CWallet -->> SendCoinsDialog: returns calculated fee
    SendCoinsDialog ->> User: Displays confirmation dialog
Loading

Poem

🪙 In a line of code, a change so sleek,
The fee now answers to nTxConfirmTarget's seek.
No more a zero stands its ground,
With dynamic fees, transactions bound,
Sending coins, swift and neat! 🚀

From CodeRabbit, a little treat.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bcaacd4 and 76292a4.

Files selected for processing (1)
  • src/qt/sendcoinsdialog.cpp (1 hunks)
Additional comments not posted (2)
src/qt/sendcoinsdialog.cpp (2)

363-363: Ensure nTxConfirmTarget is correctly initialized before use.

The variable nTxConfirmTarget is used in the GetMinimumFee method call, but it's not clear from the provided code snippet where it is initialized and whether it is set appropriately based on the transaction context. This could potentially lead to incorrect fee calculations.

Verification successful

nTxConfirmTarget is correctly initialized before use.

The variable nTxConfirmTarget is declared as an extern variable in the header file src/wallet/wallet.h and is initialized with a default value in src/wallet/wallet.cpp. This ensures that it is properly set before being used in the GetMinimumFee method call.

  • Declaration: src/wallet/wallet.h
  • Initialization: src/wallet/wallet.cpp
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the initialization and usage of `nTxConfirmTarget` in the function.

# Test: Search for the initialization of `nTxConfirmTarget`. Expect: Initialization before usage.
rg --type cpp -C 3 'int nTxConfirmTarget =|nTxConfirmTarget =' src/qt/sendcoinsdialog.cpp

Length of output: 91



Script:

#!/bin/bash
# Description: Search for all occurrences of `nTxConfirmTarget` in the codebase to trace its initialization and usage.

# Test: Find all occurrences of `nTxConfirmTarget` in the repository.
rg 'nTxConfirmTarget' --type cpp --type hpp

Length of output: 71



Script:

#!/bin/bash
# Description: Search for all occurrences of `nTxConfirmTarget` in the codebase to trace its initialization and usage.

# Test: Find all occurrences of `nTxConfirmTarget` in the repository.
rg 'nTxConfirmTarget' --type cpp

Length of output: 2782


363-363: Validate handling of extraFee in transaction amount calculations.

The calculated extraFee is added directly to the transaction amount for the new recipient. This approach assumes that the extra fee is always required and correctly calculated, which might not be the case. It’s crucial to ensure that this does not lead to overcharging the user or underpaying the transaction fee.

@@ -360,7 +360,7 @@ void SendCoinsDialog::on_sendButton_clicked()
pwalletMain->SetAddressBook(newKey.GetID(), "", "receive");
intermediateAddressScript = GetScriptForDestination(newKey.GetID());

extraFee = CWallet::GetMinimumFee(secondTxSize, 0, mempool);
extraFee = CWallet::GetMinimumFee(secondTxSize, nTxConfirmTarget, mempool);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjust the fee calculation logic for better clarity and accuracy.

The addition of extraFee calculation using CWallet::GetMinimumFee is crucial for handling the fees correctly when transactions involve EX-addresses. However, the logic here is complex and could benefit from further explanation or simplification to ensure it's maintainable and clear to other developers.

// Refactor to encapsulate fee calculation in a separate function for clarity
CAmount calculateExtraFee(uint32_t txSize, int confirmTarget) {
    return CWallet::GetMinimumFee(txSize, confirmTarget, mempool);
}

// Usage
extraFee = calculateExtraFee(secondTxSize, nTxConfirmTarget);

@reubenyap reubenyap merged commit c0ddba2 into master Jul 17, 2024
6 checks passed
@reubenyap reubenyap deleted the spark-to-ex-fee-fix branch July 17, 2024 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants