2
1

More than 3 years have passed since last update.

Blockchain API libraryを使ってBitcoinのブロック情報を取得する

Posted at

はじめに

ここ最近は株式が楽しかったのでそちらの調査やら分析やらを中心に活動していましたがたまには仮想通貨に戻ってくるのも良いよねってことで息抜きがてら記事を書いています。
(当の仮想通貨は世間的には下火になってしまったようですが、アングラなくらいが私にはちょうど良い温度感だったりします:sweat_smile:
bitcoinのブロックチェーンのネットワーク情報を見るにはBlockchain.comが有名だったりしますが毎度Webページを見るのが面倒なんですよね。
APIも提供しているようなので少し触ってみることにしました。
WebAPIなのでもちろんrequestsライブラリで取得することも可能ですがどうやらPIPで便利なライブラリ(Blockchain API library)を提供して下さっている方がいるようでしたので今回はそちらを使用してみようかと思います。
(皆トレードには興味があるがブロックチェーンそのものにはあまり興味がないからか意外と日本語情報少ないんですよね…)

実行環境

  • Windows バージョン21H1
  • Python 3.9.6
  • jupyterlab 3.0.16
  • blockchain 1.4.4

blockchain api ライブラリのインストール

pip install blockchain

blockchain api を使ってみる

  • blockchain apiではブロックの情報を取得するblockexplorerモジュールやwalletを取り扱うAPIなどいくつかのモジュールがあります。
  • 今回はブロックチェーンの情報が知りたいだけなのでblockexplorerとstatisticsのモジュールを使用します。
  • 各モジュールの詳細情報はGIT HUBのREADME情報を参照ください。

blockchainライブラリのインポート

from blockchain import blockexplorer, statistics

bitcoin ネットワークの統計情報取得

bitcoinのネットワークの情報を取得するにはstatisticsモジュールを使用します。

stats = statistics.get()
print(type(stats))
返り値
<class 'blockchain.statistics.Stats'>

どうやら返り値はテキストではなくオブジェクトが返ってくるようですのでさらにアトリビュートを指定することで中身を確認することができそうです。

print('trade_volume_btcstats :', stats.trade_volume_btc)
print('miners_revenue_usd :', stats.miners_revenue_usd)
print('btc_mined :', stats.btc_mined)
print('trade_volume_usd :', stats.trade_volume_usd)
print('difficulty :', stats.difficulty)
print('minutes_between_blocks :', stats.minutes_between_blocks)
print('number_of_transactions :', stats.number_of_transactions)
print('hash_rate :', stats.hash_rate)
print('timestamp :', stats.timestamp)
print('mined_blocks :', stats.mined_blocks)
print('blocks_size :', stats.blocks_size)
print('total_fees_btc :', stats.total_fees_btc)
print('total_btc_sent :', stats.total_btc_sent)
print('estimated_btc_sent :', stats.estimated_btc_sent)
print('total_btc :', stats.total_btc)
print('total_blocks :', stats.total_blocks)
print('next_retarget :', stats.next_retarget)
print('estimated_transaction_volume_usd :', stats.estimated_transaction_volume_usd)
print('miners_revenue_btc :', stats.miners_revenue_btc)
print('market_price_usd :', stats.market_price_usd)
返り値
trade_volume_btcstats : 2799.85
miners_revenue_usd : 41457013.287380494
btc_mined : 91250000000
trade_volume_usd : 125852725.52849999
difficulty : 18415156832118
minutes_between_blocks : 9.2759
number_of_transactions : 207623
hash_rate : 133651672063.8893
timestamp : 1631411831000.0
mined_blocks : 146
blocks_size : 144139695
total_fees_btc : 979562900
total_btc_sent : 502687616387123
estimated_btc_sent : 3956414025649
total_btc : 1881328750000000
total_blocks : 700126
next_retarget : 701567
estimated_transaction_volume_usd : 1778400587.3425775
miners_revenue_btc : 922
market_price_usd : 44949.81

界隈でよく聞く難易度、ハッシュレートなどはstatisticsモジュールで取得できる情報だけで十分確認できそうですね。

bitcoin ブロック情報取得

ブロックの詳細情報を取得するにはblockexplorerモジュールを使用します。
たとえば直近のブロック情報なら

latest_block = blockexplorer.get_latest_block()
print(latest_block)
print(dir(latest_block))
返り値
<blockchain.blockexplorer.LatestBlock object at 0x0000024D0752D490>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'block_index', 'hash', 'height', 'time', 'tx_indexes']

どうやらこれもオブジェクトが返ってくるようですね。
ということでこちらもアトリビュートを指定して出力してみると

print('block_index :', latest_block.block_index)
print('hash :', latest_block.hash)
print('height :', latest_block.height)
print('time :', latest_block.time)
print('tx_indexes :', latest_block.tx_indexes)
返り値
block_index : 1631412529
hash : 0000000000000000000451f22cd473d846c1097f81b68fca27be6918ef0aa1a8
height : 700130
time : 1631412529
tx_indexes : ["長いので割愛"]

もちろんブロックを指定して参照することもできます。
たとえばget_blockモジュールでジェネシスブロックを取得してみる

block = blockexplorer.get_block('0')

例によってオブジェクトが返ってくるのでアトリビュートを指定して出力する。

print('bits :', block.bits)
print('block_index :', block.block_index)
print('fee :', block.fee)
print('hash :', block.hash)
print('height :', block.height)
print('main_chain :', block.main_chain)
print('merkle_root :', block.merkle_root)
print('n_tx :', block.n_tx)
print('nonce :', block.nonce)
print('previous_block :', block.previous_block)
print('received_time :', block.received_time)
print('relayed_by :', block.relayed_by)
print('size :', block.size)
print('time :', block.time)
print('transactions :', block.transactions)
print('version :', block.version)
返り値
bits : 486604799
block_index : 0
fee : 0
hash : 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
height : 0
main_chain : True
merkle_root : 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
n_tx : 1
nonce : 2083236893
previous_block : 0000000000000000000000000000000000000000000000000000000000000000
received_time : 1231006505
relayed_by : None
size : 285
time : 1231006505
transactions : [<blockchain.blockexplorer.Transaction object at 0x0000024D07633A60>]
version : 1

blockexplorerモジュールは他にも様々な指定方法ができる関数が容易されています。
詳しくはGIT HUBのREADMEを参照ください。

さいごに

APIの最終更新日が3年前でしたので正常に動作するか若干不安でしたが正常に動作することが確認できました。
APIやライブラリの作者さんには感謝しかありませんね。

また何か気になることがあれば投稿します。それではまた。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1