cardano-foundation / cardano-wallet

HTTP server & command-line for managing UTxOs and HD wallets in Cardano.
Apache License 2.0
763 stars 211 forks source link

Spend specific UTXO(s) #3581

Open gernotpokorny opened 1 year ago

gernotpokorny commented 1 year ago

Related Jira Issue

https://cardanofoundation.atlassian.net/browse/ADP-3269

The problem that you wish to solve

Currently it is not possible to spend specific UTXOs.

Description

With Cardano we have an UTXO based ledger and a rewards address.

I'd like to spend specific UTXOs in particular the ones of the staking rewards and it seems that this is not possible with Daedalus.

With Electrum (a Bitcoin wallet) for example it is quite easy to optionally select the UTXOs to spend. There are various reasons why somebody would want to do that and why Electrum has implemented that feature.

Daedalus internally of course will also have some algorithm which selects the UTXOs. It shouldn't be not that difficult to instead let the user select the UTXOs.

Could this please be implemented in the future?

Thank you.

Implementation suggestions

None.

HeinrichApfelmus commented 1 year ago

Thanks for the suggestion!

However, I'd like to ask for clarification: When making a transaction, the wallet typically selects additional UTXOs to cover the desired outputs and also creates additionally change outputs that move excess funds back to the wallet.

When you say "spend specific UTXOs" — do you mean "spend exactly these specific UTXOs", or "a subset of these" or "a superset of these"?

