[Ethereum] How to query the amount of mining reward from a certain block

blockchainmining

I want to write a script to analyze mining rewards.
What is the best way to query for the amount of mining rewards of a certain block?
It seems possible to query the address of the lucky miner:

web3.eth.getBlock(200).miner
"0xbb7b8287f3f0a933474a79eae42cbca977791171"

Then I can somehow delve into the account and pick the transaction at certain block. Is this the best way? Or do you know shortcuts? And how to do it?

Best Answer

"Then I can somehow delve into the account and pick the transaction at certain block."

Mining rewards aren't transactions, so you can't query them in the usual way. As per this previous answer:

There are no inputs and outputs in Ethereum, just state changes and balances. Therefore, mining rewards don't have a transaction hash since they are not a transaction.

You'll either need to calculate the reward yourself, or use somebody else's API.

Calculating the reward yourself

The algorithm for calculating the reward - as stated on the Mining wiki page - is as follows:

The successful PoW miner of the winning block receives:

  • A static block reward for the 'winning' block, consisting of exactly 5.0 Ether
  • All of the gas expended within the block, that is, all the gas consumed by the execution of all the transactions in the block submitted by the winning miner is compensated for by the senders. The gascost incurred is credited to the miner's account as part of the consensus protocoll. Over time, it's expected these will dwarf the static block reward.
  • An extra reward for including Uncles as part of the block, in the form of an extra 1/32 per Uncle included

The data required for the second and third parts can be queried from the block using:

  • web3.eth.getBlock(<block>).gasUsed
  • web3.eth.getBlock(<block>).uncles (Note: The reward per uncle is /32 of the static reward, i.e. 5 / 32.)

Using someone else's API

Etherscan is one of the block explorers that includes details of the reward for a given block (see here for an example). Details of their APIs can be found either on their site, or, for Python bindings, on this GitHub page. (I haven't checked that these return the reward details, so YMMV.)

EDIT : The static reward is now 3.0 Ether

Related Topic