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"
|
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"
|
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-05-19 13:09:38 +00:00
|
|
|
func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client,
|
2020-01-10 11:13:39 +00:00
|
|
|
func(), error) {
|
2019-03-07 04:32:24 +00:00
|
|
|
|
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,
|
2023-05-19 13:09:38 +00:00
|
|
|
MaxLsatCost: btcutil.Amount(cfg.MaxLSATCost),
|
|
|
|
MaxLsatFee: btcutil.Amount(cfg.MaxLSATFee),
|
|
|
|
LoopOutMaxParts: cfg.LoopOutMaxParts,
|
|
|
|
TotalPaymentTimeout: cfg.TotalPaymentTimeout,
|
|
|
|
MaxPaymentRetries: cfg.MaxPaymentRetries,
|
2020-04-27 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 13:09:38 +00:00
|
|
|
// Now that we know where the database will live, we'll go ahead and
|
|
|
|
// open up the default implementation of it.
|
|
|
|
var (
|
|
|
|
db loopdb.SwapStore
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch cfg.DatabaseBackend {
|
|
|
|
case DatabaseBackendSqlite:
|
|
|
|
log.Infof("Opening sqlite3 database at: %v",
|
|
|
|
cfg.Sqlite.DatabaseFileName)
|
|
|
|
db, err = loopdb.NewSqliteStore(
|
|
|
|
cfg.Sqlite, clientConfig.Lnd.ChainParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
case DatabaseBackendPostgres:
|
|
|
|
log.Infof("Opening postgres database at: %v",
|
|
|
|
cfg.Postgres.DSN(true))
|
|
|
|
db, err = loopdb.NewPostgresStore(
|
|
|
|
cfg.Postgres, clientConfig.Lnd.ChainParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("unknown database backend: %s",
|
|
|
|
cfg.DatabaseBackend)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to open database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
swapClient, cleanUp, err := loop.NewClient(
|
|
|
|
cfg.DataDir, db, clientConfig,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return swapClient, cleanUp, nil
|
|
|
|
}
|
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,
|
2021-02-03 06:54:50 +00:00
|
|
|
Restrictions: func(ctx context.Context,
|
|
|
|
swapType swap.Type) (*liquidity.Restrictions, error) {
|
2020-09-01 07:58:08 +00:00
|
|
|
|
2021-02-03 06:54:50 +00:00
|
|
|
if swapType == swap.TypeOut {
|
|
|
|
outTerms, err := client.Server.GetLoopOutTerms(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return liquidity.NewRestrictions(
|
|
|
|
outTerms.MinSwapAmount, outTerms.MaxSwapAmount,
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
inTerms, err := client.Server.GetLoopInTerms(ctx)
|
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)
|
|
|
|
}
|