Skip to content

Commit

Permalink
wallet: adopting changes for the new input selection
Browse files Browse the repository at this point in the history
The new NewUnsignedTransaction function requires all inputs
and the input selection logic for the input selection to work.
  • Loading branch information
ziggie1984 committed Feb 13, 2023
1 parent ad17117 commit 07ef713
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
12 changes: 7 additions & 5 deletions wallet/createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
return err
}

var inputSource txauthor.InputSource
// var inputSource txauthor.InputSource
// We need to define the Selection Strategy
var inputSelectionStrategy txauthor.InputSelectionStrategy = txauthor.ConstantSelection

switch coinSelectionStrategy {
// Pick largest outputs first.
case CoinSelectionLargest:
sort.Sort(sort.Reverse(byAmount(eligible)))
inputSource = makeInputSource(eligible)
inputSelectionStrategy = txauthor.PositiveYieldingSelection

// Select coins at random. This prevents the creation of ever
// smaller utxos over time that may never become economical to
Expand All @@ -170,12 +172,12 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
positivelyYielding[i], positivelyYielding[j] =
positivelyYielding[j], positivelyYielding[i]
})

inputSource = makeInputSource(positivelyYielding)
inputSelectionStrategy = txauthor.RandomSelection
eligible = positivelyYielding
}

tx, err = txauthor.NewUnsignedTransaction(
outputs, feeSatPerKb, inputSource, changeSource,
outputs, feeSatPerKb, eligible, inputSelectionStrategy, changeSource,
)
if err != nil {
return err
Expand Down
11 changes: 5 additions & 6 deletions wallet/psbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func (w *Wallet) FundPsbt(packet *psbt.Packet, keyScope *waddrmgr.KeyScope,
PkScript: utxo.PkScript,
}
}
inputSource := constantInputSource(credits)

// Build the TxCreateOption to retrieve the change scope.
opts := defaultTxCreateOptions()
Expand All @@ -197,25 +196,25 @@ func (w *Wallet) FundPsbt(packet *psbt.Packet, keyScope *waddrmgr.KeyScope,
dbtx, opts.changeKeyScope, account,
)
if err != nil {
return err
return fmt.Errorf("could not add change address to "+
"database: %v", err)
}

// Ask the txauthor to create a transaction with our
// selected coins. This will perform fee estimation and
// add a change output if necessary.
tx, err = txauthor.NewUnsignedTransaction(
txOut, feeSatPerKB, inputSource, changeSource,
txOut, feeSatPerKB, credits, txauthor.ConstantSelection, changeSource,
)
if err != nil {
return fmt.Errorf("fee estimation not "+
"successful: %v", err)
"successful: %w", err)
}

return nil
})
if err != nil {
return 0, fmt.Errorf("could not add change address to "+
"database: %v", err)
return 0, err
}
}

Expand Down

0 comments on commit 07ef713

Please sign in to comment.