From 2ed277228be681f1721d6ed63baad257e22b99a8 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 4 Dec 2024 15:40:44 +0100 Subject: [PATCH] taprpc+rpcserver: unify marshaling, add asset amount to sell quote Fixes #1234. Simplifies and unifies the marshaling of sell quotes and adds the asset amount by converting the request's max amount using the quote. --- rpcserver.go | 48 +++++++---------------------------------------- taprpc/marshal.go | 46 ++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 60 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index f74259ba5..140f9bb61 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -6819,13 +6819,9 @@ func marshallRfqEvent(eventInterface fn.Event) (*rfqrpc.RfqEvent, error) { }, nil case *rfq.PeerAcceptedSellQuoteEvent: - rpcAcceptedQuote, err := taprpc.MarshalAcceptedSellQuoteEvent( + rpcAcceptedQuote := taprpc.MarshalAcceptedSellQuoteEvent( event, ) - if err != nil { - return nil, fmt.Errorf("error marshalling accepted "+ - "sell quote event: %w", err) - } eventRpc := &rfqrpc.RfqEvent_PeerAcceptedSellQuote{ PeerAcceptedSellQuote: &rfqrpc.PeerAcceptedSellQuoteEvent{ @@ -7061,36 +7057,12 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest, "accepted quote") } - invoice, err := zpay32.Decode( - pReq.PaymentRequest, r.cfg.Lnd.ChainParams, - ) - if err != nil { - return fmt.Errorf("error decoding payment request: %w", - err) - } - - rate := quote.AssetRate.Rate - // Calculate the equivalent asset units for the given invoice // amount based on the asset-to-BTC conversion rate. - numAssetUnits := rfqmath.MilliSatoshiToUnits( - *invoice.MilliSat, rate, - ) - - sellOrder := &rfqrpc.PeerAcceptedSellQuote{ - Peer: quote.Peer.String(), - Id: quote.ID[:], - Scid: uint64(quote.ID.Scid()), - BidAssetRate: &rfqrpc.FixedPoint{ - Coefficient: rate.Coefficient.String(), - Scale: uint32(rate.Scale), - }, - AssetAmount: numAssetUnits.ToUint64(), - Expiry: uint64(quote.AssetRate.Expiry.Unix()), - } + sellOrder := taprpc.MarshalAcceptedSellQuote(*quote) // Send out the information about the quote on the stream. - err = stream.Send(&tchrpc.SendPaymentResponse{ + err := stream.Send(&tchrpc.SendPaymentResponse{ Result: &tchrpc.SendPaymentResponse_AcceptedSellOrder{ AcceptedSellOrder: sellOrder, }, @@ -7101,8 +7073,8 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest, } rpcsLog.Infof("Using quote for %v asset units at %v asset/BTC "+ - "from peer %x with SCID %d", numAssetUnits, - rate.String(), quote.Peer, quote.ID.Scid()) + "from peer %x with SCID %d", sellOrder.AssetAmount, + quote.AssetRate.String(), quote.Peer, quote.ID.Scid()) htlc := rfqmsg.NewHtlc(nil, fn.Some(quote.ID)) @@ -7243,15 +7215,9 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest, err) } - // Calculate the equivalent asset units for the given invoice - // amount based on the asset-to-BTC conversion rate. - numAssetUnits := rfqmath.MilliSatoshiToUnits( - *invoice.MilliSat, *assetRate, - ) - rpcsLog.Infof("Got quote for %v asset units at %v asset/BTC "+ - "from peer %x with SCID %d", numAssetUnits, assetRate, - peerPubKey, acceptedQuote.Scid) + "from peer %x with SCID %d", acceptedQuote.AssetAmount, + assetRate, peerPubKey, acceptedQuote.Scid) var rfqID rfqmsg.ID copy(rfqID[:], acceptedQuote.Id) diff --git a/taprpc/marshal.go b/taprpc/marshal.go index 7e36d9448..277db59c8 100644 --- a/taprpc/marshal.go +++ b/taprpc/marshal.go @@ -15,6 +15,8 @@ import ( "github.com/lightninglabs/taproot-assets/commitment" "github.com/lightninglabs/taproot-assets/fn" "github.com/lightninglabs/taproot-assets/rfq" + "github.com/lightninglabs/taproot-assets/rfqmath" + "github.com/lightninglabs/taproot-assets/rfqmsg" "github.com/lightninglabs/taproot-assets/taprpc/rfqrpc" "github.com/lightningnetwork/lnd/keychain" ) @@ -522,25 +524,37 @@ func MarshalAsset(ctx context.Context, a *asset.Asset, } // MarshalAcceptedSellQuoteEvent marshals a peer accepted sell quote event to -// its rpc representation. +// its RPC representation. func MarshalAcceptedSellQuoteEvent( - event *rfq.PeerAcceptedSellQuoteEvent) (*rfqrpc.PeerAcceptedSellQuote, - error) { + event *rfq.PeerAcceptedSellQuoteEvent) *rfqrpc.PeerAcceptedSellQuote { + + return MarshalAcceptedSellQuote(event.SellAccept) +} + +// MarshalAcceptedSellQuote marshals a peer accepted sell quote to its RPC +// representation. +func MarshalAcceptedSellQuote( + accept rfqmsg.SellAccept) *rfqrpc.PeerAcceptedSellQuote { rpcAssetRate := &rfqrpc.FixedPoint{ - Coefficient: event.AssetRate.Rate.Coefficient.String(), - Scale: uint32(event.AssetRate.Rate.Scale), + Coefficient: accept.AssetRate.Rate.Coefficient.String(), + Scale: uint32(accept.AssetRate.Rate.Scale), } - // TODO(ffranr): Add SellRequest payment max amount to - // PeerAcceptedSellQuote. + // Calculate the equivalent asset units for the given total BTC amount + // based on the asset-to-BTC conversion rate. + numAssetUnits := rfqmath.MilliSatoshiToUnits( + accept.Request.PaymentMaxAmt, accept.AssetRate.Rate, + ) + return &rfqrpc.PeerAcceptedSellQuote{ - Peer: event.Peer.String(), - Id: event.ID[:], - Scid: uint64(event.ShortChannelId()), + Peer: accept.Peer.String(), + Id: accept.ID[:], + Scid: uint64(accept.ShortChannelId()), BidAssetRate: rpcAssetRate, - Expiry: uint64(event.AssetRate.Expiry.Unix()), - }, nil + Expiry: uint64(accept.AssetRate.Expiry.Unix()), + AssetAmount: numAssetUnits.ScaleTo(0).ToUint64(), + } } // MarshalAcceptedBuyQuoteEvent marshals a peer accepted buy quote event to @@ -636,14 +650,8 @@ func NewAddAssetSellOrderResponse( switch e := event.(type) { case *rfq.PeerAcceptedSellQuoteEvent: - rpcAcceptedQuote, err := MarshalAcceptedSellQuoteEvent(e) - if err != nil { - return nil, fmt.Errorf("unable to marshal accepted "+ - "sell quote event to RPC: %w", err) - } - resp.Response = &rfqrpc.AddAssetSellOrderResponse_AcceptedQuote{ - AcceptedQuote: rpcAcceptedQuote, + AcceptedQuote: MarshalAcceptedSellQuoteEvent(e), } return resp, nil