2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-02-21 09:27:57 +00:00
|
|
|
"strings"
|
2019-03-06 20:13:50 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2019-11-08 09:37:51 +00:00
|
|
|
"github.com/lightninglabs/loop/lsat"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/swap"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/sweep"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrSwapFeeTooHigh is returned when the swap invoice amount is too
|
|
|
|
// high.
|
|
|
|
ErrSwapFeeTooHigh = errors.New("swap fee too high")
|
|
|
|
|
|
|
|
// ErrPrepayAmountTooHigh is returned when the prepay invoice amount is
|
|
|
|
// too high.
|
|
|
|
ErrPrepayAmountTooHigh = errors.New("prepay amount too high")
|
|
|
|
|
|
|
|
// ErrSwapAmountTooLow is returned when the requested swap amount is
|
|
|
|
// less than the server minimum.
|
|
|
|
ErrSwapAmountTooLow = errors.New("swap amount too low")
|
|
|
|
|
|
|
|
// ErrSwapAmountTooHigh is returned when the requested swap amount is
|
|
|
|
// more than the server maximum.
|
|
|
|
ErrSwapAmountTooHigh = errors.New("swap amount too high")
|
|
|
|
|
|
|
|
// ErrExpiryTooSoon is returned when the server proposes an expiry that
|
|
|
|
// is too soon for us.
|
|
|
|
ErrExpiryTooSoon = errors.New("swap expiry too soon")
|
|
|
|
|
|
|
|
// ErrExpiryTooFar is returned when the server proposes an expiry that
|
|
|
|
// is too soon for us.
|
|
|
|
ErrExpiryTooFar = errors.New("swap expiry too far")
|
|
|
|
|
2019-06-25 18:41:57 +00:00
|
|
|
// ErrSweepConfTargetTooFar is returned when the client proposes a
|
|
|
|
// confirmation target to sweep the on-chain HTLC of a Loop Out that is
|
|
|
|
// beyond the expiration height proposed by the server.
|
|
|
|
ErrSweepConfTargetTooFar = errors.New("sweep confirmation target is " +
|
|
|
|
"beyond swap expiration height")
|
|
|
|
|
2019-11-27 12:36:48 +00:00
|
|
|
// serverRPCTimeout is the maximum time a gRPC request to the server
|
|
|
|
// should be allowed to take.
|
2019-03-06 20:13:50 +00:00
|
|
|
serverRPCTimeout = 30 * time.Second
|
|
|
|
|
2019-11-27 12:36:48 +00:00
|
|
|
// globalCallTimeout is the maximum time any call of the client to the
|
|
|
|
// server is allowed to take, including the time it may take to get
|
|
|
|
// and pay for an LSAT token.
|
|
|
|
globalCallTimeout = serverRPCTimeout + lsat.PaymentTimeout
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
republishDelay = 10 * time.Second
|
2020-02-21 09:27:57 +00:00
|
|
|
|
|
|
|
// MinerFeeEstimationFailed is a magic number that is returned in a
|
|
|
|
// quote call as the miner fee if the fee estimation in lnd's wallet
|
|
|
|
// failed because of insufficient funds.
|
|
|
|
MinerFeeEstimationFailed btcutil.Amount = -1
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-07 02:22:46 +00:00
|
|
|
// Client performs the client side part of swaps. This interface exists to be
|
|
|
|
// able to implement a stub.
|
2019-03-06 20:13:50 +00:00
|
|
|
type Client struct {
|
|
|
|
started uint32 // To be used atomically.
|
|
|
|
errChan chan error
|
|
|
|
|
|
|
|
lndServices *lndclient.LndServices
|
|
|
|
sweeper *sweep.Sweeper
|
|
|
|
executor *executor
|
|
|
|
|
|
|
|
resumeReady chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
clientConfig
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:03:23 +00:00
|
|
|
// ClientConfig is the exported configuration structure that is required to
|
|
|
|
// instantiate the loop client.
|
|
|
|
type ClientConfig struct {
|
|
|
|
// ServerAddress is the loop server to connect to.
|
|
|
|
ServerAddress string
|
|
|
|
|
|
|
|
// ProxyAddress is the SOCKS proxy that should be used to establish the
|
|
|
|
// connection.
|
|
|
|
ProxyAddress string
|
|
|
|
|
2020-06-15 09:04:37 +00:00
|
|
|
// SwapServerNoTLS skips TLS for the swap server connection when set.
|
|
|
|
SwapServerNoTLS bool
|
2020-04-27 12:03:23 +00:00
|
|
|
|
|
|
|
// TLSPathServer is the path to the TLS certificate that is required to
|
|
|
|
// connect to the server.
|
|
|
|
TLSPathServer string
|
|
|
|
|
|
|
|
// Lnd is an instance of the lnd proxy.
|
|
|
|
Lnd *lndclient.LndServices
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-04-27 12:03:23 +00:00
|
|
|
// MaxLsatCost is the maximum price we are willing to pay to the server
|
|
|
|
// for the token.
|
|
|
|
MaxLsatCost btcutil.Amount
|
|
|
|
|
|
|
|
// MaxLsatFee is the maximum that we are willing to pay in routing fees
|
|
|
|
// to obtain the token.
|
|
|
|
MaxLsatFee btcutil.Amount
|
2020-04-27 12:06:08 +00:00
|
|
|
|
|
|
|
// LoopOutMaxParts defines the maximum number of parts that may be used
|
|
|
|
// for a loop out swap. When greater than one, a multi-part payment may
|
|
|
|
// be attempted.
|
|
|
|
LoopOutMaxParts uint32
|
2020-04-27 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new instance to initiate swaps with.
|
|
|
|
func NewClient(dbDir string, cfg *ClientConfig) (*Client, func(), error) {
|
|
|
|
store, err := loopdb.NewBoltSwapStore(dbDir, cfg.Lnd.ChainParams)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-08 09:37:51 +00:00
|
|
|
lsatStore, err := lsat.NewFileStore(dbDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-04-27 12:03:23 +00:00
|
|
|
swapServerClient, err := newSwapServerClient(cfg, lsatStore)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &clientConfig{
|
2020-04-27 12:03:23 +00:00
|
|
|
LndServices: cfg.Lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
Server: swapServerClient,
|
|
|
|
Store: store,
|
2019-11-15 12:57:03 +00:00
|
|
|
LsatStore: lsatStore,
|
2019-03-06 20:13:50 +00:00
|
|
|
CreateExpiryTimer: func(d time.Duration) <-chan time.Time {
|
|
|
|
return time.NewTimer(d).C
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sweeper := &sweep.Sweeper{
|
2020-04-27 12:03:23 +00:00
|
|
|
Lnd: cfg.Lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
executor := newExecutor(&executorConfig{
|
2020-04-27 12:03:23 +00:00
|
|
|
lnd: cfg.Lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
store: store,
|
|
|
|
sweeper: sweeper,
|
|
|
|
createExpiryTimer: config.CreateExpiryTimer,
|
2020-04-27 12:06:08 +00:00
|
|
|
loopOutMaxParts: cfg.LoopOutMaxParts,
|
2019-03-06 20:13:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client := &Client{
|
|
|
|
errChan: make(chan error),
|
|
|
|
clientConfig: *config,
|
2020-04-27 12:03:23 +00:00
|
|
|
lndServices: cfg.Lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
sweeper: sweeper,
|
|
|
|
executor: executor,
|
|
|
|
resumeReady: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func() {
|
|
|
|
swapServerClient.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, cleanup, nil
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:06:59 +00:00
|
|
|
// FetchSwaps returns all loop in and out swaps currently in the database.
|
|
|
|
func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
|
|
|
|
loopOutSwaps, err := s.Store.FetchLoopOutSwaps()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loopInSwaps, err := s.Store.FetchLoopInSwaps()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
swaps := make([]*SwapInfo, 0, len(loopInSwaps)+len(loopOutSwaps))
|
|
|
|
|
|
|
|
for _, swp := range loopOutSwaps {
|
|
|
|
htlc, err := swap.NewHtlc(
|
|
|
|
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
|
|
|
|
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
|
|
|
|
s.lndServices.ChainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
swaps = append(swaps, &SwapInfo{
|
2020-04-24 15:35:32 +00:00
|
|
|
SwapType: swap.TypeOut,
|
|
|
|
SwapContract: swp.Contract.SwapContract,
|
|
|
|
SwapStateData: swp.State(),
|
|
|
|
SwapHash: swp.Hash,
|
|
|
|
LastUpdate: swp.LastUpdateTime(),
|
|
|
|
HtlcAddressP2WSH: htlc.Address,
|
2019-04-11 07:06:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, swp := range loopInSwaps {
|
2020-04-24 15:35:32 +00:00
|
|
|
htlcNP2WSH, err := swap.NewHtlc(
|
2019-04-11 07:06:59 +00:00
|
|
|
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
|
|
|
|
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcNP2WSH,
|
|
|
|
s.lndServices.ChainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:35:32 +00:00
|
|
|
htlcP2WSH, err := swap.NewHtlc(
|
|
|
|
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
|
|
|
|
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
|
|
|
|
s.lndServices.ChainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:06:59 +00:00
|
|
|
swaps = append(swaps, &SwapInfo{
|
2020-04-24 15:35:32 +00:00
|
|
|
SwapType: swap.TypeIn,
|
|
|
|
SwapContract: swp.Contract.SwapContract,
|
|
|
|
SwapStateData: swp.State(),
|
|
|
|
SwapHash: swp.Hash,
|
|
|
|
LastUpdate: swp.LastUpdateTime(),
|
|
|
|
HtlcAddressP2WSH: htlcP2WSH.Address,
|
|
|
|
HtlcAddressNP2WSH: htlcNP2WSH.Address,
|
2019-04-11 07:06:59 +00:00
|
|
|
})
|
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-04-11 07:06:59 +00:00
|
|
|
return swaps, nil
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// Run is a blocking call that executes all swaps. Any pending swaps are
|
2019-03-07 02:22:46 +00:00
|
|
|
// restored from persistent storage and resumed. Subsequent updates will be
|
|
|
|
// sent through the passed in statusChan. The function can be terminated by
|
|
|
|
// cancelling the context.
|
2019-03-06 20:13:50 +00:00
|
|
|
func (s *Client) Run(ctx context.Context,
|
|
|
|
statusChan chan<- SwapInfo) error {
|
|
|
|
|
|
|
|
if !atomic.CompareAndSwapUint32(&s.started, 0, 1) {
|
|
|
|
return errors.New("swap client can only be started once")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log connected node.
|
2020-04-22 13:48:34 +00:00
|
|
|
log.Infof("Connected to lnd node '%v' with pubkey %x (version %s)",
|
|
|
|
s.lndServices.NodeAlias, s.lndServices.NodePubkey,
|
|
|
|
lndclient.VersionString(s.lndServices.Version))
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Setup main context used for cancelation.
|
|
|
|
mainCtx, mainCancel := context.WithCancel(ctx)
|
|
|
|
defer mainCancel()
|
|
|
|
|
|
|
|
// Query store before starting event loop to prevent new swaps from
|
|
|
|
// being treated as swaps that need to be resumed.
|
2019-03-12 15:10:37 +00:00
|
|
|
pendingLoopOutSwaps, err := s.Store.FetchLoopOutSwaps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingLoopInSwaps, err := s.Store.FetchLoopInSwaps()
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start goroutine to deliver all pending swaps to the main loop.
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
s.resumeSwaps(mainCtx, pendingLoopOutSwaps, pendingLoopInSwaps)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Signal that new requests can be accepted. Otherwise the new
|
|
|
|
// swap could already have been added to the store and read in
|
|
|
|
// this goroutine as being a swap that needs to be resumed.
|
|
|
|
// Resulting in two goroutines executing the same swap.
|
|
|
|
close(s.resumeReady)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Main event loop.
|
|
|
|
err = s.executor.run(mainCtx, statusChan)
|
|
|
|
|
|
|
|
// Consider canceled as happy flow.
|
|
|
|
if err == context.Canceled {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Swap client terminating: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
} else {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Info("Swap client terminating")
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel all remaining active goroutines.
|
|
|
|
mainCancel()
|
|
|
|
|
|
|
|
// Wait for all to finish.
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Debug("Wait for executor to finish")
|
2019-03-06 20:13:50 +00:00
|
|
|
s.executor.waitFinished()
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Debug("Wait for goroutines to finish")
|
2019-03-06 20:13:50 +00:00
|
|
|
s.wg.Wait()
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Info("Swap client terminated")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resumeSwaps restarts all pending swaps from the provided list.
|
|
|
|
func (s *Client) resumeSwaps(ctx context.Context,
|
2019-03-12 15:10:37 +00:00
|
|
|
loopOutSwaps []*loopdb.LoopOut, loopInSwaps []*loopdb.LoopIn) {
|
|
|
|
|
2020-06-08 10:53:07 +00:00
|
|
|
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
for _, pend := range loopOutSwaps {
|
2019-05-15 11:55:41 +00:00
|
|
|
if pend.State().State.Type() != loopdb.StateTypePending {
|
2019-03-06 20:13:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-03-07 04:32:24 +00:00
|
|
|
swap, err := resumeLoopOutSwap(ctx, swapCfg, pend)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("resuming loop out swap: %v", err)
|
2019-03-12 15:10:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.executor.initiateSwap(ctx, swap)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pend := range loopInSwaps {
|
2019-05-15 11:55:41 +00:00
|
|
|
if pend.State().State.Type() != loopdb.StateTypePending {
|
2019-03-12 15:10:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
swap, err := resumeLoopInSwap(ctx, swapCfg, pend)
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("resuming loop in swap: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.executor.initiateSwap(ctx, swap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOut initiates a loop out swap. It blocks until the swap is initiation
|
2019-03-07 02:22:46 +00:00
|
|
|
// with the swap server is completed (typically this takes only a short amount
|
|
|
|
// of time). From there on further status information can be acquired through
|
|
|
|
// the status channel returned from the Run call.
|
2019-03-06 20:13:50 +00:00
|
|
|
//
|
2019-03-07 04:32:24 +00:00
|
|
|
// When the call returns, the swap has been persisted and will be resumed
|
|
|
|
// automatically after restarts.
|
2019-03-06 20:13:50 +00:00
|
|
|
//
|
|
|
|
// The return value is a hash that uniquely identifies the new swap.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *Client) LoopOut(globalCtx context.Context,
|
2020-06-30 11:45:12 +00:00
|
|
|
request *OutRequest) (*LoopOutSwapInfo, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-05-19 07:54:27 +00:00
|
|
|
log.Infof("LoopOut %v to %v (channels: %v)",
|
|
|
|
request.Amount, request.DestAddr, request.OutgoingChanSet,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.waitForInitialized(globalCtx); err != nil {
|
2020-06-30 11:45:12 +00:00
|
|
|
return nil, err
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new swap object for this swap.
|
|
|
|
initiationHeight := s.executor.height()
|
2020-06-08 10:53:07 +00:00
|
|
|
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopOutSwap(
|
2019-03-06 20:13:50 +00:00
|
|
|
globalCtx, swapCfg, initiationHeight, request,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-06-30 11:45:12 +00:00
|
|
|
return nil, err
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Post swap to the main loop.
|
|
|
|
s.executor.initiateSwap(globalCtx, swap)
|
|
|
|
|
|
|
|
// Return hash so that the caller can identify this swap in the updates
|
|
|
|
// stream.
|
2020-06-30 11:45:12 +00:00
|
|
|
return &LoopOutSwapInfo{
|
|
|
|
SwapHash: swap.hash,
|
|
|
|
HtlcAddressP2WSH: swap.htlc.Address,
|
2020-06-30 12:10:33 +00:00
|
|
|
ServerMessage: initResult.serverMessage,
|
2020-06-30 11:45:12 +00:00
|
|
|
}, nil
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOutQuote takes a LoopOut amount and returns a break down of estimated
|
2019-03-07 02:22:46 +00:00
|
|
|
// costs for the client. Both the swap server and the on-chain fee estimator
|
|
|
|
// are queried to get to build the quote response.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *Client) LoopOutQuote(ctx context.Context,
|
|
|
|
request *LoopOutQuoteRequest) (*LoopOutQuote, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
terms, err := s.Server.GetLoopOutTerms(ctx)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Amount < terms.MinSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooLow
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Amount > terms.MaxSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooHigh
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:53:18 +00:00
|
|
|
quote, err := s.Server.GetLoopOutQuote(
|
|
|
|
ctx, request.Amount, request.SwapPublicationDeadline,
|
|
|
|
)
|
2019-10-08 20:28:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Offchain swap destination: %x", quote.SwapPaymentDest)
|
2019-10-08 20:28:20 +00:00
|
|
|
|
|
|
|
swapFee := quote.SwapFee
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-07-30 11:37:47 +00:00
|
|
|
// Generate dummy p2wsh address for fee estimation. The p2wsh address
|
|
|
|
// type is chosen because it adds the most weight of all output types
|
|
|
|
// and we want the quote to return a worst case value.
|
|
|
|
wsh := [32]byte{}
|
|
|
|
p2wshAddress, err := btcutil.NewAddressWitnessScriptHash(
|
|
|
|
wsh[:], s.lndServices.ChainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
minerFee, err := s.sweeper.GetSweepFee(
|
2019-04-04 10:20:45 +00:00
|
|
|
ctx, swap.QuoteHtlc.AddSuccessToEstimator,
|
2019-07-30 11:37:47 +00:00
|
|
|
p2wshAddress, request.SweepConfTarget,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
return &LoopOutQuote{
|
2019-10-08 20:28:20 +00:00
|
|
|
SwapFee: swapFee,
|
|
|
|
MinerFee: minerFee,
|
2019-10-07 15:29:24 +00:00
|
|
|
PrepayAmount: quote.PrepayAmount,
|
2019-10-08 20:28:20 +00:00
|
|
|
SwapPaymentDest: quote.SwapPaymentDest,
|
|
|
|
CltvDelta: quote.CltvDelta,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOutTerms returns the terms on which the server executes swaps.
|
|
|
|
func (s *Client) LoopOutTerms(ctx context.Context) (
|
|
|
|
*LoopOutTerms, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
return s.Server.GetLoopOutTerms(ctx)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitForInitialized for swaps to be resumed and executor ready.
|
|
|
|
func (s *Client) waitForInitialized(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-s.executor.ready:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.resumeReady:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
// LoopIn initiates a loop in swap.
|
|
|
|
func (s *Client) LoopIn(globalCtx context.Context,
|
2020-04-29 16:30:44 +00:00
|
|
|
request *LoopInRequest) (*LoopInSwapInfo, error) {
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2020-02-11 12:25:03 +00:00
|
|
|
log.Infof("Loop in %v (last hop: %v)",
|
2019-03-12 15:10:37 +00:00
|
|
|
request.Amount,
|
2020-02-11 12:25:03 +00:00
|
|
|
request.LastHop,
|
2019-03-12 15:10:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.waitForInitialized(globalCtx); err != nil {
|
2020-04-29 16:30:44 +00:00
|
|
|
return nil, err
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new swap object for this swap.
|
|
|
|
initiationHeight := s.executor.height()
|
2020-06-08 10:53:07 +00:00
|
|
|
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopInSwap(
|
2020-06-08 10:53:07 +00:00
|
|
|
globalCtx, swapCfg, initiationHeight, request,
|
2019-03-12 15:10:37 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-04-29 16:30:44 +00:00
|
|
|
return nil, err
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
// Post swap to the main loop.
|
|
|
|
s.executor.initiateSwap(globalCtx, swap)
|
|
|
|
|
|
|
|
// Return hash so that the caller can identify this swap in the updates
|
|
|
|
// stream.
|
2020-04-29 16:30:44 +00:00
|
|
|
swapInfo := &LoopInSwapInfo{
|
2020-04-24 15:35:32 +00:00
|
|
|
SwapHash: swap.hash,
|
|
|
|
HtlcAddressP2WSH: swap.htlcP2WSH.Address,
|
|
|
|
HtlcAddressNP2WSH: swap.htlcNP2WSH.Address,
|
2020-06-30 12:10:33 +00:00
|
|
|
ServerMessage: initResult.serverMessage,
|
2020-04-29 16:30:44 +00:00
|
|
|
}
|
|
|
|
return swapInfo, nil
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoopInQuote takes an amount and returns a break down of estimated
|
|
|
|
// costs for the client. Both the swap server and the on-chain fee estimator are
|
|
|
|
// queried to get to build the quote response.
|
|
|
|
func (s *Client) LoopInQuote(ctx context.Context,
|
|
|
|
request *LoopInQuoteRequest) (*LoopInQuote, error) {
|
|
|
|
|
|
|
|
// Retrieve current server terms to calculate swap fee.
|
|
|
|
terms, err := s.Server.GetLoopInTerms(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check amount limits.
|
|
|
|
if request.Amount < terms.MinSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooLow
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Amount > terms.MaxSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooHigh
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:28:20 +00:00
|
|
|
quote, err := s.Server.GetLoopInQuote(ctx, request.Amount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
swapFee := quote.SwapFee
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2019-09-23 08:08:34 +00:00
|
|
|
// We don't calculate the on-chain fee if the HTLC is going to be
|
|
|
|
// published externally.
|
|
|
|
if request.ExternalHtlc {
|
|
|
|
return &LoopInQuote{
|
|
|
|
SwapFee: swapFee,
|
|
|
|
MinerFee: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-08 20:28:20 +00:00
|
|
|
|
2020-02-21 09:27:57 +00:00
|
|
|
// Get estimate for miner fee. If estimating the miner fee for the
|
|
|
|
// requested amount is not possible because lnd's wallet cannot
|
|
|
|
// construct a sample TX, we just return zero instead of failing the
|
|
|
|
// quote. The user interface should inform the user that fee estimation
|
|
|
|
// was not possible.
|
|
|
|
//
|
|
|
|
// TODO(guggero): Thread through error code from lnd to avoid string
|
|
|
|
// matching.
|
2019-03-12 15:10:37 +00:00
|
|
|
minerFee, err := s.lndServices.Client.EstimateFeeToP2WSH(
|
|
|
|
ctx, request.Amount, request.HtlcConfTarget,
|
|
|
|
)
|
2020-02-21 09:27:57 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "insufficient funds") {
|
|
|
|
return &LoopInQuote{
|
|
|
|
SwapFee: swapFee,
|
|
|
|
MinerFee: MinerFeeEstimationFailed,
|
|
|
|
CltvDelta: quote.CltvDelta,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LoopInQuote{
|
2019-10-08 20:28:20 +00:00
|
|
|
SwapFee: swapFee,
|
|
|
|
MinerFee: minerFee,
|
|
|
|
CltvDelta: quote.CltvDelta,
|
2019-03-12 15:10:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoopInTerms returns the terms on which the server executes swaps.
|
|
|
|
func (s *Client) LoopInTerms(ctx context.Context) (
|
|
|
|
*LoopInTerms, error) {
|
|
|
|
|
|
|
|
return s.Server.GetLoopInTerms(ctx)
|
|
|
|
}
|