Skip to content

Commit 2ed2772

Browse files
committed
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.
1 parent 4ce015d commit 2ed2772

File tree

2 files changed

+34
-60
lines changed

2 files changed

+34
-60
lines changed

rpcserver.go

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6819,13 +6819,9 @@ func marshallRfqEvent(eventInterface fn.Event) (*rfqrpc.RfqEvent, error) {
68196819
}, nil
68206820

68216821
case *rfq.PeerAcceptedSellQuoteEvent:
6822-
rpcAcceptedQuote, err := taprpc.MarshalAcceptedSellQuoteEvent(
6822+
rpcAcceptedQuote := taprpc.MarshalAcceptedSellQuoteEvent(
68236823
event,
68246824
)
6825-
if err != nil {
6826-
return nil, fmt.Errorf("error marshalling accepted "+
6827-
"sell quote event: %w", err)
6828-
}
68296825

68306826
eventRpc := &rfqrpc.RfqEvent_PeerAcceptedSellQuote{
68316827
PeerAcceptedSellQuote: &rfqrpc.PeerAcceptedSellQuoteEvent{
@@ -7061,36 +7057,12 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
70617057
"accepted quote")
70627058
}
70637059

7064-
invoice, err := zpay32.Decode(
7065-
pReq.PaymentRequest, r.cfg.Lnd.ChainParams,
7066-
)
7067-
if err != nil {
7068-
return fmt.Errorf("error decoding payment request: %w",
7069-
err)
7070-
}
7071-
7072-
rate := quote.AssetRate.Rate
7073-
70747060
// Calculate the equivalent asset units for the given invoice
70757061
// amount based on the asset-to-BTC conversion rate.
7076-
numAssetUnits := rfqmath.MilliSatoshiToUnits(
7077-
*invoice.MilliSat, rate,
7078-
)
7079-
7080-
sellOrder := &rfqrpc.PeerAcceptedSellQuote{
7081-
Peer: quote.Peer.String(),
7082-
Id: quote.ID[:],
7083-
Scid: uint64(quote.ID.Scid()),
7084-
BidAssetRate: &rfqrpc.FixedPoint{
7085-
Coefficient: rate.Coefficient.String(),
7086-
Scale: uint32(rate.Scale),
7087-
},
7088-
AssetAmount: numAssetUnits.ToUint64(),
7089-
Expiry: uint64(quote.AssetRate.Expiry.Unix()),
7090-
}
7062+
sellOrder := taprpc.MarshalAcceptedSellQuote(*quote)
70917063

70927064
// Send out the information about the quote on the stream.
7093-
err = stream.Send(&tchrpc.SendPaymentResponse{
7065+
err := stream.Send(&tchrpc.SendPaymentResponse{
70947066
Result: &tchrpc.SendPaymentResponse_AcceptedSellOrder{
70957067
AcceptedSellOrder: sellOrder,
70967068
},
@@ -7101,8 +7073,8 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
71017073
}
71027074

71037075
rpcsLog.Infof("Using quote for %v asset units at %v asset/BTC "+
7104-
"from peer %x with SCID %d", numAssetUnits,
7105-
rate.String(), quote.Peer, quote.ID.Scid())
7076+
"from peer %x with SCID %d", sellOrder.AssetAmount,
7077+
quote.AssetRate.String(), quote.Peer, quote.ID.Scid())
71067078

71077079
htlc := rfqmsg.NewHtlc(nil, fn.Some(quote.ID))
71087080

@@ -7243,15 +7215,9 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
72437215
err)
72447216
}
72457217

7246-
// Calculate the equivalent asset units for the given invoice
7247-
// amount based on the asset-to-BTC conversion rate.
7248-
numAssetUnits := rfqmath.MilliSatoshiToUnits(
7249-
*invoice.MilliSat, *assetRate,
7250-
)
7251-
72527218
rpcsLog.Infof("Got quote for %v asset units at %v asset/BTC "+
7253-
"from peer %x with SCID %d", numAssetUnits, assetRate,
7254-
peerPubKey, acceptedQuote.Scid)
7219+
"from peer %x with SCID %d", acceptedQuote.AssetAmount,
7220+
assetRate, peerPubKey, acceptedQuote.Scid)
72557221

