Skip to content

Commit 5ccd981

Browse files
GeorgeTsagkguggero
authored andcommitted
rpcserver: extract validateInvoiceAmount
1 parent a9ac111 commit 5ccd981

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

rpcserver.go

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7780,35 +7780,17 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
77807780
return nil, fmt.Errorf("unexpected response type: %T", r)
77817781
}
77827782

7783-
// If the invoice is for an asset unit amount smaller than the minimal
7784-
// transportable amount, we'll return an error, as it wouldn't be
7785-
// payable by the network.
7786-
if acceptedQuote.MinTransportableUnits > req.AssetAmount {
7787-
return nil, fmt.Errorf("cannot create invoice over %d asset "+
7788-
"units, as the minimal transportable amount is %d "+
7789-
"units with the current rate of %v units/BTC",
7790-
req.AssetAmount, acceptedQuote.MinTransportableUnits,
7791-
acceptedQuote.AskAssetRate)
7792-
}
7793-
7794-
// Now that we have the accepted quote, we know the amount in Satoshi
7795-
// that we need to pay. We can now update the invoice with this amount.
7796-
//
7797-
// First, un-marshall the ask asset rate from the accepted quote.
7798-
askAssetRate, err := rfqrpc.UnmarshalFixedPoint(
7799-
acceptedQuote.AskAssetRate,
7783+
// Now that we have the accepted quote, we know the amount in (milli)
7784+
// Satoshi that we need to pay. We can now update the invoice with this
7785+
// amount.
7786+
invoiceAmtMsat, err := validateInvoiceAmount(
7787+
acceptedQuote, req.AssetAmount,
78007788
)
78017789
if err != nil {
7802-
return nil, fmt.Errorf("error unmarshalling ask asset rate: %w",
7790+
return nil, fmt.Errorf("error validating invoice amount: %w",
78037791
err)
78047792
}
7805-
7806-
// Convert the asset amount into a fixed-point.
7807-
assetAmount := rfqmath.NewBigIntFixedPoint(req.AssetAmount, 0)
7808-
7809-
// Calculate the invoice amount in msat.
7810-
valMsat := rfqmath.UnitsToMilliSatoshi(assetAmount, *askAssetRate)
7811-
iReq.ValueMsat = int64(valMsat)
7793+
iReq.ValueMsat = int64(invoiceAmtMsat)
78127794

78137795
// The last step is to create a hop hint that includes the fake SCID of
78147796
// the quote, alongside the channel's routing policy. We need to choose
@@ -7911,6 +7893,45 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
79117893
}, nil
79127894
}
79137895

7896+
// validateInvoiceAmount validates the quote against the invoice we're trying to
7897+
// add. It returns the value in msat that should be included in the invoice.
7898+
func validateInvoiceAmount(acceptedQuote *rfqrpc.PeerAcceptedBuyQuote,
7899+
requestAssetAmount uint64) (lnwire.MilliSatoshi, error) {
7900+
7901+
// If the invoice is for an asset unit amount smaller than the minimal
7902+
// transportable amount, we'll return an error, as it wouldn't be
7903+
// payable by the network.
7904+
if acceptedQuote.MinTransportableUnits > requestAssetAmount {
7905+
return 0, fmt.Errorf("cannot create invoice over %d asset "+
7906+
"units, as the minimal transportable amount is %d "+
7907+
"units with the current rate of %v units/BTC",
7908+
requestAssetAmount, acceptedQuote.MinTransportableUnits,
7909+
acceptedQuote.AskAssetRate)
7910+
}
7911+
7912+
// Now that we have the accepted quote, we know the amount in Satoshi
7913+
// that we need to pay. We can now update the invoice with this amount.
7914+
//
7915+
// First, un-marshall the ask asset rate from the accepted quote.
7916+
askAssetRate, err := rfqrpc.UnmarshalFixedPoint(
7917+
acceptedQuote.AskAssetRate,
7918+
)
7919+
if err != nil {
7920+
return 0, fmt.Errorf("error unmarshalling ask asset rate: %w",
7921+
err)
7922+
}
7923+
7924+
// Convert the asset amount into a fixed-point.
7925+
assetAmount := rfqmath.NewBigIntFixedPoint(requestAssetAmount, 0)
7926+
7927+
// Calculate the invoice amount in msat.
7928+
newInvoiceAmtMsat := rfqmath.UnitsToMilliSatoshi(
7929+
assetAmount, *askAssetRate,
7930+
)
7931+
7932+
return newInvoiceAmtMsat, nil
7933+
}
7934+
79147935
// DeclareScriptKey declares a new script key to the wallet. This is useful
79157936
// when the script key contains scripts, which would mean it wouldn't be
79167937
// recognized by the wallet automatically. Declaring a script key will make any

0 commit comments

Comments
 (0)