Skip to content

Commit 2702cb2

Browse files
committed
multi: refactor asset rate fields in SellAccept
Refactor SellAccept by replacing multiple fields with a single asset rate field using the new AssetRate type.
1 parent 46ab13c commit 2702cb2

File tree

9 files changed

+34
-36
lines changed

9 files changed

+34
-36
lines changed

rfq/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ func (m *Manager) PeerAcceptedSellQuotes() SellAcceptMap {
826826
sellQuotesCopy := make(map[SerialisedScid]rfqmsg.SellAccept)
827827
m.peerAcceptedSellQuotes.ForEach(
828828
func(scid SerialisedScid, accept rfqmsg.SellAccept) error {
829-
if time.Now().Unix() > int64(accept.Expiry) {
829+
if time.Now().After(accept.AssetRate.Expiry) {
830830
m.peerAcceptedSellQuotes.Delete(scid)
831831
return nil
832832
}
@@ -870,7 +870,7 @@ func (m *Manager) LocalAcceptedSellQuotes() SellAcceptMap {
870870
sellQuotesCopy := make(map[SerialisedScid]rfqmsg.SellAccept)
871871
m.localAcceptedSellQuotes.ForEach(
872872
func(scid SerialisedScid, accept rfqmsg.SellAccept) error {
873-
if time.Now().Unix() > int64(accept.Expiry) {
873+
if time.Now().After(accept.AssetRate.Expiry) {
874874
m.localAcceptedSellQuotes.Delete(scid)
875875
return nil
876876
}

rfq/negotiator.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,7 @@ func (n *Negotiator) HandleIncomingSellRequest(
444444
}
445445

446446
// Construct and send a sell accept message.
447-
expiry := uint64(assetRate.Expiry.Unix())
448-
msg := rfqmsg.NewSellAcceptFromRequest(
449-
request, assetRate.Rate, expiry,
450-
)
447+
msg := rfqmsg.NewSellAcceptFromRequest(request, *assetRate)
451448
sendOutgoingMsg(msg)
452449
}()
453450

@@ -677,10 +674,12 @@ func (n *Negotiator) HandleIncomingSellAccept(msg rfqmsg.SellAccept,
677674
//
678675
// TODO(ffranr): Sanity check the quote expiry timestamp given
679676
// the expiry timestamp provided by the price oracle.
680-
if !expiryWithinBounds(msg.Expiry, minAssetRatesExpiryLifetime) {
677+
expiry := uint64(msg.AssetRate.Expiry.Unix())
678+
if !expiryWithinBounds(expiry, minAssetRatesExpiryLifetime) {
681679
// The expiry time is not within the acceptable bounds.
682680
log.Debugf("Sell accept quote expiry time is not within "+
683-
"acceptable bounds (expiry=%d)", msg.Expiry)
681+
"acceptable bounds (asset_rate=%s)",
682+
msg.AssetRate.String())
684683

685684
// Construct an invalid quote response event so that we can
686685
// inform the peer that the quote response has not validated
@@ -760,7 +759,7 @@ func (n *Negotiator) HandleIncomingSellAccept(msg rfqmsg.SellAccept,
760759
tolerance := rfqmath.NewBigIntFromUint64(
761760
n.cfg.AcceptPriceDeviationPpm,
762761
)
763-
acceptablePrice := msg.AssetRate.WithinTolerance(
762+
acceptablePrice := msg.AssetRate.Rate.WithinTolerance(
764763
assetRate.Rate, tolerance,
765764
)
766765
if !acceptablePrice {

rfq/order.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ func NewAssetPurchasePolicy(quote rfqmsg.SellAccept) *AssetPurchasePolicy {
262262
scid: quote.ShortChannelId(),
263263
AssetSpecifier: quote.Request.AssetSpecifier,
264264
AcceptedQuoteId: quote.ID,
265-
BidAssetRate: quote.AssetRate,
265+
BidAssetRate: quote.AssetRate.Rate,
266266
PaymentMaxAmt: quote.Request.PaymentMaxAmt,
267-
expiry: quote.Expiry,
267+
expiry: uint64(quote.AssetRate.Expiry.Unix()),
268268
}
269269
}
270270

rfqmsg/accept.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ func newAcceptWireMsgDataFromBuy(q BuyAccept) (acceptWireMsgData, error) {
7676
func newAcceptWireMsgDataFromSell(q SellAccept) (acceptWireMsgData, error) {
7777
version := tlv.NewPrimitiveRecord[tlv.TlvType0](q.Version)
7878
id := tlv.NewRecordT[tlv.TlvType2](q.ID)
79-
expiry := tlv.NewPrimitiveRecord[tlv.TlvType4](q.Expiry)
79+
80+
expiryUnix := q.AssetRate.Expiry.Unix()
81+
expiry := tlv.NewPrimitiveRecord[tlv.TlvType4](uint64(expiryUnix))
82+
8083
sig := tlv.NewPrimitiveRecord[tlv.TlvType6](q.sig)
8184

8285
// Currently, only BTC is supported as the incoming asset in sell
@@ -87,7 +90,7 @@ func newAcceptWireMsgDataFromSell(q SellAccept) (acceptWireMsgData, error) {
8790

8891
// The rate provided in the sell acceptance message represents the
8992
// exchange rate from the outgoing asset to BTC.
90-
rate := NewTlvFixedPointFromBigInt(q.AssetRate)
93+
rate := NewTlvFixedPointFromBigInt(q.AssetRate.Rate)
9194
outAssetRate := tlv.NewRecordT[tlv.TlvType10](rate)
9295

9396
// Encode message data component as TLV bytes.

rfqmsg/sell_accept.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package rfqmsg
22

33
import (
44
"fmt"
5+
"time"
56

6-
"github.com/lightninglabs/taproot-assets/rfqmath"
77
"github.com/lightningnetwork/lnd/routing/route"
88
)
99

@@ -30,29 +30,23 @@ type SellAccept struct {
3030
ID ID
3131

3232
// AssetRate is the accepted asset to BTC rate.
33-
AssetRate rfqmath.BigIntFixedPoint
34-
35-
// Expiry is the bid price expiry lifetime unix timestamp.
36-
Expiry uint64
33+
AssetRate AssetRate
3734

3835
// sig is a signature over the serialized contents of the message.
3936
sig [64]byte
4037
}
4138

4239
// NewSellAcceptFromRequest creates a new instance of an asset sell quote accept
4340
// message given an asset sell quote request message.
44-
//
45-
// // TODO(ffranr): Use new AssetRate type for assetRate arg.
4641
func NewSellAcceptFromRequest(request SellRequest,
47-
assetRate rfqmath.BigIntFixedPoint, expiry uint64) *SellAccept {
42+
assetRate AssetRate) *SellAccept {
4843

4944
return &SellAccept{
5045
Peer: request.Peer,
5146
Request: request,
5247
Version: latestSellAcceptVersion,
5348
ID: request.ID,
5449
AssetRate: assetRate,
55-
Expiry: expiry,
5650
}
5751
}
5852

@@ -72,15 +66,17 @@ func newSellAcceptFromWireMsg(wireMsg WireMessage,
7266
// currently assume that the in-asset is BTC.
7367
assetRate := msgData.OutAssetRate.Val.IntoBigIntFixedPoint()
7468

69+
// Convert the unix timestamp in seconds to a time.Time.
70+
expiry := time.Unix(int64(msgData.Expiry.Val), 0).UTC()
71+
7572
// Note that the `Request` field is populated later in the RFQ stream
7673
// service.
7774
return &SellAccept{
7875
Peer: wireMsg.Peer,
7976
Request: request,
8077
Version: msgData.Version.Val,
8178
ID: msgData.ID.Val,
82-
AssetRate: assetRate,
83-
Expiry: msgData.Expiry.Val,
79+
AssetRate: NewAssetRate(assetRate, expiry),
8480
sig: msgData.Sig.Val,
8581
}, nil
8682
}
@@ -133,9 +129,9 @@ func (q *SellAccept) MsgID() ID {
133129

134130
// String returns a human-readable string representation of the message.
135131
func (q *SellAccept) String() string {
136-
return fmt.Sprintf("SellAccept(peer=%x, id=%x, bid_asset_rate=%v, "+
137-
"expiry=%d, scid=%d)", q.Peer[:], q.ID[:], q.AssetRate,
138-
q.Expiry, q.ShortChannelId())
132+
return fmt.Sprintf("SellAccept(peer=%x, id=%x, asset_rate=%s, "+
133+
"scid=%d)", q.Peer[:], q.ID[:], q.AssetRate.String(),
134+
q.ShortChannelId())
139135
}
140136

141137
// Ensure that the message type implements the OutgoingMsg interface.

rpcserver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6630,8 +6630,8 @@ func marshalPeerAcceptedSellQuotes(quotes map[rfq.SerialisedScid]rfqmsg.SellAcce
66306630
rpcQuotes := make([]*rfqrpc.PeerAcceptedSellQuote, 0, len(quotes))
66316631
for scid, quote := range quotes {
66326632
rpcAssetRate := &rfqrpc.FixedPoint{
6633-
Coefficient: quote.AssetRate.Coefficient.String(),
6634-
Scale: uint32(quote.AssetRate.Scale),
6633+
Coefficient: quote.AssetRate.Rate.Coefficient.String(),
6634+
Scale: uint32(quote.AssetRate.Rate.Scale),
66356635
}
66366636

66376637
// TODO(ffranr): Add SellRequest payment max amount to
@@ -6641,7 +6641,7 @@ func marshalPeerAcceptedSellQuotes(quotes map[rfq.SerialisedScid]rfqmsg.SellAcce
66416641
Id: quote.ID[:],
66426642
Scid: uint64(scid),
66436643
BidAssetRate: rpcAssetRate,
6644-
Expiry: quote.Expiry,
6644+
Expiry: uint64(quote.AssetRate.Expiry.Unix()),
66456645
}
66466646
rpcQuotes = append(rpcQuotes, rpcQuote)
66476647
}

tapchannel/aux_invoice_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func (s *AuxInvoiceManager) priceFromQuote(rfqID rfqmsg.ID) (
273273
log.Debugf("Found sell quote for ID %x / SCID %d: %#v",
274274
rfqID[:], rfqID.Scid(), sellQuote)
275275

276-
return &sellQuote.AssetRate, nil
276+
return &sellQuote.AssetRate.Rate, nil
277277

278278
default:
279279
return nil, fmt.Errorf("no accepted quote found for RFQ SCID "+

tapchannel/aux_traffic_shaper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (s *AuxTrafficShaper) PaymentBandwidth(htlcBlob,
224224
// expressed in milli-satoshis.
225225
localBalanceFp := rfqmath.NewBigIntFixedPoint(localBalance, 0)
226226
availableBalanceMsat := rfqmath.UnitsToMilliSatoshi(
227-
localBalanceFp, quote.AssetRate,
227+
localBalanceFp, quote.AssetRate.Rate,
228228
)
229229

230230
// At this point we have acquired what we need to express the asset
@@ -283,7 +283,7 @@ func (s *AuxTrafficShaper) ProduceHtlcExtraData(totalAmount lnwire.MilliSatoshi,
283283
// corresponding number of assets, then reduce the number of satoshis of
284284
// the HTLC to the bare minimum that can be materialized on chain.
285285
numAssetUnitsFp := rfqmath.MilliSatoshiToUnits(
286-
totalAmount, quote.AssetRate,
286+
totalAmount, quote.AssetRate.Rate,
287287
)
288288
numAssetUnits := numAssetUnitsFp.ScaleTo(0).ToUint64()
289289

taprpc/marshal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ func MarshalAcceptedSellQuoteEvent(
567567
error) {
568568

569569
rpcAssetRate := &rfqrpc.FixedPoint{
570-
Coefficient: event.AssetRate.Coefficient.String(),
571-
Scale: uint32(event.AssetRate.Scale),
570+
Coefficient: event.AssetRate.Rate.Coefficient.String(),
571+
Scale: uint32(event.AssetRate.Rate.Scale),
572572
}
573573

574574
// TODO(ffranr): Add SellRequest payment max amount to
@@ -578,7 +578,7 @@ func MarshalAcceptedSellQuoteEvent(
578578
Id: event.ID[:],
579579
Scid: uint64(event.ShortChannelId()),
580580
BidAssetRate: rpcAssetRate,
581-
Expiry: event.Expiry,
581+
Expiry: uint64(event.AssetRate.Expiry.Unix()),
582582
}, nil
583583
}
584584

0 commit comments

Comments
 (0)