Skip to content

Commit ce3026d

Browse files
committed
client: LoopInQuote to support v3 htlc for fee estimate
1 parent bf59159 commit ce3026d

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

client.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ func (s *Client) LoopInQuote(ctx context.Context,
682682
//
683683
// TODO(guggero): Thread through error code from lnd to avoid string
684684
// matching.
685-
minerFee, err := s.lndServices.Client.EstimateFeeToP2WSH(
685+
minerFee, err := s.estimateFee(
686686
ctx, request.Amount, request.HtlcConfTarget,
687687
)
688688
if err != nil && strings.Contains(err.Error(), "insufficient funds") {
@@ -703,6 +703,39 @@ func (s *Client) LoopInQuote(ctx context.Context,
703703
}, nil
704704
}
705705

706+
// estimateFee is a helper method to estimate the total fee for paying the
707+
// passed amount with the given conf target. It'll assume taproot destination
708+
// if the protocol version indicates that we're using taproot htlcs.
709+
func (s *Client) estimateFee(ctx context.Context, amt btcutil.Amount,
710+
confTarget int32) (btcutil.Amount, error) {
711+
712+
var (
713+
address btcutil.Address
714+
err error
715+
)
716+
// Generate a dummy address for fee estimation.
717+
witnessProg := [32]byte{}
718+
719+
scriptVersion := GetHtlcScriptVersion(
720+
loopdb.CurrentProtocolVersion(),
721+
)
722+
723+
if scriptVersion != swap.HtlcV3 {
724+
address, err = btcutil.NewAddressWitnessScriptHash(
725+
witnessProg[:], s.lndServices.ChainParams,
726+
)
727+
} else {
728+
address, err = btcutil.NewAddressTaproot(
729+
witnessProg[:], s.lndServices.ChainParams,
730+
)
731+
}
732+
if err != nil {
733+
return 0, err
734+
}
735+
736+
return s.lndServices.Client.EstimateFee(ctx, address, amt, confTarget)
737+
}
738+
706739
// LoopInTerms returns the terms on which the server executes swaps.
707740
func (s *Client) LoopInTerms(ctx context.Context) (
708741
*LoopInTerms, error) {

0 commit comments

Comments
 (0)