<jupyter_start><jupyter_text>https://github.com/cyberFund/ethdrain Python script allowing to copy the Ethereum blockchain towards ElasticSearch, PostgreSQL and csv in an efficient way by connecting to a local RPC node<jupyter_code>import requests
import json
def print_json(json_for_print):
    print(json.dumps(json_for_print, indent=4, sort_keys=True))
    return<jupyter_output><empty_output><jupyter_text>Request function<jupyter_code>def http_post_request(url, request):
    print('url: {}'.format(str(url)))
    print('request: {} \n'.format(str(request)))
    return requests.post(url, data=request, headers={"content-type": "application/json"}).json()<jupyter_output><empty_output><jupyter_text>APIEthereum JSON RPC API: https://github.com/ethereum/wiki/wiki/JSON-RPC  getBlockByNumber<jupyter_code>def make_request_getBlockByNumber(block_nb, use_hex=True):
        return json.dumps({
                            "jsonrpc": "2.0",
                            "method": "eth_getBlockByNumber",
                            "params": [hex(block_nb) if use_hex else block_nb, True],
                            "id": 1
                          })
# eth_url = "http://localhost:8545"
eth_url = 'https://mainnet.infura.io/TzMi1NSXsXK2SzUuEY9Q'
print_json(http_post_request(eth_url,
                             make_request_getBlockByNumber("latest", False)))<jupyter_output>url: https://mainnet.infura.io/TzMi1NSXsXK2SzUuEY9Q
request: {"params": ["latest", true], "method": "eth_getBlockByNumber", "id": 1, "jsonrpc": "2.0"} 

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": {
        "difficulty": "0xb91590c441438",
        "extraData": "0x6e616e6f706f6f6c2e6f7267",
        "gasLimit": "0x7a121d",
        "gasUsed": "0x79b084",
        "hash": "0x1ca3e9ffb390a11eb2cd8611ba2d1222a8659503a7208866add2fe3fd6c9e5bc",
        "logsBloom": "0x426621090648a12008039888100080c25c2894d57040208a10f24680142801fe000084520281008314e5120a401044c014019658a002b9c01384721020610300001000c704a1431b2230409f2804050800098010040190001070c0010808106105b590a2036cc04880402ed4402109004e0130c56a2d328ec5000791000102c024042245211878200c880000140000084c53af120a0842bc4481000022022145028e04180044402da41012045700ae081201e78300108660050080c2d40a04640000c48340070230500e400012e0082010160444181d224138841820dc0c34c20114108430160034088420700464ab00402d30588a1840cc1c26010510041450",
        "miner[...]<jupyter_text>eth_getBalance<jupyter_code>def make_request_eth_getBalance(address, block_nb = 'latest', use_hex=False):
        return json.dumps({
                            "jsonrpc": "2.0",
                            "method": "eth_getBalance",
                            "params": [address, hex(block_nb) if use_hex else block_nb],
                            "id": 1
                          })

# eth_getBalance
address = '0xc8f88d1c1259060a799af77120db270cdce07e37'
print_json(http_post_request(eth_url,
                             make_request_eth_getBalance(address)))
WEI_ETH_FACTOR = 1000000000000000000.0
print('Balance of address \'{}\':\n {}'.format(address, 
                                               int(http_post_request(eth_url,
                                                                     make_request_eth_getBalance(address))['result'], 
                                                   0)/WEI_ETH_FACTOR))
address = '0xc8f88d1c1259060a799af77120db270cdce07e37'
print_json(http_post_request(eth_url,
                             make_request_eth_getBalance(address, 
                                                         block_nb = 5337125, 
                                                         use_hex = True)))<jupyter_output>url: https://mainnet.infura.io/TzMi1NSXsXK2SzUuEY9Q
request: {"params": ["0xc8f88d1c1259060a799af77120db270cdce07e37", "0x517025"], "method": "eth_getBalance", "id": 1, "jsonrpc": "2.0"} 

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": "0x1b9c02a0a23a70"
}
{'fraction_non_alphanumeric': 0.10563919367185506, 'fraction_numerical': 0.1750446542485328, 'mean_word_length': 2.900497512437811, 'pattern_counts': {'":': 29, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '666987', 'license': 'Apache-2.0', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1376, 'n_words': 163, 'path': 'Ethereum API.ipynb', 'repo_name': 'Snedashkovsky/Course_blockchain_data', 'size': '3919'}