[GEN-1815] Refactor coin selection - #563
Open
imclvr wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors coin/UTXO selection by extracting UTXO selection logic into a dedicated helper and moving UTXO→ICoin mapping onto Wallet, providing a more central and reusable place for coin selection behavior across NodeGuard’s on-chain workflows.
Changes:
- Introduces
UTXOSelectionAlgorithmsto host domain-agnostic UTXO selection logic (e.g., “oldest first”). - Removes coin/UTXO selection helpers from
LightningHelperand updatesCoinSelectionServiceto use the new helper +Walletmapping. - Adds
Wallet.ToCoins(...)to convert selected UTXOs into the correct spendable coin type (plain vs script coins).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Services/CoinSelectionService.cs | Switches input-coin selection to the new helper + wallet mapping method. |
| src/Helpers/UTXOSelectionAlgorithms.cs | New centralized UTXO selection algorithms implementation. |
| src/Helpers/LightningHelper.cs | Removes UTXO/coin selection helpers now moved elsewhere. |
| src/Data/Models/Wallet.cs | Adds ToCoins to map selected UTXOs into spendable ICoin instances. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+41
to
+55
| public static List<UTXO> SelectUTXOsByOldest( | ||
| Wallet wallet, long satsAmount, List<UTXO> availableUTXOs, ILogger logger) | ||
| { | ||
| if (wallet == null) throw new ArgumentNullException(nameof(wallet)); | ||
| if (logger == null) throw new ArgumentNullException(nameof(logger)); | ||
| if (satsAmount <= 0) throw new ArgumentOutOfRangeException(nameof(satsAmount)); | ||
|
|
||
| var selectedUTXOs = new List<UTXO>(); | ||
|
|
||
| if (!availableUTXOs.Any()) | ||
| { | ||
| logger.LogError("The PSBT cannot be generated, no UTXOs are available for walletId: {WalletId}", | ||
| wallet.Id); | ||
| return selectedUTXOs; | ||
| } |
Comment on lines
+71
to
+88
| var utxosSatsAmountAccumulator = 0M; | ||
|
|
||
| var iterations = 0; | ||
| while (satsAmount >= utxosSatsAmountAccumulator) | ||
| { | ||
| if (utxosStack.TryPop(out var utxo)) | ||
| { | ||
| selectedUTXOs.Add(utxo); | ||
| utxosSatsAmountAccumulator += ((Money)utxo.Value).Satoshi; | ||
| } | ||
|
|
||
| iterations++; | ||
|
|
||
| if (iterations == 1_000) | ||
| { | ||
| break; | ||
| } | ||
| } |
Comment on lines
+221
to
+238
| public List<ICoin> ToCoins(List<UTXO> selectedUTXOs) | ||
| { | ||
| var derivationStrategy = GetDerivationStrategy(); | ||
|
|
||
| //UTXOS to Enumerable of ICOINS | ||
| return [ | ||
| .. selectedUTXOs.Select<UTXO, ICoin>(x => | ||
| { | ||
| var coin = x.AsCoin(derivationStrategy); | ||
| if (IsHotWallet) | ||
| { | ||
| return coin; | ||
| } | ||
|
|
||
| return coin.ToScriptCoin(x.ScriptPubKey); | ||
| }) | ||
| ]; | ||
| } |
Comment on lines
+270
to
+280
| public Task<(List<ICoin> coins, List<UTXO> selectedUTXOs)> GetTxInputCoins( | ||
| List<UTXO> availableUTXOs, | ||
| IBitcoinRequest request, | ||
| DerivationStrategyBase derivationStrategy) | ||
| { | ||
| var satsAmount = request.SatsAmount; | ||
|
|
||
| var selectedUTXOs = await LightningHelper.SelectUTXOsByOldest(request.Wallet, satsAmount, availableUTXOs, _logger); | ||
| var coins = await LightningHelper.SelectCoins(request.Wallet, selectedUTXOs); | ||
| var selectedUTXOs = UTXOSelectionAlgorithms.SelectUTXOsByOldest(request.Wallet, satsAmount, availableUTXOs, _logger); | ||
| var coins = request.Wallet.ToCoins(selectedUTXOs); | ||
|
|
||
| return (coins, selectedUTXOs); | ||
| return Task.FromResult((coins, selectedUTXOs)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This refactoring allows a common and meaningful place to put utxo selection from NG.
Stack created with GitHub Stacks CLI • Give Feedback 💬