72567222
var rfqID rfqmsg.ID
72577223
copy(rfqID[:], acceptedQuote.Id)

taprpc/marshal.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/lightninglabs/taproot-assets/commitment"
1616
"github.com/lightninglabs/taproot-assets/fn"
1717
"github.com/lightninglabs/taproot-assets/rfq"
18+
"github.com/lightninglabs/taproot-assets/rfqmath"
19+
"github.com/lightninglabs/taproot-assets/rfqmsg"
1820
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
1921
"github.com/lightningnetwork/lnd/keychain"
2022
)
@@ -522,25 +524,37 @@ func MarshalAsset(ctx context.Context, a *asset.Asset,
522524
}
523525

524526
// MarshalAcceptedSellQuoteEvent marshals a peer accepted sell quote event to
525-
// its rpc representation.
527+
// its RPC representation.
526528
func MarshalAcceptedSellQuoteEvent(
527-
event *rfq.PeerAcceptedSellQuoteEvent) (*rfqrpc.PeerAcceptedSellQuote,
528-
error) {
529+
event *rfq.PeerAcceptedSellQuoteEvent) *rfqrpc.PeerAcceptedSellQuote {
530+
531+
return MarshalAcceptedSellQuote(event.SellAccept)
532+
}
533+
534+
// MarshalAcceptedSellQuote marshals a peer accepted sell quote to its RPC
535+
// representation.
536+
func MarshalAcceptedSellQuote(
537+
accept rfqmsg.SellAccept) *rfqrpc.PeerAcceptedSellQuote {
529538

530539
rpcAssetRate := &rfqrpc.FixedPoint{
531-
Coefficient: event.AssetRate.Rate.Coefficient.String(),
532-
Scale: uint32(event.AssetRate.Rate.Scale),
540+
Coefficient: accept.AssetRate.Rate.Coefficient.String(),
541+
Scale: uint32(accept.AssetRate.Rate.Scale),
533542
}
534543

535-
// TODO(ffranr): Add SellRequest payment max amount to
536-
// PeerAcceptedSellQuote.
544+
// Calculate the equivalent asset units for the given total BTC amount
545+
// based on the asset-to-BTC conversion rate.
546+
numAssetUnits := rfqmath.MilliSatoshiToUnits(
547+
accept.Request.PaymentMaxAmt, accept.AssetRate.Rate,
548+
)
549+
537550
return &rfqrpc.PeerAcceptedSellQuote{
538-
Peer: event.Peer.String(),
539-
Id: event.ID[:],
540-
Scid: uint64(event.ShortChannelId()),
551+
Peer: accept.Peer.String(),
552+
Id: accept.ID[:],
553+
Scid: uint64(accept.ShortChannelId()),
541554
BidAssetRate: rpcAssetRate,
542-
Expiry: uint64(event.AssetRate.Expiry.Unix()),
543-
}, nil
555+
Expiry: uint64(accept.AssetRate.Expiry.Unix()),
556+
AssetAmount: numAssetUnits.ScaleTo(0).ToUint64(),
557+
}
544558
}
545559

546560
// MarshalAcceptedBuyQuoteEvent marshals a peer accepted buy quote event to
@@ -636,14 +650,8 @@ func NewAddAssetSellOrderResponse(
636650

637651
switch e := event.(type) {
638652
case *rfq.PeerAcceptedSellQuoteEvent:
639-
rpcAcceptedQuote, err := MarshalAcceptedSellQuoteEvent(e)
640-
if err != nil {
641-
return nil, fmt.Errorf("unable to marshal accepted "+
642-
"sell quote event to RPC: %w", err)
643-
}
644-
645653
resp.Response = &rfqrpc.AddAssetSellOrderResponse_AcceptedQuote{
646-
AcceptedQuote: rpcAcceptedQuote,
654+
AcceptedQuote: MarshalAcceptedSellQuoteEvent(e),
647655
}
648656
return resp, nil
649657

0 commit comments

Comments
 (0)