-
Notifications
You must be signed in to change notification settings - Fork 123
Minor asset loop out fixes #876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package assets | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/btcsuite/btcd/btcutil" | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| ) | ||
|
|
||
| func TestGetPaymentMaxAmount(t *testing.T) { | ||
| tests := []struct { | ||
| satAmount btcutil.Amount | ||
| feeLimitMultiplier float64 | ||
| expectedAmount lnwire.MilliSatoshi | ||
| expectError bool | ||
| }{ | ||
| { | ||
| satAmount: btcutil.Amount(250000), | ||
| feeLimitMultiplier: 1.2, | ||
| expectedAmount: lnwire.MilliSatoshi(300000000), | ||
| expectError: false, | ||
| }, | ||
| { | ||
| satAmount: btcutil.Amount(100000), | ||
| feeLimitMultiplier: 1.5, | ||
| expectedAmount: lnwire.MilliSatoshi(150000000), | ||
| expectError: false, | ||
| }, | ||
| { | ||
| satAmount: btcutil.Amount(50000), | ||
| feeLimitMultiplier: 2.0, | ||
| expectedAmount: lnwire.MilliSatoshi(100000000), | ||
| expectError: false, | ||
| }, | ||
| { | ||
| satAmount: btcutil.Amount(0), | ||
| feeLimitMultiplier: 1.2, | ||
| expectedAmount: lnwire.MilliSatoshi(0), | ||
| expectError: true, | ||
| }, | ||
| { | ||
| satAmount: btcutil.Amount(250000), | ||
| feeLimitMultiplier: 0.8, | ||
| expectedAmount: lnwire.MilliSatoshi(0), | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result, err := getPaymentMaxAmount( | ||
| test.satAmount, test.feeLimitMultiplier, | ||
| ) | ||
| if test.expectError { | ||
| if err == nil { | ||
| t.Fatalf("expected error but got none") | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if result != test.expectedAmount { | ||
| t.Fatalf("expected %v, got %v", | ||
| test.expectedAmount, result) | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,9 @@ import ( | |
| "github.com/btcsuite/btcd/btcutil" | ||
| "github.com/lightninglabs/loop" | ||
| "github.com/lightninglabs/loop/looprpc" | ||
| "github.com/lightninglabs/taproot-assets/rfqmath" | ||
| "github.com/lightninglabs/taproot-assets/taprpc/rfqrpc" | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| "github.com/lightningnetwork/lnd/routing/route" | ||
| "github.com/urfave/cli" | ||
| ) | ||
|
|
@@ -268,8 +271,20 @@ func printQuoteOutResp(req *looprpc.QuoteRequest, | |
| totalFee := resp.HtlcSweepFeeSat + resp.SwapFeeSat | ||
|
|
||
| if resp.AssetRfqInfo != nil { | ||
| assetAmtSwap, err := getAssetAmt( | ||
| req.Amt, resp.AssetRfqInfo.SwapAssetRate, | ||
| ) | ||
| if err != nil { | ||
| fmt.Printf("Error converting asset amount: %v\n", err) | ||
| return | ||
| } | ||
| exchangeRate := float64(assetAmtSwap) / float64(req.Amt) | ||
| fmt.Printf(assetAmtFmt, "Send off-chain:", | ||
| resp.AssetRfqInfo.SwapAssetAmt, | ||
| assetAmtSwap, resp.AssetRfqInfo.AssetName) | ||
| fmt.Printf(rateFmt, "Exchange rate:", | ||
| exchangeRate, resp.AssetRfqInfo.AssetName) | ||
| fmt.Printf(assetAmtFmt, "Limit Send off-chain:", | ||
| resp.AssetRfqInfo.MaxSwapAssetAmt, | ||
| resp.AssetRfqInfo.AssetName) | ||
| } else { | ||
| fmt.Printf(satAmtFmt, "Send off-chain:", req.Amt) | ||
|
|
@@ -288,8 +303,18 @@ func printQuoteOutResp(req *looprpc.QuoteRequest, | |
| fmt.Printf(satAmtFmt, "Estimated total fee:", totalFee) | ||
| fmt.Println() | ||
| if resp.AssetRfqInfo != nil { | ||
| assetAmtPrepay, err := getAssetAmt( | ||
| resp.PrepayAmtSat, resp.AssetRfqInfo.PrepayAssetRate, | ||
| ) | ||
| if err != nil { | ||
| fmt.Printf("Error converting asset amount: %v\n", err) | ||
| return | ||
| } | ||
| fmt.Printf(assetAmtFmt, "No show penalty (prepay):", | ||
| resp.AssetRfqInfo.PrepayAssetAmt, | ||
| assetAmtPrepay, | ||
| resp.AssetRfqInfo.AssetName) | ||
| fmt.Printf(assetAmtFmt, "Limit no show penalty (prepay):", | ||
| resp.AssetRfqInfo.MaxPrepayAssetAmt, | ||
| resp.AssetRfqInfo.AssetName) | ||
| } else { | ||
| fmt.Printf(satAmtFmt, "No show penalty (prepay):", | ||
|
|
@@ -302,3 +327,33 @@ func printQuoteOutResp(req *looprpc.QuoteRequest, | |
| time.Unix(int64(req.SwapPublicationDeadline), 0), | ||
| ) | ||
| } | ||
|
|
||
| // getAssetAmt returns the asset amount for the given amount in satoshis and | ||
| // the asset rate. | ||
| func getAssetAmt(amt int64, assetRate *looprpc.FixedPoint) ( | ||
| uint64, error) { | ||
|
|
||
| askAssetRate, err := unmarshalFixedPoint(assetRate) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| msatAmt := lnwire.MilliSatoshi((amt * 1000)) | ||
|
|
||
| assetAmt := rfqmath.MilliSatoshiToUnits(msatAmt, *askAssetRate) | ||
|
|
||
| return assetAmt.ToUint64(), nil | ||
| } | ||
|
|
||
| // unmarshalFixedPoint converts an RPC FixedPoint to a BigIntFixedPoint. | ||
| func unmarshalFixedPoint(fp *looprpc.FixedPoint) (*rfqmath.BigIntFixedPoint, | ||
| error) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose to reuse https://pkg.go.dev/github.com/lightninglabs/taproot-assets/taprpc/rfqrpc#UnmarshalFixedPoint
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible to create a struct manually filling the fields.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. created a |
||
|
|
||
| // convert the looprpc.FixedPoint to a rfqrpc.FixedPoint | ||
| rfqrpcFP := &rfqrpc.FixedPoint{ | ||
| Coefficient: fp.Coefficient, | ||
| Scale: fp.Scale, | ||
| } | ||
|
|
||
| return rfqrpc.UnmarshalFixedPoint(rfqrpcFP) | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.