5

We are working on a small project and imported all block information to mongoDB. Right now we're aiming to calculate the coin supply regarding to the block number.

My plan is to loop through all transactions per block and parse vout and vin. However, I'm not sure how to distinguish mined coins and previously existing coins transferred users. Plus, how to account for transaction fees? Can I track only "valueOut" and that's all?

{
   "txid":"0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
   "version":1,
   "locktime":0,
   "vin":[
      {
         "coinbase":"04ffff001d0104",
         "sequence":4294967295,
         "n":0
      }
   ],
   "vout":[
      {
         "value":"50.00000000",
         "n":0,
         "scriptPubKey":{
            "hex":"410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
            "asm":"0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
            "addresses":[
               "12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
            ],
            "type":"pubkeyhash"
         },
         "spentTxId":null,
         "spentIndex":null,
         "spentHeight":null
      }
   ],
   "blockhash":"00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
   "blockheight":1,
   "confirmations":601155,
   "time":1231469665,
   "blocktime":1231469665,
   "isCoinBase":true,
   "valueOut":50,
   "size":134
}

Any help would be appreciated!

3
  • Hey Prologas, interesting question. I've touched up the body of your question a bit to make it easier to understand. Could you please proofread my changes to verify that I didn't change the essence of your question?
    – Murch
    Commented Oct 27, 2019 at 0:23
  • 1
    We have really good answers below, and I would like to add a nice hack to them. On Bitcoin-Core, run the command bitcoin-cli gettxoutsetinfo. It will give you all the information of the coins outstanding. It calculates that amount by summing all the block subsidies less the amount not claimed by miners, bitcoins sent to OP_RETURN outputs, 50 BTC genesis block output that cannot be spent and the two coinbase transactions Raghav Sood mentioned in his answers. It doesn't exclude the Bitcoins sent to 1BitcoinEater... type addresses as thr is no verifiable way to know if one own the priv key.
    – Ugam Kamat
    Commented Oct 27, 2019 at 8:25
  • This works indeed: root@b9f3dd35c755:/# bitcoin-cli gettxoutsetinfo { "height": 601237, "bestblock": "0000000000000000000f18a401591a0f440668ed1e3d975a64cb3687b796b8c6", "transactions": 37206777, "txouts": 63615267, "bogosize": 4784113473, "hash_serialized_2": "88e6d6b2585e83204760db1f383b942dbade97569c7556bdd30b1ad8fcfcdfa9", "disk_size": 3833721332, "total_amount": 18015292.32182326 } But minus here if you don't have RPC access and using only public explorer to import data.
    – pr0logas
    Commented Oct 27, 2019 at 10:49

3 Answers 3

5

The first transaction in every block is called the coinbase transaction, it is a special transaction that pays the miner their reward. This transaction adheres to different rules than the rest: it has no inputs, but has output(s).

The coinbase transaction outputs are allowed to be equal to or less than the block subsidy (newly minted coins) + all transaction fees in the block.

The block subsidy is a set amount, as defined by the code. The subsidy started at 50 BTC, and that amount is cut in half every 210,000 blocks (25, 12.5, 6.25, etc).

The transaction fees for the block are found by taking the rest of the transactions in the block (ie everything that isn’t the coinbase transaction), and calculating the difference between the inputs and outputs in them. This value (input value - output value) is the implied fee.

Note that there are some odd cases to account for, this question has a bunch of great info in this regard. in addition to this, there is a non-zero amount of BTC that has been ‘burned’, by being sent to addresses or scripts that are un-spendable, or at least have an extremely small chance of someone actually knowing the private key for them (eg the 1BitcoinEater... address). Depending on how you define ‘coin supply’, it may be worth account for these coins as well.

0
5

If you want the total amount of BTC created, you simply need to iterate through the coinbase transactions (first tx in every block) and add up the output. You will then need to subtract the transaction fees for that block to avoid double counting those.

If you want a somewhat accurate money supply, you will need to do the above, and then also subtract any BTC sent to OP_RETURN outputs.

There are also a number of burn addresses and accidental addresses which have had BTC sent to them, but are effectively locking the coins forever as no one has a private key or valid redeem script for them. You can attempt to subtract those, but it is not always possible to identify if an address falls into this category.

Lastly, there are two cases where coinbase transactions had the same txid, which results in burning of one of the outputs:

e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468
d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599
1

You can use my parser for all the local BTC raw (real) data.

Then you can find all transactions with

"TX from hash = 0000000000000000000000000000000000000000000000000000000000000000"

and then

"Value = XXXXXXXXXXXXXXXX"

(XX - HEX string value of Byte) would be your incremental sum of coin supply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.