psqnt / blockstream

API Wrapper for http://blockstream.info and Command Line Tool
https://blockstream.info
MIT License
8 stars 4 forks source link

How to unpack the objects? #1

Closed jodobear closed 4 years ago

jodobear commented 4 years ago

I see the object <blockstream.blockexplorer.Transaction object at 0x7f9439ee50b8> but, what type of object is it? How do i unpack it?

I tried json but, doesn't look like it is a json object.

Thanks for the wrapper!

psqnt commented 4 years ago

Hi, glad you like it. I have been working on some improvements for that exact issue you brought up and will be updating soon. But to answer your question, thats a python object:

class Transaction:
    """Bitcoin Transaction utility class."""
    def __init__(self, transaction):
        self.id = transaction['txid']
        self.version = transaction['version']
        self.locktime = transaction['locktime']
        self.vin = transaction['vin']
        self.vout = transaction['vout']
        self.size = transaction['size']
        self.weight = transaction['weight']
        self.fee = transaction['fee']
        self.status = TransactionStatus(transaction['status'])

So anything you want to access can be accessed like this:

>>> from blockstream import blockexplorer
>>>
>>> # get transaction by id
>>> tx_id = '56a5b477182cddb6edb460b39135a3dc785eaf7ea88a572052a761d6983e26a2'
>>> tx = blockexplorer.get_transaction(tx_id)
>>> tx
<blockstream.blockexplorer.Transaction object at 0x1051bb650>
>>> tx.status
<blockstream.blockexplorer.TransactionStatus object at 0x1051bb6d0>
>>> tx.id
'56a5b477182cddb6edb460b39135a3dc785eaf7ea88a572052a761d6983e26a2'
>>> tx.vin
[{'txid': '253cc92bdeae920f1e74365291c99f364845499297f09e1a71f4f1ad737260f6', 'vout': 0, 'prevout': {'scriptpubkey': '76a914701f47a9b744750ee8c806d8e9c21802165d220988ac', 'scriptpubkey_asm': 'OP_DUP OP_HASH160 OP_PUSHBYTES_20 701f47a9b744750ee8c806d8e9c21802165d2209 OP_EQUALVERIFY OP_CHECKSIG', 'scriptpubkey_type': 'p2pkh', 'scriptpubkey_address': '1BDr9rU6WBdUVZGQwDvA2xj9TLKtM7SKdS', 'value': 1657131}, 'scriptsig': '483045022100d8eeea44e9879291b4a1e626a634bfc167ab31e1d4b2aa07d598b8f5536589ca02200d179809ff098e0af7196d128e37b28b92c9d7a063a593bf7f693733d7798816012102b0476d328a1d468b520681280de2fc965626c5b1c0f1e2adacd1096ceb8a68e8', 'scriptsig_asm': 'OP_PUSHBYTES_72 3045022100d8eeea44e9879291b4a1e626a634bfc167ab31e1d4b2aa07d598b8f5536589ca02200d179809ff098e0af7196d128e37b28b92c9d7a063a593bf7f693733d779881601 OP_PUSHBYTES_33 02b0476d328a1d468b520681280de2fc965626c5b1c0f1e2adacd1096ceb8a68e8', 'is_coinbase': False, 'sequence': 4294967295}]
>>> tx.locktime
0
>>> tx.fee
10887
>>> tx.status.confirmed
True
>>> tx.status.block_height
575879

tx.status is an instance of the TransactionStatus class which has its own fields.

I hope this helps 👍

psqnt commented 4 years ago

For more reference scroll to the bottom of this file, all the classes are defined there: https://github.com/pasquantonio/blockstream/blob/master/blockstream/blockexplorer.py

jodobear commented 3 years ago

Thanks! That helps greatly!