Skip to content

Commit 32647a5

Browse files
committed
cmd/quote: add asset rate to display
1 parent 954d4d0 commit 32647a5

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"math/big"
67
"os"
78
"time"
89

910
"github.com/btcsuite/btcd/btcutil"
1011
"github.com/lightninglabs/loop"
1112
"github.com/lightninglabs/loop/looprpc"
13+
"github.com/lightninglabs/taproot-assets/rfqmath"
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,39 @@ 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+
// Return an error is the scale component of the fixed point is greater
352+
// than the max value of uint8.
353+
if fp.Scale > 255 {
354+
return nil, fmt.Errorf("scale value overflow: %v", fp.Scale)
355+
}
356+
scale := uint8(fp.Scale)
357+
358+
cBigInt := new(big.Int)
359+
cBigInt.SetString(fp.Coefficient, 10)
360+
361+
return &rfqmath.BigIntFixedPoint{
362+
Coefficient: rfqmath.NewBigInt(cBigInt),
363+
Scale: scale,
364+
}, nil
365+
}

0 commit comments

Comments
 (0)