<p>Bitcoind does not index transactions for non-wallet addresses. If you wish to build this yourself, you will need to:</p>

<ol>
<li>Enable <code>txindex=1</code> in your bitcoin.conf</li>
<li>Rescan the chain</li>
<li>Set up blocknotify and then parse the chain block by block, building an index of which outputs have been spent, and which haven't, and which addresses are involved in each transaction</li>
</ol>

<p>Then, when working with a specific address, you first query your database for all unspent outputs, and then can use <code>getrawtransaction</code> to get the unspent tx information (although, you could store enough information in your index to not need to make the <code>getrawtransaction</code> call).</p>

<p>Alternatively, you can run something such as ElectrumX or insight, which will do all of the above for you.</p>

CC BY-SA 4.0
2022-06-16T19:49:37.177Z
What are the resources / cost estimates to run ElectrumX on AWS?
Jus12
CC BY-SA 4.0
2018-07-28T03:59:56.183Z
2018-07-28T03:59:56.183Z
Raghav Sood
<p>I want to build a server, which provide following APIs:</p>

<ul>
<li>1.Get balance by address.</li>
<li>2.Get latest unspent transaction.</li>
<li>3.broadcast the transaction.</li>
</ul>

<p>First by first, I know I can do above stuffes by <a href="https://www.blockchain.com/btc" rel="nofollow noreferrer">blockchain</a> and <a href="http://coinb.in/multisig/#newTransaction" rel="nofollow noreferrer">coinb</a>, but I want to build my own APIs. What I have tried with the Python code has list as below:</p>

<pre><code>import binascii
import json
from decimal import Decimal
from pprint import pprint
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from config import ConfigBasic

class BitPort(object):

    def __init__(self, config=None):
        self.conf = ConfigBasic()
        # pass
        self.rpc = AuthServiceProxy("http://{}:{}@{}".format(
        self.conf.rpc_user, self.conf.rpc_password, self.conf.rpc_host))

    def get_amount_by_addr(self, addr):
        return self.rpc.getreceivedbyaddress(addr)

    def get_unconfirm(self, addr):
        return self.rpc.getunconfirmedbalance(addr)

    def get_unspent(self, addr):
        """
        @param addr: A array with address, ["0xxxxxx", "0x122222"]
        """
        return self.rpc.listunspent(0, 999999, addr)

    def sign_rawtransaction(self, addr):
        fee_obj = self.rpc.estimatesmartfee(6)
        fee = fee_obj.get("feerate")

    def sendrawtransaction(self, address, pubkey):
        pass




if __name__ == '__main__':
    addr = '2NCTreR1GmHXMNSYnt2J76QZgv8PH1k4PHB'
    addr = '2MsHfXEmutS2GWPgK55JD4i1gKSFYmJgKXv'
    addr = 'n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi'
    bp = BitPort(config=ConfigBasic)
    print(bp.get_amount_by_addr(addr))
    #print(bp.get_unconfirm(addr))
    print(bp.get_unspent([addr]))
</code></pre>

<p>Let's say, Why I need to do such stuff. The client-side need to check how many money are available. so I need to provide the <strong>1</strong>. The client-side also want to send its money to someone else, so I need to broadcast its transaction to the public's chain, but before do that, I the client-side need to make a transaction hex, which require the server side provide an API to get the unspent transaction. Why?</p>

<p>Because make a raw transaction need the following at least:</p>

<ul>
<li>1.vout (from unspent transaction)</li>
<li>2.txid (from unspent transaction)</li>
<li>3.amount (user custom)</li>
<li>4.fee (default value is ok)</li>
<li>5.public key (client side store or calculate it by itself)</li>
<li>6.private key client side store or calculate it by itself)</li>
</ul>

<p>From many tutorial, like <a href="https://bitcoin.stackexchange.com/questions/48454/how-to-create-raw-transaction">How to create a raw transaction</a>, which is good. but didn't mention how to get an <code>unspent</code> by a address. which don't store inside the <code>bitcoind</code> side.</p>

<p>Unfortunately, I can only fetch all transaction by my wallet's address, which live in the <code>bitcoind</code> node. Once I tried the someone address, and I got the following exception:</p>

<pre><code>Traceback (most recent call last):
  File "core.py", line 43, in &lt;module&gt;
    print(bp.get_amount_by_addr(addr))
  File "core.py", line 17, in get_amount_by_addr
    return self.rpc.getreceivedbyaddress(addr)
  File "/Users/frank/venv/bitcoin/lib/python2.7/site-packages/bitcoinrpc/authproxy.py", line 141, in __call__
    raise JSONRPCException(response['error'])
