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

<pre><code>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
</code></pre>

<p>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.</p>

<p><strong>Note that this only works if addr is contained in your electrum wallet.</strong></p>

CC BY-SA 3.0
2013-08-17T20:35:39.973Z
2013-08-17T20:35:39.973Z
Ponkadoodle
<p>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 <em>and</em> 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.  </p>

<p>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.</p>

<electrum><balance><website-integration><funds>
How to programmatically determine the number of bitcoins ever deposited to a given address?
Ponkadoodle
# How to programmatically determine the number of bitcoins ever deposited to a given address?

(Asked by: username_0 on 2013-08-17)

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.

**Tags:** electrum, balance, website-integration, funds


---

## 1 answers

### Answer by username_0 on 2013-08-17. Score: 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.**


---

