@@ -34,8 +34,10 @@ import (
34
34
"github.com/lightninglabs/loop/swap"
35
35
"github.com/lightninglabs/loop/swapserverrpc"
36
36
"github.com/lightninglabs/taproot-assets/rfqmath"
37
+ "github.com/lightningnetwork/lnd/input"
37
38
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
38
39
"github.com/lightningnetwork/lnd/lntypes"
40
+ "github.com/lightningnetwork/lnd/lnwallet"
39
41
"github.com/lightningnetwork/lnd/queue"
40
42
"github.com/lightningnetwork/lnd/routing/route"
41
43
"github.com/lightningnetwork/lnd/zpay32"
@@ -845,21 +847,30 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
845
847
log .Infof ("Loop in quote request received" )
846
848
847
849
var (
848
- numDeposits = uint32 (len (req .DepositOutpoints ))
849
- err error
850
+ selectedAmount = btcutil .Amount (req .Amt )
851
+ selectedDepositAmount btcutil.Amount
852
+ numDeposits = len (req .DepositOutpoints )
853
+ err error
850
854
)
851
855
856
+ if selectedAmount == 0 && numDeposits == 0 {
857
+ return nil , fmt .Errorf ("amount and deposit outpoints " +
858
+ "cannot both be zero" )
859
+ }
860
+
852
861
htlcConfTarget , err := validateLoopInRequest (
853
- req .ConfTarget , req .ExternalHtlc , numDeposits , req .Amt ,
862
+ req .ConfTarget , req .ExternalHtlc , uint32 ( numDeposits ) , req .Amt ,
854
863
)
855
864
if err != nil {
856
865
return nil , err
857
866
}
858
867
859
868
// Retrieve deposits to calculate their total value.
860
869
var depositList * looprpc.ListStaticAddressDepositsResponse
861
- amount := btcutil .Amount (req .Amt )
862
- if len (req .DepositOutpoints ) > 0 {
870
+
871
+ // If deposits are selected, we need to retrieve them to calculate the
872
+ // total value which we request a quote for. If a
873
+ if numDeposits > 0 {
863
874
depositList , err = s .ListStaticAddressDeposits (
864
875
ctx , & looprpc.ListStaticAddressDepositsRequest {
865
876
Outpoints : req .DepositOutpoints ,
@@ -874,20 +885,34 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
874
885
"deposit outpoints" )
875
886
}
876
887
877
- // The requested amount should be 0 here if the request
878
- // contained deposit outpoints.
879
- if amount != 0 && len (depositList .FilteredDeposits ) > 0 {
880
- return nil , fmt .Errorf ("amount should be 0 for " +
881
- "deposit quotes" )
888
+ if numDeposits != len (depositList .FilteredDeposits ) {
889
+ return nil , fmt .Errorf ("expected %d deposits, got %d" ,
890
+ numDeposits , len (depositList .FilteredDeposits ))
882
891
}
883
892
884
893
// In case we quote for deposits we send the server both the
885
- // total value and the number of deposits. This is so the server
886
- // can probe the total amount and calculate the per input fee.
887
- if amount == 0 && len (depositList .FilteredDeposits ) > 0 {
888
- for _ , deposit := range depositList .FilteredDeposits {
889
- amount += btcutil .Amount (deposit .Value )
890
- }
894
+ // selected value and the number of deposits. This is so the
895
+ // server can probe the selected value and calculate the per
896
+ // input fee.
897
+ for _ , deposit := range depositList .FilteredDeposits {
898
+ selectedDepositAmount += btcutil .Amount (
899
+ deposit .Value ,
900
+ )
901
+ }
902
+
903
+ // If the selected amount would leave a dust change output or
904
+ // exceeds the total deposits value, we return an error.
905
+ dustLimit := lnwallet .DustLimitForSize (input .P2TRSize )
906
+ if selectedDepositAmount - selectedAmount < dustLimit {
907
+ return nil , fmt .Errorf ("selected amount %v leaves " +
908
+ "dust or exceeds total deposit value %v" ,
909
+ selectedAmount , selectedDepositAmount )
910
+ }
911
+
912
+ // If the client didn't select an amount we quote for the total
913
+ // deposits value.
914
+ if selectedAmount == 0 {
915
+ selectedAmount = selectedDepositAmount
891
916
}
892
917
}
893
918
@@ -914,14 +939,14 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
914
939
}
915
940
916
941
quote , err := s .impl .LoopInQuote (ctx , & loop.LoopInQuoteRequest {
917
- Amount : amount ,
942
+ Amount : selectedAmount ,
918
943
HtlcConfTarget : htlcConfTarget ,
919
944
ExternalHtlc : req .ExternalHtlc ,
920
945
LastHop : lastHop ,
921
946
RouteHints : routeHints ,
922
947
Private : req .Private ,
923
948
Initiator : defaultLoopdInitiator ,
924
- NumDeposits : numDeposits ,
949
+ NumDeposits : uint32 ( numDeposits ) ,
925
950
})
926
951
if err != nil {
927
952
return nil , err
@@ -1764,6 +1789,7 @@ func (s *swapClientServer) StaticAddressLoopIn(ctx context.Context,
1764
1789
}
1765
1790
1766
1791
req := & loop.StaticAddressLoopInRequest {
1792
+ SelectedAmount : btcutil .Amount (in .Amount ),
1767
1793
DepositOutpoints : in .Outpoints ,
1768
1794
MaxSwapFee : btcutil .Amount (in .MaxSwapFeeSatoshis ),
1769
1795
Label : in .Label ,
0 commit comments