Skip to content

Commit 519e499

Browse files
committed
multi: refactor asset rate fields in BuyAccept
Refactor BuyAccept by replacing multiple fields with a single asset rate field using the new AssetRate type.
1 parent 987b1c9 commit 519e499

File tree

9 files changed

+52
-47
lines changed

9 files changed

+52
-47
lines changed

rfq/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ func (m *Manager) PeerAcceptedBuyQuotes() BuyAcceptMap {
788788
buyQuotesCopy := make(map[SerialisedScid]rfqmsg.BuyAccept)
789789
m.peerAcceptedBuyQuotes.ForEach(
790790
func(scid SerialisedScid, accept rfqmsg.BuyAccept) error {
791-
if time.Now().Unix() > int64(accept.Expiry) {
791+
if time.Now().After(accept.AssetRate.Expiry) {
792792
m.peerAcceptedBuyQuotes.Delete(scid)
793793
return nil
794794
}
@@ -832,7 +832,7 @@ func (m *Manager) LocalAcceptedBuyQuotes() BuyAcceptMap {
832832
buyQuotesCopy := make(map[SerialisedScid]rfqmsg.BuyAccept)
833833
m.localAcceptedBuyQuotes.ForEach(
834834
func(scid SerialisedScid, accept rfqmsg.BuyAccept) error {
835-
if time.Now().Unix() > int64(accept.Expiry) {
835+
if time.Now().After(accept.AssetRate.Expiry) {
836836
m.localAcceptedBuyQuotes.Delete(scid)
837837
return nil
838838
}

rfq/negotiator.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,7 @@ func (n *Negotiator) HandleIncomingBuyRequest(
346346
}
347347

348348
// Construct and send a buy accept message.
349-
expiry := uint64(assetRate.Expiry.Unix())
350-
msg := rfqmsg.NewBuyAcceptFromRequest(
351-
request, assetRate.Rate, expiry,
352-
)
349+
msg := rfqmsg.NewBuyAcceptFromRequest(request, *assetRate)
353350
sendOutgoingMsg(msg)
354351
}()
355352

@@ -549,10 +546,12 @@ func (n *Negotiator) HandleIncomingBuyAccept(msg rfqmsg.BuyAccept,
549546
// TODO(ffranr): Sanity check the buy accept quote expiry
550547
// timestamp given the expiry timestamp provided by the price
551548
// oracle.
552-
if !expiryWithinBounds(msg.Expiry, minAssetRatesExpiryLifetime) {
549+
expiry := uint64(msg.AssetRate.Expiry.Unix())
550+
if !expiryWithinBounds(expiry, minAssetRatesExpiryLifetime) {
553551
// The expiry time is not within the acceptable bounds.
554552
log.Debugf("Buy accept quote expiry time is not within "+
555-
"acceptable bounds (expiry=%d)", msg.Expiry)
553+
"acceptable bounds (asset_rate=%s)",
554+
msg.AssetRate.String())
556555

557556
// Construct an invalid quote response event so that we can
558557
// inform the peer that the quote response has not validated
@@ -633,17 +632,17 @@ func (n *Negotiator) HandleIncomingBuyAccept(msg rfqmsg.BuyAccept,
633632
tolerance := rfqmath.NewBigIntFromUint64(
634633
n.cfg.AcceptPriceDeviationPpm,
635634
)
636-
acceptablePrice := msg.AssetRate.WithinTolerance(
635+
acceptablePrice := msg.AssetRate.Rate.WithinTolerance(
637636
assetRate.Rate, tolerance,
638637
)
639638
if !acceptablePrice {
640639
// The price is not within the acceptable tolerance.
641640
// We will return without calling the quote accept
642641
// callback.
643642
log.Debugf("Buy accept price is not within "+
644-
"acceptable bounds (ask_asset_rate=%v, "+
645-
"oracle_asset_rate=%v)", msg.AssetRate,
646-
assetRate)
643+
"acceptable bounds (peer_asset_rate=%s, "+
644+
"oracle_asset_rate=%s)", msg.AssetRate.String(),
645+
assetRate.String())
647646

648647
// Construct an invalid quote response event so that we
649648
// can inform the peer that the quote response has not

rfq/order.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ func NewAssetSalePolicy(quote rfqmsg.BuyAccept) *AssetSalePolicy {
108108
AssetSpecifier: quote.Request.AssetSpecifier,
109109
AcceptedQuoteId: quote.ID,
110110
MaxOutboundAssetAmount: quote.Request.AssetMaxAmt,
111-
AskAssetRate: quote.AssetRate,
112-
expiry: quote.Expiry,
111+
AskAssetRate: quote.AssetRate.Rate,
112+
expiry: uint64(quote.AssetRate.Expiry.Unix()),
113113
}
114114
}
115115

rfqmsg/accept.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ type acceptWireMsgData struct {
4343
func newAcceptWireMsgDataFromBuy(q BuyAccept) (acceptWireMsgData, error) {
4444
version := tlv.NewPrimitiveRecord[tlv.TlvType0](q.Version)
4545
id := tlv.NewRecordT[tlv.TlvType2](q.ID)
46-
expiry := tlv.NewPrimitiveRecord[tlv.TlvType4](q.Expiry)
46+
47+
expiryUnix := q.AssetRate.Expiry.Unix()
48+
expiry := tlv.NewPrimitiveRecord[tlv.TlvType4](uint64(expiryUnix))
49+
4750
sig := tlv.NewPrimitiveRecord[tlv.TlvType6](q.sig)
4851

4952
// The rate provided in the buy acceptance message represents the
5053
// exchange rate from the incoming asset to BTC.
51-
rate := NewTlvFixedPointFromBigInt(q.AssetRate)
54+
rate := NewTlvFixedPointFromBigInt(q.AssetRate.Rate)
5255
inAssetRate := tlv.NewRecordT[tlv.TlvType8](rate)
5356

5457
// Currently, only BTC is supported as the outgoing asset in buy

rfqmsg/buy_accept.go

Lines changed: 9 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 BuyAccept struct {
3030
ID ID
3131

3232
// AssetRate is the accepted asset to BTC rate.
33-
AssetRate rfqmath.BigIntFixedPoint
34-
35-
// Expiry is the asking 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
// NewBuyAcceptFromRequest creates a new instance of a quote accept message
4340
// given a quote request message.
44-
//
45-
// TODO(ffranr): Use new AssetRate type for assetRate arg.
4641
func NewBuyAcceptFromRequest(request BuyRequest,
47-
assetRate rfqmath.BigIntFixedPoint, expiry uint64) *BuyAccept {
42+
assetRate AssetRate) *BuyAccept {
4843

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

@@ -70,14 +64,16 @@ func newBuyAcceptFromWireMsg(wireMsg WireMessage,
7064
// currently assume that the out-asset is BTC.
7165
assetRate := msgData.InAssetRate.Val.IntoBigIntFixedPoint()
7266

67+
// Convert the unix timestamp in seconds to a time.Time.
68+
expiry := time.Unix(int64(msgData.Expiry.Val), 0)
69+
7370
return &BuyAccept{
7471
Peer: wireMsg.Peer,
7572
Request: request,
7673
Version: msgData.Version.Val,
7774
ID: msgData.ID.Val,
78-
Expiry: msgData.Expiry.Val,
75+
AssetRate: NewAssetRate(assetRate, expiry),
7976
sig: msgData.Sig.Val,
80-
AssetRate: assetRate,
8177
}, nil
8278
}
8379

@@ -128,9 +124,8 @@ func (q *BuyAccept) MsgID() ID {
128124

129125
// String returns a human-readable string representation of the message.
130126
func (q *BuyAccept) String() string {
131-
return fmt.Sprintf("BuyAccept(peer=%x, id=%x, ask_price=%d, "+
132-
"expiry=%d, scid=%d)",
133-
q.Peer[:], q.ID[:], q.AssetRate, q.Expiry, q.ShortChannelId())
127+
return fmt.Sprintf("BuyAccept(peer=%x, id=%x, asset_rate=%s, scid=%d)",
128+
q.Peer[:], q.ID[:], q.AssetRate.String(), q.ShortChannelId())
134129
}
135130

136131
// 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
@@ -6591,10 +6591,10 @@ func marshalPeerAcceptedBuyQuotes(
65916591
[]*rfqrpc.PeerAcceptedBuyQuote, 0, len(quotes),
65926592
)
65936593
for scid, quote := range quotes {
6594-
coefficient := quote.AssetRate.Coefficient.String()
6594+
coefficient := quote.AssetRate.Rate.Coefficient.String()
65956595
rpcAskAssetRate := &rfqrpc.FixedPoint{
65966596
Coefficient: coefficient,
6597-
Scale: uint32(quote.AssetRate.Scale),
6597+
Scale: uint32(quote.AssetRate.Rate.Scale),
65986598
}
65996599

66006600
rpcQuote := &rfqrpc.PeerAcceptedBuyQuote{
@@ -6603,7 +6603,7 @@ func marshalPeerAcceptedBuyQuotes(
66036603
Scid: uint64(scid),
66046604
AssetAmount: quote.Request.AssetMaxAmt,
66056605
AskAssetRate: rpcAskAssetRate,
6606-
Expiry: quote.Expiry,
6606+
Expiry: uint64(quote.AssetRate.Expiry.Unix()),
66076607
}
66086608
rpcQuotes = append(rpcQuotes, rpcQuote)
66096609
}

tapchannel/aux_invoice_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (s *AuxInvoiceManager) priceFromQuote(rfqID rfqmsg.ID) (
266266
log.Debugf("Found buy quote for ID %x / SCID %d: %#v", rfqID[:],
267267
rfqID.Scid(), buyQuote)
268268

269-
return &buyQuote.AssetRate, nil
269+
return &buyQuote.AssetRate.Rate, nil
270270

271271
// This is a direct peer payment, so we expect to find a sell quote.
272272
case isSell:

tapchannel/aux_invoice_manager_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ func (m *mockHtlcModifierProperty) HtlcModifier(ctx context.Context,
209209
m.t.Errorf("no rfq quote found")
210210
}
211211

212-
assetRate := lnwire.MilliSatoshi(quote.AssetRate.ToUint64())
212+
assetRate := lnwire.MilliSatoshi(
213+
quote.AssetRate.Rate.ToUint64(),
214+
)
213215
msatPerBtc := float64(btcutil.SatoshiPerBitcoin * 1000)
214216
unitValue := msatPerBtc / float64(assetRate)
215217
assetUnits := lnwire.MilliSatoshi(htlc.Amounts.Val.Sum())
@@ -344,8 +346,10 @@ func TestAuxInvoiceManager(t *testing.T) {
344346
},
345347
buyQuotes: rfq.BuyAcceptMap{
346348
fn.Ptr(dummyRfqID(31)).Scid(): {
347-
Peer: testNodeID,
348-
AssetRate: testAssetRate,
349+
Peer: testNodeID,
350+
AssetRate: rfqmsg.NewAssetRate(
351+
testAssetRate, time.Now(),
352+
),
349353
},
350354
},
351355
},
@@ -376,8 +380,10 @@ func TestAuxInvoiceManager(t *testing.T) {
376380
},
377381
buyQuotes: rfq.BuyAcceptMap{
378382
fn.Ptr(dummyRfqID(31)).Scid(): {
379-
Peer: testNodeID,
380-
AssetRate: testAssetRate,
383+
Peer: testNodeID,
384+
AssetRate: rfqmsg.NewAssetRate(
385+
testAssetRate, time.Now(),
386+
),
381387
},
382388
},
383389
},
@@ -664,12 +670,14 @@ func genBuyQuotes(t *rapid.T, rfqMap rfq.BuyAcceptMap, units, amtMsat uint64,
664670
)
665671
}
666672

673+
rateFp := rfqmath.FixedPoint[rfqmath.BigInt]{
674+
Coefficient: rfqmath.NewBigInt(assetRate),
675+
Scale: 0,
676+
}
677+
667678
rfqMap[rfqScid.Scid()] = rfqmsg.BuyAccept{
668-
Peer: peer,
669-
AssetRate: rfqmath.FixedPoint[rfqmath.BigInt]{
670-
Coefficient: rfqmath.NewBigInt(assetRate),
671-
Scale: 0,
672-
},
679+
Peer: peer,
680+
AssetRate: rfqmsg.NewAssetRate(rateFp, time.Now()),
673681
}
674682
}
675683

taprpc/marshal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,10 @@ func MarshalAcceptedBuyQuoteEvent(
594594
Scid: uint64(event.ShortChannelId()),
595595
AssetAmount: event.Request.AssetMaxAmt,
596596
AskAssetRate: &rfqrpc.FixedPoint{
597-
Coefficient: event.AssetRate.Coefficient.String(),
598-
Scale: uint32(event.AssetRate.Scale),
597+
Coefficient: event.AssetRate.Rate.Coefficient.String(),
598+
Scale: uint32(event.AssetRate.Rate.Scale),
599599
},
600-
Expiry: event.Expiry,
600+
Expiry: uint64(event.AssetRate.Expiry.Unix()),
601601
}, nil
602602
}
603603

0 commit comments

Comments
 (0)