2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-16 00:12:52 +00:00
loop/loopd/utils.go

133 lines
3.7 KiB
Go
Raw Normal View History

package loopd
2019-03-06 20:13:50 +00:00
import (
"context"
"fmt"
"github.com/btcsuite/btcd/btcutil"
2023-10-12 15:04:09 +00:00
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/liquidity"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/sweepbatcher"
"github.com/lightningnetwork/lnd/clock"
"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,
sweeperDb sweepbatcher.BatcherStore, lnd *lndclient.LndServices) (
*loop.Client, func(), error) {
2020-04-27 12:03:23 +00:00
clientConfig := &loop.ClientConfig{
ServerAddress: cfg.Server.Host,
ProxyAddress: cfg.Server.Proxy,
SwapServerNoTLS: cfg.Server.NoTLS,
TLSPathServer: cfg.Server.TLSPath,
Lnd: lnd,
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-10-12 15:04:09 +00:00
swapClient, cleanUp, err := loop.NewClient(
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
var (
2023-10-12 15:04:09 +00:00
db loopdb.SwapStore
err error
baseDb loopdb.BaseDB
)
switch cfg.DatabaseBackend {
case DatabaseBackendSqlite:
log.Infof("Opening sqlite3 database at: %v",
cfg.Sqlite.DatabaseFileName)
db, err = loopdb.NewSqliteStore(
2023-10-12 15:04:09 +00:00
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
case DatabaseBackendPostgres:
log.Infof("Opening postgres database at: %v",
cfg.Postgres.DSN(true))
db, err = loopdb.NewPostgresStore(
2023-10-12 15:04:09 +00:00
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
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)
}
2023-10-12 15:04:09 +00:00
return db, &baseDb, nil
2019-03-06 20:13:50 +00:00
}
func getLiquidityManager(client *loop.Client) *liquidity.Manager {
mngrCfg := &liquidity.Config{
AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker),
LoopOut: client.LoopOut,
LoopIn: client.LoopIn,
Restrictions: func(ctx context.Context, swapType swap.Type,
initiator string) (*liquidity.Restrictions, error) {
if swapType == swap.TypeOut {
outTerms, err := client.Server.GetLoopOutTerms(ctx, initiator)
if err != nil {
return nil, err
}
return liquidity.NewRestrictions(
outTerms.MinSwapAmount, outTerms.MaxSwapAmount,
), nil
}
inTerms, err := client.Server.GetLoopInTerms(ctx, initiator)
if err != nil {
return nil, err
}
return liquidity.NewRestrictions(
inTerms.MinSwapAmount, inTerms.MaxSwapAmount,
), nil
},
Lnd: client.LndServices,
Clock: clock.NewDefaultClock(),
LoopOutQuote: client.LoopOutQuote,
LoopInQuote: client.LoopInQuote,
ListLoopOut: client.Store.FetchLoopOutSwaps,
GetLoopOut: client.Store.FetchLoopOutSwap,
ListLoopIn: client.Store.FetchLoopInSwaps,
LoopInTerms: client.LoopInTerms,
LoopOutTerms: client.LoopOutTerms,
MinimumConfirmations: minConfTarget,
PutLiquidityParams: client.Store.PutLiquidityParams,
FetchLiquidityParams: client.Store.FetchLiquidityParams,
}
return liquidity.NewManager(mngrCfg)
}