Skip to content

Commit 6e0f9ed

Browse files
GeorgeTsagkguggero
authored andcommitted
rpcserver: add marshalPeerAccepted{Buy,Sell}Quote helpers
1 parent 8b57001 commit 6e0f9ed

File tree

1 file changed

+40
-65
lines changed

1 file changed

+40
-65
lines changed

rpcserver.go

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7228,67 +7228,48 @@ func (r *rpcServer) AddAssetBuyOffer(_ context.Context,
72287228
return &rfqrpc.AddAssetBuyOfferResponse{}, nil
72297229
}
72307230

7231-
// marshalPeerAcceptedBuyQuotes marshals a map of peer accepted asset buy quotes
7232-
// into the RPC form. These are quotes that were requested by our node and have
7233-
// been accepted by our peers.
7234-
func marshalPeerAcceptedBuyQuotes(
7235-
quotes map[rfq.SerialisedScid]rfqmsg.BuyAccept) (
7236-
[]*rfqrpc.PeerAcceptedBuyQuote, error) {
7231+
// marshalPeerAcceptedBuyQuote marshals a peer accepted asset buy quote into
7232+
// the RPC form. This is a quote that was requested by our node and has been
7233+
// accepted by our peer.
7234+
func marshalPeerAcceptedBuyQuote(
7235+
quote rfqmsg.BuyAccept) *rfqrpc.PeerAcceptedBuyQuote {
72377236

7238-
// Marshal the accepted quotes into the RPC form.
7239-
rpcQuotes := make(
7240-
[]*rfqrpc.PeerAcceptedBuyQuote, 0, len(quotes),
7241-
)
7242-
for scid, quote := range quotes {
7243-
coefficient := quote.AssetRate.Rate.Coefficient.String()
7244-
rpcAskAssetRate := &rfqrpc.FixedPoint{
7245-
Coefficient: coefficient,
7246-
Scale: uint32(quote.AssetRate.Rate.Scale),
7247-
}
7248-
7249-
rpcQuote := &rfqrpc.PeerAcceptedBuyQuote{
7250-
Peer: quote.Peer.String(),
7251-
Id: quote.ID[:],
7252-
Scid: uint64(scid),
7253-
AssetMaxAmount: quote.Request.AssetMaxAmt,
7254-
AskAssetRate: rpcAskAssetRate,
7255-
Expiry: uint64(quote.AssetRate.Expiry.Unix()),
7256-
}
7257-
rpcQuotes = append(rpcQuotes, rpcQuote)
7237+
coefficient := quote.AssetRate.Rate.Coefficient.String()
7238+
rpcAskAssetRate := &rfqrpc.FixedPoint{
7239+
Coefficient: coefficient,
7240+
Scale: uint32(quote.AssetRate.Rate.Scale),
72587241
}
72597242

7260-
return rpcQuotes, nil
7243+
return &rfqrpc.PeerAcceptedBuyQuote{
7244+
Peer: quote.Peer.String(),
7245+
Id: quote.ID[:],
7246+
Scid: uint64(quote.ShortChannelId()),
7247+
AssetMaxAmount: quote.Request.AssetMaxAmt,
7248+
AskAssetRate: rpcAskAssetRate,
7249+
Expiry: uint64(quote.AssetRate.Expiry.Unix()),
7250+
}
72617251
}
72627252

7263-
// marshalPeerAcceptedSellQuotes marshals a map of peer accepted asset sell
7264-
// quotes into the RPC form. These are quotes that were requested by our node
7265-
// and have been accepted by our peers.
7266-
//
7267-
// nolint: lll
7268-
func marshalPeerAcceptedSellQuotes(quotes map[rfq.SerialisedScid]rfqmsg.SellAccept) (
7269-
[]*rfqrpc.PeerAcceptedSellQuote, error) {
7270-
7271-
// Marshal the accepted quotes into the RPC form.
7272-
rpcQuotes := make([]*rfqrpc.PeerAcceptedSellQuote, 0, len(quotes))
7273-
for scid, quote := range quotes {
7274-
rpcAssetRate := &rfqrpc.FixedPoint{
7275-
Coefficient: quote.AssetRate.Rate.Coefficient.String(),
7276-
Scale: uint32(quote.AssetRate.Rate.Scale),
7277-
}
7253+
// marshalPeerAcceptedSellQuote marshals peer accepted asset sell quote into the
7254+
// RPC form. This is a quote that was requested by our node and has been
7255+
// accepted by our peers.
7256+
func marshalPeerAcceptedSellQuote(
7257+
quote rfqmsg.SellAccept) *rfqrpc.PeerAcceptedSellQuote {
72787258

7279-
// TODO(ffranr): Add SellRequest payment max amount to
7280-
// PeerAcceptedSellQuote.
7281-
rpcQuote := &rfqrpc.PeerAcceptedSellQuote{
7282-
Peer: quote.Peer.String(),
7283-
Id: quote.ID[:],
7284-
Scid: uint64(scid),
7285-
BidAssetRate: rpcAssetRate,
7286-
Expiry: uint64(quote.AssetRate.Expiry.Unix()),
7287-
}
7288-
rpcQuotes = append(rpcQuotes, rpcQuote)
7259+
rpcAssetRate := &rfqrpc.FixedPoint{
7260+
Coefficient: quote.AssetRate.Rate.Coefficient.String(),
7261+
Scale: uint32(quote.AssetRate.Rate.Scale),
72897262
}
72907263

7291-
return rpcQuotes, nil
7264+
// TODO(ffranr): Add SellRequest payment max amount to
7265+
// PeerAcceptedSellQuote.
7266+
return &rfqrpc.PeerAcceptedSellQuote{
7267+
Peer: quote.Peer.String(),
7268+
Id: quote.ID[:],
7269+
Scid: uint64(quote.ShortChannelId()),
7270+
BidAssetRate: rpcAssetRate,
7271+
Expiry: uint64(quote.AssetRate.Expiry.Unix()),
7272+
}
72927273
}
72937274

72947275
// QueryPeerAcceptedQuotes is used to query for quotes that were requested by
@@ -7302,19 +7283,13 @@ func (r *rpcServer) QueryPeerAcceptedQuotes(_ context.Context,
73027283
peerAcceptedBuyQuotes := r.cfg.RfqManager.PeerAcceptedBuyQuotes()
73037284
peerAcceptedSellQuotes := r.cfg.RfqManager.PeerAcceptedSellQuotes()
73047285

7305-
rpcBuyQuotes, err := marshalPeerAcceptedBuyQuotes(peerAcceptedBuyQuotes)
7306-
if err != nil {
7307-
return nil, fmt.Errorf("error marshalling peer accepted buy "+
7308-
"quotes: %w", err)
7309-
}
7310-
7311-
rpcSellQuotes, err := marshalPeerAcceptedSellQuotes(
7312-
peerAcceptedSellQuotes,
7286+
rpcBuyQuotes := fn.Map(
7287+
maps.Values(peerAcceptedBuyQuotes), marshalPeerAcceptedBuyQuote,
7288+
)
7289+
rpcSellQuotes := fn.Map(
7290+
maps.Values(peerAcceptedSellQuotes),
7291+
marshalPeerAcceptedSellQuote,
73137292
)
7314-
if err != nil {
7315-
return nil, fmt.Errorf("error marshalling peer accepted sell "+
7316-
"quotes: %w", err)
7317-
}
73187293

73197294
return &rfqrpc.QueryPeerAcceptedQuotesResponse{
73207295
BuyQuotes: rpcBuyQuotes,

0 commit comments

Comments
 (0)