Skip to content

Commit

Permalink
multi: replace SellAccept.BidPrice with AssetRate
Browse files Browse the repository at this point in the history
This commit replaces the SellAccept.BidPrice field with
AssetRate, changing a `uint64` price to an asset-to-BTC rate
represented as a fixed-point number.
  • Loading branch information
ffranr committed Oct 3, 2024
1 parent 6fbf2d6 commit 73c30e0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 52 deletions.
30 changes: 11 additions & 19 deletions rfq/negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package rfq
import (
"fmt"
"math"
"math/big"
"sync"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire"
Expand Down Expand Up @@ -425,16 +427,8 @@ func (n *Negotiator) HandleIncomingSellRequest(
}

// Construct and send a sell accept message.
//
// 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.
bidPrice := lnwire.MilliSatoshi(
assetRate.Coefficient.ToUint64(),
)

msg := rfqmsg.NewSellAcceptFromRequest(
request, bidPrice, rateExpiry,
request, *assetRate, rateExpiry,
)
sendOutgoingMsg(msg)
}()
Expand Down Expand Up @@ -760,24 +754,22 @@ func (n *Negotiator) HandleIncomingSellAccept(msg rfqmsg.SellAccept,
return
}

// TODO(ffranr): Temp solution.
oraclePrice := lnwire.MilliSatoshi(
assetRate.Coefficient.ToUint64(),
)

// Ensure that the peer provided price is reasonable given the
// price provided by the price oracle service.
acceptablePrice := pricesWithinBounds(
msg.BidPrice, oraclePrice,
n.cfg.AcceptPriceDeviationPpm,
tolerance := rfqmath.NewBigInt(
big.NewInt(0).SetUint64(n.cfg.AcceptPriceDeviationPpm),
)
acceptablePrice := msg.AssetRate.WithinTolerance(
*assetRate, tolerance,
)
if !acceptablePrice {
// The price is not within the acceptable bounds.
// We will return without calling the quote accept
// callback.
log.Debugf("Sell accept quote price is not within "+
"acceptable bounds (peer_price=%d, "+
"oracle_price=%d)", msg.BidPrice, oraclePrice)
"acceptable bounds (asset_rate=%v, "+
"oracle_asset_rate=%v)", msg.AssetRate,
assetRate)

// Construct an invalid quote response event so that we
// can inform the peer that the quote response has not
Expand Down
5 changes: 3 additions & 2 deletions rfq/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ func NewAssetPurchasePolicy(quote rfqmsg.SellAccept) *AssetPurchasePolicy {
scid: quote.ShortChannelId(),
AcceptedQuoteId: quote.ID,
AssetAmount: quote.Request.AssetAmount,
BidPrice: quote.BidPrice,
expiry: quote.Expiry,
// TODO(ffranr): Temp solution.
BidPrice: lnwire.MilliSatoshi(quote.AssetRate.ToUint64()),
expiry: quote.Expiry,
}
}

