loop/swap.go

102 lines
2 KiB
Go
Raw Permalink Normal View History

package loop
2019-03-06 21:13:50 +01:00
import (
"context"
"time"
"github.com/lightninglabs/lndclient"
2025-01-09 17:08:33 +01:00
"github.com/lightninglabs/loop/assets"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap"
2023-11-14 02:52:39 +02:00
"github.com/lightninglabs/loop/utils"
"github.com/lightningnetwork/lnd/clock"
2019-03-06 21:13:50 +01:00
"github.com/lightningnetwork/lnd/lntypes"
)
type swapKit struct {
swapConfig
2019-03-06 21:13:50 +01:00
hash lntypes.Hash
height int32 //nolint:structcheck
2019-03-06 21:13:50 +01:00
log *swap.PrefixLog
2019-03-06 21:13:50 +01:00
lastUpdateTime time.Time
cost loopdb.SwapCost
state loopdb.SwapState
2019-03-06 21:13:50 +01:00
contract *loopdb.SwapContract
swapType swap.Type
2019-03-06 21:13:50 +01:00
}
func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
contract *loopdb.SwapContract) *swapKit {
2019-03-06 21:13:50 +01:00
log := &swap.PrefixLog{
2019-03-06 21:13:50 +01:00
Hash: hash,
2019-10-28 17:06:07 +01:00
Logger: log,
2019-03-06 21:13:50 +01:00
}
return &swapKit{
swapConfig: *cfg,
hash: hash,
log: log,
state: loopdb.StateInitiated,
2019-03-06 21:13:50 +01:00
contract: contract,
swapType: swapType,
}
}
// IsTaprootSwap returns true if the swap referenced by the passed swap contract
// uses the v3 (taproot) htlc.
func IsTaprootSwap(swapContract *loopdb.SwapContract) bool {
version := utils.GetHtlcScriptVersion(swapContract.ProtocolVersion)
return version == swap.HtlcV3
2019-03-06 21:13:50 +01:00
}
// swapInfo constructs and returns a filled SwapInfo from
// the swapKit.
func (s *swapKit) swapInfo() *SwapInfo {
return &SwapInfo{
2019-03-06 21:13:50 +01:00
SwapContract: *s.contract,
SwapHash: s.hash,
SwapType: s.swapType,
LastUpdate: s.lastUpdateTime,
SwapStateData: loopdb.SwapStateData{
State: s.state,
2019-05-15 14:01:27 +02:00
Cost: s.cost,
},
2019-03-06 21:13:50 +01:00
}
}
type genericSwap interface {
execute(mainCtx context.Context, cfg *executeConfig,
height int32) error
}
type swapConfig struct {
lnd *lndclient.LndServices
store loopdb.SwapStore
2019-03-06 21:13:50 +01:00
server swapServerClient
2025-01-09 17:08:33 +01:00
assets *assets.TapdClient
clock clock.Clock
2019-03-06 21:13:50 +01:00
}
2020-06-08 12:53:07 +02:00
func newSwapConfig(lnd *lndclient.LndServices, store loopdb.SwapStore,
server swapServerClient, assets *assets.TapdClient,
clock clock.Clock) *swapConfig {
2020-06-08 12:53:07 +02:00
return &swapConfig{
lnd: lnd,
store: store,
server: server,
2025-01-09 17:08:33 +01:00
assets: assets,
clock: clock,
2020-06-08 12:53:07 +02:00
}
}