3

I am building an application for which I wish users to be given a unique address to which they can freely deposit funds into their account. Meanwhile, I want to be able to both track their balance and use the bitcoins they've given me. Their balance is effectively the total amount of bitcoins that their assigned address has ever received (regardless of if the coins have been spent) minus whatever I've charged them, and I need a way to determine the first bit.

Also, my server does not have enough storage space to use bitcoind, so I've been using electrum, though I'm fine using another piece of software or even a web api so long as it's reasonably secure.

1 Answer 1

1

Using electrum, after instantiating a Wallet instance, the code to achieve this looks like so:

def get_total_received(addr): 
    """Returns the number satoshis ever received by this addr.
    Includes coins sent to this addr from this addr (eg change)
    and coins received by addr, but already spent"""
    load_interface()
    hist = wallet.get_history(addr)
    s = 0
    for tx_hash, tx_height in hist:
        tx = wallet.transactions.get(tx_hash)
        for out_addr, value in tx.outputs:
            if out_addr==addr:
                s += value
    return s

The main difference between this method and what is presented by blockchain.info as 'Total Received' is that if the address sends bitcoins to itself, this method will count those towards the total received whereas blockchain.info will not.

Note that this only works if addr is contained in your electrum wallet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.