5

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?

8

"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

  • It would be web3.eth.getBlock(<block>).gasUsed or web3.eth.getBlock(<block>).cumulativeGasUsed? – AdrianClv Aug 18 '16 at 14:11
  • 1
    Just .gasUsed. Have a look at the return values for eth_getBlockByHash - you'll see that .gasUsed is the total for that block. .cumulativeGasUsed is when you use one of the transaction APIs (not block-level APIs) and you want the total for the block in which the transaction resides. – Richard Horrocks Aug 18 '16 at 14:20
  • Thank you, you are right. I was mixing the getBlock() function with getTransactionReceipt() for some reason. And for the reward from uncles, then it would be enough to do numberOfUncles*5/32? Reading the wiki page, it also says: "Uncles included in a block formed by the successful PoW miner receive 7/8 of the static block reward = 4.375 ether A maximum of 2 uncles allowed per block." – AdrianClv Aug 18 '16 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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