Skip to content

Commit fbc9e29

Browse files
committed
tapgarden: refactor fundGenesisPsbt to pass wallet funding as closure
Extract the wallet funding call into a closure that is passed as an argument. This prepares fundGenesisPsbt to become a standalone function, making it easier to call in unit tests.
1 parent 74bd5d8 commit fbc9e29

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

tapgarden/planter.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,10 @@ func (c *ChainPlanter) anchorTxFeeRate(ctx context.Context,
927927
return feeRate, nil
928928
}
929929

930+
// WalletFundPsbt is a function that funds a PSBT packet.
931+
type WalletFundPsbt = func(ctx context.Context,
932+
anchorPkt psbt.Packet) (tapsend.FundedPsbt, error)
933+
930934
// fundGenesisPsbt generates a PSBT packet we'll use to create an asset. In
931935
// order to be able to create an asset, we need an initial genesis outpoint. To
932936
// obtain this we'll ask the wallet to fund a PSBT template for GenesisAmtSats
@@ -935,7 +939,7 @@ func (c *ChainPlanter) anchorTxFeeRate(ctx context.Context,
935939
// that's dependent on the genesis outpoint.
936940
func (c *ChainPlanter) fundGenesisPsbt(ctx context.Context,
937941
batchKey asset.SerializedKey,
938-
feeRate chainfee.SatPerKWeight) (FundedMintAnchorPsbt, error) {
942+
walletFundPsbt WalletFundPsbt) (FundedMintAnchorPsbt, error) {
939943

940944
var zero FundedMintAnchorPsbt
941945
log.Infof("Attempting to fund batch: %x", batchKey)
@@ -979,9 +983,7 @@ func (c *ChainPlanter) fundGenesisPsbt(ctx context.Context,
979983
}
980984
log.Tracef("Unfunded batch anchor PSBT: %v", spew.Sdump(genesisPkt))
981985

982-
fundedGenesisPkt, err := c.cfg.Wallet.FundPsbt(
983-
ctx, &genesisPkt, 1, feeRate, -1,
984-
)
986+
fundedGenesisPkt, err := walletFundPsbt(ctx, genesisPkt)
985987
if err != nil {
986988
return zero, fmt.Errorf("unable to fund psbt: %w", err)
987989
}
@@ -997,7 +999,7 @@ func (c *ChainPlanter) fundGenesisPsbt(ctx context.Context,
997999

9981000
// Classify anchor transaction output indexes.
9991001
anchorOutIndexes, err := anchorTxOutputIndexes(
1000-
*fundedGenesisPkt, preCommitmentTxOut,
1002+
fundedGenesisPkt, preCommitmentTxOut,
10011003
)
10021004
if err != nil {
10031005
return zero, fmt.Errorf("unable to determine output indexes: "+
@@ -1034,7 +1036,7 @@ func (c *ChainPlanter) fundGenesisPsbt(ctx context.Context,
10341036

10351037
// Formulate a funded minting anchor PSBT from the funded PSBT.
10361038
fundedMintAnchorPsbt, err := NewFundedMintAnchorPsbt(
1037-
*fundedGenesisPkt, anchorOutIndexes, preCommitmentOut,
1039+
fundedGenesisPkt, anchorOutIndexes, preCommitmentOut,
10381040
)
10391041
if err != nil {
10401042
return zero, fmt.Errorf("unable to create funded minting "+
@@ -2024,7 +2026,27 @@ func (c *ChainPlanter) fundBatch(ctx context.Context, params FundParams,
20242026
}
20252027

20262028
batchKey := asset.ToSerialized(batch.BatchKey.PubKey)
2027-
mintAnchorTx, err := c.fundGenesisPsbt(ctx, batchKey, feeRate)
2029+
2030+
// walletFundPsbt is a closure that will be used to fund the
2031+
// batch with the specified fee rate.
2032+
walletFundPsbt := func(ctx context.Context,
2033+
anchorPkt psbt.Packet) (tapsend.FundedPsbt, error) {
2034+
2035+
var zero tapsend.FundedPsbt
2036+
2037+
fundedPkt, err := c.cfg.Wallet.FundPsbt(
2038+
ctx, &anchorPkt, 1, feeRate, -1,
2039+
)
2040+
if err != nil {
2041+
return zero, err
2042+
}
2043+
2044+
return *fundedPkt, nil
2045+
}
2046+
2047+
mintAnchorTx, err := c.fundGenesisPsbt(
2048+
ctx, batchKey, walletFundPsbt,
2049+
)
20282050
if err != nil {
20292051
return fmt.Errorf("unable to fund minting PSBT for "+
20302052
"batch: %x %w", batchKey[:], err)

0 commit comments

Comments
 (0)