Skip to content

Commit b7ed69a

Browse files
committed
rpcserver: add acquireBuyOrder helper
We extract away another piece of logic that will be re-used in the new reformed AddInvoice endpoint. Since we will be querying multiple peers for quotes, we need to abstract this away into a function.
1 parent 0b1c1e5 commit b7ed69a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

rpcserver.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8322,6 +8322,57 @@ func validateInvoiceAmount(acceptedQuote *rfqrpc.PeerAcceptedBuyQuote,
83228322
return newInvoiceAmtMsat, nil
83238323
}
83248324

8325+
// acquireBuyOrder performs an RFQ negotiation with the target peer and quote
8326+
// parameters and returns the quote if the negotiation was successful.
8327+
func (r *rpcServer) acquireBuyOrder(ctx context.Context,
8328+
rpcSpecifier *rfqrpc.AssetSpecifier, assetMaxAmt uint64,
8329+
expiryTimestamp time.Time,
8330+
peerPubKey *route.Vertex) (*rfqrpc.PeerAcceptedBuyQuote, error) {
8331+
8332+
var quote *rfqrpc.PeerAcceptedBuyQuote
8333+
8334+
resp, err := r.AddAssetBuyOrder(ctx, &rfqrpc.AddAssetBuyOrderRequest{
8335+
AssetSpecifier: rpcSpecifier,
8336+
AssetMaxAmt: assetMaxAmt,
8337+
Expiry: uint64(expiryTimestamp.Unix()),
8338+
PeerPubKey: peerPubKey[:],
8339+
TimeoutSeconds: uint32(
8340+
rfq.DefaultTimeout.Seconds(),
8341+
),
8342+
})
8343+
if err != nil {
8344+
return quote, fmt.Errorf("error adding buy order: %w", err)
8345+
}
8346+
8347+
switch r := resp.Response.(type) {
8348+
case *rfqrpc.AddAssetBuyOrderResponse_AcceptedQuote:
8349+
quote = r.AcceptedQuote
8350+
8351+
case *rfqrpc.AddAssetBuyOrderResponse_InvalidQuote:
8352+
return nil, fmt.Errorf("peer %v sent back an invalid quote, "+
8353+
"status: %v", r.InvalidQuote.Peer,
8354+
r.InvalidQuote.Status.String())
8355+
8356+
case *rfqrpc.AddAssetBuyOrderResponse_RejectedQuote:
8357+
return nil, fmt.Errorf("peer %v rejected the quote, code: %v, "+
8358+
"error message: %v", r.RejectedQuote.Peer,
8359+
r.RejectedQuote.ErrorCode, r.RejectedQuote.ErrorMessage)
8360+
8361+
default:
8362+
return nil, fmt.Errorf("unexpected response type: %T", r)
8363+
}
8364+
8365+
if quote.MinTransportableUnits > assetMaxAmt {
8366+
return nil, fmt.Errorf("cannot create invoice over %d asset "+
8367+
"units, as the minimal transportable amount is %d "+
8368+
"units with the current rate of %v units/BTC",
8369+
assetMaxAmt, quote.MinTransportableUnits,
8370+
quote.AskAssetRate)
8371+
}
8372+
8373+
return quote, nil
8374+
}
8375+
83258376
// DeclareScriptKey declares a new script key to the wallet. This is useful
83268377
// when the script key contains scripts, which would mean it wouldn't be
83278378
// recognized by the wallet automatically. Declaring a script key will make any

0 commit comments

Comments
 (0)