Skip to content

Commit d0191d2

Browse files
committed
cmd/quote: add asset rate to display
1 parent 95aae0e commit d0191d2

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

cmd/loop/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ const (
103103
// Amount: 50 USD
104104
assetAmtFmt = "%-36s %12d %s\n"
105105

106+
// rateFmt formats an exchange rate into a one line string, intended to
107+
// prettify the terminal output. For Instance,
108+
// fmt.Printf(f, "Exchange rate:", rate, "USD")
109+
// prints out as,
110+
// Exchange rate: 0.0002 USD/SAT
111+
rateFmt = "%-36s %12.4f %s/SAT\n"
112+
106113
// blkFmt formats the number of blocks into a one line string, intended
107114
// to prettify the terminal output. For Instance,
108115
// fmt.Printf(f, "Conf target", target)

cmd/loop/quote.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/btcsuite/btcd/btcutil"
1010
"github.com/lightninglabs/loop"
1111
"github.com/lightninglabs/loop/looprpc"
12+
"github.com/lightninglabs/taproot-assets/rfqmath"
13+
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
14+
"github.com/lightningnetwork/lnd/lnwire"
1215
"github.com/lightningnetwork/lnd/routing/route"
1316
"github.com/urfave/cli"
1417
)
@@ -268,7 +271,19 @@ func printQuoteOutResp(req *looprpc.QuoteRequest,
268271
totalFee := resp.HtlcSweepFeeSat + resp.SwapFeeSat
269272

270273
if resp.AssetRfqInfo != nil {
274+
assetAmtSwap, err := getAssetAmt(
275+
req.Amt, resp.AssetRfqInfo.SwapAssetRate,
276+
)
277+
if err != nil {
278+
fmt.Printf("Error converting asset amount: %v\n", err)
279+
return
280+
}
281+
exchangeRate := float64(assetAmtSwap) / float64(req.Amt)
271282
fmt.Printf(assetAmtFmt, "Send off-chain:",
283+
assetAmtSwap, resp.AssetRfqInfo.AssetName)
284+
fmt.Printf(rateFmt, "Exchange rate:",
285+
exchangeRate, resp.AssetRfqInfo.AssetName)
286+
fmt.Printf(assetAmtFmt, "Limit Send off-chain:",
272287
resp.AssetRfqInfo.MaxSwapAssetAmt,
273288
resp.AssetRfqInfo.AssetName)
274289
} else {
@@ -288,7 +303,17 @@ func printQuoteOutResp(req *looprpc.QuoteRequest,
288303
fmt.Printf(satAmtFmt, "Estimated total fee:", totalFee)
289304
fmt.Println()
290305
if resp.AssetRfqInfo != nil {
306+
assetAmtPrepay, err := getAssetAmt(
307+
resp.PrepayAmtSat, resp.AssetRfqInfo.PrepayAssetRate,
308+
)
309+
if err != nil {
310+
fmt.Printf("Error converting asset amount: %v\n", err)
311+
return
312+
}
291313
fmt.Printf(assetAmtFmt, "No show penalty (prepay):",
314+
assetAmtPrepay,
315+
resp.AssetRfqInfo.AssetName)
316+
fmt.Printf(assetAmtFmt, "Limit no show penalty (prepay):",
292317
resp.AssetRfqInfo.MaxPrepayAssetAmt,
293318
resp.AssetRfqInfo.AssetName)
294319
} else {
@@ -302,3 +327,33 @@ func printQuoteOutResp(req *looprpc.QuoteRequest,
302327
time.Unix(int64(req.SwapPublicationDeadline), 0),
303328
)
304329
}
330+
331+
// getAssetAmt returns the asset amount for the given amount in satoshis and
332+
// the asset rate.
333+
func getAssetAmt(amt int64, assetRate *looprpc.FixedPoint) (
334+
uint64, error) {
335+
336+
askAssetRate, err := unmarshalFixedPoint(assetRate)
337+
if err != nil {
338+
return 0, err
339+
}
340+
341+
msatAmt := lnwire.MilliSatoshi((amt * 1000))
342+
343+
assetAmt := rfqmath.MilliSatoshiToUnits(msatAmt, *askAssetRate)
344+
345+
return assetAmt.ToUint64(), nil
346+
}
347+
348+
// unmarshalFixedPoint converts an RPC FixedPoint to a BigIntFixedPoint.
349+
func unmarshalFixedPoint(fp *looprpc.FixedPoint) (*rfqmath.BigIntFixedPoint,
350+
error) {
351+
352+
// convert the looprpc.FixedPoint to a rfqrpc.FixedPoint
353+
rfqrpcFP := &rfqrpc.FixedPoint{
354+
Coefficient: fp.Coefficient,
355+
Scale: fp.Scale,
356+
}
357+
358+
return rfqrpc.UnmarshalFixedPoint(rfqrpcFP)
359+
}

0 commit comments

Comments
 (0)