Skip to content

Commit 118fdf3

Browse files
committed
loopout/asset: add asset rate
1 parent 2955081 commit 118fdf3

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightninglabs/loop/sweep"
2121
"github.com/lightninglabs/loop/sweepbatcher"
2222
"github.com/lightninglabs/loop/utils"
23+
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
2324
"github.com/lightningnetwork/lnd/lntypes"
2425
"github.com/lightningnetwork/lnd/routing/route"
2526
"google.golang.org/grpc"
@@ -661,6 +662,10 @@ func (s *Client) LoopOutQuote(ctx context.Context,
661662
// If we use an Asset we'll rfq to get the asset amounts to use for
662663
// the swap.
663664
if request.AssetRFQRequest != nil {
665+
if s.assetClient == nil {
666+
return nil, errors.New("asset client must be set " +
667+
"when trying to loop out with an asset")
668+
}
664669
rfqReq := request.AssetRFQRequest
665670
if rfqReq.Expiry == 0 {
666671
rfqReq.Expiry = time.Now().Add(defaultRFQExpiry).Unix()
@@ -680,6 +685,13 @@ func (s *Client) LoopOutQuote(ctx context.Context,
680685
return nil, err
681686
}
682687

688+
prepayAssetRate, err := rfqrpc.UnmarshalFixedPoint(
689+
prepayRfq.BidAssetRate,
690+
)
691+
if err != nil {
692+
return nil, err
693+
}
694+
683695
// The actual invoice swap amount is the requested amount plus
684696
// the swap fee minus the prepay amount.
685697
invoiceAmt := request.Amount + quote.SwapFee -
@@ -694,6 +706,13 @@ func (s *Client) LoopOutQuote(ctx context.Context,
694706
return nil, err
695707
}
696708

709+
swapAssetRate, err := rfqrpc.UnmarshalFixedPoint(
710+
swapRfq.BidAssetRate,
711+
)
712+
if err != nil {
713+
return nil, err
714+
}
715+
697716
// We'll also want the asset name to verify for the client.
698717
assetName, err := s.assetClient.GetAssetName(
699718
ctx, rfqReq.AssetId,
@@ -705,8 +724,10 @@ func (s *Client) LoopOutQuote(ctx context.Context,
705724
loopOutQuote.LoopOutRfq = &LoopOutRfq{
706725
PrepayRfqId: prepayRfq.Id,
707726
MaxPrepayAssetAmt: prepayRfq.AssetAmount,
727+
PrepayAssetRate: prepayAssetRate,
708728
SwapRfqId: swapRfq.Id,
709729
MaxSwapAssetAmt: swapRfq.AssetAmount,
730+
SwapAssetRate: swapAssetRate,
710731
AssetName: assetName,
711732
}
712733
}

interface.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/btcsuite/btcd/btcutil"
77
"github.com/lightninglabs/loop/loopdb"
88
"github.com/lightninglabs/loop/swap"
9+
"github.com/lightninglabs/taproot-assets/rfqmath"
910
"github.com/lightningnetwork/lnd/lntypes"
1011
"github.com/lightningnetwork/lnd/routing/route"
1112
"github.com/lightningnetwork/lnd/zpay32"
@@ -228,13 +229,20 @@ type LoopOutRfq struct {
228229
// used to pay for the prepay invoice.
229230
MaxPrepayAssetAmt uint64
230231

232+
// PrepayAssetRate is the rate at which the asset is exchanged for
233+
// bitcoin.
234+
PrepayAssetRate *rfqmath.BigIntFixedPoint
235+
231236
// SwapRfqId is the ID of the swap RFQ.
232237
SwapRfqId []byte
233238

234239
// MaxSwapAssetAmt is the maxmimum amount of the asset that will be used
235240
// to pay for the swap invoice.
236241
MaxSwapAssetAmt uint64
237242

243+
// SwapAssetRate is the rate at which the asset is exchanged for bitcoin.
244+
SwapAssetRate *rfqmath.BigIntFixedPoint
245+
238246
// AssetName is the human readable name of the asset.
239247
AssetName string
240248
}

loopd/swapclient_server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,16 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
804804
response.AssetRfqInfo = &looprpc.AssetRfqInfo{
805805
PrepayRfqId: quote.LoopOutRfq.PrepayRfqId,
806806
MaxPrepayAssetAmt: quote.LoopOutRfq.MaxPrepayAssetAmt,
807+
PrepayAssetRate: &looprpc.FixedPoint{
808+
Scale: uint32(quote.LoopOutRfq.PrepayAssetRate.Scale),
809+
Coefficient: quote.LoopOutRfq.PrepayAssetRate.Coefficient.String(),
810+
},
807811
SwapRfqId: quote.LoopOutRfq.SwapRfqId,
808812
MaxSwapAssetAmt: quote.LoopOutRfq.MaxSwapAssetAmt,
813+
SwapAssetRate: &looprpc.FixedPoint{
814+
Scale: uint32(quote.LoopOutRfq.SwapAssetRate.Scale),
815+
Coefficient: quote.LoopOutRfq.SwapAssetRate.Coefficient.String(),
816+
},
809817
AssetName: quote.LoopOutRfq.AssetName,
810818
}
811819
}

0 commit comments

Comments
 (0)