mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
loopd: add instantout quote
This commit is contained in:
parent
ca8f91ab77
commit
b3fe9a9c61
3 changed files with 72 additions and 0 deletions
|
|
@ -7,7 +7,9 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/lightninglabs/loop/instantout/reservation"
|
||||
"github.com/lightninglabs/loop/swapserverrpc"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
)
|
||||
|
||||
|
|
@ -199,3 +201,50 @@ func (m *Manager) GetActiveInstantOut(swapHash lntypes.Hash) (*FSM, error) {
|
|||
|
||||
return fsm, nil
|
||||
}
|
||||
|
||||
type Quote struct {
|
||||
// ServiceFee is the fee in sat that is paid to the loop service.
|
||||
ServiceFee btcutil.Amount
|
||||
|
||||
// OnChainFee is the estimated on chain fee in sat.
|
||||
OnChainFee btcutil.Amount
|
||||
}
|
||||
|
||||
// GetInstantOutQuote returns a quote for an instant out.
|
||||
func (m *Manager) GetInstantOutQuote(ctx context.Context,
|
||||
amt btcutil.Amount, numReservations int) (Quote, error) {
|
||||
|
||||
if numReservations <= 0 {
|
||||
return Quote{}, fmt.Errorf("no reservations selected")
|
||||
}
|
||||
|
||||
if amt <= 0 {
|
||||
return Quote{}, fmt.Errorf("no amount selected")
|
||||
}
|
||||
|
||||
// Get the service fee.
|
||||
quoteRes, err := m.cfg.InstantOutClient.GetInstantOutQuote(
|
||||
ctx, &swapserverrpc.GetInstantOutQuoteRequest{
|
||||
Amount: uint64(amt),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return Quote{}, err
|
||||
}
|
||||
|
||||
// Get the offchain fee by getting the fee estimate from the lnd client
|
||||
// and multiplying it by the estimated sweepless sweep transaction.
|
||||
feeRate, err := m.cfg.Wallet.EstimateFeeRate(ctx, normalConfTarget)
|
||||
if err != nil {
|
||||
return Quote{}, err
|
||||
}
|
||||
|
||||
// The on chain chainFee is the chainFee rate times the estimated
|
||||
// sweepless sweep transaction size.
|
||||
chainFee := feeRate.FeeForWeight(sweeplessSweepWeight(numReservations))
|
||||
|
||||
return Quote{
|
||||
ServiceFee: btcutil.Amount(quoteRes.SwapFee),
|
||||
OnChainFee: chainFee,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,4 +104,8 @@ var RequiredPermissions = map[string][]bakery.Op{
|
|||
Entity: "swap",
|
||||
Action: "execute",
|
||||
}},
|
||||
"/looprpc.SwapClient/InstantOutQuote": {{
|
||||
Entity: "swap",
|
||||
Action: "read",
|
||||
}},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1209,6 +1209,25 @@ func (s *swapClientServer) InstantOut(ctx context.Context,
|
|||
return res, nil
|
||||
}
|
||||
|
||||
// InstantOutQuote returns a quote for an instant out swap with the provided
|
||||
// parameters.
|
||||
func (s *swapClientServer) InstantOutQuote(ctx context.Context,
|
||||
req *clientrpc.InstantOutQuoteRequest) (
|
||||
*clientrpc.InstantOutQuoteResponse, error) {
|
||||
|
||||
quote, err := s.instantOutManager.GetInstantOutQuote(
|
||||
ctx, btcutil.Amount(req.Amt), int(req.NumReservations),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &clientrpc.InstantOutQuoteResponse{
|
||||
ServiceFeeSat: int64(quote.ServiceFee),
|
||||
SweepFeeSat: int64(quote.OnChainFee),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func rpcAutoloopReason(reason liquidity.Reason) (clientrpc.AutoReason, error) {
|
||||
switch reason {
|
||||
case liquidity.ReasonNone:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue