2020-01-03 13:01:31 +00:00
|
|
|
package loopd
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-01 07:58:08 +00:00
|
|
|
"context"
|
2023-05-19 13:09:38 +00:00
|
|
|
"fmt"
|
2020-09-01 07:58:08 +00:00
|
|
|
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2023-10-12 15:04:09 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2024-04-24 16:50:24 +00:00
|
|
|
"github.com/lightninglabs/aperture/l402"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop"
|
2020-09-01 07:58:08 +00:00
|
|
|
"github.com/lightninglabs/loop/liquidity"
|
2023-05-19 13:09:38 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2021-02-03 06:54:50 +00:00
|
|
|
"github.com/lightninglabs/loop/swap"
|
2024-01-18 14:18:36 +00:00
|
|
|
"github.com/lightninglabs/loop/sweepbatcher"
|
2020-09-30 10:34:05 +00:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2020-10-12 11:34:55 +00:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// getClient returns an instance of the swap client.
|
2023-10-12 15:04:09 +00:00
|
|
|
func getClient(cfg *Config, swapDb loopdb.SwapStore,
|
2024-01-18 14:18:36 +00:00
|
|
|
sweeperDb sweepbatcher.BatcherStore, lnd *lndclient.LndServices) (
|
|
|
|
*loop.Client, func(), error) {
|
2019-03-07 04:32:24 +00:00
|
|
|
|
2024-04-24 16:50:24 +00:00
|
|
|
// Default is not set for MaxLSATCost and MaxLSATFee to distinguish
|
|
|
|
// it from user explicitly setting the option to default value.
|
|
|
|
// So if MaxL402Cost and MaxLSATFee are not set in the config file
|
|
|
|
// and command line, they are set to 0.
|
|
|
|
const (
|
|
|
|
defaultCost = l402.DefaultMaxCostSats
|
|
|
|
defaultFee = l402.DefaultMaxRoutingFeeSats
|
|
|
|
)
|
|
|
|
if cfg.MaxL402Cost != defaultCost && cfg.MaxLSATCost != 0 {
|
|
|
|
return nil, nil, fmt.Errorf("both maxl402cost and maxlsatcost" +
|
|
|
|
" were specified; they are not allowed together")
|
|
|
|
}
|
|
|
|
if cfg.MaxL402Fee != defaultFee && cfg.MaxLSATFee != 0 {
|
|
|
|
return nil, nil, fmt.Errorf("both maxl402fee and maxlsatfee" +
|
|
|
|
" were specified; they are not allowed together")
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:03:23 +00:00
|
|
|
clientConfig := &loop.ClientConfig{
|
2023-05-19 13:09:38 +00:00
|
|
|
ServerAddress: cfg.Server.Host,
|
|
|
|
ProxyAddress: cfg.Server.Proxy,
|
|
|
|
SwapServerNoTLS: cfg.Server.NoTLS,
|
|
|
|
TLSPathServer: cfg.Server.TLSPath,
|
2021-12-01 14:18:10 +00:00
|
|
|
Lnd: lnd,
|
2024-04-17 18:57:59 +00:00
|
|
|
MaxL402Cost: btcutil.Amount(cfg.MaxL402Cost),
|
|
|
|
MaxL402Fee: btcutil.Amount(cfg.MaxL402Fee),
|
2023-05-19 13:09:38 +00:00
|
|
|
LoopOutMaxParts: cfg.LoopOutMaxParts,
|
|
|
|
TotalPaymentTimeout: cfg.TotalPaymentTimeout,
|
|
|
|
MaxPaymentRetries: cfg.MaxPaymentRetries,
|
2020-04-27 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 16:50:24 +00:00
|
|
|
if cfg.MaxL402Cost == defaultCost && cfg.MaxLSATCost != 0 {
|
|
|
|
log.Warnf("Option maxlsatcost is deprecated and will be " +
|
|
|
|
"removed. Switch to maxl402cost.")
|
|
|
|
clientConfig.MaxL402Cost = btcutil.Amount(cfg.MaxLSATCost)
|
|
|
|
}
|
|
|
|
if cfg.MaxL402Fee == defaultFee && cfg.MaxLSATFee != 0 {
|
|
|
|
log.Warnf("Option maxlsatfee is deprecated and will be " +
|
|
|
|
"removed. Switch to maxl402fee.")
|
|
|
|
clientConfig.MaxL402Fee = btcutil.Amount(cfg.MaxLSATFee)
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:04:09 +00:00
|
|
|
swapClient, cleanUp, err := loop.NewClient(
|
2024-01-18 14:18:36 +00:00
|
|
|
cfg.DataDir, swapDb, sweeperDb, clientConfig,
|
2023-10-12 15:04:09 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return swapClient, cleanUp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openDatabase(cfg *Config, chainParams *chaincfg.Params) (loopdb.SwapStore,
|
|
|
|
*loopdb.BaseDB, error) { //nolint:unparam
|
|
|
|
|
2023-05-19 13:09:38 +00:00
|
|
|
var (
|
2023-10-12 15:04:09 +00:00
|
|
|
db loopdb.SwapStore
|
|
|
|
err error
|
|
|
|
baseDb loopdb.BaseDB
|
2023-05-19 13:09:38 +00:00
|
|
|
)
|
|
|
|
switch cfg.DatabaseBackend {
|
|
|
|
case DatabaseBackendSqlite:
|
|
|
|
log.Infof("Opening sqlite3 database at: %v",
|
|
|
|
cfg.Sqlite.DatabaseFileName)
|
2024-03-01 09:02:29 +00:00
|
|
|
|
|
|
|
db, err = loopdb.NewSqliteStore(cfg.Sqlite, chainParams)
|
2023-10-25 21:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-10-12 15:04:09 +00:00
|
|
|
baseDb = *db.(*loopdb.SqliteSwapStore).BaseDB
|
2023-05-19 13:09:38 +00:00
|
|
|
|
|
|
|
case DatabaseBackendPostgres:
|
|
|
|
log.Infof("Opening postgres database at: %v",
|
|
|
|
cfg.Postgres.DSN(true))
|
2024-03-01 09:02:29 +00:00
|
|
|
|
|
|
|
db, err = loopdb.NewPostgresStore(cfg.Postgres, chainParams)
|
2023-10-25 21:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-10-12 15:04:09 +00:00
|
|
|
baseDb = *db.(*loopdb.PostgresStore).BaseDB
|
2023-05-19 13:09:38 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("unknown database backend: %s",
|
|
|
|
cfg.DatabaseBackend)
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:04:09 +00:00
|
|
|
return db, &baseDb, nil
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-09-01 07:58:08 +00:00
|
|
|
|
|
|
|
func getLiquidityManager(client *loop.Client) *liquidity.Manager {
|
|
|
|
mngrCfg := &liquidity.Config{
|
2023-03-14 14:51:19 +00:00
|
|
|
AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker),
|
|
|
|
LoopOut: client.LoopOut,
|
|
|
|
LoopIn: client.LoopIn,
|
2023-06-20 18:52:07 +00:00
|
|
|
Restrictions: func(ctx context.Context, swapType swap.Type,
|
|
|
|
initiator string) (*liquidity.Restrictions, error) {
|
2020-09-01 07:58:08 +00:00
|
|
|
|
2021-02-03 06:54:50 +00:00
|
|
|
if swapType == swap.TypeOut {
|
2023-06-20 18:52:07 +00:00
|
|
|
outTerms, err := client.Server.GetLoopOutTerms(ctx, initiator)
|
2021-02-03 06:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return liquidity.NewRestrictions(
|
|
|
|
outTerms.MinSwapAmount, outTerms.MaxSwapAmount,
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2023-06-20 18:52:07 +00:00
|
|
|
inTerms, err := client.Server.GetLoopInTerms(ctx, initiator)
|
2020-09-01 07:58:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return liquidity.NewRestrictions(
|
2021-02-03 06:54:50 +00:00
|
|
|
inTerms.MinSwapAmount, inTerms.MaxSwapAmount,
|
2020-09-01 07:58:08 +00:00
|
|
|
), nil
|
|
|
|
},
|
2020-09-30 10:34:08 +00:00
|
|
|
Lnd: client.LndServices,
|
|
|
|
Clock: clock.NewDefaultClock(),
|
2020-09-30 10:34:09 +00:00
|
|
|
LoopOutQuote: client.LoopOutQuote,
|
2021-12-15 07:01:20 +00:00
|
|
|
LoopInQuote: client.LoopInQuote,
|
2020-09-30 10:34:08 +00:00
|
|
|
ListLoopOut: client.Store.FetchLoopOutSwaps,
|
2023-02-02 12:54:27 +00:00
|
|
|
GetLoopOut: client.Store.FetchLoopOutSwap,
|
2020-09-30 10:34:08 +00:00
|
|
|
ListLoopIn: client.Store.FetchLoopInSwaps,
|
2023-04-11 12:14:03 +00:00
|
|
|
LoopInTerms: client.LoopInTerms,
|
|
|
|
LoopOutTerms: client.LoopOutTerms,
|
2020-09-30 10:34:08 +00:00
|
|
|
MinimumConfirmations: minConfTarget,
|
2022-05-20 04:51:58 +00:00
|
|
|
PutLiquidityParams: client.Store.PutLiquidityParams,
|
|
|
|
FetchLiquidityParams: client.Store.FetchLiquidityParams,
|
2020-09-01 07:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return liquidity.NewManager(mngrCfg)
|
|
|
|
}
|