@@ -7707,54 +7707,40 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
77077707 // will help us establish a quote with the correct amount of asset
77087708 // units.
77097709 if satsMode {
7710- oracleRes , err := r .cfg .PriceOracle .QueryBidPrice (
7711- ctx , specifier , fn .None [uint64 ](), fn .Some (amtMsat ),
7712- fn .None [rfqmsg.AssetRate ](),
7710+ maxUnits , err = rfq .EstimateAssetUnits (
7711+ ctx , r .cfg .PriceOracle , specifier , amtMsat ,
77137712 )
77147713 if err != nil {
77157714 return nil , err
77167715 }
77177716
7718- if oracleRes .Err != nil {
7719- return nil , fmt .Errorf ("cannot query oracle: %v" ,
7720- oracleRes .Err .Error ())
7721- }
7717+ maxMathUnits := rfqmath .NewBigIntFromUint64 (maxUnits )
77227718
7723- assetUnits := rfqmath .MilliSatoshiToUnits (
7724- amtMsat , oracleRes . AssetRate . Rate ,
7719+ tolerance := rfqmath .NewBigIntFromUint64 (
7720+ r . cfg . RfqManager . GetPriceDeviationPpm () ,
77257721 )
77267722
7727- maxUnits = assetUnits .ToUint64 ()
7723+ // Since we used a different oracle price query above to
7724+ // calculate the max amount of units, we want to add some
7725+ // breathing room to account for price fluctuations caused by
7726+ // the small time delay, plus the fact that the agreed upon
7727+ // quote may be different. If we don't add this safety window
7728+ // the peer may allow a routable amount that evaluates to less
7729+ // than what we ask for. This is also checked below, after
7730+ // acquiring the quote.
7731+
7732+ // Apply the tolerance margin twice. Once due to the ask/bid
7733+ // price deviation that may occur during rfq negotiation, and
7734+ // once for the price movement that may occur between querying
7735+ // the oracle and acquiring the quote. We don't really care
7736+ // about this margin being too big, this only affects the max
7737+ // units our peer agrees to route.
7738+ maxMathUnits = rfqmath .AddTolerance (maxMathUnits , tolerance )
7739+ maxMathUnits = rfqmath .AddTolerance (maxMathUnits , tolerance )
7740+
7741+ maxUnits = maxMathUnits .ToUint64 ()
77287742 }
77297743
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-
77587744 resp , err := r .AddAssetBuyOrder (ctx , & rfqrpc.AddAssetBuyOrderRequest {
77597745 AssetSpecifier : & rfqrpc.AssetSpecifier {
77607746 Id : & rfqrpc.AssetSpecifier_AssetId {
0 commit comments