2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
|
|
|
"github.com/lightninglabs/loop/swap"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type swapKit struct {
|
|
|
|
hash lntypes.Hash
|
|
|
|
|
|
|
|
height int32
|
|
|
|
|
2019-10-09 10:36:16 +00:00
|
|
|
log *swap.PrefixLog
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
lastUpdateTime time.Time
|
2020-04-29 17:47:27 +00:00
|
|
|
|
|
|
|
cost loopdb.SwapCost
|
|
|
|
|
|
|
|
state loopdb.SwapState
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
contract *loopdb.SwapContract
|
2020-04-29 17:47:27 +00:00
|
|
|
|
2019-10-09 10:36:16 +00:00
|
|
|
swapType swap.Type
|
2020-04-29 17:47:27 +00:00
|
|
|
|
|
|
|
swapConfig
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 10:36:16 +00:00
|
|
|
func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
|
2020-04-29 17:47:27 +00:00
|
|
|
contract *loopdb.SwapContract) *swapKit {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-09 10:36:16 +00:00
|
|
|
log := &swap.PrefixLog{
|
2019-03-06 20:13:50 +00:00
|
|
|
Hash: hash,
|
2019-10-28 16:06:07 +00:00
|
|
|
Logger: log,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &swapKit{
|
|
|
|
swapConfig: *cfg,
|
|
|
|
hash: hash,
|
|
|
|
log: log,
|
2019-03-07 04:32:24 +00:00
|
|
|
state: loopdb.StateInitiated,
|
2019-03-06 20:13:50 +00:00
|
|
|
contract: contract,
|
|
|
|
swapType: swapType,
|
2020-04-29 17:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getHtlc composes and returns the on-chain swap script.
|
|
|
|
func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {
|
|
|
|
return swap.NewHtlc(
|
|
|
|
s.contract.CltvExpiry, s.contract.SenderKey,
|
|
|
|
s.contract.ReceiverKey, s.hash, outputType,
|
|
|
|
s.swapConfig.lnd.ChainParams,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
// swapInfo constructs and returns a filled SwapInfo from
|
|
|
|
// the swapKit.
|
|
|
|
func (s *swapKit) swapInfo() *SwapInfo {
|
|
|
|
return &SwapInfo{
|
2019-03-06 20:13:50 +00:00
|
|
|
SwapContract: *s.contract,
|
|
|
|
SwapHash: s.hash,
|
|
|
|
SwapType: s.swapType,
|
|
|
|
LastUpdate: s.lastUpdateTime,
|
2019-05-15 11:55:41 +00:00
|
|
|
SwapStateData: loopdb.SwapStateData{
|
|
|
|
State: s.state,
|
2019-05-15 12:01:27 +00:00
|
|
|
Cost: s.cost,
|
2019-05-15 11:55:41 +00:00
|
|
|
},
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type genericSwap interface {
|
|
|
|
execute(mainCtx context.Context, cfg *executeConfig,
|
|
|
|
height int32) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type swapConfig struct {
|
|
|
|
lnd *lndclient.LndServices
|
2019-03-07 04:32:24 +00:00
|
|
|
store loopdb.SwapStore
|
2019-03-06 20:13:50 +00:00
|
|
|
server swapServerClient
|
|
|
|
}
|
2020-06-08 10:53:07 +00:00
|
|
|
|
|
|
|
func newSwapConfig(lnd *lndclient.LndServices, store loopdb.SwapStore,
|
|
|
|
server swapServerClient) *swapConfig {
|
|
|
|
|
|
|
|
return &swapConfig{
|
|
|
|
lnd: lnd,
|
|
|
|
store: store,
|
|
|
|
server: server,
|
|
|
|
}
|
|
|
|
}
|