Skip to content

Commit 628d1dc

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 b229664 commit 628d1dc

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
@@ -7727,6 +7727,34 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
77277727
maxUnits = assetUnits.ToUint64()
77287728
}
77297729

7730+
// Since we used a different oracle price query above calculate the max
7731+
// amount of units, we want to add some breathing room to account for
7732+
// price fluctuations caused by the small time delay, plus the fact that
7733+
// the agreed upon quote may be different. If we don't add this safety
7734+
// window the peer may allow a routable amount that evaluates to less
7735+
// than what we ask for.
7736+
tolerance := rfqmath.NewBigIntFromUint64(
7737+
r.cfg.RfqManager.GetPriceDeviationPpm(),
7738+
)
7739+
7740+
// Parse the max asset units to the rfqmath type.
7741+
maxMathUnits := rfqmath.NewBigIntFromUint64(maxUnits)
7742+
7743+
// Calculate the tolerance margin.
7744+
toleranceUnits := maxMathUnits.Mul(tolerance).Div(
7745+
rfqmath.NewBigIntFromUint64(1_000_000),
7746+
)
7747+
7748+
// Apply the tolerance margin twice. Once due to the ask/bid price
7749+
// deviation that may occur during rfq negotiation, and once for the
7750+
// price movement that may occur between querying the oracle and
7751+
// acquiring the quote. We don't really care about this margin being too
7752+
// big, this only affects the max units our peer agrees to route.
7753+
maxMathUnits = maxMathUnits.Add(toleranceUnits).Add(toleranceUnits)
7754+
7755+
// Now parse the result back to uint64.
7756+
maxUnits = maxMathUnits.ToUint64()
7757+
77307758
resp, err := r.AddAssetBuyOrder(ctx, &rfqrpc.AddAssetBuyOrderRequest{
77317759
AssetSpecifier: &rfqrpc.AssetSpecifier{
77327760
Id: &rfqrpc.AssetSpecifier_AssetId{
@@ -7813,9 +7841,6 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
78137841

78147842
switch {
78157843
case satsMode:
7816-
amtMsat := lnwire.MilliSatoshi(req.InvoiceRequest.ValueMsat +
7817-
req.InvoiceRequest.Value*(1000))
7818-
78197844
iReq.ValueMsat = int64(amtMsat)
78207845

78217846
default:

0 commit comments

Comments
 (0)