blockchain-etl / bitcoin-etl

ETL scripts for Bitcoin, Litecoin, Dash, Zcash, Doge, Bitcoin Cash. Available in Google BigQuery https://goo.gl/oY5BCQ
https://twitter.com/BlockchainETL
MIT License
406 stars 122 forks source link

incorrect transaction fee when exporting transactions #24

Closed thelazyliz closed 4 years ago

thelazyliz commented 5 years ago

When I export the transactions to json in the following manner,

bitcoinetl export_blocks_and_transactions --start-block 500000 --end-block 500001 \
--provider-uri [REMOVED] --chain bitcoin \
--batch-size 100 --transactions-output transactions500000to500001.json

the transactions that I get all have a negative (or 0) mining fee. Here's a specific example.

{
   "hash":"d89197f886482cdb9d0828d69b19b8cb3d3831cded628250bd0506fac687313c",
   "size":226,
   "virtual_size":226,
   "version":1,
   "lock_time":0,
   "block_number":500000,
   "block_hash":"00000000000000000024fb37364cbf81fd49cc2d51c09c75c35433c3a1945d04",
   "block_timestamp":1513622125,
   "is_coinbase":false,
   "inputs":[
      {
         "index":0,
         "spent_transaction_hash":"f2926dd6fdc48342b0fc791ff9d7d7db77420e7e55eaf6d9d8d3a058b6eb113f",
         "spent_output_index":38,
         "script_asm":"3045022100c3b2715a5173fb4aa5a21cceb323e83e74b24fee5d616b700241bd09d1736709022055ec015e30c5f020bda62e6bbb693a4768df9cc8152c93e4c1fd8696165b8a20[ALL] 0211cc09ad5f8a10e8ed6ea995a67bb1142ce5d85a2655290e37f04596745220aa",
         "script_hex":"483045022100c3b2715a5173fb4aa5a21cceb323e83e74b24fee5d616b700241bd09d1736709022055ec015e30c5f020bda62e6bbb693a4768df9cc8152c93e4c1fd8696165b8a2001210211cc09ad5f8a10e8ed6ea995a67bb1142ce5d85a2655290e37f04596745220aa",
         "sequence":4294967295,
         "required_signatures":null,
         "type":null,
         "addresses":[

         ],
         "value":null
      }
   ],
   "outputs":[
      {
         "index":0,
         "script_asm":"OP_DUP OP_HASH160 3e9c503b43723ed5deb1fa33d86b93d4c186b347 OP_EQUALVERIFY OP_CHECKSIG",
         "script_hex":"76a9143e9c503b43723ed5deb1fa33d86b93d4c186b34788ac",
         "required_signatures":1,
         "type":"pubkeyhash",
         "addresses":[
            "16i49jsjaVdAKZVXebziBxa3RjwB1T1Qh5"
         ],
         "value":1149000
      },
      {
         "index":1,
         "script_asm":"OP_DUP OP_HASH160 3287defaad1ab8ec836a355692ac1717dcac9ca3 OP_EQUALVERIFY OP_CHECKSIG",
         "script_hex":"76a9143287defaad1ab8ec836a355692ac1717dcac9ca388ac",
         "required_signatures":1,
         "type":"pubkeyhash",
         "addresses":[
            "15cBZ5MDCJmGLmbLPubyBJBESv1WP2jyDx"
         ],
         "value":2197064
      }
   ],
   "input_count":1,
   "output_count":2,
   "input_value":0,
   "output_value":3346064,
   "fee":-3346064
}

If I'm streaming the fees are of the correct value.

medvedev1088 commented 5 years ago

Thanks for reporting this. The reason is export_blocks_and_transactions doesn't enrich inputs. Try using export_all command with --enrich True:

bitcoinetl export_all -s 500000 -e 500001 -b 100 -w 4 -p [REMOVED] --chain bitcoin --enrich True

Make sure you used bitcoin-etl>=1.3.0

The result will be in output/.../enriched_transactions_500000_500001.json.

Note that enriching transactions is a very time-consuming process.

thelazyliz commented 5 years ago

Thanks for the quick response. Would it be possible to return the transaction fee without having to enrich inputs? Or is enriching inputs the only way?

medvedev1088 commented 5 years ago

Enriching is the only way that I'm aware of. The reason is the Bitcoin's JSON RPC API only returns references to spent outputs in transaction inputs, but no values, addresses or types. To get the input values, those spent outputs need to be queried separately, this is what we call enrichment.

thelazyliz commented 5 years ago

You are right. I thought about it and realised that to get the fee you would need something along the lines of total input value - total output value, which would require the input values. Thanks for the clarification!