Exemple #1
0
// CalculateAddressBalance sums the amounts of all unspent transaction
// outputs to a single address's pubkey hash and returns the balance
// as a float64.
//
// If confirmations is 0, all UTXOs, even those not present in a
// block (height -1), will be used to get the balance.  Otherwise,
// a UTXO must be in a block.  If confirmations is 1 or greater,
// the balance will be calculated based on how many how many blocks
// include a UTXO.
func (a *Account) CalculateAddressBalance(addr btcutil.Address, confirms int) float64 {
	bs, err := GetCurBlock()
	if bs.Height == int32(btcutil.BlockHeightUnknown) || err != nil {
		return 0.
	}

	var bal btcutil.Amount
	unspent, err := a.TxStore.UnspentOutputs()
	if err != nil {
		return 0.
	}
	for _, credit := range unspent {
		if credit.Confirmed(confirms, bs.Height) {
			// We only care about the case where len(addrs) == 1, and err
			// will never be non-nil in that case
			_, addrs, _, _ := credit.Addresses(activeNet.Params)
			if len(addrs) != 1 {
				continue
			}
			if addrs[0].EncodeAddress() == addr.EncodeAddress() {
				bal += credit.Amount()
			}
		}
	}
	return bal.ToUnit(btcutil.AmountBTC)
}
Exemple #2
0
// TotalReceived iterates through an account's transaction history, returning the
// total amount of bitcoins received for any account address.  Amounts received
// through multisig transactions are ignored.
func (a *Account) TotalReceived(confirms int) (float64, error) {
	bs, err := GetCurBlock()
	if err != nil {
		return 0, err
	}

	var amount btcutil.Amount
	for _, r := range a.TxStore.Records() {
		for _, c := range r.Credits() {
			// Ignore change.
			if c.Change() {
				continue
			}

			// Tally if the appropiate number of block confirmations have passed.
			if c.Confirmed(confirms, bs.Height) {
				amount += c.Amount()
			}
		}
	}

	return amount.ToUnit(btcutil.AmountBTC), nil
}