Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: DrakonX on September 09, 2021, 08:12:22 PM



Title: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: DrakonX on September 09, 2021, 08:12:22 PM
Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate freqently (especially with JavaScript)? How?


Title: Re: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: bitmover on September 09, 2021, 08:34:14 PM
Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate freqently (especially with JavaScript)? How?

Yes, you can get this data using blockchair api

You can read the documentation here:
https://blockchair.com/api/docs#link_M1

And the api you need is this one:
https://api.blockchair.com/bitcoin/stats

This is the return. It has  hashrate_24h and difficulty values.

Code:
{"data":{"blocks":699810,"transactions":669216274,"outputs":1822960945,"circulation":1881128354497096,"blocks_24h":139,"transactions_24h":263592,"difficulty":18415156832118,"volume_24h":382466097248610,"mempool_transactions":15894,"mempool_size":9213750,"mempool_tps":3.5833333333333335,"mempool_total_fee_usd":12094.621,"best_block_height":699809,"best_block_hash":"0000000000000000000bbbee45774498ad2ee340c5c92bdfa9aee202113ed159","best_block_time":"2021-09-09 20:22:34","blockchain_size":363580998494,"average_transaction_fee_24h":5286,"inflation_24h":86875000000,"median_transaction_fee_24h":1407,"cdd_24h":13879282.242647348,"mempool_outputs":88861,"largest_transaction_24h":{"hash":"358256922857e1fd9ac1fd212d8e08c0e7f28028d2485c13903fecaaec8a5cc1","value_usd":1489689216},"nodes":8209,"hashrate_24h":"127363118107339408072","inflation_usd_24h":40514156.25,"average_transaction_fee_usd_24h":2.4654470845055996,"median_transaction_fee_usd_24h":0.6561544500000001,"market_price_usd":46635,"market_price_btc":1,"market_price_usd_change_24h_percentage":1.06546,"market_cap_usd":879704699184,"market_dominance_percentage":39.76,"next_retarget_time_estimate":"2021-09-22 10:18:56","next_difficulty_estimate":17103153766662,"countdowns":[],"suggested_transaction_fee_per_byte_sat":5,"hodling_addresses":38792149},"context":{"code":200,"source":"A","state":699809,"market_price_usd":46635,"cache":{"live":false,"duration":"Ignore","since":"2021-09-09 20:32:47","until":"2021-09-09 20:33:58","time":4.0531158447265625e-6},"api":{"version":"2.0.88","last_major_update":"2021-07-19 00:00:00","next_major_update":null,"documentation":"https:\/\/blockchair.com\/api\/docs","notice":":)"},"server":"BTC0","time":1.090975046157837,"render_time":0.004091978073120117,"full_time":0.004096031188964844,"request_cost":1}}

You can make the API call easily using javascript, such as fetch  (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)

Code:
fetch('https://api.blockchair.com/bitcoin/stats')
  .then(response => response.json())
  .then(data => console.log(data));


Title: Re: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: odolvlobo on September 10, 2021, 06:39:02 PM
Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate freqently (especially with JavaScript)? How?

Note that network hash rate is a computation based on block timestamps and there is no way to get the true value because blocks are produced at random intervals and the timestamps are not accurate.


Title: Re: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: DrakonX on September 10, 2021, 11:04:52 PM
Thank you, bitmover.

@adolvlobo: I just want to get the same info I can retrieve from the Bitcoin network by typing the command within the Bitcoin Core CLI without using it.


Title: Re: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: Pmalek on September 11, 2021, 08:33:57 AM
I have never looked into it, but I know that Mempool.space has a dedicated API service for Bitcoin. You can find it here > https://mempool.space/api.
Check and see if the features you are interested in can be found among the different tabs at the top.


Title: Re: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: bitmover on September 11, 2021, 11:50:45 AM
I have never looked into it, but I know that Mempool.space has a dedicated API service for Bitcoin. You can find it here > https://mempool.space/api.
Check and see if the features you are interested in can be found among the different tabs at the top.

Unfortunately their API doesn't offer difficulty/hashrate on specific time, but only difficulty adjustment.

Yes, mempool.space API won't work for what OP need.

I found another API which will also provide the data he requested. Sochain API. (https://sochain.com/api/)

You can call it here:
https://chain.so/api/v2/get_info/BTC

and the response:
Code:
{
  "status": "success",
  "data": {
    "name": "Bitcoin",
    "acronym": "BTC",
    "network": "BTC",
    "symbol_htmlcode": "฿",
    "url": "https://bitcoin.org",
    "mining_difficulty": "18415156832118.24",
    "unconfirmed_txs": 2350,
    "blocks": 700041,
    "price": "45633.63000000",
    "price_base": "USD",
    "price_update_time": 1631360991,
    "hashrate": "130102593297717500000.0"
  }
}

He needs mining_difficulty and hashrate



Comparing both API (requested both now at time of writing):

Sochain
"mining_difficulty": "18415156832118.24",
"hashrate": "130102593297717500000.0"

Blockchair:
"difficulty":18415156832118,
"hashrate_24h":"128188810931374023359"

Difficult matches, while hashrate has a close value, but doesn't match.


Title: Re: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: ranochigo on September 11, 2021, 03:20:40 PM
I just want to get the same info I can retrieve from the Bitcoin network by typing the command within the Bitcoin Core CLI without using it.
It is actually quite easy to do so. The difficulty and the target are both stored within the block headers, it will be sufficient to just call the block headers at each difficulty epoch to get the difficulty for that period. It is pointless to get the hashrate, because any estimation based on timestamps within a short period of time is often quite inaccurate. You can determine it based on the block intervals or use the difficulty as a metric.


Title: yep
Post by: MineSweep on September 11, 2021, 08:39:00 PM
block difficulty should be adjusted every two weeks and hashrate depends on the miners who can be found hashing in https://ycharts.com/indicators/bitcoin_network_hash_rate


Title: Re: Is there a way to retrieve bitcoin statistics, such as Difficulty and Hashrate
Post by: MineSweep on September 11, 2021, 08:50:27 PM
I just want to get the same info I can retrieve from the Bitcoin network by typing the command within the Bitcoin Core CLI without using it.
It is actually quite easy to do so. The difficulty and the target are both stored within the block headers, it will be sufficient to just call the block headers at each difficulty epoch to get the difficulty for that period. It is pointless to get the hashrate, because any estimation based on timestamps within a short period of time is often quite inaccurate. You can determine it based on the block intervals or use the difficulty as a metric.

Block intervals coinside with the nonce interation achieved in each epoch of bitcoin. In the bitcoin network an epoch is defined as a block interval where production exists once a target hash is SHA-256'd to a low enough byte.