2020-01-03 14:01:31 +01:00
|
|
|
package loopd
|
2019-03-06 21:13:50 +01:00
|
|
|
|
|
|
|
|
import (
|
2020-09-01 09:58:08 +02:00
|
|
|
"context"
|
2023-05-19 15:09:38 +02:00
|
|
|
"fmt"
|
2026-04-10 23:51:19 -05:00
|
|
|
"slices"
|
2020-09-01 09:58:08 +02:00
|
|
|
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2023-10-12 17:04:09 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2024-04-24 13:50:24 -03:00
|
|
|
"github.com/lightninglabs/aperture/l402"
|
2020-06-17 22:25:57 +02:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-06 20:32:24 -08:00
|
|
|
"github.com/lightninglabs/loop"
|
2025-01-09 17:08:33 +01:00
|
|
|
"github.com/lightninglabs/loop/assets"
|
2020-09-01 09:58:08 +02:00
|
|
|
"github.com/lightninglabs/loop/liquidity"
|
2023-05-19 15:09:38 +02:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2026-04-10 23:51:19 -05:00
|
|
|
"github.com/lightninglabs/loop/staticaddr/loopin"
|
2021-02-03 08:54:50 +02:00
|
|
|
"github.com/lightninglabs/loop/swap"
|
2024-01-18 15:18:36 +01:00
|
|
|
"github.com/lightninglabs/loop/sweepbatcher"
|
2020-09-30 12:34:05 +02:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2020-10-12 13:34:55 +02:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
2019-03-06 21:13:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// getClient returns an instance of the swap client.
|
2023-10-12 17:04:09 +02:00
|
|
|
func getClient(cfg *Config, swapDb loopdb.SwapStore,
|
2025-01-09 17:08:33 +01:00
|
|
|
sweeperDb sweepbatcher.BatcherStore, lnd *lndclient.LndServices,
|
|
|
|
|
assets *assets.TapdClient) (*loop.Client, func(), error) {
|
2019-03-06 20:32:24 -08:00
|
|
|
|
2024-04-24 13:50:24 -03: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 14:03:23 +02:00
|
|
|
clientConfig := &loop.ClientConfig{
|
2024-10-17 13:29:43 +02:00
|
|
|
ServerAddress: cfg.Server.Host,
|
|
|
|
|
ProxyAddress: cfg.Server.Proxy,
|
|
|
|
|
SwapServerNoTLS: cfg.Server.NoTLS,
|
|
|
|
|
TLSPathServer: cfg.Server.TLSPath,
|
|
|
|
|
Lnd: lnd,
|
2025-01-09 17:08:33 +01:00
|
|
|
AssetClient: assets,
|
2024-10-17 13:29:43 +02:00
|
|
|
MaxL402Cost: btcutil.Amount(cfg.MaxL402Cost),
|
|
|
|
|
MaxL402Fee: btcutil.Amount(cfg.MaxL402Fee),
|
|
|
|
|
LoopOutMaxParts: cfg.LoopOutMaxParts,
|
2025-06-17 14:01:59 -03:00
|
|
|
SkippedTxns: cfg.SkippedTxns,
|
2024-10-17 13:29:43 +02:00
|
|
|
TotalPaymentTimeout: cfg.TotalPaymentTimeout,
|
|
|
|
|
MaxPaymentRetries: cfg.MaxPaymentRetries,
|
|
|
|
|
MaxStaticAddrHtlcFeePercentage: cfg.MaxStaticAddrHtlcFeePercentage,
|
|
|
|
|
MaxStaticAddrHtlcBackupFeePercentage: cfg.MaxStaticAddrHtlcBackupFeePercentage,
|
2020-04-27 14:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-24 13:50:24 -03:00
|
|
|
if cfg.MaxL402Cost == defaultCost && cfg.MaxLSATCost != 0 {
|
2025-03-10 19:20:20 -03:00
|
|
|
warnf("Option maxlsatcost is deprecated and will be " +
|
2024-04-24 13:50:24 -03:00
|
|
|
"removed. Switch to maxl402cost.")
|
|
|
|
|
clientConfig.MaxL402Cost = btcutil.Amount(cfg.MaxLSATCost)
|
|
|
|
|
}
|
|
|
|
|
if cfg.MaxL402Fee == defaultFee && cfg.MaxLSATFee != 0 {
|
2025-03-10 19:20:20 -03:00
|
|
|
warnf("Option maxlsatfee is deprecated and will be " +
|
2024-04-24 13:50:24 -03:00
|
|
|
"removed. Switch to maxl402fee.")
|
|
|
|
|
clientConfig.MaxL402Fee = btcutil.Amount(cfg.MaxLSATFee)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 17:04:09 +02:00
|
|
|
swapClient, cleanUp, err := loop.NewClient(
|
2024-01-18 15:18:36 +01:00
|
|
|
cfg.DataDir, swapDb, sweeperDb, clientConfig,
|
2023-10-12 17:04:09 +02: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 15:09:38 +02:00
|
|
|
var (
|
2023-10-12 17:04:09 +02:00
|
|
|
db loopdb.SwapStore
|
|
|
|
|
err error
|
|
|
|
|
baseDb loopdb.BaseDB
|
2023-05-19 15:09:38 +02:00
|
|
|
)
|
|
|
|
|
switch cfg.DatabaseBackend {
|
|
|
|
|
case DatabaseBackendSqlite:
|
2025-03-10 19:20:20 -03:00
|
|
|
infof("Opening sqlite3 database at: %v",
|
2023-05-19 15:09:38 +02:00
|
|
|
cfg.Sqlite.DatabaseFileName)
|
2024-03-01 10:02:29 +01:00
|
|
|
|
|
|
|
|
db, err = loopdb.NewSqliteStore(cfg.Sqlite, chainParams)
|
2023-10-25 23:32:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
2023-10-12 17:04:09 +02:00
|
|
|
baseDb = *db.(*loopdb.SqliteSwapStore).BaseDB
|
2023-05-19 15:09:38 +02:00
|
|
|
|
|
|
|
|
case DatabaseBackendPostgres:
|
2025-03-10 19:20:20 -03:00
|
|
|
infof("Opening postgres database at: %v",
|
2023-05-19 15:09:38 +02:00
|
|
|
cfg.Postgres.DSN(true))
|
2024-03-01 10:02:29 +01:00
|
|
|
|
|
|
|
|
db, err = loopdb.NewPostgresStore(cfg.Postgres, chainParams)
|
2023-10-25 23:32:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
2023-10-12 17:04:09 +02:00
|
|
|
baseDb = *db.(*loopdb.PostgresStore).BaseDB
|
2023-05-19 15:09:38 +02:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return nil, nil, fmt.Errorf("unknown database backend: %s",
|
|
|
|
|
cfg.DatabaseBackend)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 17:04:09 +02:00
|
|
|
return db, &baseDb, nil
|
2019-03-06 21:13:50 +01:00
|
|
|
}
|
2020-09-01 09:58:08 +02:00
|
|
|
|
2026-04-10 23:51:19 -05:00
|
|
|
func getLiquidityManager(client *loop.Client,
|
|
|
|
|
staticLoopInManager *loopin.Manager) *liquidity.Manager {
|
|
|
|
|
|
|
|
|
|
listStaticLoopIn := func(
|
|
|
|
|
ctx context.Context) ([]*liquidity.StaticLoopInInfo, error) {
|
|
|
|
|
|
|
|
|
|
if staticLoopInManager == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
swaps, err := staticLoopInManager.GetAllSwaps(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := make(
|
|
|
|
|
[]*liquidity.StaticLoopInInfo, 0, len(swaps),
|
|
|
|
|
)
|
|
|
|
|
for _, staticSwap := range swaps {
|
|
|
|
|
state := staticSwap.GetState()
|
|
|
|
|
pending := slices.Contains(loopin.PendingStates, state)
|
|
|
|
|
failed := state == loopin.Failed ||
|
|
|
|
|
state == loopin.HtlcTimeoutSwept
|
|
|
|
|
|
|
|
|
|
result = append(result, &liquidity.StaticLoopInInfo{
|
|
|
|
|
Label: staticSwap.Label,
|
|
|
|
|
QuotedSwapFee: staticSwap.QuotedSwapFee,
|
|
|
|
|
HtlcTxFeeRate: staticSwap.HtlcTxFeeRate,
|
|
|
|
|
LastHop: staticSwap.LastHopVertex(),
|
|
|
|
|
LastUpdateTime: staticSwap.LastUpdateTime,
|
|
|
|
|
Pending: pending,
|
|
|
|
|
Failed: failed,
|
|
|
|
|
BlocksLoopIn: pending &&
|
|
|
|
|
state != loopin.PaymentReceived,
|
|
|
|
|
NumDeposits: len(staticSwap.Deposits),
|
|
|
|
|
HasChange: staticSwap.SelectedAmount > 0 &&
|
|
|
|
|
staticSwap.SelectedAmount <
|
|
|
|
|
staticSwap.TotalDepositAmount(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 09:58:08 +02:00
|
|
|
mngrCfg := &liquidity.Config{
|
2023-03-14 16:51:19 +02:00
|
|
|
AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker),
|
|
|
|
|
LoopOut: client.LoopOut,
|
|
|
|
|
LoopIn: client.LoopIn,
|
2023-06-20 21:52:07 +03:00
|
|
|
Restrictions: func(ctx context.Context, swapType swap.Type,
|
|
|
|
|
initiator string) (*liquidity.Restrictions, error) {
|
2020-09-01 09:58:08 +02:00
|
|
|
|
2021-02-03 08:54:50 +02:00
|
|
|
if swapType == swap.TypeOut {
|
2023-06-20 21:52:07 +03:00
|
|
|
outTerms, err := client.Server.GetLoopOutTerms(ctx, initiator)
|
2021-02-03 08:54:50 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return liquidity.NewRestrictions(
|
|
|
|
|
outTerms.MinSwapAmount, outTerms.MaxSwapAmount,
|
|
|
|
|
), nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 21:52:07 +03:00
|
|
|
inTerms, err := client.Server.GetLoopInTerms(ctx, initiator)
|
2020-09-01 09:58:08 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return liquidity.NewRestrictions(
|
2021-02-03 08:54:50 +02:00
|
|
|
inTerms.MinSwapAmount, inTerms.MaxSwapAmount,
|
2020-09-01 09:58:08 +02:00
|
|
|
), nil
|
|
|
|
|
},
|
2020-09-30 12:34:08 +02:00
|
|
|
Lnd: client.LndServices,
|
|
|
|
|
Clock: clock.NewDefaultClock(),
|
2020-09-30 12:34:09 +02:00
|
|
|
LoopOutQuote: client.LoopOutQuote,
|
2021-12-15 09:01:20 +02:00
|
|
|
LoopInQuote: client.LoopInQuote,
|
2020-09-30 12:34:08 +02:00
|
|
|
ListLoopOut: client.Store.FetchLoopOutSwaps,
|
2023-02-02 14:54:27 +02:00
|
|
|
GetLoopOut: client.Store.FetchLoopOutSwap,
|
2020-09-30 12:34:08 +02:00
|
|
|
ListLoopIn: client.Store.FetchLoopInSwaps,
|
2026-04-10 23:51:19 -05:00
|
|
|
ListStaticLoopIn: listStaticLoopIn,
|
2023-04-11 15:14:03 +03:00
|
|
|
LoopInTerms: client.LoopInTerms,
|
|
|
|
|
LoopOutTerms: client.LoopOutTerms,
|
2025-02-17 15:58:41 +01:00
|
|
|
GetAssetPrice: client.AssetClient.GetAssetPrice,
|
2020-09-30 12:34:08 +02:00
|
|
|
MinimumConfirmations: minConfTarget,
|
2022-05-20 12:51:58 +08:00
|
|
|
PutLiquidityParams: client.Store.PutLiquidityParams,
|
|
|
|
|
FetchLiquidityParams: client.Store.FetchLiquidityParams,
|
2020-09-01 09:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return liquidity.NewManager(mngrCfg)
|
|
|
|
|
}
|