|
| 1 | +package staticaddr |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/btcsuite/btcd/wire" |
| 8 | + "github.com/lightninglabs/lndclient" |
| 9 | + "github.com/lightninglabs/loop/fsm" |
| 10 | + "github.com/lightninglabs/loop/staticaddr/script" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + defaultConfTarget = 3 |
| 15 | +) |
| 16 | + |
| 17 | +// SweepExpiredDepositAction ... |
| 18 | +func (f *FSM) SweepExpiredDepositAction( |
| 19 | + eventCtx fsm.EventContext) fsm.EventType { |
| 20 | + |
| 21 | + depoCtx, ok := eventCtx.(*DepositContext) |
| 22 | + if !ok { |
| 23 | + return f.HandleError(fsm.ErrInvalidContextType) |
| 24 | + } |
| 25 | + f.Debugf("SweepExpiredDepositAction: %v", depoCtx) |
| 26 | + |
| 27 | + msgTx := wire.NewMsgTx(2) |
| 28 | + |
| 29 | + msgTx.AddTxIn(&wire.TxIn{ |
| 30 | + PreviousOutPoint: f.deposit.OutPoint, |
| 31 | + Sequence: f.addressParameters.Expiry, |
| 32 | + SignatureScript: nil, |
| 33 | + }) |
| 34 | + |
| 35 | + // Estimate the fee rate of an expiry spend transaction. |
| 36 | + feeRateEstimator, err := f.cfg.WalletKit.EstimateFeeRate( |
| 37 | + f.ctx, defaultConfTarget, |
| 38 | + ) |
| 39 | + if err != nil { |
| 40 | + return f.HandleError(fmt.Errorf("timeout sweep fee "+ |
| 41 | + "estimation failed: %v", err)) |
| 42 | + } |
| 43 | + |
| 44 | + weight := script.ExpirySpendWeight() |
| 45 | + |
| 46 | + fee := feeRateEstimator.FeeForWeight(weight) |
| 47 | + |
| 48 | + // We cap the fee at 20% of the deposit value. |
| 49 | + if fee > f.deposit.Value/5 { |
| 50 | + return f.HandleError(errors.New("fee is higher than 20% of " + |
| 51 | + "deposit value")) |
| 52 | + } |
| 53 | + |
| 54 | + output := &wire.TxOut{ |
| 55 | + Value: int64(f.deposit.Value - fee), |
| 56 | + PkScript: f.deposit.TimeOutSweepPkScript, |
| 57 | + } |
| 58 | + msgTx.AddTxOut(output) |
| 59 | + |
| 60 | + signDesc, err := f.SignDescriptor() |
| 61 | + if err != nil { |
| 62 | + return f.HandleError(err) |
| 63 | + } |
| 64 | + |
| 65 | + txOut := &wire.TxOut{ |
| 66 | + Value: int64(f.deposit.Value), |
| 67 | + PkScript: f.addressParameters.PkScript, |
| 68 | + } |
| 69 | + |
| 70 | + prevOut := []*wire.TxOut{txOut} |
| 71 | + |
| 72 | + rawSigs, err := f.cfg.Signer.SignOutputRaw( |
| 73 | + f.ctx, msgTx, []*lndclient.SignDescriptor{&signDesc}, prevOut, |
| 74 | + ) |
| 75 | + if err != nil { |
| 76 | + return f.HandleError(err) |
| 77 | + } |
| 78 | + |
| 79 | + sig := rawSigs[0] |
| 80 | + msgTx.TxIn[0].Witness, err = f.staticAddress.GenTimeoutWitness(sig) |
| 81 | + if err != nil { |
| 82 | + return f.HandleError(err) |
| 83 | + } |
| 84 | + |
| 85 | + err = f.cfg.WalletKit.PublishTransaction( |
| 86 | + f.ctx, msgTx, f.deposit.OutPoint.Hash.String()+"-close-sweep", |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return f.HandleError(err) |
| 90 | + } |
| 91 | + |
| 92 | + f.Debugf("publishing timeout sweep with txid: %v", msgTx.TxHash()) |
| 93 | + |
| 94 | + return OnSwept |
| 95 | +} |
0 commit comments