Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions pkg/client-lib/batch_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func (a *service) RedeemNotes(
return "", err
}

info, err := a.client.GetInfo(ctx)
if err != nil {
return "", err
}

feeEstimator, err := arkfee.New(info.Fees.IntentFees)
if err != nil {
return "", err
}

amount := uint64(0)

options := newDefaultSettleOptions()
Expand All @@ -88,7 +98,22 @@ func (a *service) RedeemNotes(
if err != nil {
return "", err
}
amount += uint64(v.Value)
fees, err := feeEstimator.EvalOffchainInput(arkfee.OffchainInput{
Amount: uint64(v.Value),
Type: arkfee.VtxoTypeNote,
})
if err != nil {
return "", err
}

feesSats := uint64(fees.ToSatoshis())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Initially, I was concerned about using uint64 here for the value, because the feeEstimator returns a float64 and it would just truncate the number down, which would mean that the fee would be less than what the operator needs and the redeem would not work. But I checked the ToSatoshis() method in pkg/ark-lib/arkfee/types.go and it already uses math.Ceil, which rounds the number up, so it's fine.

noteValue := uint64(v.Value)
if feesSats >= noteValue {
// not profitable to redeem this note
return "", fmt.Errorf("fees (%d) exceed note value (%d)", feesSats, noteValue)
}

amount += noteValue - feesSats
Comment thread
louisinger marked this conversation as resolved.
}

_, offchainAddrs, _, _, err := a.wallet.GetAddresses(ctx)
Expand All @@ -99,10 +124,22 @@ func (a *service) RedeemNotes(
return "", fmt.Errorf("no funds detected")
}

receiversOutput := []types.Receiver{{
receiver := types.Receiver{
To: offchainAddrs[0].Address,
Amount: amount,
}}
}
outputFees, err := feeEstimator.EvalOffchainOutput(receiver.ToArkFeeOutput())
if err != nil {
return "", err
}

outputFeesSats := uint64(outputFees.ToSatoshis())
if outputFeesSats > amount {
return "", fmt.Errorf("fees (%d) exceed amount (%d)", outputFeesSats, amount)
}

receiver.Amount = amount - outputFeesSats
receiversOutput := []types.Receiver{receiver}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return a.joinBatchWithRetry(ctx, notes, receiversOutput, *options, nil, nil)
}
Expand Down
Loading