bitcoinrpc.authproxy.JSONRPCException: -4: Address not found in wallet
</code></pre>

<p>BTW, I run it on <code>testnet</code>, Anyone help me?</p>

<python><sendrawtransaction><listunspent>
How to fetch the latest unspent transaction by an address using bitcoind?
Frank AK
# How to fetch the latest unspent transaction by an address using bitcoind?

(Asked by: username_0 on 2018-07-28)

I want to build a server, which provide following APIs:

* 1.Get balance by address.
* 2.Get latest unspent transaction.
* 3.broadcast the transaction.

First by first, I know I can do above stuffes by [blockchain](https://www.blockchain.com/btc) and [coinb](http://coinb.in/multisig/#newTransaction), but I want to build my own APIs. What I have tried with the Python code has list as below:

```
import binascii
import json
from decimal import Decimal
from pprint import pprint
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from config import ConfigBasic

class BitPort(object):

    def __init__(self, config=None):
        self.conf = ConfigBasic()
        # pass
        self.rpc = AuthServiceProxy("http://{}:{}@{}".format(
        self.conf.rpc_user, self.conf.rpc_password, self.conf.rpc_host))

    def get_amount_by_addr(self, addr):
        return self.rpc.getreceivedbyaddress(addr)

    def get_unconfirm(self, addr):
        return self.rpc.getunconfirmedbalance(addr)

    def get_unspent(self, addr):
        """
        @param addr: A array with address, ["0xxxxxx", "0x122222"]
        """
        return self.rpc.listunspent(0, 999999, addr)

    def sign_rawtransaction(self, addr):
        fee_obj = self.rpc.estimatesmartfee(6)
        fee = fee_obj.get("feerate")

    def sendrawtransaction(self, address, pubkey):
        pass

if __name__ == '__main__':
    addr = '2NCTreR1GmHXMNSYnt2J76QZgv8PH1k4PHB'
    addr = '2MsHfXEmutS2GWPgK55JD4i1gKSFYmJgKXv'
    addr = 'n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi'
    bp = BitPort(config=ConfigBasic)
    print(bp.get_amount_by_addr(addr))
    #print(bp.get_unconfirm(addr))
    print(bp.get_unspent([addr]))

```

Let's say, Why I need to do such stuff. The client-side need to check how many money are available. so I need to provide the **1**. The client-side also want to send its money to someone else, so I need to broadcast its transaction to the public's chain, but before do that, I the client-side need to make a transaction hex, which require the server side provide an API to get the unspent transaction. Why?

Because make a raw transaction need the following at least:

* 1.vout (from unspent transaction)
* 2.txid (from unspent transaction)
* 3.amount (user custom)
* 4.fee (default value is ok)
* 5.public key (client side store or calculate it by itself)
* 6.private key client side store or calculate it by itself)

From many tutorial, like [How to create a raw transaction](https://bitcoin.stackexchange.com/questions/48454/how-to-create-raw-transaction), which is good. but didn't mention how to get an `unspent` by a address. which don't store inside the `bitcoind` side.

Unfortunately, I can only fetch all transaction by my wallet's address, which live in the `bitcoind` node. Once I tried the someone address, and I got the following exception:

```
Traceback (most recent call last):
  File "core.py", line 43, in <module>
    print(bp.get_amount_by_addr(addr))
  File "core.py", line 17, in get_amount_by_addr
    return self.rpc.getreceivedbyaddress(addr)
  File "/Users/username_0/venv/bitcoin/lib/python2.7/site-packages/bitcoinrpc/authproxy.py", line 141, in __call__
    raise JSONRPCException(response['error'])
bitcoinrpc.authproxy.JSONRPCException: -4: Address not found in wallet

```

BTW, I run it on `testnet`, Anyone help me?

**Tags:** python, sendrawtransaction, listunspent


---

## 1 answers

### Answer by username_1 on 2018-07-28. Score: 4 | Accepted answer

Bitcoind does not index transactions for non-wallet addresses. If you wish to build this yourself, you will need to:

1. Enable `txindex=1` in your bitcoin.conf
2. Rescan the chain
3. Set up blocknotify and then parse the chain block by block, building an index of which outputs have been spent, and which haven't, and which addresses are involved in each transaction

Then, when working with a specific address, you first query your database for all unspent outputs, and then can use `getrawtransaction` to get the unspent tx information (although, you could store enough information in your index to not need to make the `getrawtransaction` call).

Alternatively, you can run something such as ElectrumX or insight, which will do all of the above for you.

> **Comments:**
> **username_2**: What are the resources / cost estimates to run ElectrumX on AWS?  

---

