OpenBazaar/spvwallet

Address Balance

andreygrehov opened this issue · 3 comments

Is there any way to get the balance of the given address rather than the entire wallet?

There's no code to do so. You could patch it to scan the utxo db. Or just grab all the transactions and calculate the balance from that.

@cpacia thanks. Does something like that look right to you?

func (w *BitcoinWallet) AddressBalance(addr btc.Address) (confirmed, unconfirmed int64) {
	utxos, _ := w.db.Utxos().GetAll()
	var filteredUtxos []wi.Utxo
	for _, utxo := range utxos {
		utxoAddr, _ := w.ScriptToAddress(utxo.ScriptPubkey)
		if addr.String() == utxoAddr.String() {
			filteredUtxos = append(filteredUtxos, utxo)
		}
	}
	txns, _ := w.db.Txns().GetAll(false)
	return util.CalcBalance(filteredUtxos, txns)
}

yeah I think so