You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
1.8 KiB

  1. package loop
  2. import (
  3. "context"
  4. "time"
  5. "github.com/lightninglabs/loop/lndclient"
  6. "github.com/lightninglabs/loop/loopdb"
  7. "github.com/lightninglabs/loop/swap"
  8. "github.com/lightningnetwork/lnd/lntypes"
  9. )
  10. type swapKit struct {
  11. htlc *swap.Htlc
  12. hash lntypes.Hash
  13. height int32
  14. log *SwapLog
  15. lastUpdateTime time.Time
  16. cost loopdb.SwapCost
  17. state loopdb.SwapState
  18. executeConfig
  19. swapConfig
  20. contract *loopdb.SwapContract
  21. swapType Type
  22. }
  23. func newSwapKit(hash lntypes.Hash, swapType Type, cfg *swapConfig,
  24. contract *loopdb.SwapContract, outputType swap.HtlcOutputType) (
  25. *swapKit, error) {
  26. // Compose expected on-chain swap script
  27. htlc, err := swap.NewHtlc(
  28. contract.CltvExpiry, contract.SenderKey,
  29. contract.ReceiverKey, hash, outputType,
  30. cfg.lnd.ChainParams,
  31. )
  32. if err != nil {
  33. return nil, err
  34. }
  35. log := &SwapLog{
  36. Hash: hash,
  37. Logger: logger,
  38. }
  39. // Log htlc address for debugging.
  40. log.Infof("Htlc address: %v", htlc.Address)
  41. return &swapKit{
  42. swapConfig: *cfg,
  43. hash: hash,
  44. log: log,
  45. htlc: htlc,
  46. state: loopdb.StateInitiated,
  47. contract: contract,
  48. swapType: swapType,
  49. }, nil
  50. }
  51. // sendUpdate reports an update to the swap state.
  52. func (s *swapKit) sendUpdate(ctx context.Context) error {
  53. info := &SwapInfo{
  54. SwapContract: *s.contract,
  55. SwapHash: s.hash,
  56. SwapType: s.swapType,
  57. LastUpdate: s.lastUpdateTime,
  58. SwapStateData: loopdb.SwapStateData{
  59. State: s.state,
  60. Cost: s.cost,
  61. },
  62. HtlcAddress: s.htlc.Address,
  63. }
  64. s.log.Infof("state %v", info.State)
  65. select {
  66. case s.statusChan <- *info:
  67. case <-ctx.Done():
  68. return ctx.Err()
  69. }
  70. return nil
  71. }
  72. type genericSwap interface {
  73. execute(mainCtx context.Context, cfg *executeConfig,
  74. height int32) error
  75. }
  76. type swapConfig struct {
  77. lnd *lndclient.LndServices
  78. store loopdb.SwapStore
  79. server swapServerClient
  80. }