<p>It looks like you are trying to calculate address balances by what I assume to be transactions. The way the data is presented in your table, I get the impression that you are assuming that there is only one input address and one output address in every transaction. That's not the case, though.</p>
<p>Each input in a transaction spends an Unspent Transaction Output (UTXO) created by a previous transaction. Transactions can have many inputs and there is no requirement for all the inputs to have been received to the same address. In fact, address reuse is not recommended, so most of the time every input is associated with a different address.</p>
<p>Let's look for example at this random transaction (<a href="https://blockchair.com/bitcoin/transaction/ae45caa1615bf023da72fd3d9c6c46220cc1af520158adc8d5d5249776de9486" rel="nofollow noreferrer">via blockchair.com</a>) with four inputs and two outputs:
<a href="https://i.sstatic.net/Sadit.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Sadit.png" alt="enter image description here" /></a></p>
<p>It uses funds that were previously received to four different addresses and reassigns the amounts to two new addresses. So, you'll need to amend your approach slightly: instead of looking at transactions as atomic units to feed your script, parse each input and each output separately.</p>

CC BY-SA 4.0
2021-03-19T21:03:33.290Z
2021-03-19T21:03:33.290Z
Murch
<blockquote>
<p>I'm not sure if I could do something straightforward like sum all of the inputs and outputs for a given address.</p>
</blockquote>
<p>You should do that.</p>
<blockquote>
<p>the input/output isn't necessarily 1:1</p>
</blockquote>
<p>I guess you are referring to transaction fees? You don't need to account for that.</p>
<p>Maybe you are referring to the lack of meaningful correspondence between individual inputs and individual outputs when there are multiple of either or both inputs and outputs? This doesn't affect the calculation.</p>
<p>Maybe you are referring to change? This doesn't affect how you calculate the balance for an address.</p>
<blockquote>
<p>there are some nuances here that make it more complex than this</p>
</blockquote>
<p>None that I can think of.</p>

CC BY-SA 4.0
2021-03-13T23:59:31.000Z
2021-03-13T23:59:31.000Z
RedGrittyBrick
<p>I'm writing an application that attempts to calculate the running balance of a given BTC address by looking at data including block height, input amount, output amount, etc. However, I'm not certain about the logic involved here. Would it be possible to create this running balance by checking an input/output address and then tracking the input/output amount?</p>
<p>Here's a quick sample of the data that I'm looking:</p>
<pre><code>BLOCK_DATE | BLOCK_HEIGHT | TRANS_HASH | INPUT_ADDRESS | OUTPUT_ADDRESS | INPUT_AMOUNT | OUTPUT_AMOUNT
01/11/2020    190            15c7853       abc             xyz1             -0.01          0.0001
01/11/2020    190            14v9876       abc             xyz2             -0.50          0.70
01/11/2020    191            19vc842       abc             xyz3             -5.03          0.413
01/12/2020    192            20ff4d3       abc             xyz4             -0.06          0.201
01/12/2020    192            154gf34       xyz1            abc              -0.07          0.18
01/12/2020    192            45f4ti5       ggg             abc              -0.10          0.24
01/12/2020    192            33cv5c5       jjj             abc              -0.08          1.13 
</code></pre>
<p>So using this dummy data, how would I go about calculating the running balance for address <strong>abc</strong>? I know that the input/output isn't necessarily 1:1 and there are some nuances here that make it more complex than this. So I'm not sure if I could do something straightforward like sum all of the inputs and outputs for a given address.</p>

<transactions><balance>
How to determine the balance associated with a given BTC address based on input/output amounts
David
# How to determine the balance associated with a given BTC address based on input/output amounts

(Asked by: username_0 on 2021-03-13)

I'm writing an application that attempts to calculate the running balance of a given BTC address by looking at data including block height, input amount, output amount, etc. However, I'm not certain about the logic involved here. Would it be possible to create this running balance by checking an input/output address and then tracking the input/output amount?

Here's a quick sample of the data that I'm looking:

```
BLOCK_DATE | BLOCK_HEIGHT | TRANS_HASH | INPUT_ADDRESS | OUTPUT_ADDRESS | INPUT_AMOUNT | OUTPUT_AMOUNT
01/11/2020    190            15c7853       abc             xyz1             -0.01          0.0001
01/11/2020    190            14v9876       abc             xyz2             -0.50          0.70
01/11/2020    191            19vc842       abc             xyz3             -5.03          0.413
01/12/2020    192            20ff4d3       abc             xyz4             -0.06          0.201
01/12/2020    192            154gf34       xyz1            abc              -0.07          0.18
01/12/2020    192            45f4ti5       ggg             abc              -0.10          0.24
01/12/2020    192            33cv5c5       jjj             abc              -0.08          1.13 

```

So using this dummy data, how would I go about calculating the running balance for address **abc**? I know that the input/output isn't necessarily 1:1 and there are some nuances here that make it more complex than this. So I'm not sure if I could do something straightforward like sum all of the inputs and outputs for a given address.

**Tags:** transactions, balance


---

## 2 answers

### Answer by username_1 on 2021-03-19. Score: 1

It looks like you are trying to calculate address balances by what I assume to be transactions. The way the data is presented in your table, I get the impression that you are assuming that there is only one input address and one output address in every transaction. That's not the case, though.

Each input in a transaction spends an Unspent Transaction Output (UTXO) created by a previous transaction. Transactions can have many inputs and there is no requirement for all the inputs to have been received to the same address. In fact, address reuse is not recommended, so most of the time every input is associated with a different address.

Let's look for example at this random transaction ([via blockchair.com](https://blockchair.com/bitcoin/transaction/ae45caa1615bf023da72fd3d9c6c46220cc1af520158adc8d5d5249776de9486)) with four inputs and two outputs:
[![enter image description here](https://i.sstatic.net/Sadit.png)](https://i.sstatic.net/Sadit.png)

It uses funds that were previously received to four different addresses and reassigns the amounts to two new addresses. So, you'll need to amend your approach slightly: instead of looking at transactions as atomic units to feed your script, parse each input and each output separately.


---

### Answer by username_2 on 2021-03-13. Score: 0

> I'm not sure if I could do something straightforward like sum all of the inputs and outputs for a given address.

You should do that.

> the input/output isn't necessarily 1:1

I guess you are referring to transaction fees? You don't need to account for that.

Maybe you are referring to the lack of meaningful correspondence between individual inputs and individual outputs when there are multiple of either or both inputs and outputs? This doesn't affect the calculation.

Maybe you are referring to change? This doesn't affect how you calculate the balance for an address.

> there are some nuances here that make it more complex than this

None that I can think of.


---

