Skip to content

Commit 89e1069

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 ba7d6eb commit 89e1069

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
@@ -7730,6 +7730,34 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
77307730
maxUnits = assetUnits.ToUint64()
77317731
}
77327732

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

78027830
switch {
78037831
case satsMode:
7804-
amtMsat := lnwire.MilliSatoshi(req.InvoiceRequest.ValueMsat +
7805-
req.InvoiceRequest.Value*(1000))
7806-
78077832
iReq.ValueMsat = int64(amtMsat)
78087833

78097834
default:

0 commit comments

Comments
 (0)