Skip to content

Commit 034abd8

Browse files
committed
rpcserver: add breathing room to AddInvoice quote max amt
Use the rfq system's configured tolerance to apply it to the max units we'll use with the actual negotiated rfq quote with the peer. These units along with the peer negotiated price need to be sufficient to route the requested amount in sats/msats.
1 parent a697844 commit 034abd8

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

rpcserver.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7785,6 +7785,34 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
77857785
maxUnits = assetUnits.ToUint64()
77867786
}
77877787

7788+
// Since we used a different oracle price query above calculate the max
7789+
// amount of units, we want to add some breathing room to account for
7790+
// price fluctuations caused by the small time delay, plus the fact that
7791+
// the agreed upon quote may be different. If we don't add this safety
7792+
// window the peer may allow a routable amount that evaluates to less
7793+
// than what we ask for.
7794+
tolerance := rfqmath.NewBigIntFromUint64(
7795+
r.cfg.RfqManager.GetPriceDeviationPpm(),
7796+
)
7797+
7798+
// Parse the max asset units to the rfqmath type.
7799+
maxMathUnits := rfqmath.NewBigIntFromUint64(maxUnits)
7800+
7801+
// Calculate the tolerance margin.
7802+
toleranceUnits := maxMathUnits.Mul(tolerance).Div(
7803+
rfqmath.NewBigIntFromUint64(1_000_000),
7804+
)
7805+
7806+
// Apply the tolerance margin twice. Once due to the ask/bid price
7807+
// deviation that may occur during rfq negotiation, and once for the
7808+
// price movement that may occur between querying the oracle and
7809+
// acquiring the quote. We don't really care about this margin being too
7810+
// big, this only affects the max units our peer agrees to route.
7811+
maxMathUnits = maxMathUnits.Add(toleranceUnits).Add(toleranceUnits)
7812+
7813+
// Now parse the result back to uint64.
7814+
maxUnits = maxMathUnits.ToUint64()
7815+
77887816
rpcSpecifier := marshalAssetSpecifier(specifier)
77897817

77907818
resp, err := r.AddAssetBuyOrder(ctx, &rfqrpc.AddAssetBuyOrderRequest{
@@ -7869,9 +7897,6 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
78697897

78707898
switch {
78717899
case satsMode:
7872-
amtMsat := lnwire.MilliSatoshi(req.InvoiceRequest.ValueMsat +
7873-
req.InvoiceRequest.Value*(1000))
7874-
78757900
iReq.ValueMsat = int64(amtMsat)
78767901

78777902
default:

0 commit comments

Comments
 (0)