Skip to content

Commit 9d49804

Browse files
committed
assetclient: fix wrong fee limit
1 parent 684db85 commit 9d49804

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

assets/client.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lightninglabs/taproot-assets/taprpc/tapchannelrpc"
1717
"github.com/lightninglabs/taproot-assets/taprpc/universerpc"
1818
"github.com/lightningnetwork/lnd/lnrpc"
19+
"github.com/lightningnetwork/lnd/lnwire"
1920
"github.com/lightningnetwork/lnd/macaroons"
2021
"google.golang.org/grpc"
2122
"google.golang.org/grpc/credentials"
@@ -105,9 +106,12 @@ func (c *TapdClient) GetRfqForAsset(ctx context.Context,
105106
expiry int64, feeLimitMultiplier float64) (
106107
*rfqrpc.PeerAcceptedSellQuote, error) {
107108

108-
feeLimit, err := lnrpc.UnmarshallAmt(
109-
int64(satAmount)+int64(satAmount.MulF64(feeLimitMultiplier)), 0,
110-
)
109+
// paymentMaxAmt is the maximum amount we are willing to pay for the
110+
// payment.
111+
// E.g. on a 250k sats payment we'll multiply the sat amount by 1.2.
112+
// The resulting maximum amount we're willing to pay is 300k sats.
113+
// The response asset amount will be for those 300k sats.
114+
paymentMaxAmt, err := getPaymentMaxAmount(satAmount, feeLimitMultiplier)
111115
if err != nil {
112116
return nil, err
113117
}
@@ -120,7 +124,7 @@ func (c *TapdClient) GetRfqForAsset(ctx context.Context,
120124
},
121125
},
122126
PeerPubKey: peerPubkey,
123-
PaymentMaxAmt: uint64(feeLimit),
127+
PaymentMaxAmt: uint64(paymentMaxAmt),
124128
Expiry: uint64(expiry),
125129
TimeoutSeconds: uint32(c.cfg.RFQtimeout.Seconds()),
126130
})
@@ -180,6 +184,28 @@ func (c *TapdClient) GetAssetName(ctx context.Context,
180184
return assetName, nil
181185
}
182186

187+
// getPaymentMaxAmount returns the milisat amount we are willing to pay for the
188+
// payment.
189+
func getPaymentMaxAmount(satAmount btcutil.Amount, feeLimitMultiplier float64) (
190+
lnwire.MilliSatoshi, error) {
191+
192+
if satAmount == 0 {
193+
return 0, fmt.Errorf("satAmount cannot be zero")
194+
}
195+
if feeLimitMultiplier < 1 {
196+
return 0, fmt.Errorf("feeLimitMultiplier must be at least 1")
197+
}
198+
199+
// paymentMaxAmt is the maximum amount we are willing to pay for the
200+
// payment.
201+
// E.g. on a 250k sats payment we'll multiply the sat amount by 1.2.
202+
// The resulting maximum amount we're willing to pay is 300k sats.
203+
// The response asset amount will be for those 300k sats.
204+
return lnrpc.UnmarshallAmt(
205+
int64(satAmount.MulF64(feeLimitMultiplier)), 0,
206+
)
207+
}
208+
183209
func getClientConn(config *TapdConfig) (*grpc.ClientConn, error) {
184210
// Load the specified TLS certificate and build transport credentials.
185211
creds, err := credentials.NewClientTLSFromFile(config.TLSPath, "")

assets/client_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package assets
2+
3+
import (
4+
"testing"
5+
6+
"github.com/btcsuite/btcd/btcutil"
7+
"github.com/lightningnetwork/lnd/lnwire"
8+
)
9+
10+
func TestGetPaymentMaxAmount(t *testing.T) {
11+
tests := []struct {
12+
satAmount btcutil.Amount
13+
feeLimitMultiplier float64
14+
expectedAmount lnwire.MilliSatoshi
15+
expectError bool
16+
}{
17+
{
18+
satAmount: btcutil.Amount(250000),
19+
feeLimitMultiplier: 1.2,
20+
expectedAmount: lnwire.MilliSatoshi(300000000),
21+
expectError: false,
22+
},
23+
{
24+
satAmount: btcutil.Amount(100000),
25+
feeLimitMultiplier: 1.5,
26+
expectedAmount: lnwire.MilliSatoshi(150000000),
27+
expectError: false,
28+
},
29+
{
30+
satAmount: btcutil.Amount(50000),
31+
feeLimitMultiplier: 2.0,
32+
expectedAmount: lnwire.MilliSatoshi(100000000),
33+
expectError: false,
34+
},
35+
{
36+
satAmount: btcutil.Amount(0),
37+
feeLimitMultiplier: 1.2,
38+
expectedAmount: lnwire.MilliSatoshi(0),
39+
expectError: true,
40+
},
41+
{
42+
satAmount: btcutil.Amount(250000),
43+
feeLimitMultiplier: 0.8,
44+
expectedAmount: lnwire.MilliSatoshi(0),
45+
expectError: true,
46+
},
47+
}
48+
49+
for _, test := range tests {
50+
result, err := getPaymentMaxAmount(
51+
test.satAmount, test.feeLimitMultiplier,
52+
)
53+
if test.expectError {
54+
if err == nil {
55+
t.Fatalf("expected error but got none")
56+
}
57+
} else {
58+
if err != nil {
59+
t.Fatalf("unexpected error: %v", err)
60+
}
61+
if result != test.expectedAmount {
62+
t.Fatalf("expected %v, got %v",
63+
test.expectedAmount, result)
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)