Skip to content

Commit

Permalink
rfq+rfqmsg: replace SellRequest.AskPrice with SuggestedAssetRate
Browse files Browse the repository at this point in the history
This commit replaces the SellRequest.AskPrice field with
SuggestedAssetRate, changing a `uint64` price to an asset-to-BTC rate
represented as a fixed-point number.
  • Loading branch information
ffranr committed Oct 11, 2024
1 parent d5bc38d commit 92c00cd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
14 changes: 4 additions & 10 deletions rfq/negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,11 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {
// We calculate a proposed ask price for our peer's
// consideration. If a price oracle is not specified we will
// skip this step.
var askPrice lnwire.MilliSatoshi
var assetRate fn.Option[rfqmsg.BigIntFixedPoint]

if n.cfg.PriceOracle != nil {
// Query the price oracle for an asking price.
var err error
assetRate, _, err := n.queryAskFromPriceOracle(
rate, _, err := n.queryAskFromPriceOracle(
order.Peer, order.AssetID, order.AssetGroupKey,
order.MaxAssetAmount,
fn.None[rfqmsg.BigIntFixedPoint](),
Expand All @@ -473,17 +472,12 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {
return
}

// TODO(ffranr): This is a temporary solution which will
// be re-written once RFQ quote request messages are
// updated to include a suggested asset rate.
askPrice = lnwire.MilliSatoshi(
assetRate.Coefficient.ToUint64(),
)
assetRate = fn.Some[rfqmsg.BigIntFixedPoint](*rate)
}

request, err := rfqmsg.NewSellRequest(
*order.Peer, order.AssetID, order.AssetGroupKey,
order.MaxAssetAmount, askPrice,
order.MaxAssetAmount, assetRate,
)
if err != nil {
err := fmt.Errorf("unable to create sell request "+
Expand Down
7 changes: 4 additions & 3 deletions rfqmsg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@ func newRequestWireMsgDataFromSell(q SellRequest) requestWireMsgData {
assetMaxAmount := tlv.NewPrimitiveRecord[tlv.TlvType3](q.AssetAmount)

var suggestedRateTick requestSuggestedTickRate
if uint64(q.AskPrice) != 0 {
q.SuggestedAssetRate.WhenSome(func(rate BigIntFixedPoint) {
suggestedRateTick = tlv.SomeRecordT[tlv.TlvType4](
// TODO(ffranr): Temp solution.
tlv.NewPrimitiveRecord[tlv.TlvType4](
uint64(q.AskPrice),
rate.Coefficient.ToUint64(),
),
)
}
})

// We are constructing a sell request. Therefore, the requesting peer's
// outbound asset is the taproot asset, and the inbound asset is BTC.
Expand Down
53 changes: 29 additions & 24 deletions rfqmsg/sell_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/tlv"
)
Expand Down Expand Up @@ -40,18 +40,19 @@ type SellRequest struct {
// peer intends to sell.
AssetAmount uint64

// AskPrice is the peer's proposed ask price for the asset amount. This
// is not the final price, but a suggested price that the requesting
// peer is willing to accept.
AskPrice lnwire.MilliSatoshi
// SuggestedAssetRate represents a proposed conversion rate between the
// subject asset and BTC. This rate is an initial suggestion intended to
// initiate the RFQ negotiation process and may differ from the final
// agreed rate.
SuggestedAssetRate fn.Option[BigIntFixedPoint]

// TODO(ffranr): Add expiry time for suggested ask price.
}

// NewSellRequest creates a new asset sell quote request.
func NewSellRequest(peer route.Vertex, assetID *asset.ID,
assetGroupKey *btcec.PublicKey, assetAmount uint64,
askPrice lnwire.MilliSatoshi) (*SellRequest, error) {
suggestedAssetRate fn.Option[BigIntFixedPoint]) (*SellRequest, error) {

var id [32]byte
_, err := rand.Read(id[:])
Expand All @@ -60,13 +61,13 @@ func NewSellRequest(peer route.Vertex, assetID *asset.ID,
}

return &SellRequest{
Peer: peer,
Version: latestSellRequestVersion,
ID: id,
AssetID: assetID,
AssetGroupKey: assetGroupKey,
AssetAmount: assetAmount,
AskPrice: askPrice,
Peer: peer,
Version: latestSellRequestVersion,
ID: id,
AssetID: assetID,
AssetGroupKey: assetGroupKey,
AssetAmount: assetAmount,
SuggestedAssetRate: suggestedAssetRate,
}, nil
}

Expand Down Expand Up @@ -105,22 +106,25 @@ func NewSellRequestMsgFromWire(wireMsg WireMessage,
}

// Extract the suggested rate tick if provided.
var askPrice lnwire.MilliSatoshi
//
// TODO(ffranr): Temp solution.
var suggestedAssetRate fn.Option[BigIntFixedPoint]
msgData.SuggestedRateTick.WhenSome(
// nolint: lll
func(suggestedRateTick tlv.RecordT[tlv.TlvType4, uint64]) {
askPrice = lnwire.MilliSatoshi(suggestedRateTick.Val)
r := NewBigIntFixedPoint(suggestedRateTick.Val, 0)
suggestedAssetRate = fn.Some[BigIntFixedPoint](r)
},
)

req := SellRequest{
Peer: wireMsg.Peer,
Version: msgData.Version.Val,
ID: msgData.ID.Val,
AssetID: assetID,
AssetGroupKey: assetGroupKey,
AssetAmount: msgData.AssetMaxAmount.Val,
AskPrice: askPrice,
Peer: wireMsg.Peer,
Version: msgData.Version.Val,
ID: msgData.ID.Val,
AssetID: assetID,
AssetGroupKey: assetGroupKey,
AssetAmount: msgData.AssetMaxAmount.Val,
SuggestedAssetRate: suggestedAssetRate,
}

// Perform basic sanity checks on the quote request.
Expand Down Expand Up @@ -192,8 +196,9 @@ func (q *SellRequest) String() string {
}

return fmt.Sprintf("SellRequest(peer=%x, id=%x, asset_id=%s, "+
"asset_group_key=%x, asset_amount=%d, ask_price=%d)", q.Peer[:],
q.ID[:], q.AssetID, groupKeyBytes, q.AssetAmount, q.AskPrice)
"asset_group_key=%x, asset_amount=%d, ask_asset_rate=%v)",
q.Peer[:], q.ID[:], q.AssetID, groupKeyBytes, q.AssetAmount,
q.SuggestedAssetRate)
}

// Ensure that the message type implements the OutgoingMsg interface.
Expand Down

0 comments on commit 92c00cd

Please sign in to comment.