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

Add calculate_fee and calculate_fee_rate on wallet #437

Merged
merged 1 commit into from
Jan 10, 2024

Conversation

reez
Copy link
Collaborator

@reez reez commented Dec 17, 2023

Description

Add calculate_fee on wallet.
Add calculate_fee and calculate_fee_rate on wallet.

Continuing to implement functions/types associated with PR on bdk "Remove TransactionDetails from Wallet API".

Notes to the reviewers

Open to hearing where to best place the new code in each file too (should BdkCalculateFeeError be placed somewhere different in the UDL, etc).

Currently I have BdkCalculateFeeError with associated values, but I am ignoring associated values in the UDL, and will address that in a follow up PR with all of the other changes we need to possibly do to Error types and associating data with them.
I have CalculateFeeError with associated values and expose them in the UDL as well.

Changelog notice

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@reez reez changed the title feat: add calculate_fee on wallet Add calculate_fee on wallet Dec 17, 2023
@reez reez force-pushed the calculate-fee branch 3 times, most recently from 8d01bd4 to c47aed1 Compare December 17, 2023 01:43
@reez reez marked this pull request as ready for review December 18, 2023 16:40
Copy link
Member

@thunderbiscuit thunderbiscuit left a comment

Choose a reason for hiding this comment

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

Nice little addition! Only small issue I think is that the name is reversed; you're exposing a new error type called BdkCalculateFeeError, but we usually expose the a type with the same name as in Rust and internally transform the Rust type into a BdkSomethingSomething type. Just a switcharoo of those will make this PR ready to go.

@reez reez changed the title Add calculate_fee on wallet Add calculate_fee and calculate_fee_rate on wallet Dec 18, 2023
@reez reez force-pushed the calculate-fee branch 4 times, most recently from b6d2270 to 2459eb4 Compare December 19, 2023 17:02
@reez
Copy link
Collaborator Author

reez commented Dec 19, 2023

Updates:


  • did switcharoo of CalculateFeeError and BdkCalculateFeeError per Thunder suggestion
  • exposed CalculateFeeError with associated values in the UDL per uniffi
  • added calculate_fee_rate
  • added a simplified FeeRate constructor and removed the from_sat_per_vb constructor

  • updated tests across all 4 platforms 





This is ready for review again, but any suggestions welcome.

Copy link
Member

@thunderbiscuit thunderbiscuit left a comment

Choose a reason for hiding this comment

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

Awesome work! I have a few comments but overall this is shaping up.

bdk-ffi/src/types.rs Outdated Show resolved Hide resolved
bdk-ffi/src/types.rs Show resolved Hide resolved

pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, CalculateFeeError> {
self.get_wallet()
.calculate_fee(&tx.clone().into())
Copy link
Member

Choose a reason for hiding this comment

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

The clone is required here because we've only implemented the From trait on the owned Transaction type and you're passing in a reference. But if you define it for the referenced version (should be the exact same code really) then you won't need the clones here on both the new methods.

Copy link
Member

Choose a reason for hiding this comment

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

Mmmm after playing with it, I realize it actually requires the clone anyway haha. But I do think it's maybe more elegant and leaves the implementation out of the calculate_fee method.

impl From<&Transaction> for BdkTransaction {
    fn from(tx: &Transaction) -> Self { tx.inner.clone() }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds like a plan to me. I added your suggested code snippet:

impl From<&Transaction> for BdkTransaction {
    fn from(tx: &Transaction) -> Self { tx.inner.clone() }
}

... then updated 4 functions that had a line related to transaction that could have .clone() removed.

wallet.rs

  • sent_and_received
  • calculate_fee
  • calculate_fee_rate

esplora.rs

  • broadcast

... there was still 1 transaction related .clone() and it was wallet.rs transactions so I added the reference version (we already have the owned version):

impl From<&BdkTransaction> for Transaction {
    fn from(tx: &BdkTransaction) -> Self {
        Transaction { inner: tx.clone() }
    }
}

... and then updated the transactions function code to remove .clone().

I tested bdk-ffi bdk-swift bdk-jvm locally with these changes and tests still passed.

Hopefully that all makes sense to have updated and added along with your suggestion!

bdk-ffi/src/wallet.rs Outdated Show resolved Hide resolved
bdk-ffi/src/wallet.rs Show resolved Hide resolved
bdk-python/tests/test_live_wallet.py Outdated Show resolved Hide resolved
Comment on lines +87 to +88
MissingTxOut(sequence<OutPoint> out_points);
NegativeFee(i64 fee);
Copy link
Member

Choose a reason for hiding this comment

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

This is the first time we use this pattern of defining errors as interfaces in the UDL. Can you confirm that the errors print out what we expect them to? Just making sure it all works well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just added two tests in the error.rs file to address this. Let me know if:

  • those two tests make sense to you
  • that is the best place to test these two errors

... or any other thoughts you had on it too. Thanks, great suggestion!

@reez
Copy link
Collaborator Author

reez commented Dec 28, 2023

Awesome work! I have a few comments but overall this is shaping up.

Great suggestions/thoughts/catches. Appreciated!

I updated all of the small items (and just marked the conversation resolved since they had been updated and were now “Outdated”) hopefully that’s cool, but I left the 2 items that I updated code for but you might still want to converse about too:


  • trait on Transaction. I added comments to this as well as updated code, but just leaving it open for any other feedback or thoughts you might have
  • error testing. I added comments and code/tests for this but just leaving it open too for any other feedback or thoughts.

@reez
Copy link
Collaborator Author

reez commented Jan 8, 2024

Rebased

Copy link
Member

@thunderbiscuit thunderbiscuit left a comment

Choose a reason for hiding this comment

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

ACK 3789c1d. Thanks!

@thunderbiscuit thunderbiscuit merged commit 3789c1d into bitcoindevkit:master Jan 10, 2024
28 checks passed
@reez reez deleted the calculate-fee branch January 10, 2024 20:35
@reez reez mentioned this pull request Jan 19, 2024
8 tasks
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.

None yet

2 participants