Skip to content

Instantly share code, notes, and snippets.

@gsalgado
Last active August 29, 2015 14:06
Show Gist options
  • Save gsalgado/29a13cb17eb501d77bb0 to your computer and use it in GitHub Desktop.
Save gsalgado/29a13cb17eb501d77bb0 to your computer and use it in GitHub Desktop.
type TxConfig struct {
selector coinset.CoinSelector
eligibleOutputs []txstore.Credit
}
func createTx(config TxConfig, outputs map[string]btcutil.Amount, changeAddr btcutil.Address) *MsgTx {
msgtx := btcwire.NewMsgTx()
addOutputs(msgtx, outputs)
// Calculate the total amount needed to fulfil all outputs.
amount := sumAmounts(outputs)
// This one is pluggable; we just need a custom CoinSelector for VPs
chosen := config.selector.CoinSelect(amount, coins)
inputAmount := 0
for _, coin := chosen {
inputAmount += coin.amount
tx.AddTxIn(btcwire.NewTxIn(coin, nil))
}
// Estimate fee. We may be able to use the standard one for VPs
fee := estimateFee(...)
//
change := inputAmount - amount - fee
if change > 0 {
addOutputs(msgtx, map[string]btcutil.Amount{changeAddr: change})
}
return msgtx
}
func (w *Wallet) txToPairs(pairs map[string]btcutil.Amount, minconf int) (*CreatedTx, error) {
bs, err := w.chainSvr.BlockStamp()
if err != nil {
return nil, err
}
eligible, err := w.findEligibleOuptuts(minconf, bs)
if err != nil {
return nil, err
}
cfg := TxConfig{selector: MinNumberCoinSelector, eligibleOutputs: eligible}
changeAddr := w.KeyStore.ChangeAddress(bs)
msgtx := createTx(cfg, pairs, changeAddr)
...
}
@larshesel
Copy link

We still need to add the loop over the which inputs to include and calculating the minimum fee. Looking at the btcwallet code it seems they are doing this:
// 1. start with fee = 0
// 2. construct new tx based on the utxos
// 3. calculate minFee := minFee(tx)
// 3. if minFee < fee then done otherwise set fee := minFee and goto 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment