Output address transactions with Ethereum's Explorer (Etherscan) API (Ethereum)

This article uses Mac OS Sierra 10.12.4. The python version is 3.5.0.

Introduction

Bitcoin and other virtual currencies have an explorer for checking addresses and transactions. Etherscan is available as an Explorer for Ethereum. And Etherscan also has an API. If you use this, you can easily extract transactions without going directly from the blockchain. So I will use it.

Etherscan API registration

To use the Etherscan API, you need to register Etherscan and obtain the API-KEY. The registration and acquisition procedure is described on the official Etherscan API page. You can get the API-KEY by newly registering with Etherscan and looking at ClientPortal-> MyApiKey.

Install py-etherscan-api

This time, we will use etherscan api using python. To do this, go to the py-etherscan-api github page (https://github.com/corpetty/py-etherscan-api/tree/cebc169dfca7d4fea5eec01c875550216de67039) and git clone it.

git-clone


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

python


cd py-etherscan-api

Install locally after cloning.

python


python setup.py install

Output the transaction of the specified address

Etherscan ALL Account Obtain an appropriate address from the top of the amount and confirm the transaction.

get_tx_values.py(2019/2/7 version)


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(old version. Does not work with error)


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'])

result

	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

It matches the transaction on Etherscan. (Since python outputs from the oldest one, it comes out in the reverse order of Etherscan)

eth.png

Explorer's API seems to be useful when analyzing address transactions.

Recommended Posts

Output address transactions with Ethereum's Explorer (Etherscan) API (Ethereum)
Problems with output results with Google's Cloud Vision API