pythonpython-requestsblockchainbitcoinblockchain.info-api

How to get the transaction amount from https://blockchain.info/rawaddr/$address


I'm trying to keep track of a few Bitcoin addresses and their transactions using https://blockchain.info/rawaddr/ api. It does return all the transactions I need but I don't see the transferred amount in the response.

This is the code I use to make the request.

import pandas, json

transactions_url = 'https://blockchain.info/rawaddr/$address?limit=2'

df = pandas.read_json(transactions_url)
transactions = df['txs']
print(json.dumps(transactions[0], indent=4))


And the result looks like this

{
    "hash": "1b99a2bf4c6031a502317b458505f7d3b281382fcd356b4a772b44359ace97c2",
    "ver": 2,
    "vin_sz": 1,
    "vout_sz": 3,
    "size": 330,
    "weight": 876,
    "fee": 443,
    "relayed_by": "0.0.0.0",
    "lock_time": 0,
    "tx_index": 6846632386013317,
    "double_spend": false,
    "time": 1646966970,
    "block_index": 726792,
    "block_height": 726792,
    "inputs": [
        {
            "sequence": 4294967293,
            "witness": "03004730440220034fc9a74b0c33a52b23775ba9bbec914e37a6aadacb81555dba82516c20e5030220227a5be7eb380f18dc00a7cdcbb55624d847c0f92cee971ce07a4c406bf2e77f014751210316544e44223d8a1ecac82c34f5356ee75afa0f7077e8e853108ba762d02c93742103787b6696302010cac658700e7eb9e3cb21a7fc2b5d70476d8c4478134a5bab7052ae",
            "script": "22002088d160a79f20a5b77c3992b7d3b3cf95f050b3189d8d90d2a15718563ea15aae",
            "index": 0,
            "prev_out": {
                "tx_index": 3467002486392617,
                "value": 951574,
                "n": 4,
                "type": 0,
                "spent": true,
                "script": "a9146df81ff90e8fa2506d1010fd212d6d10794ceda187",
                "spending_outpoints": [
                    {
                        "tx_index": 6846632386013317,
                        "n": 0
                    }
                ],
                "addr": "3BiUpUPvCsLqp2WP981EM8gci1tAwWsgJ8"
            }
        }
    ],
    "out": [
        {
            "type": 0,
            "spent": false,
            "value": 7558,
            "spending_outpoints": [],
            "n": 0,
            "tx_index": 6846632386013317,
            "script": "76a914ee2dd6a6b295ece7f4f90e958fc5dcffdf73b50588ac",
            "addr": "1NiNja1bUmhSoTXozBRBEtR8LeF9TGbZBN"
        },
        {
            "type": 0,
            "spent": false,
            "value": 37761,
            "spending_outpoints": [],
            "n": 1,
            "tx_index": 6846632386013317,
            "script": "0014b5aea3218d45f143d344c3bfecca196aaea15e62",
            "addr": "bc1qkkh2xgvdghc5856ycwl7ejsed2h2zhnzs432t9"
        },
        {
            "type": 0,
            "spent": true,
            "value": 905812,
            "spending_outpoints": [
                {
                    "tx_index": 2010943634944169,
                    "n": 8
                }
            ],
            "n": 2,
            "tx_index": 6846632386013317,
            "script": "00149cb174567efa8e3a83737bdbb52c2902dfdef92c",
            "addr": "bc1qnjchg4n7l28r4qmn00dm2tpfqt0aa7fvlezahn"
        }
    ],
    "result": 7558,
    "balance": 3657877538
}

I'm unable to see the transferred amount (0.00951131 BTC / $388.28) in the JSON response. (This is just a random transaction I found)

Is there anything I'm not seeing or do I have to calculate the amount somehow using the returned data?

Thank you.


Solution

  • I figured out how to get the transferred amount. Just had to add all the inputs together and subtract the fee. (Too bad I didn't know how exactly blockchain works earlier). I put up this code to get the amount.

    import pandas, json
    
    
    transactions_url = 'https://blockchain.info/rawaddr/$address?limit=10'
    df = pandas.read_json(transactions_url)
    transactions = df['txs']
    
    for transaction in transactions:
    
        inputValue = 0
        for i in transaction['inputs']:
            inputValue += i['prev_out']['value']
    
        print(transaction['hash'])
        print((inputValue - transaction['fee'])*0.00000001)