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

99 lines
1.9 KiB
Go
Raw Normal View History

package loop
2019-03-06 20:13:50 +00:00
import (
"context"
"time"
"github.com/lightninglabs/loop/lndclient"
"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 {
htlc *swap.Htlc
2019-03-06 20:13:50 +00:00
hash lntypes.Hash
height int32
log *swap.PrefixLog
2019-03-06 20:13:50 +00:00
lastUpdateTime time.Time
2019-05-15 12:01:27 +00:00
cost loopdb.SwapCost
state loopdb.SwapState
2019-03-06 20:13:50 +00:00
executeConfig
swapConfig
contract *loopdb.SwapContract
swapType swap.Type
2019-03-06 20:13:50 +00:00
}
func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
2019-04-04 10:20:45 +00:00
contract *loopdb.SwapContract, outputType swap.HtlcOutputType) (
*swapKit, error) {
2019-03-06 20:13:50 +00:00
// Compose expected on-chain swap script
htlc, err := swap.NewHtlc(
2019-03-06 20:13:50 +00:00
contract.CltvExpiry, contract.SenderKey,
2019-04-04 10:20:45 +00:00
contract.ReceiverKey, hash, outputType,
cfg.lnd.ChainParams,
2019-03-06 20:13:50 +00:00
)
if err != nil {
return nil, err
}
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
}
2019-04-04 10:20:45 +00:00
// Log htlc address for debugging.
log.Infof("Htlc address: %v", htlc.Address)
2019-03-06 20:13:50 +00:00
return &swapKit{
swapConfig: *cfg,
hash: hash,
log: log,
htlc: htlc,
state: loopdb.StateInitiated,
2019-03-06 20:13:50 +00:00
contract: contract,
swapType: swapType,
}, nil
}
// sendUpdate reports an update to the swap state.
func (s *swapKit) sendUpdate(ctx context.Context) error {
info := &SwapInfo{
SwapContract: *s.contract,
SwapHash: s.hash,
SwapType: s.swapType,
LastUpdate: s.lastUpdateTime,
SwapStateData: loopdb.SwapStateData{
State: s.state,
2019-05-15 12:01:27 +00:00
Cost: s.cost,
},
HtlcAddress: s.htlc.Address,
2019-03-06 20:13:50 +00:00
}
s.log.Infof("state %v", info.State)
select {
case s.statusChan <- *info:
case <-ctx.Done():
return ctx.Err()
}
return nil
}
type genericSwap interface {
execute(mainCtx context.Context, cfg *executeConfig,
height int32) error
}
type swapConfig struct {
lnd *lndclient.LndServices
store loopdb.SwapStore
2019-03-06 20:13:50 +00:00
server swapServerClient
}