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

StaticAddr: Withdraw arbitrary amounts #860

Merged
merged 4 commits into from
Feb 9, 2025

Conversation

hieblmi
Copy link
Collaborator

@hieblmi hieblmi commented Dec 2, 2024

This PR adds an amount field to the withdrawal rpc.

It allows to withdraw a fraction of the sum of selected deposits to withdraw. The change is sent back to the static address.
If the change is below the dust limit it goes towards miner fees, but warns the user beforehand.

loop static withdraw --utxo XXX ---utxo YYY --dest_addr ZZZ --amount 1500000

@hieblmi hieblmi self-assigned this Dec 2, 2024
@hieblmi hieblmi changed the base branch from master to static-addr-staging December 2, 2024 11:03
@hieblmi hieblmi marked this pull request as draft December 2, 2024 11:03
looprpc/client.proto Outdated Show resolved Hide resolved
@hieblmi hieblmi force-pushed the withdraw-amounts branch 5 times, most recently from 4f2ff5d to f7af7b4 Compare December 3, 2024 12:44
@hieblmi hieblmi marked this pull request as ready for review December 3, 2024 12:44
swapserverrpc/staticaddr.proto Show resolved Hide resolved
staticaddr/withdraw/manager.go Outdated Show resolved Hide resolved
@hieblmi hieblmi force-pushed the static-addr-staging branch from 5b68e42 to fb9562b Compare December 3, 2024 18:28
@hieblmi hieblmi requested a review from starius December 5, 2024 13:13
@hieblmi hieblmi force-pushed the static-addr-staging branch 3 times, most recently from 818562f to 9342b0f Compare December 12, 2024 17:31
Copy link
Collaborator

@starius starius left a comment

Choose a reason for hiding this comment

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

LGTM 🎉
I left few comments.
Also I notices that there are no tests in staticaddr/withdraw package. Does it make sense to add some to cover all withdrawing cases (full amount, with change, gifting dust change to miners)?

outpoints []wire.OutPoint, destAddr string, satPerVbyte int64) (string,
string, error) {
outpoints []wire.OutPoint, destAddr string, satPerVbyte int64,
amount int64) (string, string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I propose to add to godoc, that amount=0 means "withdraw everything".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point.

}

addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
ctx,
// Send change back to the static address.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I propose this change to make it more clear that address is the same.

s/to the static address/to the same static address/

// the withdrawal.
return nil, 0, 0, fmt.Errorf("the change doesn't " +
"cover for fees. Consider lowering the fee " +
"rate or increase the withdrawal amount")
Copy link
Collaborator

Choose a reason for hiding this comment

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

s/increase/decrease/ IIUC

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you are correct, so that the remaining amount can cover for fees.

withdrawalAmount = selectedWithdrawalAmount

default:
// If the fees eat into our withdrawal amount, we fail
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, the feeWithChange fees eat into our withdrawal amount, but this doesn't mean that feeWithoutChange do, since it is lower than feeWithChange. This means, that some amounts fail, even though it is possible to withdraw without change. And we fallback to withdrawing without change in general case when it is not enough funds for the change. So I think for consistency we should do the same here. Can we replace case change-feeWithChange >= 0: with case change-feeWithoutChange >= 0: above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a good catch 🙏 You are right in that the withdrawal shouldn't fail until the feeWithoutChange eats into our withdrawal amount.

Changed it.

@hieblmi hieblmi changed the base branch from static-addr-staging to master December 14, 2024 11:22
@bhandras
Copy link
Member

Needs a rebase.

@bhandras
Copy link
Member

Needs a rebase 🙏

@hieblmi hieblmi force-pushed the withdraw-amounts branch 2 times, most recently from f944edf to 87eb78f Compare January 27, 2025 09:48
@hieblmi hieblmi requested review from starius and removed request for starius January 27, 2025 09:56
@hieblmi hieblmi requested review from starius and removed request for starius January 27, 2025 09:57
@hieblmi
Copy link
Collaborator Author

hieblmi commented Jan 27, 2025

Rebased and addressed Boris' comments, thanks for the improvement @starius.

Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

swapserverrpc/staticaddr.proto Show resolved Hide resolved
})

if hasChange {
changeScript, err := txscript.PayToAddrScript(changeAddress)
Copy link
Member

Choose a reason for hiding this comment

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

I think we should just get the change address here (so no need to pass it around and it is not generated if unused).

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 moved all change address data into this if-branch.

@hieblmi hieblmi force-pushed the withdraw-amounts branch 2 times, most recently from f931282 to c5e4b52 Compare January 31, 2025 08:53
@lightninglabs-deploy
Copy link

@starius: review reminder

Copy link
Collaborator

@starius starius left a comment

Choose a reason for hiding this comment

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

LGTM!

I added one proposal. It is not blocking.


if changeAmount < 0 {
return nil, 0, 0, fmt.Errorf("change amount is negative")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional. Can we add a check that changeAmount is lower than each input UTXO? If it is larger, this means we could save on fees by not including that UTXO into the transaction. Can we show a warning to the user asking to remove some UTXOs or to confirm that they want to proceed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a neat tweak, I will add that.

@hieblmi hieblmi merged commit e41e7f5 into lightninglabs:master Feb 9, 2025
4 checks passed
@hieblmi hieblmi deleted the withdraw-amounts branch February 9, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants