mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 22:20:22 -04:00
💾
This commit is contained in:
parent
b85003ecb7
commit
58a160dcf5
18 changed files with 1 additions and 1 deletions
1
web3toolkit/scripts/__init__.py
Normal file
1
web3toolkit/scripts/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# -*- encoding: utf-8 -*-
|
113
web3toolkit/scripts/get_contracts_deployed.py
Executable file
113
web3toolkit/scripts/get_contracts_deployed.py
Executable file
|
@ -0,0 +1,113 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- encoding: utf-8 -*-
|
||||
# author: steinkirch
|
||||
|
||||
import os
|
||||
import ethereumetl
|
||||
import pandas as pd
|
||||
from utils.os import load_config, create_dir, run_exec
|
||||
from utils.plots import plot_bar, open_csv, save_csv
|
||||
|
||||
|
||||
|
||||
def get_data_for_contracts_by_block() -> dict:
|
||||
"""Prepare a dict of data to extract contracts by block."""
|
||||
|
||||
data = {}
|
||||
create_dir('data')
|
||||
env_keys = ['PROVIDER_URL',
|
||||
'TX_FILE',
|
||||
'START_BLOCK',
|
||||
'OUTPUT_FILE']
|
||||
env_vars = load_config(env_keys)
|
||||
|
||||
data['provider_uri'] = env_vars['PROVIDER_URL']
|
||||
data['tx_file'] = env_vars['TX_FILE']
|
||||
data['start_block'] = env_vars['START_BLOCK']
|
||||
data['output_file'] = env_vars['OUTPUT_FILE']
|
||||
|
||||
# adding this manually
|
||||
data['last_block_2015'] = 778482
|
||||
data['last_block_2016'] = 2912406
|
||||
data['last_block_2017'] = 4832685
|
||||
data['last_block_2018'] = 6988614
|
||||
data['last_block_2019'] = 9193265
|
||||
data['last_block_2020'] = 11565018
|
||||
data['last_block_2021'] = 13916165
|
||||
data['last_block_2022'] = 15978869
|
||||
data['buffer_for_chunk_size'] = 10000
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def export_blocks_and_transactions(end_block, data) -> dict:
|
||||
"""Run ethereumetl export_blocks_and_transactions."""
|
||||
|
||||
run_exec(['ethereumetl', 'export_blocks_and_transactions', \
|
||||
f'--start-block {data["start_block"]}', \
|
||||
f'--end-block {end_block}', \
|
||||
f'--provider-uri {data["provider_uri"]}', \
|
||||
f'--transactions-output {data["tx_file"]}'])
|
||||
|
||||
txs = open_csv(data['tx_file'])
|
||||
contracts = txs[txs['to_address'].isnull()]
|
||||
os.remove(data['tx_file'])
|
||||
return contracts + txs['from_address'].unique().tolist()
|
||||
|
||||
|
||||
def get_contracts_by_block(data, year) -> dict:
|
||||
"""Extract unique contracts by block for a given year."""
|
||||
|
||||
contracts = []
|
||||
last_block_year = data[f'last_block_{year}']
|
||||
start_block = int(data['start_block'])
|
||||
end_block = start_block + 9999
|
||||
|
||||
while (end_block <= last_block_year + data['buffer_for_chunk_size']):
|
||||
end_block_used = min(end_block, last_block_year)
|
||||
contracts.append(export_blocks_and_transactions(end_block_used, data))
|
||||
start_block += data['buffer_for_chunk_size']
|
||||
end_block += data['buffer_for_chunk_size']
|
||||
|
||||
return contracts
|
||||
|
||||
|
||||
def get_unique_contracts(contracts, year) -> None:
|
||||
"""Extract and save a list of unique contracts."""
|
||||
|
||||
unique_contract = [*set(contracts)]
|
||||
print(f"✅ Unique contract for {year})): {str(len(unique_contract))}")
|
||||
return pd.DataFrame(unique_contract, columns=["contracts"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
###########
|
||||
# Set up
|
||||
###########
|
||||
contracts_by_year = {}
|
||||
all_contracts = pd.DataFrame()
|
||||
years = list(range(2015, 2022))
|
||||
data = get_data_for_contracts_by_block()
|
||||
start_block = int(data['start_block'])
|
||||
|
||||
###########
|
||||
# Get data
|
||||
###########
|
||||
for year in years:
|
||||
contracts = get_contracts_by_block(data, year)
|
||||
contracts_by_year[year] = len(contracts)
|
||||
all_contracts.append(get_unique_contracts(contracts, year))
|
||||
start_block += 1
|
||||
|
||||
all_contracts = all_contracts['contract_deployers'].unique()
|
||||
all_contracts_df = pd.DataFrame(all_contracts, columns=['contract'])
|
||||
save_csv(all_contracts_df, data['output_file'])
|
||||
|
||||
###########
|
||||
# Plot data
|
||||
###########
|
||||
y_data = [y for y in contracts_by_year[year] if year == years.revers().pop()]
|
||||
plot_bar({'contract deployed': y_data}, years)
|
||||
|
||||
|
37
web3toolkit/scripts/get_deep_block_data.py
Executable file
37
web3toolkit/scripts/get_deep_block_data.py
Executable file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- encoding: utf-8 -*-
|
||||
# author: steinkirch
|
||||
|
||||
from utils.strings import pprint
|
||||
from utils.os import load_config
|
||||
from utils.web3_wrapper import Web3Wrapper
|
||||
|
||||
|
||||
def get_data_for_connection() -> dict:
|
||||
"""Prepare a dict of data for connection."""
|
||||
|
||||
data = {}
|
||||
env_keys = ['PROVIDER_TYPE',
|
||||
'PROVIDER_URL',
|
||||
'BLOCK_NUMBER']
|
||||
env_vars = load_config(env_keys)
|
||||
|
||||
data['network'] = env_vars['PROVIDER_URL']
|
||||
data['block'] = env_vars['BLOCK_NUMBER']
|
||||
data['provider_type'] = env_vars['PROVIDER_TYPE']
|
||||
return data
|
||||
|
||||
|
||||
def get_deep_block_data(data) -> dict:
|
||||
|
||||
w3 = Web3Wrapper(mode=data['provider_type'],
|
||||
network=data['network'])
|
||||
return w3.get_block()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
data = get_data_for_connection()
|
||||
results = get_deep_block_data(data)
|
||||
pprint(results)
|
||||
|
40
web3toolkit/scripts/get_deep_tx_data.py
Executable file
40
web3toolkit/scripts/get_deep_tx_data.py
Executable file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- encoding: utf-8 -*-
|
||||
# author: steinkirch
|
||||
|
||||
from utils.strings import pprint
|
||||
from utils.os import load_config
|
||||
from utils.web3_wrapper import Web3Wrapper
|
||||
|
||||
|
||||
def get_data_for_connection() -> dict:
|
||||
"""Prepare a dict of data for connection."""
|
||||
|
||||
data = {}
|
||||
env_keys = ['PROVIDER_TYPE',
|
||||
'PROVIDER_URL',
|
||||
'TRANSACTION']
|
||||
env_vars = load_config(env_keys)
|
||||
|
||||
data['network'] = env_vars['PROVIDER_URL']
|
||||
data['provider_type'] = env_vars['PROVIDER_TYPE']
|
||||
data['tx'] = env_vars['TRANSACTION']
|
||||
return data
|
||||
|
||||
|
||||
def get_deep_tx_data(data) -> dict:
|
||||
|
||||
w3 = Web3Wrapper(mode=data['provider_type'],
|
||||
network=data['network'])
|
||||
tx_data = w3.get_tx(data['tx'])
|
||||
tx_data.update(w3.get_tx_receipt(data['tx']))
|
||||
|
||||
return tx_data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
data = get_data_for_connection()
|
||||
tx_data = get_deep_tx_data(data)
|
||||
pprint(tx_data)
|
||||
|
42
web3toolkit/scripts/get_reserve_history_by_block.py
Executable file
42
web3toolkit/scripts/get_reserve_history_by_block.py
Executable file
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- encoding: utf-8 -*-
|
||||
# author: steinkirch
|
||||
|
||||
from utils.os import load_config, open_json, log_info
|
||||
from utils.web3_wrapper import Web3Wrapper
|
||||
|
||||
|
||||
def get_data_for_connection() -> dict:
|
||||
"""Prepare a dict of data for connection."""
|
||||
|
||||
data = {}
|
||||
env_keys = ['PAIR_ADDRESSES',
|
||||
'PROVIDER_URL',
|
||||
'BLOCK_NUMBER',
|
||||
'ABI_JSON_PATH',
|
||||
'PROVIDER_TYPE']
|
||||
env_vars = load_config(env_keys)
|
||||
|
||||
data['addresses'] = env_vars['PAIR_ADDRESSES']
|
||||
data['network'] = env_vars['PROVIDER_URL']
|
||||
data['block'] = env_vars['BLOCK_NUMBER']
|
||||
data['abi'] = env_vars['ABI_JSON_PATH']
|
||||
data['provider_type'] = env_vars['PROVIDER_TYPE']
|
||||
return data
|
||||
|
||||
|
||||
def get_reserve_by_block(data) -> None:
|
||||
"""Establish connection to retrieve reserve history."""
|
||||
|
||||
w3 = Web3Wrapper(mode=data['provider_type'],
|
||||
network=data['network'])
|
||||
w3.inject_middleware()
|
||||
w3.get_pair_contract(data['addresses'], open_json(data['abi']))
|
||||
return w3.get_reserves(int(data['block']))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
data = get_data_for_connection()
|
||||
reserve1, reserve2 = get_reserve_by_block(data)
|
||||
log_info(f'reserves: {reserve1}, {reserve2}')
|
1
web3toolkit/scripts/utils/__init__.py
Normal file
1
web3toolkit/scripts/utils/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# -*- encoding: utf-8 -*-
|
25
web3toolkit/scripts/utils/arithmetics.py
Normal file
25
web3toolkit/scripts/utils/arithmetics.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
# arithmetics.py
|
||||
# This class implements math methods used by the other classes.
|
||||
# author: steinkirch
|
||||
|
||||
|
||||
from decimal import Decimal, getcontext
|
||||
from utils.strings import log_error
|
||||
|
||||
|
||||
def div(dividend, divisor) -> Decimal:
|
||||
"""Return higher precision division."""
|
||||
|
||||
if divisor == 0:
|
||||
log_error('Found a zero division error. Returning 0.')
|
||||
return 0
|
||||
return to_decimal(dividend) / to_decimal(divisor)
|
||||
|
||||
|
||||
def to_decimal(value, precision=None) -> Decimal:
|
||||
"""Return Decimal value for higher (defined) precision."""
|
||||
|
||||
precision = precision or 22
|
||||
getcontext().prec = precision
|
||||
return Decimal(value)
|
149
web3toolkit/scripts/utils/os.py
Normal file
149
web3toolkit/scripts/utils/os.py
Normal file
|
@ -0,0 +1,149 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
# This class implements OS/file system util methods used by the other classes.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import copy
|
||||
import logging
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
||||
def set_logging(log_level) -> None:
|
||||
"""Set logging level according to .env config."""
|
||||
|
||||
if log_level == 'info':
|
||||
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
||||
|
||||
elif log_level == 'error':
|
||||
logging.basicConfig(level=logging.ERROR, format='%(message)s')
|
||||
|
||||
elif log_level == 'debug':
|
||||
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
|
||||
|
||||
else:
|
||||
print(f'Logging level {log_level} is not available. Setting to ERROR')
|
||||
logging.basicConfig(level=logging.ERROR, format='%(message)s')
|
||||
|
||||
|
||||
def load_config(keys) -> dict:
|
||||
"""Load and set environment variables."""
|
||||
|
||||
env_file = Path('..') / '.env'
|
||||
if not os.path.isfile(env_file):
|
||||
exit_with_error('Please create an .env file')
|
||||
|
||||
load_dotenv(env_file)
|
||||
env_vars = {}
|
||||
|
||||
try:
|
||||
for key in keys:
|
||||
env_vars[key] = os.getenv(key)
|
||||
set_logging(os.getenv("LOG_LEVEL"))
|
||||
return env_vars
|
||||
except KeyError as e:
|
||||
exit_with_error(f'Cannot extract env variables: {e}. Exiting.')
|
||||
|
||||
|
||||
def log_error(string) -> None:
|
||||
"""Print STDOUT error using the logging library."""
|
||||
|
||||
logging.error('🚨 %s', string)
|
||||
|
||||
|
||||
def log_info(string) -> None:
|
||||
"""Print STDOUT info using the logging library."""
|
||||
|
||||
logging.info('✅ %s', string)
|
||||
|
||||
|
||||
def log_debug(string) -> None:
|
||||
"""Print STDOUT debug using the logging library."""
|
||||
|
||||
logging.debug('🟨 %s', string)
|
||||
|
||||
|
||||
def open_json(filepath) -> dict:
|
||||
"""Load and parse a json file."""
|
||||
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as infile:
|
||||
return json.load(infile)
|
||||
|
||||
except (IOError, FileNotFoundError, TypeError) as e:
|
||||
exit_with_error(f'Failed to parse: "{filepath}": {e}')
|
||||
|
||||
|
||||
def format_path(dir_path, filename) -> str:
|
||||
"""Format a OS full filepath."""
|
||||
|
||||
return os.path.join(dir_path, filename)
|
||||
|
||||
|
||||
def format_output_file(name) -> str:
|
||||
"""Format the name for the result file."""
|
||||
|
||||
return f'{name}.json'
|
||||
|
||||
|
||||
def save_json(destination, data) -> None:
|
||||
"""Save data from memory to a json destination in disk."""
|
||||
|
||||
try:
|
||||
with open(destination, 'w', encoding='utf-8') as outfile:
|
||||
json.dump(data, outfile, indent=4)
|
||||
|
||||
except (IOError, TypeError) as e:
|
||||
log_error(f'Could not save {destination}: {e}')
|
||||
|
||||
|
||||
def create_dir(result_dir) -> None:
|
||||
"""Check whether a directory exists and create it if needed."""
|
||||
|
||||
try:
|
||||
if not os.path.isdir(result_dir):
|
||||
os.mkdir(result_dir)
|
||||
|
||||
except OSError as e:
|
||||
log_error(f'Could not create {result_dir}: {e}')
|
||||
|
||||
|
||||
def set_output(env_vars, input_file) -> str:
|
||||
"""Create an output destination to save solutions."""
|
||||
|
||||
try:
|
||||
output_dir = env_vars['OUTPUT_DIR']
|
||||
create_dir(output_dir)
|
||||
|
||||
output_str = input_file.split('_')[1].split('.json')[0]
|
||||
output_file_str = env_vars['OUTPUT_FILE_STR']
|
||||
output_file = output_file_str.format(output_str)
|
||||
return format_path(output_dir, output_file)
|
||||
|
||||
except (TypeError, KeyError) as e:
|
||||
exit_with_error(f'Could not format output file: {e}')
|
||||
|
||||
|
||||
def deep_copy(dict_to_clone) -> dict:
|
||||
"""Deep copy (not reference copy) to a dict."""
|
||||
|
||||
return copy.deepcopy(dict_to_clone)
|
||||
|
||||
|
||||
def exit_with_error(message) -> None:
|
||||
"""Log an error message and halt the program."""
|
||||
|
||||
log_error(message)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def run_exec(command) -> None:
|
||||
"""Exec a bash commnad (remember: not safe!)."""
|
||||
|
||||
try:
|
||||
this_command = subprocess.run(command)
|
||||
log_info(f'Exit code: {this_command.returncode}')
|
||||
except Exception as e:
|
||||
log_error(f'Error running {command}: {e}')
|
31
web3toolkit/scripts/utils/plots.py
Normal file
31
web3toolkit/scripts/utils/plots.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
# This class implements plot scripts
|
||||
# author: steinkirch
|
||||
|
||||
import pandas as pd
|
||||
from utils.os import exit_with_error
|
||||
|
||||
|
||||
def open_csv(filepath) -> dict:
|
||||
"""Load and parse a csv file."""
|
||||
|
||||
try:
|
||||
return pd.read_csv(filepath)
|
||||
except (IOError, FileNotFoundError, TypeError) as e:
|
||||
exit_with_error(f'Failed to parse: "{filepath}": {e}')
|
||||
|
||||
|
||||
def save_csv(destination, data, index=False) -> None:
|
||||
"""Save data from memory to a csv destination in disk."""
|
||||
|
||||
try:
|
||||
data.to_csv(destination, index=index)
|
||||
|
||||
except (IOError, TypeError) as e:
|
||||
log_error(f'Could not save {destination}: {e}')
|
||||
|
||||
|
||||
def plot_bar(y, x) -> None:
|
||||
"""Simplest plot for two sets."""
|
||||
df = pd.DataFrame(y, index=x)
|
||||
df.plot.bar(rot=0, subplots=True)
|
40
web3toolkit/scripts/utils/strings.py
Normal file
40
web3toolkit/scripts/utils/strings.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
# This class implements string methods used by the other classes.
|
||||
# author: steinkirch
|
||||
|
||||
from pprint import PrettyPrinter
|
||||
|
||||
from utils.os import log_error
|
||||
from utils.arithmetics import to_decimal
|
||||
|
||||
|
||||
def to_decimal_str(value) -> str:
|
||||
"""Format a reserve amount to a suitable string."""
|
||||
|
||||
return str(to_decimal(value))
|
||||
|
||||
|
||||
def to_wei_str(value, decimals=None) -> str:
|
||||
"""Parse an order string to wei value."""
|
||||
|
||||
decimals = decimals or 18
|
||||
try:
|
||||
return str(value)[:-decimals] + '_' + str(value)[-decimals:]
|
||||
except ValueError as e:
|
||||
log_error(f'Cannot convert to wei: {e}')
|
||||
|
||||
|
||||
def to_solution(value) -> str:
|
||||
"""Format decimal wei with an underscore for easier reading."""
|
||||
|
||||
return to_wei_str(to_decimal_str(value))
|
||||
|
||||
|
||||
def pprint(data, indent=None) -> None:
|
||||
"""Print dicts and data in a suitable format"""
|
||||
|
||||
print()
|
||||
indent = indent or 4
|
||||
pp = PrettyPrinter(indent=indent)
|
||||
pp.pprint(data)
|
||||
print()
|
67
web3toolkit/scripts/utils/web3_wrapper.py
Normal file
67
web3toolkit/scripts/utils/web3_wrapper.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
# This class implements an (ongoing) wrapper for web3 libs.
|
||||
# author: steinkirch
|
||||
|
||||
from web3 import Web3, HTTPProvider, WebsocketProvider, IPCProvider
|
||||
from web3.middleware import geth_poa_middleware
|
||||
from utils.os import log_info
|
||||
|
||||
|
||||
class Web3Wrapper():
|
||||
|
||||
def __init__(self, mode, network):
|
||||
self.mode = mode
|
||||
self.network = network
|
||||
|
||||
self.w3 = None
|
||||
self.pair_contract = None
|
||||
|
||||
self._setup()
|
||||
|
||||
##################
|
||||
# PRIVATE METHODS
|
||||
##################
|
||||
def _setup(self) -> None:
|
||||
self._get_web3_object()
|
||||
|
||||
def _get_web3_object(self) -> None:
|
||||
log_info(f'Setting mode {self.mode} for {self.network}')
|
||||
if self.mode == 'http' or self.mode == 'local_http':
|
||||
self.w3 = Web3(HTTPProvider(self.network))
|
||||
elif self.mode == 'ws' or self.mode == 'local_ws':
|
||||
self.w3 = Web3(WebsocketProvider(self.network))
|
||||
elif self.mode == 'ipc' or self.mode == 'local_ipc':
|
||||
self.w3 = Web3(IPCProvider(self.network))
|
||||
else:
|
||||
log_info(f'Provider type is invalid: {self.mode}. Fix .env.')
|
||||
|
||||
|
||||
##################
|
||||
# BLOCK METHODS
|
||||
##################
|
||||
def get_block(self, block_number='latest') -> dict:
|
||||
return dict(self.w3.eth.get_block(block_number))
|
||||
|
||||
|
||||
##################
|
||||
# LP PAIR METHODS
|
||||
##################
|
||||
def get_pair_contract(self, address, abi) -> str:
|
||||
self.pair_contract = self.w3.eth.contract(address=address, abi=abi)
|
||||
|
||||
def inject_middleware(self, layer=0) -> None:
|
||||
self.w3.middleware_onion.inject(geth_poa_middleware,
|
||||
layer=layer)
|
||||
|
||||
def get_reserves(self, block) -> list:
|
||||
return self.pair_contract.functions.getReserves().call({}, block)[:2]
|
||||
|
||||
|
||||
##############
|
||||
# TX METHODS
|
||||
##############
|
||||
def get_tx(self, tx) -> dict:
|
||||
return dict(self.w3.eth.get_transaction(tx))
|
||||
|
||||
def get_tx_receipt(self, tx) -> dict:
|
||||
return dict(self.w3.eth.get_transaction_receipt(tx))
|
Loading…
Add table
Add a link
Reference in a new issue