Exemplo n.º 1
0
Arquivo: outputs.go Projeto: mm3/Sia
// findOutputs returns a set of spendable outputs that add up to at least
// `amount` of coins, returning an error if it cannot. It also returns the
// `total`, which is the sum of all the outputs that were found, since it's
// unlikely that it will equal amount exaclty.
func (w *Wallet) findOutputs(amount types.Currency) (knownOutputs []*knownOutput, total types.Currency, err error) {
	if amount.IsZero() {
		err = errors.New("cannot fund amount <= 0")
		return
	}

	// Iterate through all outputs until enough coins have been assembled.
	for _, key := range w.keys {
		if !key.spendable {
			continue
		}
		for _, knownOutput := range key.outputs {
			if !knownOutput.spendable {
				continue
			}
			if knownOutput.age > w.age-AgeDelay {
				continue
			}
			total = total.Add(knownOutput.output.Value)
			knownOutputs = append(knownOutputs, knownOutput)

			if total.Cmp(amount) >= 0 {
				return
			}
		}
	}

	// This code will only be reached if total < amount, meaning insufficient
	// funds.
	err = modules.LowBalanceErr
	return
}