-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rpc: implement DecodeAssetInvoice #1253
Open
Roasbeef
wants to merge
2
commits into
lightninglabs:main
Choose a base branch
from
Roasbeef:decode-asset-invoice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,005
−166
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7822,3 +7822,144 @@ func (r *rpcServer) getInboundPolicy(ctx context.Context, chanID uint64, | |
|
||
return policy, nil | ||
} | ||
|
||
// assetInvoiceAmt calculates the amount of asset units to pay for an invoice | ||
// which is expressed in sats. | ||
func (r *rpcServer) assetInvoiceAmt(ctx context.Context, targetAsset asset.Specifier, | ||
invoiceAmt lnwire.MilliSatoshi) (uint64, error) { | ||
|
||
oracle := r.cfg.PriceOracle | ||
|
||
oracleResp, err := oracle.QueryAskPrice( | ||
Roasbeef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx, targetAsset, fn.None[uint64](), fn.Some(invoiceAmt), | ||
fn.None[rfqmsg.AssetRate](), | ||
) | ||
if err != nil { | ||
return 0, fmt.Errorf("error querying ask price: %w", err) | ||
} | ||
if oracleResp.Err != nil { | ||
return 0, fmt.Errorf("error querying ask price: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: use |
||
} | ||
|
||
assetRate := oracleResp.AssetRate.Rate | ||
|
||
numAssetUnits := rfqmath.MilliSatoshiToUnits( | ||
invoiceAmt, assetRate, | ||
).ScaleTo(0) | ||
|
||
return numAssetUnits.ToUint64(), nil | ||
} | ||
|
||
// DecodeAssetPayReq decodes an incoming invoice, then uses the RFQ system to | ||
// map the BTC amount to the amount of asset units for the specified asset ID. | ||
func (r *rpcServer) DecodeAssetPayReq(ctx context.Context, | ||
payReq *tchrpc.AssetPayReq) (*tchrpc.AssetPayReqResponse, error) { | ||
|
||
if r.cfg.PriceOracle == nil { | ||
return nil, fmt.Errorf("price oracle is not set") | ||
} | ||
|
||
// First, we'll perform some basic input validation. | ||
switch { | ||
case len(payReq.AssetId) == 0: | ||
return nil, fmt.Errorf("asset ID must be specified") | ||
|
||
case len(payReq.AssetId) != 32: | ||
return nil, fmt.Errorf("asset ID must be 32 bytes, "+ | ||
"was %d", len(payReq.AssetId)) | ||
|
||
case len(payReq.PayReqString) == 0: | ||
return nil, fmt.Errorf("payment request must be specified") | ||
} | ||
|
||
var ( | ||
resp tchrpc.AssetPayReqResponse | ||
assetID asset.ID | ||
) | ||
|
||
copy(assetID[:], payReq.AssetId) | ||
|
||
// With the inputs validated, we'll first call out to lnd to decode the | ||
// payment request. | ||
rpcCtx, _, rawClient := r.cfg.Lnd.Client.RawClientWithMacAuth(ctx) | ||
payReqInfo, err := rawClient.DecodePayReq(rpcCtx, &lnrpc.PayReqString{ | ||
PayReq: payReq.PayReqString, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to fetch channel: %w", err) | ||
} | ||
|
||
resp.PayReq = payReqInfo | ||
|
||
// Next, we'll fetch the information for this asset ID through the addr | ||
// book. This'll automatically fetch the asset if needed. | ||
assetGroup, err := r.cfg.AddrBook.QueryAssetInfo(ctx, assetID) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to fetch asset info for "+ | ||
"asset_id=%x: %w", assetID[:], err) | ||
} | ||
|
||
resp.GenesisInfo = &taprpc.GenesisInfo{ | ||
GenesisPoint: assetGroup.FirstPrevOut.String(), | ||
AssetType: taprpc.AssetType(assetGroup.Type), | ||
Name: assetGroup.Tag, | ||
MetaHash: assetGroup.MetaHash[:], | ||
AssetId: assetID[:], | ||
} | ||
|
||
// If this asset ID belongs to an asset group, then we'll display thiat | ||
// information as well. | ||
// | ||
// nolint:lll | ||
if assetGroup.GroupKey != nil { | ||
groupInfo := assetGroup.GroupKey | ||
resp.AssetGroup = &taprpc.AssetGroup{ | ||
RawGroupKey: groupInfo.RawKey.PubKey.SerializeCompressed(), | ||
TweakedGroupKey: groupInfo.GroupPubKey.SerializeCompressed(), | ||
TapscriptRoot: groupInfo.TapscriptRoot, | ||
} | ||
|
||
if len(groupInfo.Witness) != 0 { | ||
resp.AssetGroup.AssetWitness, err = asset.SerializeGroupWitness( | ||
groupInfo.Witness, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
// Now that we have the basic invoice information, we'll query the RFQ | ||
// system to obtain a quote to send this amount of BTC. Note that this | ||
// doesn't factor in the fee limit, so this attempts just to map the | ||
// sats amount to an asset unit. | ||
numMsat := lnwire.NewMSatFromSatoshis( | ||
btcutil.Amount(payReqInfo.NumSatoshis), | ||
) | ||
targetAsset := asset.NewSpecifierOptionalGroupKey( | ||
assetGroup.ID(), assetGroup.GroupKey, | ||
) | ||
invoiceAmt, err := r.assetInvoiceAmt(ctx, targetAsset, numMsat) | ||
if err != nil { | ||
return nil, fmt.Errorf("error deriving asset amount: %w", err) | ||
} | ||
|
||
resp.AssetAmount = invoiceAmt | ||
|
||
// The final piece of information we need is the decimal display | ||
// information for this asset ID. | ||
decDisplay, err := r.DecDisplayForAssetID(ctx, assetID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp.DecimalDisplay = fn.MapOptionZ( | ||
decDisplay, func(d uint32) *taprpc.DecimalDisplay { | ||
return &taprpc.DecimalDisplay{ | ||
DecimalDisplay: d, | ||
} | ||
}, | ||
) | ||
|
||
return &resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: line too long (linter error is somewhat cryptic).