The superset case has the most alignment with current wallet philosophy and can be handled by the "balance transaction" endpoint of cardano-wallet (though I'm not aware of a graphical UX for this specific use case).

gernotpokorny commented 1 year ago

I mean spend exactly those UTXOs that you selected like it works in Electrum. Not a subset and not a superset, exactly those. And you can only spend what's at max available at the selected UTXOs minus fees of course.

Lets say you have three UTXOs

A. 1000 ADA B. 1500 ADA C. 2000 ADA

And you want to send lets say 1750 to somebody then you should have the choice which outputs you want to spend. You could select A + B or only C for example.

The change would go to a new address as it always does with UTXO based chains.

Simply exactly how it works in Electrum.

If you select A + B but only decide to spend 500 ADA, then A + B would be a superset, but that's just a sidenote and an edgecase. I would just handle this edgecase as Electrum does, because it is not important.

Or for example if I only want to spend my staking rewards and nothing else then I should be able to select the UTXO(s) from the staking rewards and just send an amount smaller or equal (minus fees) of that UTXO.

gernotpokorny commented 1 year ago

Anbody :? :(

jonathanknowles commented 1 year ago

Hi @gernotpokorny

Apologies for the slow response!

In principle this should be achievable by using the balanceTransaction endpoint in conjunction with the minimal coin selection strategy:

When using the minimal strategy, the wallet’s coin selection algorithm will only select just enough of each asset from the available UTxO set to meet the minimum amount. The selection process will terminate as soon as the minimum amount of each asset is covered.

So if you supply a partial transaction with a set of inputs that already cover the cost of your transaction, and if balanceTransaction uses the minimal coin selection strategy, then no additional UTxOs will be selected.

In practice however, the choice of strategy is currently not configurable. At the moment, balanceTransaction will always first attempt to use the optimal coin selection strategy:

When using the optimal strategy, the wallet’s coin selection algorithm will attempt to select around twice the minimum possible amount of each asset from the available UTxO set, making it possible to generate change outputs that are roughly the same sizes and shapes as user-specified outputs.

Currently, balanceTransaction will only fall back to the minimal strategy if the optimal strategy would generate a transaction that is larger than the size limit, or too costly for the wallet to pay for.

We could make this configurable, so that callers can specify which strategy they want when calling balanceTransaction. Would that satisfy your use case? If so, then we can consider doing it!

cc-ing @teddyjfpender

gernotpokorny commented 1 year ago

I'm not 100% sure, but I think this is not the same as what I asked for :/. I really want to select the UTXOs myself and not some algorithm.

jonathanknowles commented 1 year ago

I'm not 100% sure, but I think this is not the same as what I asked for :/. I really want to select the UTXOs myself and not some algorithm.

Hi @gernotpokorny

I'll try to explain a bit more!

Under the minimal strategy, balanceTransaction will only select further UTxOs if the original UTxOs do not provide enough value to pay for the transaction. So as long as your original selection is sufficient to pay for the entire transaction, balanceTransaction will not add any further UTxOs to your starting selection.

In other words, you would be able to select the UTxOs yourself, and guarantee that only those UTxOs end up in the final transaction.

As for the algorithm: balanceTransaction uses the same underlying algorithm regardless of the choice of strategy. The choice of strategy only affects the termination condition. The difference between the two strategies is that:

gernotpokorny commented 1 year ago

Hello @jonathanknowles,

And thanks for the clarification.

In other words, you would be able to select the UTxOs yourself, and guarantee that only those UTxOs end up in the final transaction.

Yes, I think a UI to select the UTXOs plus the minimal strategy would satisfy the use case.

In other words, you would be able to select the UTxOs yourself

In Daedalus right? That would be nice.

...making it possible to generate change outputs that are roughly the same sizes and shapes as user-specified outputs.

What is actually the point of this? Why would you want to do that?

Thank you very much.

gernotpokorny commented 1 year ago

@jonathanknowles May I ask, is sombody of IOHK actually working on this issue? Thanks.

jonathanknowles commented 1 year ago

@jonathanknowles May I ask, is sombody of IOHK actually working on this issue? Thanks.

Hi @gernotpokorny

Thanks for writing again, and sorry for the delay in getting back to you.

I've discussed this with our team. We agree that exposing the minimal selection strategy (as a configuration option) is something that would bring value in several use cases, so it's something that we almost certainly want to provide in a future release. As mentioned before, this configuration option would allow you to spend specific UTxOs, and only those UTxOs, provided that the UTxOs specified already cover the output value of the transaction. The functionality would be exposed through the cardano-wallet API, via the balanceTransaction endpoint.

However, our current workload is currently full with high priority items, so we can't implement this option immediately. Nevertheless, we are considering this for inclusion in future releases this year.

Best wishes

Jonathan

gernotpokorny commented 1 year ago

@jonathanknowles Sounds good. Thank you very much.

Do you also consider providing some UI for this feature within Daedalus? There could be a toggle within the Settings of Daedalus to show/hide this feature.

jonathanknowles commented 1 year ago

Do you also consider providing some UI for this feature within Daedalus? There could be a toggle within the Settings of Daedalus to show/hide this feature.

This sounds like a nice idea to me! ➕

However, I'd recommend opening an issue on the Daedalus repository itself, describing the kind of UI that you'd like to use, so that the Daedalus maintainers are aware of it: https://github.com/input-output-hk/daedalus/issues. (Over here in the cardano-wallet back end team, we don't have control over the scope of what goes into the Daedalus front end, although we can certainly express our support for new features, and I would certainly advocate supporting it in the back-end.)

At the end of the day, if we expose this strategy in cardano-wallet, then Daedalus would just need to pass the "minimal selection strategy" option when calling cardano-wallet.

gernotpokorny commented 1 year ago

I'll open the issue in Daedalus once this feature is implemented in cardano-wallet.

Thank you very much.

gernotpokorny commented 9 months ago

Is there any chance that this gets solved within 2024? I think you underestimate the issue. ADA is unfortunately not legal tender in as far as I know all countries of the world so it actually matters which UTXOs you spend.

Just do give you an example. Lets say you send some ADA to an exchange and sell them and the government demands some taxes for that (no comment on that). You said hey I spend some of my staking rewards. They say show me the transaction to the exchange. Ohh that transaction does actually not spend your staking rewards, but something else, which results in different taxes. This is the case for many if not all countries in the world at the moment which tax cryptocurrency gains. And even if it's a grayzone if UTXOs are treated differently then accounts in some countries, the burden will always be at the ADA user. Being able to spend specific UTXOs is extremely important, that's by Bitcoin wallets like Electrum have this feature. Cardano is UTXO based and needs this feature. As an ADA user you want to have control over what you are actually spending. In an ideal world where cryptocurrencies are treated as currencies, this is not necessary, but this is unfortunately not todays reality.

HeinrichApfelmus commented 9 months ago

Ohh that transaction does actually not spend your staking rewards, but something else, which results in different taxes.

Ah, thank you for providing the larger context of this feature, I was not aware of the tax implications at all. 🤔

Indeed, in some countries, the time between receiving a UTxO ("coin") and spending it may affect the immediate tax on it (short-term capital gains vs long-term capital gains).

Ohh that transaction does actually not spend your staking rewards

Note that staking rewards are not contained in any UTxO — instead, they are contained in a so-called reward account, and you need to spend at least one UTxO that is completely unrelated to the rewards in order to access them. This complicates issue and may imply that this feature, in the specific form requested, may not even solve your problem. 🤔

Could you expand more on the larger problem that you are trying to solve? Is it

As a wallet user I want to spend specific UTxO or specifically from the reward account So that my transaction becomes tax-efficient

? Could you expand a little on the taxation rules that you want to optimize?

Tax reporting is probably outside the scope of cardano-wallet, but I can see that in order to report low taxes, you need a means to influence the transactions being made.

We will discuss this in the team, though without support from the UI, implementing it in cardano-wallet will probably not be much help.

Lets say you have three UTXOs

A. 1000 ADA B. 1500 ADA C. 2000 ADA

And you want to send lets say 1750 to somebody then you should have the choice which outputs you want to spend. You could select A + B or only C for example.

I would consider this "spending a subset of the given UTxO".

In contrast, spending exactly those UTxO would mean to select A + B + C to cover the 1750 ADA and return a change of 2750 ADA.

jonathanknowles commented 8 months ago

I've created a related Jira ticket here: https://cardanofoundation.atlassian.net/browse/ADP-3269