Add the start of an inspector, build out a testing file a bit

This commit is contained in:
Robert Miller 2021-06-28 21:52:24 -04:00
parent aa82ee8ea3
commit ff1b73f633
5 changed files with 58 additions and 13 deletions

View File

@ -4,11 +4,9 @@ import json
rpc_end_point = ""
base_provider = Web3.HTTPProvider(rpc_end_point)
w3 = Web3(base_provider)
cache_directoty = './cache'
class BlockData:
def __init__(self, block_number, data, receipts, calls, logs) -> None:
self.block_number = block_number
@ -62,20 +60,23 @@ class BlockData:
## Creates a block object, either from the cache or from the chain itself
def createFromBlockNumber(block_number):
## Note that you need to pass in the provider, not the web3 wrapped provider object!
## This is because only the provider allows you to make json rpc requests
def createFromBlockNumber(block_number, base_provider):
cache_file = '{cacheDirectory}/{blockNumber}.json'.format(cacheDirectory=cache_directoty, blockNumber=block_number)
## Check to see if the data already exists in the cache
## if it exists load the data from cache
## If not then get the data from the chain and save it to the cache
if (Path(cache_file).is_file()):
print("Cache for this block exists, loading again")
print(('Cache for block {block_number} exists, loading data from cache').format(block_number=block_number))
block_file = open(cache_file)
block_json = json.load(block_file)
block = BlockData(block_number, block_json['data'], block_json['receipts'], block_json['calls'], block_json['logs'])
return block
else:
print("Cache for this block did not exist, getting data")
w3 = Web3(base_provider)
print(("Cache for block {block_number} did not exist, getting data").format(block_number=block_number))
## Get block data
block_data = w3.eth.get_block(block_number, True)

25
inspector_uniswap.py Normal file
View File

@ -0,0 +1,25 @@
from web3 import Web3
import configparser
import json
## Config file is used for addresses/ABIs
config = configparser.ConfigParser()
config.read('./utils/config.ini')
uniswap_router_abi = json.loads(config['ABI']['UniswapV2Router'])
class UniswapInspector:
def __init__(self, base_provider) -> None:
self.w3 = Web3(base_provider)
self.trading_functions = self.get_trading_functions()
self.uniswapV2RouterContract = self.w3.eth.contract(abi=uniswap_router_abi, address=config['ADDRESSES']['UniswapV2Router'])
def get_trading_functions(self):
## Gets all functions used for swapping
result = []
for abi in uniswap_router_abi:
if abi['type'] == 'function' and abi['name'].startswith('swap'):
result.append(abi['name'])
return result

View File

@ -1,8 +0,0 @@
import block
test_block_num = 12412732
block_data = block.createFromBlockNumber(test_block_num)
for transaction_hash in block_data.transaction_hashes:
calls = block_data.get_filtered_calls(transaction_hash)
print(calls)

20
testing_file.py Normal file
View File

@ -0,0 +1,20 @@
from web3.providers import base
from inspector_uniswap import UniswapInspector
import block
from web3 import Web3
import argparse
parser = argparse.ArgumentParser(description='Inspect some blocks.')
parser.add_argument('-block_number', metavar='b', type=int, nargs='+',
help='the block number you are targetting, eventually this will need to be changed')
parser.add_argument('-rpc', metavar='r', help='rpc endpoint, this needs to have parity style traces')
args = parser.parse_args()
## Set up the base provider, but don't wrap it in web3 so we can make requests to it with make_request()
base_provider = Web3.HTTPProvider(args.rpc)
## Get block data that we need
block_data = block.createFromBlockNumber(args.block_number[0], base_provider)
## Build a Uniswap inspector
uniswap_inspector = UniswapInspector(base_provider)

7
utils/config.ini Normal file

File diff suppressed because one or more lines are too long