LoginSignup
19
22

More than 5 years have passed since last update.

Ethereumのエクスプローラー(Etherscan)のAPIでアドレスの取引を出力する(イーサリアム)

Last updated at Posted at 2017-07-17

この記事では、Mac OS Sierra 10.12.4を使っています。
python バージョンは3.5.0です。

はじめに

ビットコインをはじめとした仮想通貨には、アドレスやトランザクションを確認するためのエクスプローラーが用意されています。
EthereumのエクスプローラーとしてはEtherscanが用意されています。
そして、EtherscanではAPIも用意されています。
これを用いれば、ブロックチェーン上から直接見に行かなくても、簡単にトランザクションが抽出できるのでは?ということで使って見ます。

EtherscanのAPI登録

EtherscanのAPI利用にはEtherscanの登録と、API-KEYの取得が必要です。
Etherscan公式のAPIページに登録と取得手順が書いてあります。
Etherscanに新規登録して ClientPortal->MyApiKey を見ればAPI-KEYが取得できます。

py-etherscan-apiのインストール

今回、pythonを用いてetherscanのapiを利用します。
そのために、py-etherscan-apiのgithubページに遷移して、git cloneします。

git-clone
git clone https://github.com/corpetty/py-etherscan-api.git

※2019/02/07追記
setup.pyに移動するcdコマンドを書いていなかったので追記

cd py-etherscan-api

cloneしたローカルでinstallします。

python setup.py install

指定アドレスの取引を出力する

EtherscanのALLAcount金額上位から適当なアドレスを取ってきて取引を確認します。
※2019/2/7追記
・現在だとget_all_transactions()がエラーになっていたため(参考:get_all_transactions leads to error in connect #34
)、get_transaction_page()というメソッドに変更しました。多数のトランザクションを取得する場合はget_transaction_page()に入れるpage_no変数とoffset変数を加工してループ処理することでうまく行くはずです。
・またdatetimeのimportが漏れていたので追記しました。
・ついでにtsv出力するようにしました。

get_tx_values.py(2019/2/7バージョン)
from etherscan import accounts
import pandas as pd
import datetime as dt
API_KEY = 'API_KEY' # change API_KEY 
ADDRESS = '0xb794f5ea0ba39494ce839613fffba74279579268' # change address 

def get_tx_value(page_no, address, api_key):
    ac = accounts.Account(address=address,api_key=api_key)
#     txes = ac.get_all_transactions()
    txes = ac.get_transaction_page(page_no)

    ret = []
    for t in txes:
        tmp = []
        tmp.append(t.get('from'))
        tmp.append(t.get('to'))
        tmp.append(int(t.get('value')) / 1000000000000000000 )
        tmp.append(dt.datetime.fromtimestamp(int(t.get('timeStamp'))))
        ret.append(tmp)
    return ret

ret = get_tx_value(page_no=1, address=ADDRESS,api_key=API_KEY)
df = pd.DataFrame(ret,columns=['from','to','value','datetime'])
df.to_csv('txes.tsv',sep='\t')
get_tx_values.py(旧バージョン。エラーで動きません)
from etherscan import accounts
import pandas as pd
import datetime as dt
API_KEY = 'API_KEY' # change API_KEY 
ADDRESS = '0xb794f5ea0ba39494ce839613fffba74279579268' # change address 

def get_tx_value(address,api_key):
    ac = accounts.Account(address=address,api_key=api_key)
    txes = ac.get_all_transactions()
    ret = []
    for t in txes:
        tmp = []
        tmp.append(t.get('from'))
        tmp.append(t.get('to'))
        tmp.append(int(t.get('value')) / 1000000000000000000 )
        tmp.append(dt.datetime.fromtimestamp(int(t.get('timeStamp'))))
        ret.append(tmp)
    return ret

ret = get_tx_value(address=ADDRESS,api_key=API_KEY)
df = pd.DataFrame(ret,columns=['from','to','value','datetime'])

結果

    from    to  value   datetime
0   0x32be343b94f860124dc4fee278fdcbd38c102d88  0xb794f5ea0ba39494ce839613fffba74279579268  100.000000  2015-08-09 07:47:35
1   0xb794f5ea0ba39494ce839613fffba74279579268  0x32be343b94f860124dc4fee278fdcbd38c102d88  99.000000   2015-08-09 07:53:13
2   0x32be343b94f860124dc4fee278fdcbd38c102d88  0xb794f5ea0ba39494ce839613fffba74279579268  99.001192   2015-08-09 07:55:43
3   0x32be343b94f860124dc4fee278fdcbd38c102d88  0xb794f5ea0ba39494ce839613fffba74279579268  900.000000  2015-08-09 07:58:23
4   0x32be343b94f860124dc4fee278fdcbd38c102d88  0xb794f5ea0ba39494ce839613fffba74279579268  9000.000000 2015-08-09 07:59:15

Etherscan上のトランザクションと一致していますね。
(pythonでは古いものから出力しているのでEtherscanとは逆順で出ています)

eth.png

アドレスの取引を分析する時なんかはExplorerのAPIも便利そうです。

19
22
1

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
19
22