# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Example of usage in Jupyter notebook

# ## Usual imports for pandas/numpy/matplotlib

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

# ## Imports and environment set up for API

# +
from ethereum_stats import levelDB, statedataset, blockrange
from eth_utils import decode_hex
import logging
import time

LOGGING_LEVEL = logging.WARNING
logging.basicConfig(level=LOGGING_LEVEL)

DB_DIR = '/home/ethereum/eth-rinkeby/geth/chaindata'
db = levelDB.LevelDB(DB_DIR)
# -

# ## Get the last block header and its state

last_block = blockrange.BlockHeader.get_latest_block_header(db)
state = statedataset.StateDataset(db, decode_hex(last_block.state_root))
df = state.to_panda_dataframe()


df.describe()


# +
df[df.nonce>10000]['nonce'].plot.hist()


# -

df[df.is_contract==True].describe()


# +
s = df[df.is_contract==True].reset_index().storage_size

s.plot.hist()
# -

aRange = blockrange.BlockRange.date_range(db, '1/1/2018', '7/1/2018')
s = pd.Series(np.zeros(aRange.upper_blk_nbr - aRange.lower_blk_nbr + 1))
i = 0
for blk in aRange:
    s[i] = blk.gas_used
    i = i + 1 
s.plot()

s.plot.hist()

df.nonce.idxmax()

df.loc[df.nonce.idxmax()]

factor = 10 ** 18
accAddress = df.loc[df[df.key_in_db==True].nonce.idxmax()].account
aRange= blockrange.BlockRange.date_range(db, '7/1/2018 15:30:00', '7/1/2018 18:00:00')
s = pd.Series(np.zeros(aRange.upper_blk_nbr - aRange.lower_blk_nbr + 1))
i = 0
for blk in aRange:
    st = statedataset.StateDataset(db, decode_hex(blk.state_root))
    acc = st.get_account(accAddress)
    s[i] = acc.balance / factor
    i = i + 1 
s.plot()

# +




StarcoderdataJupyterScriptsDedupFiltered