Skip to content

Commit

Permalink
rpc: switch AddInvoice RPC endpoint to fixed-point pricing
Browse files Browse the repository at this point in the history
This commit changes the PeerAcceptedBuyQuote.AskPrice field from a
`uint64` to a fixed-point representation for the asset-to-BTC rate.

It also updates the invoice milli-sat calculation to use the newly
introduced fixed-point field.
  • Loading branch information
ffranr committed Sep 24, 2024
1 parent bb0c117 commit bceafd1
Show file tree
Hide file tree
Showing 6 changed files with 548 additions and 367 deletions.
34 changes: 25 additions & 9 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/lightninglabs/taproot-assets/mssmt"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightninglabs/taproot-assets/rpcperms"
"github.com/lightninglabs/taproot-assets/tapchannel"
Expand Down Expand Up @@ -6585,14 +6586,18 @@ func marshalPeerAcceptedBuyQuotes(
[]*rfqrpc.PeerAcceptedBuyQuote, 0, len(quotes),
)
for scid, quote := range quotes {
rpcAskAssetRate := &rfqrpc.FixedPoint{
Coefficient: quote.AssetRate.Coefficient.ToUint64(),
Scale: uint32(quote.AssetRate.Scale),
}

rpcQuote := &rfqrpc.PeerAcceptedBuyQuote{
Peer: quote.Peer.String(),
Id: quote.ID[:],
Scid: uint64(scid),
AssetAmount: quote.Request.AssetAmount,
// TODO(ffranr): Temp solution.
AskPrice: quote.AssetRate.Coefficient.ToUint64(),
Expiry: quote.Expiry,
Peer: quote.Peer.String(),
Id: quote.ID[:],
Scid: uint64(scid),
AssetAmount: quote.Request.AssetAmount,
AskAssetRate: rpcAskAssetRate,
Expiry: quote.Expiry,
}
rpcQuotes = append(rpcQuotes, rpcQuote)
}
Expand Down Expand Up @@ -7130,8 +7135,19 @@ func (r *rpcServer) AddInvoice(ctx context.Context,

// Now that we have the accepted quote, we know the amount in Satoshi
// that we need to pay. We can now update the invoice with this amount.
mSatPerUnit := acceptedQuote.AskPrice
iReq.ValueMsat = int64(req.AssetAmount * mSatPerUnit)
askAssetRate := rfqmath.FixedPoint[rfqmath.GoInt[uint64]]{
Coefficient: rfqmath.NewGoInt(
acceptedQuote.AskAssetRate.Coefficient,
),
Scale: uint8(acceptedQuote.AskAssetRate.Scale),
}
assetAmount := rfqmath.FixedPoint[rfqmath.GoInt[uint64]]{
Coefficient: rfqmath.NewGoInt(req.AssetAmount),
Scale: 0,
}

valMsat := rfqmath.UnitsToMilliSatoshi(assetAmount, askAssetRate)
iReq.ValueMsat = int64(valMsat)

// The last step is to create a hop hint that includes the fake SCID of
// the quote, alongside the channel's routing policy. We need to choose
Expand Down
8 changes: 5 additions & 3 deletions taprpc/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,11 @@ func MarshalAcceptedBuyQuoteEvent(
Id: event.ID[:],
Scid: uint64(event.ShortChannelId()),
AssetAmount: event.Request.AssetAmount,
// TODO(ffranr): Temp solution.
AskPrice: event.AssetRate.Coefficient.ToUint64(),
Expiry: event.Expiry,
AskAssetRate: &rfqrpc.FixedPoint{
Coefficient: event.AssetRate.Coefficient.ToUint64(),
Scale: uint32(event.AssetRate.Scale),
},
Expiry: event.Expiry,
}
}

Expand Down
Loading

0 comments on commit bceafd1

Please sign in to comment.