Skip to content

Commit

Permalink
taprpc+rpcserver: unify marshaling, add asset amount to sell quote
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guggero committed Dec 4, 2024
1 parent de2eef6 commit 1039ab6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 60 deletions.
48 changes: 7 additions & 41 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6818,13 +6818,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{
Expand Down Expand Up @@ -7060,36 +7056,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,
},
Expand All @@ -7100,8 +7072,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))

Expand Down Expand Up @@ -7242,15 +7214,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)
Expand Down
46 changes: 27 additions & 19 deletions taprpc/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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"
)
Expand Down Expand Up @@ -561,25 +563,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
Expand Down Expand Up @@ -675,14 +689,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

Expand Down

0 comments on commit 1039ab6

Please sign in to comment.