-
Notifications
You must be signed in to change notification settings - Fork 135
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
Improve dust txs when sending max amount (#4979) #5472
base: dev
Are you sure you want to change the base?
Conversation
WalkthroughThe changes aim to prevent remaining dust when sending the maximum amount of Bitcoin. This involves refactoring the Changes
Sequence Diagram(s) (Beta)sequenceDiagram
participant User
participant BitcoinCustomFee
participant UseBitcoinCustomFee
participant DetermineUtxosForSpend
User->>BitcoinCustomFee: Select send max
BitcoinCustomFee->>UseBitcoinCustomFee: Calculate custom fee without amount
UseBitcoinCustomFee->>DetermineUtxosForSpend: Determine UTXOs
DetermineUtxosForSpend->>UseBitcoinCustomFee: Return UTXOs and change
UseBitcoinCustomFee->>BitcoinCustomFee: Return fee and UTXOs
BitcoinCustomFee->>User: Display fee and UTXOs
Assessment against linked issues
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
}, | ||
]; | ||
}[] = | ||
changeAmount <= BTC_P2WPKH_DUST_AMOUNT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok to leave the dust amount in the wallet? @fbwoolf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alter-eggo, thoughts on these changes? It looks like you've made the most recent changes to this code?
|
||
const determineUtxosArgs = { | ||
amount: satAmount, | ||
const determineUtxosArgs: DetermineUtxosForSpendArgs = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
satAmount is not used in determineUtxosForSpendArgs. Is the amount provided for other reasons? Is it ok to remove it? Is this the source of this issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it's ok to remove it since we provide amounts in recipient
model
@alter-eggo it might make sense for you to review these changes? I think this touches code you've worked on recently adding multiple recipients? |
@fbwoolf yeah will take a look, @kyranjamie and me refactored it not so long ago |
Thanks a lot @friedger, great improvement! Taking a look at implementation now, may tidy commits |
Rewrote logic a little to make more readable, but otherwise think this is good. @friedger to verify changed logic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Outside diff range and nitpick comments (6)
src/app/components/bitcoin-custom-fee/hooks/use-bitcoin-custom-fee.tsx (1)
Line range hint
23-47
: Refactor to improve readability and maintainability.The
useBitcoinCustomFee
function could be refactored for better readability by separating the logic into smaller, more manageable functions. This would make the code easier to understand and maintain, especially as it grows in complexity.export function useBitcoinCustomFee({ isSendingMax, recipients }: UseBitcoinCustomFeeArgs) { const { data: utxos = [] } = useCurrentNativeSegwitUtxos(); const btcMarketData = useCryptoCurrencyMarketDataMeanAverage('BTC'); const calculateFee = useCallback((feeRate: number) => { if (!feeRate || !utxos.length) return { fee: 0, fiatFeeValue: '' }; const determineUtxosArgs: DetermineUtxosForSpendArgs = { recipients, utxos, feeRate, }; const { fee } = isSendingMax ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs); return { fee, fiatFeeValue: `~ ${i18nFormatCurrency( baseCurrencyAmountInQuote(createMoney(Math.ceil(fee), 'BTC'), btcMarketData) )}`, }; }, [utxos, isSendingMax, recipients, btcMarketData]); return calculateFee; }src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx (1)
Line range hint
3-3
: Remove unused import to clean up the code.- import { createMoney, satToBtc } from '@leather-wallet/utils'; + import { satToBtc } from '@leather-wallet/utils';src/app/common/transactions/bitcoin/coinselect/local-coin-selection.ts (1)
Line range hint
37-40
: ReplaceforEach
withfor...of
for better performance and readability.- recipients.forEach(recipient => { + for (const recipient of recipients) { if (!validate(recipient.address)) throw new Error('Cannot calculate spend of invalid address type'); });Also applies to: 66-69
src/app/components/bitcoin-custom-fee/bitcoin-custom-fee.tsx (1)
Line range hint
1-1
: Optimize imports by removing unused ones.- import { Dispatch, SetStateAction, useCallback, useRef } from 'react'; + import { Dispatch, SetStateAction, useCallback, useRef } from 'react'; // Assuming these are used elsewhere in the file - import { createMoney } from '@leather-wallet/utils'; - import { SendCryptoAssetSelectors } from '@tests/selectors/send.selectors'; - import { Form, Formik } from 'formik'; - import { Stack, styled } from 'leather-styles/jsx'; - import * as yup from 'yup';Also applies to: 8-9, 15-16
src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx (1)
Line range hint
7-8
: Remove unused imports to clean up the code.- import { btcToSat, createMoney, formatMoney } from '@leather-wallet/utils'; + import { btcToSat, formatMoney } from '@leather-wallet/utils'; // Assuming createMoney is not used elsewhere in the filesrc/app/common/transactions/bitcoin/coinselect/local-coin-selection.spec.ts (1)
Line range hint
26-26
: Specify a more appropriate type instead ofany
.In several places in the test file,
any
is used where a more specific type could be applied. This would improve type safety and code clarity.- const demoUtxos = [{ value: 8200 }, { value: 8490 }, ...] as any; + const demoUtxos: UtxoResponseItem[] = [{ value: 8200 }, { value: 8490 }, ...];Also applies to: 36-36, 52-52, 79-79, 155-155, 183-183, 191-191, 224-224
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (6)
- src/app/common/transactions/bitcoin/coinselect/local-coin-selection.spec.ts (2 hunks)
- src/app/common/transactions/bitcoin/coinselect/local-coin-selection.ts (3 hunks)
- src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx (3 hunks)
- src/app/components/bitcoin-custom-fee/bitcoin-custom-fee.tsx (3 hunks)
- src/app/components/bitcoin-custom-fee/hooks/use-bitcoin-custom-fee.tsx (2 hunks)
- src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx (1 hunks)
Additional context used
GitHub Check: typecheck
src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx
[failure] 3-3:
'createMoney' is declared but its value is never read.src/app/components/bitcoin-custom-fee/bitcoin-custom-fee.tsx
[failure] 3-3:
'createMoney' is declared but its value is never read.
Biome
src/app/common/transactions/bitcoin/coinselect/local-coin-selection.ts
[error] 37-40: Prefer for...of instead of forEach.
[error] 66-69: Prefer for...of instead of forEach.
src/app/components/bitcoin-custom-fee/bitcoin-custom-fee.tsx
[error] 1-1: Some named imports are only used as types.
[error] 8-9: All these imports are only used as types.
[error] 15-16: All these imports are only used as types.
src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx
[error] 7-8: All these imports are only used as types.
src/app/common/transactions/bitcoin/coinselect/local-coin-selection.spec.ts
[error] 26-26: Unexpected any. Specify a different type.
[error] 36-36: Unexpected any. Specify a different type.
[error] 52-52: Unexpected any. Specify a different type.
[error] 79-79: Unexpected any. Specify a different type.
[error] 155-155: Unexpected any. Specify a different type.
[error] 183-183: Unexpected any. Specify a different type.
[error] 191-191: Unexpected any. Specify a different type.
[error] 224-224: Unexpected any. Specify a different type.
@alter-eggo want to review this PR before we merge in? @kyranjamie has already approved |
@markmhendrickson from the code side - lgtm |
@alter-eggo to fix conflicts and merge |
This PR
Summary by CodeRabbit
Refactor
Bug Fixes
amount
prop fromBitcoinCustomFee
components and hooks, streamlining the fee calculation process.Chores