Expand Down
3 changes: 2 additions & 1 deletion rfqmsg/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func newAcceptWireMsgDataFromSell(q SellAccept) acceptWireMsgData {
// request, we set the out-in rate tick instead of the in-out rate tick.
outInRateTick := tlv.SomeRecordT[tlv.TlvType5](
tlv.NewPrimitiveRecord[tlv.TlvType5](
uint64(q.BidPrice),
// TODO(ffranr): Temp solution.
q.AssetRate.Coefficient.ToUint64(),
),
)

Expand Down
45 changes: 22 additions & 23 deletions rfqmsg/sell_accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rfqmsg
import (
"fmt"

"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/tlv"
)
Expand All @@ -30,9 +29,8 @@ type SellAccept struct {
// message that this response is associated with.
ID ID

// BidPrice is the bid price that the message author is willing to pay
// for the asset that is for sale.
BidPrice lnwire.MilliSatoshi
// AssetRate is the accepted asset to BTC rate.
AssetRate BigIntFixedPoint

// Expiry is the bid price expiry lifetime unix timestamp.
Expiry uint64
Expand All @@ -43,16 +41,16 @@ type SellAccept struct {

// NewSellAcceptFromRequest creates a new instance of an asset sell quote accept
// message given an asset sell quote request message.
func NewSellAcceptFromRequest(request SellRequest, bidPrice lnwire.MilliSatoshi,
func NewSellAcceptFromRequest(request SellRequest, assetRate BigIntFixedPoint,
expiry uint64) *SellAccept {

return &SellAccept{
Peer: request.Peer,
Request: request,
Version: latestSellAcceptVersion,
ID: request.ID,
BidPrice: bidPrice,
Expiry: expiry,
Peer: request.Peer,
Request: request,
Version: latestSellAcceptVersion,
ID: request.ID,
AssetRate: assetRate,
Expiry: expiry,
}
}

Expand All @@ -72,23 +70,24 @@ func newSellAcceptFromWireMsg(wireMsg WireMessage,
// field (and not the in-out rate tick field) because this is the rate
// tick field populated in response to a peer initiated sell quote
// request.
var bidPrice lnwire.MilliSatoshi
var assetRate BigIntFixedPoint
msgData.OutInRateTick.WhenSome(
func(rate tlv.RecordT[tlv.TlvType5, uint64]) {
bidPrice = lnwire.MilliSatoshi(rate.Val)
// TODO(ffranr): Temp solution.
assetRate = NewBigIntFixedPoint(rate.Val, 0)
},
)

// Note that the `Request` field is populated later in the RFQ stream
// service.
return &SellAccept{
Peer: wireMsg.Peer,
Request: request,
Version: msgData.Version.Val,
ID: msgData.ID.Val,
BidPrice: bidPrice,
Expiry: msgData.Expiry.Val,
sig: msgData.Sig.Val,
Peer: wireMsg.Peer,
Request: request,
Version: msgData.Version.Val,
ID: msgData.ID.Val,
AssetRate: assetRate,
Expiry: msgData.Expiry.Val,
sig: msgData.Sig.Val,
}, nil
}

Expand Down Expand Up @@ -135,9 +134,9 @@ func (q *SellAccept) MsgID() ID {

// String returns a human-readable string representation of the message.
func (q *SellAccept) String() string {
return fmt.Sprintf("SellAccept(peer=%x, id=%x, bid_price=%d, "+
"expiry=%d, scid=%d)", q.Peer[:], q.ID[:], q.BidPrice, q.Expiry,
q.ShortChannelId())
return fmt.Sprintf("SellAccept(peer=%x, id=%x, bid_asset_rate=%v, "+
"expiry=%d, scid=%d)", q.Peer[:], q.ID[:], q.AssetRate,
q.Expiry, q.ShortChannelId())
}

// Ensure that the message type implements the OutgoingMsg interface.
Expand Down
5 changes: 3 additions & 2 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6632,8 +6632,9 @@ func marshalPeerAcceptedSellQuotes(
Id: quote.ID[:],
Scid: uint64(scid),
AssetAmount: quote.Request.AssetAmount,
BidPrice: uint64(quote.BidPrice),
Expiry: quote.Expiry,
// TODO(ffranr): Temp solution.
BidPrice: quote.AssetRate.ToUint64(),
Expiry: quote.Expiry,
}
rpcQuotes = append(rpcQuotes, rpcQuote)
}
Expand Down
3 changes: 2 additions & 1 deletion tapchannel/aux_invoice_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ func (s *AuxInvoiceManager) priceFromQuote(rfqID rfqmsg.ID) (
log.Debugf("Found sell quote for ID %x / SCID %d: %#v",
rfqID[:], rfqID.Scid(), sellQuote)

return sellQuote.BidPrice, nil
// TODO(ffranr): Temp solution.
return lnwire.MilliSatoshi(sellQuote.AssetRate.ToUint64()), nil

default:
return 0, fmt.Errorf("no accepted quote found for RFQ SCID "+
Expand Down
7 changes: 5 additions & 2 deletions tapchannel/aux_traffic_shaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ func (s *AuxTrafficShaper) PaymentBandwidth(htlcBlob,
"%x (SCID %d)", rfqID[:], rfqID.Scid())
}

mSatPerAssetUnit := quote.BidPrice
// TODO(ffranr): Temp solution.
mSatPerAssetUnit := lnwire.MilliSatoshi(quote.AssetRate.ToUint64())

// At this point we have acquired what we need to express the asset
// bandwidth expressed in satoshis. Before we return the result, we need
Expand Down Expand Up @@ -285,7 +286,9 @@ func (s *AuxTrafficShaper) ProduceHtlcExtraData(totalAmount lnwire.MilliSatoshi,
// nearest 10 units to avoid more than half an asset unit of rounding
// error that we would get if we did normal integer division (rounding
// down).
mSatPerAssetUnit := quote.BidPrice
//
// TODO(ffranr): Temp solution.
mSatPerAssetUnit := lnwire.MilliSatoshi(quote.AssetRate.ToUint64())
numAssetUnits := uint64(totalAmount*10/mSatPerAssetUnit) / 10

// We now know how many units we need. We take the asset ID from the
Expand Down
5 changes: 3 additions & 2 deletions taprpc/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,9 @@ func MarshalAcceptedSellQuoteEvent(
Id: event.ID[:],
Scid: uint64(event.ShortChannelId()),
AssetAmount: event.Request.AssetAmount,
BidPrice: uint64(event.BidPrice),
Expiry: event.Expiry,
// TODO(ffranr): Temp solution.
BidPrice: event.AssetRate.ToUint64(),
Expiry: event.Expiry,
}
}

Expand Down

0 comments on commit 73c30e0

Please sign in to comment.